When I redirect input to my yacc p开发者_StackOverflowrogram 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?
on the grammar you can call a function the function should be on the third part
FIRST PART
%%
second PART
GRAMMAR { CALL YOUR FUNCTION();}
GRAMMAR2 VAR1 VAR2 VAR3 {CALL YOUR FUNCTION2($1,$2,$3);}
%%
third PART
YOUR FUNCTION(){}
YOUR FUNCTION2(int c, char*y , int z){}
int main (void) {
}
void yyerror (char *s) {fprintf (stderr, "%s\n", s);}
your function can print any thing you want also the grammar becouse you now from where it call the function by the parameter you give
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.
精彩评论