0

So I have the following grammar rules for an SQL expression editor (simplified):

FUNCTIONID: 'sum' | 'avg';
functionExpr: FUNCTIONID '(' expr ')'

AGGFUNCTIONID: 'sum' | 'avg'
aggFunctionExpr: AGGFUNCTIONID '(' expr ')' 'over' ...

The code works for expressions such as "sum(1) over ..." but not when there's no over.

How could I make this grammar work for both cases and still have these two rules?

escanda
  • 3
  • 1
  • ANTLR4's lexer works in many ways the same as in ANTLR3, which is explained here: http://stackoverflow.com/questions/9251048/antlr-v3-error-with-parser-lexer-rules – Bart Kiers Oct 31 '14 at 07:47
  • The issue there is that they comment the first rule wins, which is exactly what is happening right now. What I'd like the parser to do is look ahead for an `over` token and decide based on whether there's one or not. Is that possible? – escanda Oct 31 '14 at 08:27
  • No. That is what I meant: ANTLR4 and ANTLR3 work the same in this regard. I'll explain further in an answer. – Bart Kiers Oct 31 '14 at 08:39

1 Answers1

0

'sum' and 'avg' will always be tokenized as FUNCTIONID, there's no way around it. For an explanation, see: Antlr v3 error with parser/lexer rules

You can do something like this:

aggFunctionExpr
 : aggFunctionId '(' expr ')' 'over' ...
 ;

functionExpr
 : functionId '(' expr ')'
 ;

functionId    : SUM | AVG;
aggFunctionId : SUM | AVG;

SUM : 'sum';
AVG : 'avg';

...
Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • You gave me the correct hint of just tokenizing it as a function call and have an optional expression for 'over' and what follows. Thanks – escanda Oct 31 '14 at 09:56