Search code examples
bisonyacc

Printing multi-character tokens in YACC error messages


If a syntax error is detected by Yacc and verbose errors is defined, an error message is printed, for example

unexpected '[', expecting BECOMES

Is there a way to replace the token name for multi-character tokens (e.g. BECOMES) with the actual string (e.g. :=) in the error message? The reason I'm asking is that it may not be obvious to the user of the parser that BECOMES stand for :=.


Solution

  • You can define a human-readable version of a token name by adding it in quotes after the token name in the %token definition:

    %token BECOMES "':='"
    

    This will change the error message to:

    unexpected '[', expecting ':='