Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

an exception upon simple rule with double recursion in an alternative #26

Closed
parrt opened this issue Feb 27, 2012 · 3 comments
Closed

Comments

@parrt
Copy link
Member

parrt commented Feb 27, 2012

expr
   : 'x'
   | e1=expr e2=expr
   ;

then the following exception is thrown:

         [java] can't find rule grammarSpec or tree structure error: 
(COMBINED_GRAMMAR ANTLRv4 (RULES (RULE expr (BLOCK (ALT 'x') (ALT (= e1 
expr[1]) (= e2 expr[1]))))))
         [java] java.lang.reflect.InvocationTargetException
         [java]     at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         [java]     at 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
         [java]     at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
         [java]     at java.lang.reflect.Method.invoke(Unknown Source)
         [java]     at 
org.antlr.v4.parse.GrammarTreeVisitor.visit(GrammarTreeVisitor.java:192)
         [java]     at 
org.antlr.v4.parse.GrammarTreeVisitor.visitGrammar(GrammarTreeVisitor.java:186)
         [java]     at 
org.antlr.v4.semantics.SymbolCollector.process(SymbolCollector.java:69)
         [java]     at 
@sharwell
Copy link
Member

As far as the actual input symbols which could be matched by such a rule, it's easy to see the following rule would match exactly the same thing:

expr2
    : 'x'+
    ;

The problem lies in how to build the parse tree. If the input is x x, the only possible parse tree from the expr rule is:

(expr x x)

However, if the input is x x x, either of the following two trees is possible, and without an operator in place to specify an associativity there's no clear choice:

(expr (expr x x) x)
(expr x (expr x x))

Depending on the intended associativity and desired tree structure, the rule expr would need to be written in one of the following forms.

expr
    : 'x'
    | expr 'x'
    ;

expr
    : 'x'
    | 'x' expr
    ;

expr
    : 'x'+
    ;

@sharwell
Copy link
Member

You may also be able to produce a similar rule by moving the closure outside the expr rule:

exprs
    : expr+
    ;

expr
    : 'x'
    ;

sharwell added a commit to sharwell/antlr4 that referenced this issue Oct 26, 2012
@sharwell
Copy link
Member

This issue has been resolved; pull request parrt#105 contains a regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants