antlr - Java.g: ambiguous 'statement' rule? (assert) -


in java.g, can find following rule (whitespace removed clarity):

statement       :   block          |   ('assert') expression (':' expression)? ';'          |   'assert'  expression (':' expression)? ';'                      |   'if' parexpression statement ('else' statement)?                    (...)          ; 

i've 2 questions:

  1. are 2 'assert' subrules needed?
  2. if not, why doesn't antlr complain of ambiguity, since seem define same input?

thanks in advance.

pedro f. wrote:

are 2 'assert' subrules needed?

no, they're equivalent. however, looking @ way grammar java.g formatted:

statement      :   block      |   ('assert'         )         expression (':' expression)? ';'     |   'assert'  expression (':' expression)? ';'          |   ... 

i suspect (by looking @ empty spaces) grammar had embedded code in it:

statement      :   block     { /* embedded code */         }         |   ('assert' { /* embedded code */ }         )         expression (':' expression)? ';'     |   'assert'  expression (':' expression)? ';'          |   ... 

and perhaps more rules/tokens stripped (all guesswork, of course!).

pedro f. wrote:

if not, why doesn't antlr complain of ambiguity, since seem define same input?

because global backtracking (the backtrack=true; in options) enabled.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -