Monday, 20 October 2014

Chapter 3 Concepts of Programming Languages, Robert W. Sebesta. Describing Syntax and Semantics

Nama : Billy
NIM   : 1801374785

Review Question:

6. Define a left-recursive grammar rule.
When a grammar rule has its left hand side (LHS) also appearing at the beginning of its right hand side (RHS), the rule is said to be left recursive. 
7. What three extensions are common to most EBNFs?
Three extensions are common to most EBNFs:
a. The first extension denotes an optional part of a RHS, which is delimited by brackets. For example: <if_stmt> -> if ( <expr> ) stmt [ else <stmt> ]
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. For example: <ident_list> -> ident {, <ident> }
c. The third extension deals with multiple choice options. For example: <term> -> <term> ( *|/|% ) <factor>
8. Distinguish between static and dynamic semantics.
Static semantics is more on the legal forms of programs (syntax rather semantics) and is only indirectly related to the meaning of the programs during execution. Static semantics is so named because the analysis required checking these specifications can be done at compile time. Dynamic semantics is describing the meaning of the programs. Programmers need to know precisely what statements of a language do. Compile writers determine the semantics of a language for which they are writing compilers from English descriptions.

9. What purpose do predicates serve in an attribute grammar?
The use of predicates functions is to state the static semantic rules of the language.

10. What is the difference between a synthesized and an inherited attribute
The synthesized attributes are the result of the attribute evaluation rules, and may also use the values of the inherited attributes. The inherited attributes are passed down from parent nodes. In some approaches, synthesized attributes are used to pass semantic information up the parse tree, while inherited attributes help pass semantic information down it.
For instance, when constructing a language translation tool, such as a compiler, it may be used to assign semantic values to syntax constructions. Also, it is possible to validate semantic checks associated with a grammar, representing the rules of a language not explicitly imparted by the syntax.


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))
















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

c. 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

b. A = B + C + A

c. A = A * (B + C)

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


8. Prove that the following grammar is ambiguous:
<S> → <A>
<A> → <A> + <A> | <id>
<id> → a | b | c



For the grammar of this problem and the expression a=a+b+c there are two different leftmost derivations:

<S>-><A>
-> <A>+<A>
-> <A>+<A>+<A>
-> <id>+<A>+<A>
-> a+<A>+<A>
-> a+<id>+<A>
-> a+b+<A>
-> a+b+<id>
-> a+b+c

Or 

<S>-><A>
-><A>+<A>
-><id>+<A>
->a+<A>
->a+<A>+<A>
->a+<id>+<A>
->a+b+<A>
->a+b+<id>


->a+b+c
9. Modify the grammar of Example 3.4 to add a unary minus operator that
has higher precedence than either + or *.

<assign> → <id> = <expr>

<id>        → A | B | C

<expr>    → <expr> + <term>   
                     | <term>
                     | - <factor>

<term>    → <term>*<factor>
                     | <factor>
<factor>  → (<expr>)
                     | <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

No comments:

Post a Comment