Help with Grammar - Adding Top Level Parentheses

Jul 28, 2011 at 2:59 PM

Hi

(Hopefully my last question for a while!)

So I now have a very simple grammar

            'BNF Rules
            rootExpression.Rule = expressionList
            expressionList.Rule = MakePlusRule(expressionList, comma, expression)
            expression.Rule = term Or binaryExpression Or equalityExpression
            bracketedExpression.Rule = "(" + expression + ")"
            term.Rule = dataitemExpression Or literalExpression Or quotedExpression Or
                              functionExpression Or bracketedExpression

            literalExpression.Rule = number Or identifier
            binaryOperator.Rule = ToTerm("+") Or "-" Or "/" Or "*" Or "^"
            equalityOperator.Rule = ToTerm("=") Or "!=" Or "<>" Or ">=" Or "<=" Or ">" Or "<"
            binaryExpression.Rule = expression + binaryOperator + expression
            equalityExpression.Rule = expression + equalityOperator + expression
            functionExpression.Rule = functionNameExpression + "(" + expressionList + ")"
            dataitemExpression.Rule = "[" + dataitem + "]" Or dataitem
            quotedExpression.Rule = quotedLiteral
            functionNameExpression.Rule = identifier
           
            'Terminal priority
            identifier.Priority = 10
            dataitem.Priority = 20

            'Operator precedence           
            RegisterOperators(10, "*", "/", "%")
            RegisterOperators(9, "+", "-")
            RegisterOperators(8, "=", ">", "<", ">=", "<=", "<>", "!=", "!<", "!>")
            RegisterOperators(7, "^", "&", "|")
            RegisterOperators(6, "NOT")
            RegisterOperators(5, "AND")
            RegisterOperators(4, "OR", "LIKE", "IN")

            MarkPunctuation("(", ")", ".")
            MarkTransient(term, expression, rootExpression)

            Me.Root = rootExpression
            Me.LanguageFlags = LanguageFlags.CreateAst

This correctly parses text such as "1,1", "(1)", "((1))", "1,(1+1)","Func(1)" etc etc. But I cannot work out how to introduce the ability parse text such as "(1,1)" without it generating ambiguity. So for example if I change the rootExpression to

            rootExpression.Rule = expressionList OR "(" + expressionList + ")"

It generates a shift-reduce conflict

State S37 (Inadequate)
  Shift items:
    bracketedExpression -> ( expression ·)
    binaryExpression -> expression ·binaryOperator expression
    binaryOperator -> ·+
    binaryOperator -> ·-
    binaryOperator -> ·/
    binaryOperator -> ·*
    binaryOperator -> ·^
    equalityExpression -> expression ·equalityOperator expression
    equalityOperator -> ·=
    equalityOperator -> ·!=
    equalityOperator -> ·<>
    equalityOperator -> ·>=
    equalityOperator -> ·<=
    equalityOperator -> ·>
    equalityOperator -> ·<
  Reduce items:
    expressionList -> expression · [) comma]
  Transitions: )->S45, binaryOperator->S21, +->S22, -->S23, /->S24, *->S25, ^->S26, equalityOperator->S27, =->S28, !=->S29, <>->S30, >=->S31, <=->S32, >->S33, <->S34

I am assuming some sort of ambiguity with the bracketedExpression as defined in the term rule.

Sorry to ask for further help, but many thx again for any pointers.

Simon

Jul 28, 2011 at 3:08 PM

OK. As is the way, as soon as I posted, I worked out what I needed to do, just by enhancing the bracketedExpression.Rule

bracketedExpression.Rule = "(" + expressionList + ")"

Thx again

S