Web References on Lex & Yacc?

7 Aug 1999 01:57:04 -0400

          From comp.compilers

Related articles
Web References on Lex & Yacc? weedlet@my-deja.com (1999-08-07)
| List of all articles for this month |

From: weedlet@my-deja.com
Newsgroups: comp.compilers
Date: 7 Aug 1999 01:57:04 -0400
Organization: Compilers Central
Keywords: lex, question

Anybody has good technical references about lex & yacc on the Web? Most
resources I found are too introductive and not so technical.

BTW, here is a question I have:

Suppose during paring, you scanned a "id" of value "myvar", and you want
to return the type "ID" AND the value of it, i.e., the pair (ID,
"myvar"). Later on, suppose for the rule "statement: id EQUALS value",
you want the action to print out "$1 $3", then "$1" will generate a
syntax error. See below for the codes.





ID [a-zA-Z]+
INTEGER [0-9]+

INTEGER {yylval = atoi(yytext); return(INTEGER);}
ID {return(ID);} //adding "yylval = yytext" gives the same error ...


%token ID

                    ID EQUALS INTEGER
                    {printf("%s %d", $1, $3);}
[You need to use %union and %type so the parser knows that ID has a
string value and INTEGER has an int value, sort of like this:

%union {
int ival;
char *sval;

%token <ival> INTEGER
%token <sval> ID

In the lexer, you include y.tab.h to get the definition of yylval, and
write code that sets the right union members, e.g.:

INTEGER {yylval.ival = atoi(yytext); return(INTEGER);}
ID { yylval.sval = strdup(yytext); return(ID);}

For more details, you can always take my book out of the library. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.