Using Flex and Bison to parse the variable declaration section in Pascal
Hi everyone, I'm undertaking a project to convert Pascal to ARM assembler using Flex and Bison with C++. I have a problem when attempting to add variable information to a symbol table. The problem is as follows:
In Pascal, when a variable is declared, the TYPE of the variable follows the actual identifier e.g.
VAR a,b : int;
When my parser interprets this line, it cannot tell that "a" and "b" are integers until it reaches the end of the line. So, when adding to my symbol table, I must use some sort of temporary store to save a list of identifiers encountered before the end of the line, at which point I finally know the TYPE of variable, and it can be added to the symbol table.
The grammar I am using is as follows:
D -> L : T (Variable declaration)
T -> identifier (The TYPE i.e. integer, char or user defined type)
L -> L , identifier | identifier (list of variables)
I was wondering if there is a better method of solving this problem, for example changing the underlying grammar rules, rather than implementing an extra data structure to temporarily store the variable names.
Thanks in advance,
M
|