问题描述:

I use JAVACC to parse some string defined by a bnf grammar with initial non-terminal G.

I would like to catch errors thrown by TokenMgrError.

In particular, I want to handle the following two cases:

  1. If some prefix of the input satisfies G, but not all of the symbols are read from the input, consider this case as normal and return AST for found prefix by a call to G().
  2. If the input has no prefix satisfying G, return null from G().

Currently I'm getting TokenMgrError 's in each of this case instead.

I started to modify the generated files (i.e, to change Error to Exception and add appropriate try/catch/throws statements), but I found it to be tedious. In addition, automatic generation of the modified files produced by JAVACC does not work. Is there a smarter way to accomplish this?

网友答案:

You can always eliminate all TokenMgrErrors by including

<*> TOKEN : { <UNEXPECTED: ~[] > }

as the final rule. This pushes all you issues to the grammar level where you can generally deal with them more easily.

相关阅读:
Top