Lex / Yacc: print a message before entering

I am trying to figure out how I can display a message / prompt when using lex / yacc (flex / bison).

For example, the main thing looks like this:

int main(int argc, char *argv[]) { yyparse(); } 

What a yacc call that calls yylex (). This gives an empty string waiting for STDIN. How can I display a message like ...

  message $ _ 

instead

  _ 

If the underscore represents the cursor position, waiting for input from STDIN ...

I forgot to mention, I would like the invitation to be printed multiple times ... therefore, before each input of the lex / yacc request from stdin ..

+4
source share
1 answer

Figured it out. I had to integrate it as an action into the yacc file.

My mine is as follows:

 int main(int argc, char *argv[]) { prompt(); yyparse(); } 

And my yacc (.y) file looks like ...

  stmnt
      : / * empty * /
      |  stmnt whatever {do_something ();  prompt ();  }
      ;

So that every time it parses stmnt (top level), it displays a prompt later.

+8
source

All Articles