Help with Grammar

May 26, 2010 at 4:40 PM
Edited May 26, 2010 at 4:43 PM

Hello! I have a grammar that doesn't work quite right. I can understand what it must be doing, but I'd like to fix it without rearranging my grammar too much and I'm not sure how.

The full grammar is large, so I'll include only the relevant part for now (in case this is an obvious issue), and include the rest of it later if needed.

First, here is the relevant grammar:

      Expression.Rule = Term | UnExpr | BinExpr;
      Term.Rule = Id | StringLiteral | CharLiteral | NumLiteral | FunCall;
      UnExpr.Rule = UnOp + Term;
      UnOp.Rule = NOT | "+" | "-" | "~";
      BinExpr.Rule = Expression + binOp + Expression;
      binOp.Rule = BinMathOp | BinCompOp | BinCondOp;
      BinMathOp.Rule = ToTerm("+") | "-" | "*" | "/" | "%" | "&" | "|" | "^";
      BinCompOp.Rule = ToTerm("=") | ">" | "<" | ">=" | "<=" | "<>" | "!=" | "!<" | "!>";
      BinCondOp.Rule = ToTerm("AND") | "OR";

      FunCall.Rule = Id + "(" + ArgList + ")" | Id + "(" + ArgList + ")" + WhereClause;
      WhereClause.Rule = ToTerm("WHERE") + Expression;
      ArgList.Rule = MakeStarRule(ArgList, Comma, Expression);

The interesting thing about this grammar is that it allows a "where" clause on a function, so you can do something like this:

  sum(Task.Estimated_Hours) where Task.Completion_Date < now()
This is SQL-like, but it isn't SQL. We need to also be able to support something like this (for example):
  process(currentIteration, sum(Task.Estimated_Hours) where Task.Completion_Date < now())
In fact, this parses perfectly. What doesn't parse perfectly (or at least, how I'd like it to) is this:
sum(Task.Estimated_Hours) where Task.Completion_Date < now() and Task.Flag != true

This parses, but produces a tree rooted at the AND. In other words, the root node is a BinExpr with AND as the binOp and "sum(Task.Estimated_Hours) where Task.Completion_Date < now()" as the left hand side and "Task.Flag != true" as the right hand side.

I guess this sort of makes sense, but what are my options on getting the full expression (including the AND) to the right of the WHERE to parse under the WHERE so that the FunCall expression is the root?


Thanks in advance!
Coordinator
May 27, 2010 at 5:25 PM

My very first guess would be to register "where" as an operator (RegisterOperator) with the lowest precedence value. 

If it does not work, please post the entire grammar file with test examples

Roman