Docsity

Ambiguous Grammar and Parsing: Ambiguity Resolution and Parse Trees, Papers of Computer Science

Toggle sidebar
bg1
1. Here is a grammar for part of C expressions:
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.)
2. Consider this grammar:
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.
3. Consider this grammar:
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.
pf2

Partial preview of the text

Download Ambiguous Grammar and Parsing: Ambiguity Resolution and Parse Trees and more Papers Computer Science in PDF only on Docsity!

  1. Here is a grammar for part of C expressions:

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

  1. Consider this grammar:

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.

  1. Consider this grammar:

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.

  1. This grammar is not LR(1). It has a shift-reduce conflict:

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.