

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


E -> id | E? E : E | E << E
This grammar is ambiguous.
(a) Give a sentence that has two distinct parse trees, and show those parse trees.
(b) Explain how you might use ocamlyacc associativity declarations to resolve the conflict caused by this ambiguity. Be specific about what declarations you would use and what their effect would be. (The actual rules for resolving this ambiguity in C or Java do not matter; you just need to use the rules to resolve it in some way.)
F -> id ( A ) A -> ε | B B -> id id | B, id id
(a) Show a parse tree for the following sentence: f (C x, D y) (b) Write down all the sentential forms in that parse tree. (c) Calculate FIRST(F), FIRST(A), and FIRST(B). (d) Calculate FOLLOW(F), FOLLOW(A), and FOLLOW(B). (e) Why is this not an LL(1) grammar? (f) Transform it to an equivalent LL(1) grammar, and write a recursive descent parser for it. Assume the toke type is
type token = Id | LParen | RParen | Comma
and the parseF function has type
parseF: token list -> (token list) option
where option is the type defined as: type ’a option = Some ’a | None.
E -> E + T | T T -> T * id | id
Show a complete shift-reduce parse for the sentence x+y*z. Include columns Action, Stack, and Input, as we did in class. (Warning: this may take more than one piece of paper, depending upon how big you write. There are 11 steps, counting the final “Accept” step.
A -> B , $ B -> id | id , B
Show parse trees for two sentences with the following property: they lead to the same stack configuration (meaning, the roots of the trees on the stack or the same), with the same lookahead symbol, but in one case the correct action is the shift and in the other it is to reduce. Show that stack configuration.