Lex & Yacc

Robert Malmgren TK/DG rom at mvsun.ericsson.se
Tue Mar 5 02:17:16 AEST 1991


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 at 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



More information about the Comp.unix.questions mailing list