|
|
Questions
Homework assignments must be done individually. Collaboration on homework assignments is
not allowed.
Question 1:
Here's a CFG for declarations of structs in a language simliar to C.
(Classes in C++ and Java are generalizations of structs.)
strt → STRUCT ID LCURLY members RCURLY SEMICOLON
members → decllist | λ
declList → declList decl | decl
decl → TYPE ID SEMICOLON | TYPE ID ASSIGN INTLIT SEMICOLON
Although the CFG given above is not LL(1), some correct struct declarations can be parsed by a predictive parser.
This is because certain inputs may never cause the parser to look at a table entry that contains two or more CFG rules.
However, some declaration cannot be parsed by a predictive parser because the CFG is not LL(1).
- Give a struct declaration that is in the language of the CFG and can be parsed by a predictive parser.
- Give the shortest struct declaration that is the language of the CFG but cannot be parsed by a predictive parser.
What is the earliest point in the course at which the parser broke?
(i.e., a sequence of tokens that is a prefix of a valid input, but
for which the parser would not know how
to continue to build the parse tree top-down because it looks at
a table entry that contains two or more CFG rules)?
To answer this question, give all of the following:
- The sequence of tokens (a prefix of a valid input).
- The CFG rules that the predictive parser can't choose between
to continue to grow the parse tree.
-
One problem with the CFG is that it has a common prefix problem (it has not been left factored).
Find the CFG rules with a common prefix and transform them by doing
left factoring.
-
Another problem with the CFG is that is has left
recursion.
Find the CFG rules that cause this and remove the
left recursion.
Question 2:
This question concerns the following grammar.
program -> { stmts }
stmts -> stmt stmts
| λ
stmt -> ID = exp ;
exp -> ID tail
tail -> + exp
| λ
Fill in the LL(1) Parse Table for this grammar.
|
{ |
} |
ID |
+ |
; |
= |
EOF |
program |
|
|
|
|
|
|
|
stmts |
|
|
|
|
|
|
|
stmt |
|
|
|
|
|
|
|
exp |
|
|
|
|
|
|
|
tail |
|
|
|
|
|
|
|
|