xwcg
xwcg

Reputation: 196

JavaScript (ECMA) Grammar - Left Recursion Problems

First off: Using the LL(*) parser from the Actipro SyntaxEditor. This could apply to any LL(k) parser though.

I am currently trying to write a valid LL(k) grammar for Javascript (using the ECMA-262 reference), figuring out left recursion is okay sometimes, but I have been running against a wall in figuring out a solution for this problem. ( And may have lost focus since I have been working on this for three days non-stop now )

Okay, solution example:

Take this rule:

BitwiseORExpression :
    BitwiseXORExpression
    BitwiseORExpression | BitwiseXORExpression

which would literally become:

expBitwiseOR.Production = expBitwiseXOR | expBitwiseOR + @bitwiseOrSign + expBitwiseXOR;

and to avoid left recursion:

expBitwiseOR.Production = expBitwiseXOR + ( @bitwiseOrSign + expBitwiseXOR ).ZeroOrMore();

Simple enough. I am however stuck with more complex rules such as:

MemberExpression :
    PrimaryExpression
    FunctionExpression
    MemberExpression [ Expression ]
    MemberExpression . IdentifierName
    new MemberExpression Arguments

I have tried several things, some of which sort of work (i.e. they "work" but not as they should) and most of which don't (i.e. Left Recursive). I am honestly not hoping for a full answer or a complete solution, but maybe someone will have an idea on how to avoid this problem with a complex rule as that or something else.

No need to put this into a proper syntax for the Actipro Parser either, any mock-up code would still help!

Upvotes: 2

Views: 248

Answers (1)

xwcg
xwcg

Reputation: 196

As it is so often with these things, shortly after desperately asking, the answer comes to you while eating a sandwich. TL;DR: Solved like this:

expMemberExpression.Production = 
            (
            expPrimaryExpression
            |stFunctionExpression
            | (@NewKeyword + expMemberExpression + expArguments)
            ) + expMemberExpressionHelper.ZeroOrMore();

expMemberExpressionHelper.Production = (symLiteralOpenSquare + expExpression + symLiteralCloseSquare)
            | ( @punctuator + symIdentifier );

Upvotes: 2

Related Questions