SQL 89 Table Alias Shift-reduce/Reduce-reduce conflicts

Mar 10, 2014 at 11:30 AM
Morning Folks,

Just trying to add table aliases to the existing SQL 89 sample as I noticed that they cause a parsing error. The following change seems to work, changing:
Id.Rule = MakePlusRule(Id, dot, Id_simple);
to:
Id.Rule = MakePlusRule(Id, dot, Id_simple) | Id + asOpt + Id_simple;
in the SqlGrammer.cs file.

However this leaves me with the following Grammar errors:
  • Shift-reduce conflict. State S59, lookahead [AS]. Selected shift as preferred action.
  • Reduce-reduce conflict. State S59, lookaheads: id_simple. Selected reduce on first production in conflict set.
  • Shift-reduce conflict. State S154, lookahead [id_simple]. Selected shift as preferred action.
How can I suppress these conflicts?
Coordinator
Mar 15, 2014 at 12:38 AM
sorry for long silence, will try to get to your problem this weekend
Coordinator
Mar 17, 2014 at 9:17 PM
Edited Mar 17, 2014 at 9:33 PM
here's a fix - add the following to the grammar:
  var fromList = new NonTerminal("fromList");
  var fromElem = new NonTerminal("fromElem");
  var aliasedTable = new NonTerminal("aliasedTable");
  var joinExpr = new NonTerminal("joinExpr");
  var joinKind = new NonTerminal("joinKind");
  var outerOpt = new NonTerminal("outerOpt");
  fromList.Rule = MakePlusRule(fromList, comma, fromElem);
  fromElem.Rule = aliasedTable | fromElem + joinExpr; 
  joinExpr.Rule = joinKind + aliasedTable + ON + expression;
  aliasedTable.Rule = Id + aliasOpt;
  joinKind.Rule = ToTerm("JOIN") | ToTerm("INNER") + "JOIN" | "LEFT" + outerOpt + "JOIN" | "RIGHT" + outerOpt + "JOIN";
  outerOpt.Rule = Empty | "OUTER";
and change the existing expression:
  fromClauseOpt.Rule = Empty | FROM + fromList;

then aliases should work. I tested an example:

-- testing operator precedence with various capitalization of OR,AND operators
SELECT Name
FROM Product x left outer join y on x.yId = y.Id
where A oR B and C OR D + X * 5;
Coordinator
Mar 17, 2014 at 9:34 PM
Edited Mar 17, 2014 at 9:36 PM
I think codeplex site's code formatter went nuts - if you see all these crazy "&43;" - just replace these with PLUS symbol
could not find a way to make it appear properly
Mar 17, 2014 at 10:34 PM
Fantastic! Thanks a lot rivantsov.

Slightly more to it than my feeble effort. :-)