I'm using Bison with %define parse.error verbose option to provide more informative error messages. Bison generates parser in Java language. Currently parser prints messages such as

syntax error, unexpected IDENTIFIER, expecting RPAREN or COMMA

I wanted to use %printer declarations to replace default token names, but I have found in bison docs that they are not supported in Java:

Java parsers do not support %printer, as toString() can be used to print the semantic values. This however may change (in a backwards-compatible way) in future versions of Bison.

Each token has declared sematic value's type, e.g. %token <Symbol> IDENTIFIER where Symbol has custom toString() method.

But still I do not know how to tell parser to call toSting() method instead of printing default token's name.

Do you have any suggestions?

Thanks in advance!

Grzegorz

有帮助吗?

解决方案

Declare the tokens with an alias:

%token <id> IDENTIFIER "identifier"
%token RPAREN "right parenthesis"

The %printer function wouldn't be used in an error message, even if it were implemented in Java, because the error is talking about tokens which have not been encountered, whereas the %printer method is used to print the semantic value of tokens which were encountered. The unencountered token does not exist, and therefore its semantic value cannot be printed, only its description.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top