tags:

views:

62

answers:

2

Hi all. I'm writing a program with Yacc/Flex and I'm using the following code (not exactly the same because I'm mixing code from other file):

DataType datat;
%union {  
    int integer;  
    char *string;  
    DataType type;  
}

Integer { yylval.type = INTEGER; return INT; }
%type <type> INT
data : INTNUM { yylval.type = INTEGER; }

Then if I write something like this:

foo : data { bar(yylval.type); }

bar gets correctly the datatype INTEGER, but if I have this:

foo : data data { ??? }

How do I get the yylval.type for the first and second member separately?

Thanks a lot!

+1  A: 

I don't completely understand your question, but in lex/yacc (or flex/bison) you have to use the following conventions:

  • Use $1 to refer to the yylval of the first item
  • Use $2 to refer to the yylval of the second item
  • Use $$ to refer to the yylval of the resulting (target) item

E.g. if you would write a simple integer calculator, the rule that defines the sum, would be written something like this:

Sum : Term '+' Term
   {
   $$.Value = $1.Value + $3.Value
   }

Hope this helps.

Patrick
Thanks but *$1.type* or *$2.type* don't work for me. I get compiler errors.
Menda
Look at the Lex and Yacc manuals at http://dinosaur.compilertools.net/.On the page http://memphis.compilertools.net/interpreter.html you can find a simple interpreter/calculator that shows that you should really use $1, $2, ...
Patrick
A: 

yylval.type will return you the type from the last token scanned, which might be the Integer that got reduced to data, but might be the token after it. You should NEVER access yylval directly in the yacc/bison file, as its not reliable.

Instead, you should use $1, $2, etc to access the YYSTYPE info of the items as noted by Patrick. To make this work for non-terminals, you need to use %type declarations for them in the yacc/bison file, and the rules for those non terminals need to set $$, eg:

%type <type> data

%%

data : INTNUM { $$ = INTEGER; }

foo : data { bar($1); }

foo : data data { bar($1, $2); }
Chris Dodd