This project has moved and is read-only. For the latest updates, please go here.

Shift-reduce conflict catering for SELECT DISTINCT and COUNT(DISTINCT

Oct 8, 2015 at 4:42 PM
Edited Oct 8, 2015 at 5:13 PM

I have a sql-like grammar which was previously working fine. It would successfully parse


but I've recently needed to introduce

SELECT DISTINCT option and it has thrown up this error.
State S4 (Inadequate)
  Shift-reduce conflicts on inputs: DISTINCT
  Shift items:
    selectClause -> SELECT ·distinctExpression expressionList 
    distinctExpression -> ·DISTINCT 
  Reduce items:
    distinctExpression -> · [[ ... COUNT DISTINCT ... ]
  Transitions: distinctExpression->S94, DISTINCT->S95
The salient parts of the grammar (as far as I can tell are)

selectClause.Rule = [SELECT] + distinctExpression + listExpression
distinctExpression.Rule = [DISTINCT] Or Empty
listExpression.Rule = MakePlusRule(listExpression, [COMMA], columnExpression)
columnExpression.Rule = expression Or ...
expression.Rule = term Or ...
term.Rule = functionExpression Or ...
functionExpression.Rule = functionNameExpression + parenExpression Or
                                      functionNameExpression + [EMPTYPAREN]
functionNameExpression.Rule = [COUNT] Or [DISTINCT] ...
parenExpression.Rule = LPAREN + listExpression + RPAREN    
DISTINCT as a function I don't think would ever appear without COUNT so perhaps I haven't defined it correctly with respect to function names. It previously returned a COUNT node containing a DISTINCT node nested within it.

OTOH, I wasn't sure if I should be explicitly given hints as to how to process the DISTINCT.

I've looked at the sql grammar example and it looks like the DISTINCT is just being treated as identifier in the context of COUNT(DISTINCT ...

Is there a (quick!) fix for the way that I am doing it or should I rework it to match the example.

Again, I'd be grateful of any pointers

Thx in advance