[comp.unix.programmer] lex, yacc question

ricks@nrl.navy.mil (Richard Schumeyer) (06/13/91)

Is there a way to get lex and yacc to read from somewhere other than
stdin?  
--
------------------------------------------------------------
Rick Schumeyer           ricks@luke.nrl.navy.mil 
Naval Research Lab       (202)404-7966
Space Science Division
------------------------------------------------------------

plona@romulus.rutgers.edu (Lawrence Plona) (06/13/91)

From: ricks@nrl.navy.mil (Richard Schumeyer)
> Is there a way to get lex and yacc to read from somewhere other than
> stdin?  

   yacc declares "FILE *yyin".  A pre-processor might be set up like:

   extern FILE *yyin;

   yyin = popen( "/usr/bin/m4 infile", "r" );
   yyparse();
   pclose( yyin );
-- 

Sit denique inscriptum in fronte unius  "Let it finally be written on
cuiusque quid de rebus publicis sentiat  the forehead of each man what he
- Cicero                                 thinks of open systems"

eoshaugh@nmsu.edu (Erik Oshaughnessy) (06/14/91)

plona@romulus.rutgers.edu (Lawrence Plona) writes:
]From: ricks@nrl.navy.mil (Richard Schumeyer)
]> Is there a way to get lex and yacc to read from somewhere other than
]> stdin?  
]
]   yacc declares "FILE *yyin".  A pre-processor might be set up like:
]
]   extern FILE *yyin;
]
]   yyin = popen( "/usr/bin/m4 infile", "r" );
]   yyparse();
]   pclose( yyin );
	
Both yacc and lex use the file pointers yyin and yyout to communicate
with the environment.  I'm not exactly sure why Mr. Plona used
popen(3) to start another process when the same thing can be
accomplished by an fopen(3) call, without the extra overhead of the
second process.  What benifits are gained from this, if at all?

Inquiring minds want to know!

--
___________________________________________________________________________
Erik O'Shaughnessy Mac/Unix Support Guy	|Phrase of the day:  
Small Systems				|       "Poetically Inert"
New Mexico State University		|
eoshaugh@nmsu.edu			|"Help! I'm in ed!" - me
----------------------------------------------------------------------------

tim@cs.columbia.edu (Timothy Jones) (06/14/91)

Or, you could do a reopen() on the input file and stdin.
--
					- Tim Jones
					  tim@cs.columbia.edu

ricks@luke.nrl.navy.mil (Richard Schumeyer) (06/14/91)

I appreciate everyone's response to my lex, yacc question.  I now have
another one:  I want to be able to call yyparse() more than once to
parse different files.  For testing, I have it set up to parse the
same file twice.  The first time, everything works fine.  The
second time the parser returns an error.  Do I need to reinitialize
yyparse somehow before using it again?  How?

--
------------------------------------------------------------
Rick Schumeyer           ricks@luke.nrl.navy.mil 
Naval Research Lab       (202)404-7966
Space Science Division
------------------------------------------------------------