質問

Unfortunately, it is not possible for ANTLR to support direct-left recursion when the rule has parameters passed. The only viable option is to remove the left recursion. Is there a way to remove the left-recursion in the following grammar ?


a[int x]
    : b a[$x] c
    | a[$x - 1] 
    (
          c a[$x - 1]
        | b c
    )
    ;

The problem is in the second alternative involving left recursion. Any kind of help would be much appreciated.

役に立ちましたか?

解決

Without the parameters and easier formatting, it would look like this:

a
 : b a c
 | a (c a | b c)
 ;

When a's left recursive alternative is matched n times, it would just mean that (c a | b c) will be matched n times, pre-pended with the terminating b a c (the first alternative). That means that this rule will always start with b a c, followed by zero or more occurrences of (c a | b c):

a
 : b a c (c a | b c)*
 ;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top