Project 2 FAQs

Project 2 FAQs

Q) I get a lot of compile errors and link errors.

A) A few pointers:

    • The assignment sheet did #include "lex.yy.c" inside grammar.y. That should be removed since we are now linking lex.yy.c in a separate object, if you look at the diagram.

    • Inside lex.l, Instead of including "token.h", you should include the following:

    • #include "proj2.h"

    • #include "y.tab.h"

    • Yacc should have generated all the token definitions inside "y.tab.h", so you'll no longer need "token.h". Also, you need to include "proj2.h" before "y.tab.h" since it contains the YYSTYPE type definition needed inside "y.tab.h".

Q) Yacc complains with the error "typedef redefinition with different types ('int' vs 'union YYSTYPE')" after having done the above.

A) That is because you are using a different version of yacc than the one installed on the elements machines, possible a Mac version. The above problem will not occur if you try compiling on the elements machines. The Mac version inserts the following lines (or similar) in y.tab.h:

#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLAREDtypedef int YYSTYPE;# define yystype YYSTYPE# define YYSTYPE_IS_DECLARED 1# define YYSTYPE_IS_TRIVIAL 1#endif

You can fix proj2.h to make it compile, even on fhe Mac. Note that the typedef in y.tab.h is enclosed in a #if directive. You only need to disable it by doing:

#define YYSTYPE_IS_DECLARED

somewhere in your proj2.h.

So, you can change the typedef in proj2.h to the following:

Q) What is the '.' token in the Type flow chart supposed to be?

A) You can ignore that dot. It was meant to allow type declarations for nested classes but it is no longer used.

Q) Is there a "correct" grammar that we need to get right?

A) No, you can write whatever grammar will produce the parse trees and accept the strings described in the flow chart diagrams in the assignment sheet. In fact, grammars will differ slightly from student to student, even though they produce the same syntax tree.

Q) Do I need to print multiple errors when there are many errors?

A) While we learned how to do error recovery with an LR parser in theory during class, I will not ask you to do it for this project. You just need to exit after printing the error line number.

Q) My parser just refuses to work for the most basic grammars.

A) Make sure you are not defining token values twice. When you compile a .y file with yacc, it will generate two files: y.tab.h and y.tab.c. The file y.tab.h contains #defines for all the tokens declared in your .y file. Now, if you include the #defines you used in your lex project that have different values, that is going to cause problems. The token values returned by your lexer will be different from the values your parser expect.

Q) My parser gives a syntax error at EOF (end-of-file), after it successfully reduced the start symbol.

A) According to the Yacc manual: "For historical reasons, the endmarker must have token number 0 or negative. This token number cannot be redefined by the user; thus, all lexical analyzers should be prepared to return 0 or negative as a token number upon reaching the end of their input." So when you reach EOF in your lexer, you must return 0 or a negative number instead of returning EOFnum that Yacc defines as a positive number in y.tab.h.