Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!ericom!mvsun.ericsson.se!rom From: rom@mvsun.ericsson.se (Robert Malmgren TK/DG) Newsgroups: comp.unix.questions Subject: Lex & Yacc Summary: Why doesn't my example work? Keywords: lex, yacc Message-ID: Date: 4 Mar 91 16:17:16 GMT Sender: news@ericsson.se Distribution: comp Organization: Ericsson Lines: 49 I'm trying to learn yacc and lex, but I'm having a tough time. I've written a simple testprogram that does an arithmetic operation on two args. The problem is that it works the first time I enter the digits and prints the correct result, but fails with a 'syntax error'-message the second time. Why? Could someone please enlighten me. --------------------------------------- c.l ----------------------------------- %% [ \t]+ ; [0-9]+ { sscanf(yytext, "%d", &yylval); return INTEGER; } [-+\*] return yytext[0]; \n return '\n'; %% --------------------------------------- c.y ----------------------------------- %token INTEGER %% line: '\n' | expr '\n' {printf("%d\n", $1);} ; expr: expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | int ; int: INTEGER { $$ = $1; } ; %% #include "lex.yy.c" ------------------------------------------------------------------------------- Robert Malmgren ! Phone: +46 8 7197937 ! Internet: rom@miranda.ericsson.se MV/ETX/TK/DG ! Fax : +46 8 7196443 ! UUCP: ..uunet!mvsun.ericsson.se!rom Ericsson Telecom ! Home : +46 8 933733 ! SUNET : MINDA::ROBERT