LEX / YACC yywrap() question

LEX / YACC yywrap() question

Post by David You » Sun, 11 Dec 1994 03:34:12

I'm currently writing some lex/yacc code to interpret somebodies
"made up" langugage, in which much source code has already been written.
This "language" features a primitive type of scope on a "per file" basis.

This is to say that the language specification allows source files
to include other source files.  A variable is valid in the source file in
which it is declared and in any child source files included by it.

I intended to utilize the yywrap() function in lex to detect when I
hit EOF upon a source file and using a primitive form of stack, redirect
input to the file which included the source file that we have just run out of.
This would have meant that yacc just got one stream of tokens and was
blissfully oblivious to which source files the input tokens were actually
coming from.

However, upon EOF of an included file, I wish to rid myself of those symbols
within a symbol table ( well, list of defined variables anyway ;-) ) which
had been declared within that source file ( the symbol table is to be
stored and manipulated in yacc ).
I had hoped to "sort of" insert a "END_OF_BLOCK" token into the
stream of input tokens from lex to yacc each time that I returned from an
included file to it's parent file.

To my understanding, yacc calls lex ( yyparse() ) for each token it wishes
to process, so my question is "Can I force-feed yacc() extra tokens from

Is there another way  around this problem that my lack of experience with
lex and yacc hasn't revealed to me yet? Can I simulate putting extra
characters into the source input character stream upon hitting EOF of
an included file ( these extra characters being some special token I can
recognise and then inform lex that I have found )?

I'd like to avoid "messy" solutions such as preprocessing the source files
to place extra text at the end of the input source files. I'm really aiming
to implement this entirely in lex adn yacc if possible.

Any help would be appreciated, please keep in mind that I'm no expert on
this subject. I don't have much access to network news ( I'm actually
telnetting to a machine in Perth, Australia to send you this ), so could you

is a non-trivial problem, maybe you'd like to post a response to this
newsgroup as well.


Dave Young
Software Engineer
Comsys Int'l, Australia

| David Young, Software engineer, Comsys, Perth, Western Australia |


1. lex/yacc parsing: ending parse with yywrap?

I'm parsing a config file with lex/yacc, but for some reason it tries
to keep going after it reaches EOF.  It's correctly calling yywrap when
EOF is detected, but I'm closing yyin and returning 1 in yywrap and it
still keeps going.  With yydebug on it looks like:

[Lots of yydebug stmts: shifts, reads, reduces, working correctly]
yydebug: state 14, reading 0 (end-of-file)
syntax error
yydebug: error recovery discarding state 14
[Error recovery, then bails because yyparse returns 1]

I tried putting in a <<EOF>> state in the lexer, but it didn't
help, even though it was properly detected.  I must be missing
something stupid here -- help?

TIA --


WWW: http://www.primus.com/staff/paulp/       PHONE: (619) 220-0850

"Just where in the constitution does it explain this ludicrous concept of
 bills being passed into law without a presidential signature or a
 legislative override?"
     -- Charles Eicher, misc.legal

2. sony PCI memory stick under red hat 9

3. lex yywrap() call

4. problems using printf !!

5. lex undefined reference to yywrap

6. cannot varyonvg

7. Simple lex v. yacc question

8. Apache log entry

9. Lex (Flex) and Yacc (or Bison) questions.

10. Followup YACC & LEX Question

11. YACC and LEX question

12. lex/yacc question

13. lex, yacc question