Left recursion: What does precpred do?

10 views
Skip to first unread message

Schrello

unread,
Dec 5, 2019, 4:21:10 PM12/5/19
to antlr-discussion
Hey guys :)

I am writing my master thesis about ANTLRv4.

I am trying to figure out how the new Version rewrites a left-recursiv rule. 

With respect to the book, ANTLR used to use a recursive alternative. But in the new version (7.4.2) it seems to use a method "precpred()". 

I'm trying to figure out what precpred (RuleContext localctx, int precedence), a class parser method, does.

It seems to compare the passed precedence with the top value on an _precedenceStack. What does the Stack contain?
And why uses the Method an RuleContex as a parameter... it doesn't seem to need or to use it.


The rewritten version of the example "expr" in the book looks like this now:

expr
    :   ( {} INT<tokenIndex=31> 
        | ID<tokenIndex=35> 
        )
        (
          {precpred(_ctx, 4)}?<p=4> '*'<tokenIndex=17> expr<tokenIndex=19,p=5>
                  | {precpred(_ctx, 3)}?<p=3> '+'<tokenIndex=25> expr<tokenIndex=27,p=4>
        )*
    ;


I hope you can help me :))
Reply all
Reply to author
Forward
0 new messages