You are on page 1of 7

Assignment Chapter 3

6. Define a left-recursive grammar rule.


-grammar rule has its LHS also appearing at the beginning of its RHS so the rule is said to be left-
recursive.
7. What three extensions are common to most EBNFs?
-The first of these denotes an optional part of an RHS, which is delimited by brackets.
-The second extension is the use of braces in an RHS to indicate that the enclosed part can be
repeated.
-The third common extension deals with multiple-choice options.
8. Distinguish between static and dynamic semantics.
-static semantics of a language is only indirectly related to the meaning of programs during execution and
because the analysis required to check these specifications can be done at compile time.

9. What purpose do predicates serve in an attribute grammar?
-Predicates function state the static semantic rules of the language,which associated with grammar
rules. A false value in a predicate function indicates that there is a violation of syntax or static semantics
and thus determines whether an action is allowed or not.

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

-Synthesized attributes are used to pass semantic information up the parse tree, while inherited
attributes help pass semantic information down and across it.


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 + (<id> * <id>))
=> 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 = <id>* (<expr>)
=> B = C * (<expr>)
=> B = C * (<id> * <expr>)
=> B = C * (A * <expr>)
=> B = C * (A * <id> + <expr>)
=> B = C * (A * <id> + <id>)
=> 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 = <factor> * <factor>
=> A = <factor> * <id>
=> A = ( <expr> ) * <id>
=> A = ( <term> + <term> ) * <id>
=> A = ( <id> + <term> ) * <id>
=> A = ( <id> + <id> ) * <id>
=> A = ( A + <id> ) * <id>
=> A = ( A + B ) * <id>
=> A = ( A + B ) * C

b. A = B + C + A
<assign> => <id> = <expr>
=> A = <expr>
=> A = <term> + <expr>
=> A = <id> + <expr>
=> A = <id> + <term> + <term>
=> A = <id> + <id> + <term>
=> A = <id> + <id> + <id>
=> A = B + <id> + <id>
=> A = B + C + <id>
=> A = B + C + A

c. A = A * (B + C)
<assign> => <id> = <expr>
=> A = <expr>
=> A = <term>
=> A = <term> * <factor>
=> A = <id> * <factor>
=> A = A * <factor>
=> A = A * ( <expr> )
=> A = A * ( <term> + <term> )
=> A = A * ( <id> + <term> )
=> A = A * ( <id> + <id> )
=> A = A * ( B + <id> )
=> A = A * ( B + C )

d. A = B * (C * (A + B))
<assign> => <id> = <expr>
=> A = <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 * ( <id> + <term> ) )
=> A = B * ( C * ( <id> + <id> ) )
=> 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
-This grammar generates a different parse tree, that we can say the grammar are ambiguous. This one
generates two different parse trees.
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> <expr>
<expr> => <expr> + <expr>
| <term>
<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
S is equal with A B C while A is equal with a and B is equal with b and C is equal with c.

You might also like