For G22.2130-001: Compiler Construction Course, Spring 2010
This page contains answers from the instructor to frequently asked questions from students related to the course material. This will benefit everybody and also will reduce the amound of repeated questions.
In lecture 7, Slide 11(Page 11 in PDF), In the SLR table,the Action for state 5 is populated at "+',"*",")","$" symbols. Can you please explain why these symbols in particular were populated with r6.
State 5 corresponds to F->id. There are no outgoing arrows from that state as you can see from the figure in the same page. This means we cannot shift, but have to reduce. The head of the production rule F->id. is F. Therefore, in order to ensure that we are paring along the correct grammar, the reduction must be done only if the next token is in FOLLOW(F), otherwise it is a parsing error.
[The following tip is given by Paul Londino] Multi character tokens in Bison:
It turns out that there is an array called yytoknum, and
you need to pass the index from yytname to yytoknum to get the actual value of
the token. This is not documented in the Bison 1.875 manual.
Additionally, I found that you have to specify "#define YYPRINT" in order
for the yytoknum array to be created.
Here is the link to the bug report I
found:
http://osdir.com/ml/parsers.
The classic IF-THEN-ELSE conflict in grammars
Section 4.3 in the textbook give a partial solution (also see slide 21 lecture 6). However, this partial solution needs to be incorporated in the while grammar. Here are some links that can help.
Here is how to do it:
Here is the correct solutions:
StructuredStatement :
MatchedStatement
| OpenStatement
;
MatchedStatement : IF Expression THEN
MatchedStatement ELSE MatchedStatement
| CompoundStatement
| WHILE Expression DO
MatchedStatement
|
FOR ID COLEQ Expression TO Expression DO MatchedStatement
;
OpenStatement : IF Expression
THEN Statement
| IF
Expression THEN MatchedStatement ELSE OpenStatement
| WHILE Expression DO OpenStatement
| FOR ID COLEQ Expression TO
Expression DO OpenStatement
;
Debugging Tip
If you are using yacc, use
yacc -v parser.y
This will generate
a file y.output that contains a lot of information
about the conflict and
other stuff.
This can help in debugging.