Search code examples
cbisonflex-lexer

How to check semantic values of tokens in Flex/Bison


I'm trying to create simple Pascal compiler using Flex/Bison and I want to check what semantic values are stored withing tokens. I have following code for flex:

...
{ID}        {yylval.stringValue= strdup(yytext); return(ID);}
...

And following code in bison:

...
program: PROGRAM ID LBRACKET identifier_list RBRACKET DELIM declarations subprogram_declarations compound_statement DOT {printf($2);}
...

And following test file:

program example(input, output);
...

Flex and bison recognize all perfectly and parse is ok, but if I want check token values like in code before it has no effect:

Starting parse
Entering state 0
Reading a token: Next token is token PROGRAM ()
Shifting token PROGRAM ()
Entering state 1
Reading a token: Next token is token ID ()
Shifting token ID ()
Entering state 3

Is there a way to print token value inside (), like token ID (example). I've checked similar questions and they do it the same way, or maybe I'm just missing something.

P.S. When I enable debug mode for flex it shows that it accepted "example" by rule {ID}, but where does that example stored and how should I use it in advance.


Solution

  • Bison cannot know for itself, where the semantic values shall be taken from. So you have to define %printers for your tokens. In your case you have to define the type of the token and a corresponding printer:

    %token <stringValue> ID
    %printer { fprintf(yyoutput, "%s", $$); } ID;
    

    Define one printer for each token, which you want to deep-inspect in traces, then it should work as you expect.