Flex/Lex - How to know if a variable was declared

My grammar allows: C → id := E // assign a value/expression to a variable (VAR) C → print(id) // print variables(VAR) values To get it done, my lex file is:

How can I solve it?

Piece of yacc file:

 %union < int int_val; int var_index; >%token INTEGER %token VAR . | PRINT '(' VAR ')' < n_lines++; printf("%d\n",values[$3]); >. | VAR
Renaro Santos asked Dec 4, 2014 at 1:04 Renaro Santos Renaro Santos 403 1 1 gold badge 9 9 silver badges 19 19 bronze badges

1 Answer 1

This does seem a bit like a Computer Science class homework question for us to do.

Normally one would not use bison/yacc in this way. One would do the parse with bison/yacc and make a parse tree which then gets walked to perform semantic checks, such as checking for declaration before use and so on. The identifiers would normally be managed in a symbol table, rather than just a table of values to enable other attributes, such as declared to be managed. It's for these reasons that it looks like an exercise rather than a realistic application of the tools. OK; those disclaimers disposed of, lets get to an answer.

The problem would be solved by remembering what has been declared and what not. If one does not plan to use a full symbol table then a simple array of booleans indicating which are the valid values could be used. The array can be initialised to false and set to true on declaration. This value can be checked when a variable is used. As C uses ints for boolean we can use that. The only changes needed are in the bison/yacc. You omitted any syntax for the declarations, but as you indicated they are declared there must be some. I guessed.

%union < int int_val; int var_index; >int [MAX_TABLE_SIZE] declared; /* initialize to zero before starting parse */ %token INTEGER %token VAR . | DECLARE '(' VAR ')' < n_lines++; declared[$3] = 1; >. | PRINT '(' VAR ')' < n_lines++; if (declared[$3]) printf("%d\n",values[$3]); else printf("Variable undeclared\n"); >. | VAR