Grammar Designer recursion question

SyntaxEditor for Windows Forms Forum

Posted 17 years ago by Anton Filimonov
Version: 4.0.0233
Hi, I'm creating my custom language add-on using Grammar Designer. My grammar requires these NonTerminals:

PrimaryExpression :

InvocationExpression :
PrimaryExpression ParenthesisOpen arguments ParenthesisClose

In xml grammar definition I write these rules like this:

    <NonTerminal Key="PrimaryExpression" Parameters="out PrimaryExpression pr">
          <%pr = null;%>
          ("InvocationExpression<@out pr@>")
          <%pr.EndOffset = this.Token.EndOffset;%>

    <NonTerminal Key="InvocationExpression" Parameters="out InvocationExpression expr">
          PrimaryExpression pr;
          ArrayList args;
          expr = null;
        "PrimaryExpression<@out pr@>"
        "Arguments<@out args@>"
          expr = new InvocationExpression();
          expr.Primary = pr;
          expr.Arguments = args
The problem is that when trying to generate semantic parser I get this error message:

A parser generator exception occured:
Production PrimaryStatement allows for infinite left recursion

Does this mean that parser for such type of grammar can't be automatically generated? Or is there a way I should rewrite production code to make it work with Grammar Designer?

Thank you,
Anton Filimonov

Comments (1)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Correct, left recursion is not allowed with any LL(k) type parser. If you look at the code that a LL(k) parser generates, doing recursion the way you have it will cause an infinite stack recursion since the MatchPrimaryExpression method will call the MatchInvocationExpression method, which will call MatchPrimaryExpression, and so on, thus leading to a stack overflow.

I did a quick google and found this page which explains left recursion pretty well for LL(k) languages and how to alter your language to handle it.

Sometimes eliminating left recursion can be tricky. In our C# language (for the .NET Languages add-on) we spent a little while trying to eliminate it for expressions. Our Visual Basic language development has been difficult, maybe even moreso, for the same reason.

Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.