Monday, October 20, 2014

Chapter 4 Programming Language Concepts R Sebesta

Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #4 dari Chapter 4 Programming Language Concepts R Sebesta


Review Questions

6. What is a state transition diagram?
*A state transition diagram is a type of diagram used in computer science and related fields to describe the behavior of systems. State diagrams require that the system described is composed of a finite number of states.

7. Why are character classes used, rather than individual characters, for the letter and digit transitions of a state diagram for a lexical analyzer?
* The first thing to observe is that there are 52 different characters (any uppercase or lowercase letter) that can begin a name, which would require 52 transitions from the transition diagram’s initial state. However, a lexical analyzer is interested only in determining that it is a name and is not concerned with which specific name it happens to be. Therefore, we define a character class named LETTER for all 52 letters and use a single transition on the first letter of any name.

8. What are the two distinct goals of syntax analysis?
*There are two distinct goals of syntax analysis. First, the syntax analyzer must check the input program to determine whether it is syntactically correct. When an error is found, the analyzer must produce a diagnostic message and recover. In this case, recovery means it must get back to a normal state and continue its analysis of the input program. This step is required so that the
compiler finds as many errors as possible during a single analysis of the input program. If it is not done well, error recovery may create more errors, or St least more error messages. The second goal of syntax analysis is to produce a complete parse tree, or at least trace the structure of the complete parse tree, for syntactically correct input. The parse tree (or its trace) is used as the basis for translation.

9. Describe the differences between top-down and bottom-up parsers.
*Bottom-up parsing is a strategy for analyzing unknown data relationships that attempts to identify the most fundamental units first, and then to infer higher-order structures from them. It attempts to build trees upward toward the start symbol.
Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis.

10. Describe the parsing problem for a top-down parser.
*The parsing problems for a top-down parser:
-Only judges grammaticality.
-Stops when it finds a single derivation.
-No semantic knowledge employed.
-No way to rank the derivations.
-Problems with left-recursive rules.
-Problems with ungrammatical sentences.




Problem Set

6. Given the following grammar and the right sentential form, draw a parse tree and show the phrases and simple phrases, as well as the handle.
S → AbB | bAc    A → Ab | aBB    B → Ac | cBb | c
 a. Acccbbc
Phrases: aAcccbbc, aAcccb, Ac, ccb, c_1, c_2
Simple phrases: Ac, c_1, c_2
Handle: Ac

b. AbcaBccb
Phrases: AbcaBccb, caBccb, aBcc, aBc, c_1
Simple Phrases: c_1
Handle: c_1


c. baBcBbbc
Phrases: baBcBbbc, aBcBbb, aBcBb, cBb
Simple Phrases:cBb
Handle: cBb




7. Show a complete parse, including the parse stack contents, input string, and action for the string id * (id + id), using the grammar and parse table in Section 4.5.3.
*
Stack
Input
Action
0
id * (id + id) $
Shift 5
0id5
* (id + id) $
Reduce 6 (Use GOTO[0, F])
0F3
* (id + id) $
Reduce 4 (Use GOTO[0, T])
0T2
* (id + id) $
Reduce 2 (Use GOTO[0, E])
0T2*7
(id + id) $
Shift 7
0T2*7(4
id + id ) $
Shift 4
0T2*7(4id5
+ id ) $
Shift 5
0T2*7(4F3
+ id ) $
Reduce 6 (Use GOTO[4, F])
0T2*7(4T2
+ id ) $
Reduce 4 (Use GOTO[4, T])
0T2*7(4E8
+ id ) $
Reduce 2 (Use GOTO[4, E])
0T2*7(4E8+6
id ) $
Shift 6
0T2*7(4E8+6id5
) $
Shift 5
0T2*7(4E8+6F3
) $
Reduce 6 (Use GOTO[6, F])
0T2*7(4E8+6T9
) $
Reduce 4 (Use GOTO[6, T])
0T2*7(4E8
) $
Reduce 1 (Use GOTO[4, E])
0T2*7(4E8)11
$
Shift 11
0T2*7F10
$
Reduce 5 (Use GOTO[7, F])
0T2
$
Reduce 5 (Use GOTO[0, T])
0E1
$
Reduce 2 (Use GOTO[0, E])

8. Show a complete parse, including the parse stack contents, input string, and action for the string (id + id) * id, using the grammar and parse table in Section 4.5.3.
*
Stack
Input
Action
0
id * (id + id) $
Shift 5
0id5
* (id + id) $
Reduce 6 (Use GOTO[0, F])
0F3
* (id + id) $
Reduce 4 (Use GOTO[0, T])
0T2
* (id + id) $
Reduce 2 (Use GOTO[0, E])
0T2*7
(id + id) $
Shift 7
0T2*7(4
id + id ) $
Shift 4
0T2*7(4id5
+ id ) $
Shift 5
0T2*7(4F3
+ id ) $
Reduce 6 (Use GOTO[4, F])
0T2*7(4T2
+ id ) $
Reduce 4 (Use GOTO[4, T])
0T2*7(4E8
+ id ) $
Reduce 2 (Use GOTO[4, E])
0T2*7(4E8+6
id ) $
Shift 6
0T2*7(4E8+6id5
) $
Shift 5
0T2*7(4E8+6F3
) $
Reduce 6 (Use GOTO[6, F])
0T2*7(4E8+6T9
) $
Reduce 4 (Use GOTO[6, T])
0T2*7(4E8
) $
Reduce 1 (Use GOTO[4, E])
0T2*7(4E8)11
$
Shift 11
0T2*7F10
$
Reduce 5 (Use GOTO[7, F])
0T2
$
Reduce 5 (Use GOTO[0, T])
0E1
$
Reduce 2 (Use GOTO[0, E])

9. Write an EBNF rule that describes the while statement of Java or C++. Write the recursive-descent subprogram in Java or C++ for this rule.
*<while_stmt> -> WHILE '(' (<arith_expr> | <logic_expr>) ')'  <block>
  <block> -> <stmt> | '{' <stmt> {<stmt>} '}'

10. Write an EBNF rule that describes the for statement of Java or C++. Write the recursive-descent subprogram in Java or C++ for this rule.
*<while_stmt> -> WHILE '(' (<arith_expr> | <logic_expr>) ')'  <block>
  <block> -> <stmt> | '{' <stmt> {<stmt>} '}'

Chapter 3 Programming Language Concepts R Sebesta



Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #3 dari Chapter 3 programming Language Concepts R Sebesta


Review Questions

6. Define a left-recursive grammar rule.

*A left-recursive grammar means that the parser can get into a loop in the parsing rules without making any progress consuming the input. A grammar is left-recursive if we can find some non-terminal A which will eventually derive a sentential form with itself as the left-symbol. Also, this left recursion specifies left associativity.


7. What three extensions are common to most EBNFs?

*Three extensions that are common to most EBNFs:
a. The first extension denotes an optional part of a RHS, which is delimited by brackets.
b. The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether.
c. The third extension deals with multiple choice options.


8. Distinguish between static and dynamic semantics.

* Static semantis are represented declaratively. It is represented in passive data that is descriptive and can be retrieved, e.g. a Web page on a Web server. It also changes relatively slowly. The Web publishing events happen rarely relative to actions such as browsing a page or clicking a link.
The dynamic semantis are represented procedurally. It can be computed by programs running on the client or server side, based on immediate interactive user input. This computation can depend on the immediate context - including time, personal information about the user, user-initiated actions, etc. It also changes relatively rapidly. A single user click can cause the semantics to be generated or to change, or it can be changed by the actions of programs continuously in real-time.


9. What purpose do predicates serve in an attribute grammar?

*Predicates in attribute grammar states the static semantic rules a language needs to follow. A false value in predicates indicates that there is an invalid syntax or static semantics and determines whether an action is allowed or not.


10. What is the difference between a synthesized and an inherited attribute?

*A synthesized attribute proceeds in a bottom-up order, from leaves to roots. While an inherited attribute proceeds in a top-down order, from roots to leaves.



Problem Set

6. Using the grammar in Example 3.2, show a parse tree and a leftmost derivation for each of the following statements:

a. A = A * (B + (C * A))


< assign > = > <id> = <expr>

= > A = <expr>

= > A = <id> * <expr>

= > A = A * <expr>

= > A = A * ( <expr> )

= > A = A * ( <id> + <expr> )

= > A = A * ( B + <expr> )

= > A = A * ( B + ( <expr> ) )

= > A = A * ( B + ( <id> * <expr> ) )

= > A = A * ( B + ( C * <expr> ) )

= > A = A * ( B + ( C * <id> ) )

= > A = A * ( B + ( C * A ) )


b. B = C * (A * C + B)

< assign > = > <id> = <expr>

= > B = <expr>

= > B = <id> + <expr>

= > B = C * <expr>

= > B = C * ( <expr> )

= > B = C * ( <id> + <expr> )

= > B = C * ( A * <expr> )

= > B = C * ( A * <id> + <expr> )

= > B = C * ( A * C + <expr> )

= > B = C * ( A * C + <id> )

= > B = C * ( A * C + B )


c. A = A * (B + (C))

< assign > = > <id> = <expr>

= > A = <expr>

= > A = <id> + <expr>

= > A = A * <expr>

= > A = A * (<expr> )

= > A = A * ( <id> + <expr> )

= > A = A * ( B + <expr> )

= > A = A * ( B + ( <expr> ) )

= > A = A * ( B + ( <id> ) )

= > A = A * ( B + ( C ) )




7. Using the grammar in Example 3.4, show a parse tree and a leftmost derivation for each of the following statements:

a. A = ( A + B ) * C


<assign> => <id>=<expr>

=> A=<expr>

=> A=<term>

=> A=<term>*<factor>

=> A=<factor>*<factor>

=> A=(<expr>)*<factor>

=> A=(<expr>+<term>)*<factor>

=> A=(<term>+<term>)*<factor>

=> A=(<factor>+<term>)*<factor>

=> A=(<id>+<term>)*<factor>

=> A=(A+<term>)*<factor>

=> A=(A+<factor>)*<factor>

=> A=(A+<id>)*<factor>

=> A=(A+B)*<factor>

=> A=(A+B)*<id>

=> A=(A+B)*C



b. A = B + C + A

<assign> => <id>=<expr>

=> A=<expr>

=> A=<expr>+<term>

=> A=<expr>+<term>+<term>

=> A=<term>+<term>+<term>

=> A=<factor>+<term>+<term>

=> A=<id>+<term>+<term>

=> A=A+<term>+<term>

=> A=A+<factor>+<term>

=> A=A+<id>+<term>

=> A=A+C+<term>

=> A=A+C+<factor>

=> A=A+C+<id>

=> A=A+C+A


c. A = A * (B + C)

<assign> => <id>=<expr>

=> A=<expr>

=> A=<term>

=> A=<term>*<factor>

=> A=<factor>*<factor>

=> A=<id>*<factor>

=> A=A*<factor>

=> A=A*(<expr>)

=> A=A*(<expr>+<term>)

=> A=A*(<term>+<term>)

=> A=A*(<factor>+<term>)

=> A=A*(<id>+<term>)

=> A=A*(B+<term>)

=> A=A*(B+<factor>)

=> A=A*(B+<id>)

=> A=A*(B+C)


d. A = B * (C * (A + B))

<assign> => <id>=<expr>

=> A=<term>

=> A=<term>*<factor>

=> A=<factor>*<factor>

=> A=<id>*<factor>

=> A=B*<factor>

=> A=B*(<expr>)

=> A=B*(<term>)

=> A=B*(<term>*<factor>)

=> A=B*(<factor>*<factor>)

=> A=B*(<id>*<factor>)

=> A=B*(C*<factor>)

=> A=B*(C*(<expr>))

=> A=B*(C*(<expr>+<term>))                                                            

=> A=B*(C*(<term>+<term>))

=> A=B*(C*(<factor>+<term>))

=> A=B*(C*(<id>+<term>))

=> A=B*(C*(A+<term>))

=> A=B*(C*(A+<factor>))

=> A=B*(C*(A+<id>)) => A=B*(C*(A+B))
8. Prove that the following grammar is ambiguous:
<S> → <A>
<A> → <A> + <A> | <id>
<id> → a | b | c


*A grammar is ambiguous if there are two different trees for derivation of same expression.
The following string is a sentence for this grammar and it has two
different left-most derivations:

   a + b + c

Left-most derivation 1:
                               Rule Used
    1   <S> => <A>                     1        
    2       => <A> + <A>             2        
    3       => id + <A>                 3        
    4       => a + <A>                  4        
    5       => a + <A> + <A>       2
    6       => a + id + <A>          3
    7       => a + b + <A>           5
    8       => a + b + id              3
    9       => a + b + c               6

Left-most derivation 2:
                                                Rule Used
    1   <S> => <A>                                     1        
    2       => <A> + <A>                             2                
    3       => <A> + <A> + <A>                2
    4       => id + <A> + <A>                     3
    5       => a + <A> + <A>                      4
    6       => a + id + <A>                           3
    7       => a + b + <A>                            5
    8       => a + b + id                               3
    9       => a + b + c                                 6


9. Modify the grammar of Example 3.4 to add a unary minus operator that has higher precedence than either + or *.
*We can assume that the operators can precede any operand then

<factor> → <id>

with

<factor> → + <id>

10. Describe, in English, the language defined by the following grammar:
<S> → <A> <B> <C>
<A> → a <A> | a
<B> → b <B> | b
<C> → c <C> | c

*All sentences consisting of one or more a’s followed by one or more b’s followed by one or more c’s

Friday, October 3, 2014

Chapter 2 Programming Language Concepts R Sebesta


Nama: Stefanus Eduard Adrian
NIM: 1801382963

Kali ini saya akan menjawab Assignment #2 dari Chapter 2 Programming Language Concepts R Sebesta


Review Questions

6. What hardware capability that first appeared in the IBM 704 computer strongly affected the evolution of programming languages ? Explain why.
*Hardware capability that strongly affected the evolution of programming languages is the capability of both indexing and floating-point instructions in hardware. Because the floating-point  operations didn’t have to be simulated in software anymore, and that can save time for processing.

7. In what year was the Fortran design project begun ?
*The Fortran design project was begun in 1954.

8. What was the primary application area of computers at the time Fortran was designed ?
*Mathematics application area was the primary application area of computers at the time Fortarn was designed

9. What was the source of all of the control flow statements of Fortran I ?
*The control flow statements of Fortran I were based on 704 instructions. Although it is not clear whether the 704 designers dictated the control statement design of Fortran I or whether the designers of Fortran I suggested these instructions to the 704 designers.

10. What was the most significant feature added to Fortran I to get Fortran II ?
*The most significant feature added to Fortran I to get Fortran II was the independent compilation of subroutines. Without independent compilation, any change in a program required that the entire program be recompiled.



Problem Set

6. Make an educated guess as to the most common syntax error in LISP programs.
*The most common syntax error in LISP programs is probably the placement and number of parentheses. We can place parentheses wrongly in our programs if we are not careful. We can also has too many or too little parentheses if we are not careful. The use of parentheses is very important in LISP programs because parentheses is used pretty much in LISP programming.

7. LISP began as a pure functional language but gradually acquired more and more imperative features. Why ?
*Originally, John McCarthy intended to develop LISP to meet the demand for artificial intelligence as a functional programming language. But, most programming has been done in the imperative style, so we have many good imperative algorithms. And, in order to build efficient code, different LISP implementations provided more and more imperative fetures.

8. Describe in detail the three most important reasons, in your opinion, why ALGOL 60 did not become a very widely used language.
*The three most important reasons in my opinion: 
-Some of the features of ALGOL 60 were too flexible, they made understanding difficult and implementation inefficient. The best example of this is the pass-by-name method of passing parameters to subprograms.
-ALGOL 60 lacks of input and output statement. It was another major reason for its lack of acceptance. Implementation-dependent input/output made programs difficult to port to other computers.
-One of the most vital reason why ALGOL 60 didn’t become a very widely used language is because the entrenchment of Fortran among users and the lack of support by IBM. They were probably the most important factors in ALGOL 60’s failure to gain widespread use.

9. Why, in your opinion, did COBOL allow long identifiers when Fortran and ALGOL did not ?
*COBOL allowed long identifiers because COBOL was more of a reporting language than Fortran and ALGOL. COBOL used long identifiers because it made it easier to read the programs. If the programs used standard mathematical notation, it would be harder to read te programs. Also, Fortran was also a much older language that was built during a time of very little memory leaving little room for long identifiers.

10. Outline the major motivation of IBM in developing PL/I.
*By the early 1960s, the users of computers in industry had settled into two separate and quite different camps: scientific and business. From the IBM point of view, scientific programmers used programming language that had power in floating-point data and arrays. For business applications, people used programs that had power in decimal and character string data types, as well as elaborate and efficient input and output facilities. These perceptions naturally led to the concept of designing a single universal computer that would be capable of doing both floating-point and decimal arithmetic, and therefore both scientific and business applications. So, that motivated IBM to develop PL/I.