views:

114

answers:

1

When I redirect input to my yacc program from an input file, after it finishes parsing the file I want the yacc parser to print a summary of what it did. I want it to do the same thing if I am entering input through the keyboard and then I press Ctrl-D. Is there a way to do that?

A: 

I found out that the solution to my problem is to create a function yywrap() in the .y file. That function will run when yacc encounters an EOF, which is either the end of an input file or the user pressing Ctrl-D. This is the solution.

Phenom
This runs when _lex_ encounters EOF, not when yacc does. These are two different times. It is an important distinction to make. `yywrap` shouldn't be doing anything anyway - it's a terrible form of "flow control" built into lex. The kind of actions you might do in `yywrap` should be handled in your program's `main()` function.
Chris Lutz
-1: agreeing with Chris, it's there so you can change to the next input file, when processing many files.
Simeon Pilgrim
I don't have a main function, and even if I did, I doubt that it could do anything since main would be in the .l file and all my variables are in the .y file.
Phenom
All (C/C++) programs have a `main()` function, you're just using the one provided for you by the yacc library.
Chris Lutz