Left recursion in grammar

59 views
Skip to first unread message

anpv

unread,
Nov 18, 2013, 2:09:20 AM11/18/13
to sprache...@googlegroups.com

I am developing a parser for the language similar to SQL.
And I have the problem of creating some of the rules of language, such as: expression IS NULL and expression IN (expression1, expression2, ...) with priority between logical and mathematical operators.
I put on GitHub test project https://github.com/anpv/SpracheTest/ but this variant is not good.
I tried to use the following rules:

private static readonly Parser<AstNode> InOperator =
    from expr in Parse.Ref(() => Expression)
    from inKeyword in Parse.IgnoreCase("in").Token()
    from values in Parse
        .Ref(() => Expression)
        .DelimitedBy(Comma)
        .Contained(OpenParenthesis, CloseParenthesis)
    select new InOperator(expr, values);

private static readonly Parser<AstNode> IsNullOperator =
    from expr in Parse.Ref(() => Expression)
    from isNullKeyword in Parse
         .IgnoreCase("is")
         .Then(_ => Parse.WhiteSpace.AtLeastOnce())
         .Then(_ => Parse.IgnoreCase("null"))
    select new IsNullOperator(expr);

private static readonly Parser<AstNode> Equality =
    Parse
        .ChainOperator(Eq, IsNullOperator.Or(InOperator).Or(Additive), MakeBinary);

which throw ParseException in code like ScriptParser.ParseExpression("1 is null") orScriptParser.ParseExpression("1 in (1, 2, 3)"): "Parsing failure: Left recursion in the grammar.". How to look-ahead than Expression or exists other variants solve this problem?

Reply all
Reply to author
Forward
0 new messages