Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!infonode!ingr!b11!dclark From: dclark@b11.ingr.com (Dave Clark) Newsgroups: comp.unix.questions Subject: Re: Lex & Yacc Keywords: lex, yacc Message-ID: <1991Mar6.185805.7930@b11.ingr.com> Date: 6 Mar 91 18:58:05 GMT References: Distribution: comp Organization: Intergraph Corp. Huntsville, AL Lines: 40 rom@mvsun.ericsson.se (Robert Malmgren TK/DG) writes: >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. {Lex stuff deleted} >--------------------------------------- c.y ----------------------------------- >%token INTEGER >%% >line: '\n' > | expr '\n' > {printf("%d\n", $1);} > ; {Yacc stuff deleted} Yacc begins parsing with the first production ("line" in your example). To change this, use the %start directive. The first production is successfully parsed -- fine. But your grammar defines that there should be only one "line" per file. To fix this, you can add the following production (before "line" in your file). file: /* empty */ | line file ; This allows a "file" to consist of zero or more lines. -- ---------------------------------------------------------------------------- Dave Clark | System Development | I among men bear the same wounded hand, Intergraph Corp., CR1102 | suffer the same reddened cup