34. Given the following grammar <Exp> -> <Exp> <Addop> <Term> | <Term> <Addop> -
ID: 3550246 • Letter: 3
Question
34. Given the following grammar
<Exp> -> <Exp> <Addop> <Term> | <Term>
<Addop> -> + | -
<Term> -> <Term> <Multop> <Factor> | <Factor>
<Multop> -> *
<Factor> -> ( <Exp> ) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9
a. Show a left-most derivation for 2 * (3 + 4 ).
b. Draw a parse tree for this derivation.
35. In the grammar above, what is the associativity of <Addop>? Explain your answer.
36. When looking at a parse tree, how can you tell when one operator has precedence over
another? In the grammar above, does <Addop> have precedence over <Multop> or does
<Multop> have precedence over <Addop>? How can you tell the precedence just by
looking at the grammar?
Explanation / Answer
1. <Exp> - <Exp> <Term> => 2 * (3 + 4 )
2. <Term> -> <Term> <Multop> <Factor> => 2* (3+4)
3. <Factor> -> ( <Exp> ) => 2* (3+4)
4. <Exp> -> <Exp> <Addop> <Term> => 2*(3+4)
5. 2*7
6. 14
associativity of <Addop> = <Exp> <Addop> <Term>
<Term> <Multop> <Factor> | <Factor>
( <Exp> ) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9
it will first do multiplication. First term will be solved.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.