CST 8152 Compilers - Assignment #5 Demonstration required in Labs March 26-27
This assignment builds on Assignment #4. It has you use your scanner in a recursive-descent parser that recognizes assignment statements. Next assignment will build on this assignment to recognize expressions as well as simple assignment statements.
1. Adapt your scanner to set and return the address of a global token structure containing the type of the token and its value. (As per class/WWW notes.)
2. Write a single recursive descent parsing function named p_assignment() that handles simple assignment statements expressed by this grammar rule:
IDENTIFIER = IDENTIFIER | STRING | INTEGER ;
3. When your parser first recognizes an IDENTIFIER on the left-hand-side of the assignment statement, it should look up the identifier in your symbol table and add an empty entry for it if it is not already there.
4. When the right-hand-side of the assignment statement is recognized, the value and type of the recognized token should be entered into the symbol table entry of the identifier found on the left-hand-side. Integers should be stored as integers, not as strings of digits. You can convert a string of digits to an integer either in your scanner or in your parser. Having the scanner do it is best; your scanner will have to be able to return either strings or integers via the global token structure. A C-language union would work very well in the global token structure and in the symbol table to share storage among the various possible return types. (See the class notes.)
5. The first time an identifier is assigned-to sets its type. If the identifier on the left-hand-side already has a type (because of a previous assignment statement), and that type is not the same as the type being assigned in a subsequent assignment statement, decide what to do. You may issue a warning and change the type. You may decide to ignore the assignment and issue an error message. You may attempt to convert the type to be compatible with the type already set; for example, you might attempt to convert a string into an integer using sscanf(), or convert an integer into a string using sprintf(). A failure to convert is an error that should be reported. (Ambitious students may want to implement a <_declare> statement to declare and type all variables before use!)