delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/06/11:58:14

From: Andrew Crabtree <andrewc AT typhoon DOT rose DOT hp DOT com>
Message-Id: <199706061555.AA006622503@typhoon.rose.hp.com>
Subject: Re: Can't get YACC parser to run
To: tron DOT thomas AT sierra DOT com (Tron Thomas)
Date: Fri, 06 Jun 1997 8:55:03 PDT
Cc: djgpp AT delorie DOT com
In-Reply-To: <33970040.7AF0@sierra.com>; from "Tron Thomas" at Jun 05, 97 11:06 am

> I created the program in example 1-7 (a Yacc Parser) of chapter one, but
That example is just bad.

> main( )
> {
> 	while(!feof(yyin)){

Normally, if you don't do anything else, yyin will be initialized
to be stdin.  This happens in the yylex function

lex.yy.c

YY_DECL prototype
<snip>
if (!yyin) yyin = stdin;

which won't get called until you are in yyparse.  So, you can just
manually assign yyin=stdin first thing in main.  I would probably be
better though, to change to grammar file so that it accepts 
an infinite number of sentences, and then write a special rule for eof.  

sentence_list : sentence
       | sentence_list sentence

sentence : subject VERB object
<more stuff here from example>

Then maybe add a 

<<EOF>> {return 0}

in lex.

Now you can just call yyparse directly in main.

int main()
{
yyparse();
return 0;
}

HTH

Andrew

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019