tags:

views:

29

answers:

1
%{
    #include "y.tab.h"
    extern int yylval;
%}
%%
    [0-9]+ {yylval = atoi (yytext); return NUM;}
    [ \t]  ;
    \n     return 0;
    .      return yytext[0];
%%

When I use yylval which is pointing to the value, why should I return the NUM? I mean what does NUM represent (NUM is decalred in YACC section)? Is NUM storing any value?

I am very confused with this section even when it has proper solution. I am just not getting the right reason for returning NUM as I have pointer yylval in YACC.

+4  A: 

NUM is the token type. It's the category of the value rather than the value itself.

In other words, some of your own YACC code may not care what the actual value is, just that it's of a specific type.

A classic example is a (very) simple expression evaluator that takes NUM + NUM. You don't want a special rule for every case:

0 + 0
0 + 1
0 + 2
:
65535 + 65534
65535 + 65535

so you have one rule that handles the general case, then use yylval for the specific value.

paxdiablo