[antlr-interest] NoViableAltException in ()? rule

Ingo Maier i.maier at gmx.de
Thu Dec 2 14:35:46 PST 2004


Hi,

for a (x)? the generated code seems not only to check if an x comes
next, but also what can come after (or instead of) that x. If such a
token doesn't follow it throws a NoViableAltException.

For example in the java grammar:

tryBlock: "try"^ compoundStatement (handler)* (finallyClause)?;

If no finally clause comes next, it also checks for possibly following
tokens in the generated typeBlock() method:

switch ( LA(1)) {
  case LITERAL_finally:
  {
    ...
  }
  case FINAL:
  case ABSTRACT:
   ...
     break;


Why? Those following tokens have nothing to do with the rule tryBlock
itself, but only with the enclosing grammar, right?

As a consequence java snippets followed by some non-java text cannot be
parsed without an exception: 

try { /*...*/ } catch(Exception e) { /*...*/ } /* no finally clause */
here comes some text...

When I call tryBlock(), "here" is not expected because of the generated
switch block as pasted above. However, "here" should have nothing to do
with the tryBlock() rule.

Thanks in advance,
Ingo



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list