Parser not seeing alternative, how to resolve

Nov 13, 2012 at 4:45 PM
Edited Nov 13, 2012 at 4:46 PM

Hi,  I'm not sure if this is the place to ask grammar questions but here goes.  I have a grammar that is not recognizing a valid path when a certain alternative is present.

Background definitions (partial):

valueTerm = bracketedProp | optBracketedProp | expr;

optBracketedProp.Rule = bracketedProp + "?";

expr.Rule = valueExpr | parExpr;

parExpr.Rule = "(" + expr + ")";            

valueExpr.Rule = strOrNum | binValueExpr | valueUnExpr;

binValueExpr.Rule = valueTerm + binOpValue + valueTerm;

binOpValue.Rule = ToTerm("+") | "-" | "*" | "/" | "%"; 

optNot.Rule = Empty | not;

The breaking case is when valueTerm is an optBracketedProp and the optNot is present then the following path is recognized only when the "existsExpr" definition is absent.  When the "existsExpr" is included then I get a syntax error "expecting 'exists'" error during parsing.

inExpr.Rule = valueTerm + optNot + in_ + funcOrParStrOrNumOrNullList;           


existsExpr.Rule = optBracketedProp + optNot + "exists";

Breaking Example:  

[1234]? not in (1,2,3)

These always work even when both definitions are present

[1234]? in (1,2,3)

[1234] not in (1,2,3)

[1234] in (1,2,3)

Thanks in advance for any pointers.

Nov 13, 2012 at 6:23 PM

too many things are missing from your rules extract, hard to see what's going on

Main question - when you load the grammar into Grammar Explorer, are there any Grammar Errors (conflicts)?

Nov 13, 2012 at 6:53 PM

Unfortunately, there aren't any conflicts.  I was hoping that the required definitions were included.

In the state diagram I do see a few Inadequate's at the point of interest.  Here is an attempt to reduce to critical definitions:

X.Rule = val + not + in + list;

Y.Rule = prop + not + exists

val.Rule = prop | expr

expr.Rule = val + op + val

op.Rule = "+" | "-"

And the parser always follows rule "Y" expecting "exists" after "not" (never "in") unless the Y.Rule is deleted or modified (e.g. add keyword before "not").

Nov 13, 2012 at 6:55 PM

missing prop.Rule; also, what is the 'root', top-level expression?

Nov 13, 2012 at 7:31 PM

I'm not saying this simplified example will exhibit the problem (it may be close though).  I'm just trying to get across the relationships and if you can guess at the problem maybe I can resolve or refactor the original.

Root = statement;

statement.Rule = X | Y;

For prop:  Easy version:

prop.Rule = NumberLiteral("propIntegerId", NumberOptions.IntOnly);

For prop:  Closer to reality:

prop.Rule = bracketedProp | optBracketedProp

optBracketedProp.Rule = bracketedProp + "?"

bracketedProp.Rule = "[" + propId + "]"

propId = NumberLiteral("propIntegerId", NumberOptions.IntOnly);




Nov 13, 2012 at 8:02 PM

for this simplified version, looks like there must be a conflict, shift/reduce conflict on 'not' lookahead. 

When parser has in stack the 'prop' node containing


and lookahead (next input) is 'not', it might be beginning of rule X - in this case parser should REDUCE 'val <-- prop', or it might be rule Y - in this case parser should SHIFT the 'not' token. 

Does not look like this simplified versions help, you'd better paste the entire grammar if possible.

Nov 14, 2012 at 5:53 PM
Edited Nov 14, 2012 at 8:39 PM

I may be able to make a simplified test case from the grammar if I get some time later (doubtful though).  

However the problem you found may help to understand...  Since both X and Y are valid and the correct choice can be made on encountering the token _after_ the 'not' ('in' vs 'exists'), could you explain how to adjust the simple grammar to support both X and Y? 

Nov 18, 2012 at 10:45 PM

sorry, i'm lost a bit in fragments here and there. Can you please repost a full grammar constructor (simplified and without conflicts in grammar explorer), and with input samples that do not work correctly