0

I would like like to stop the execution of the program when facing some error. How to do that when using lex? I can just call normal C exit like:

exit( status );

or is there a more neat way to do it with Lex?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
user836026
  • 10,608
  • 15
  • 73
  • 129

1 Answers1

1

You can do just that - but:

  • Just printing a message to the standard error and exiting is what lex does—when it encounters a fatal error, e.g., buffer allocation problems. See for example YY_FATAL_ERROR, which is used for that purpose. It can be overridden, e.g., to an error recovery routine.
  • A typical lexer does not exit with an error: it returns state information to the parser which decides whether a state is legal.

If your lexer serves as the parser, then exiting from the lexer would make sense.

Further reading:

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105