0
votes

I have a SQL-like grammar written in Antlr, and I'm having problems with the IN operator. A sample query/input (without an IN clause) would look like

select sum(sum(sum.........N)) from table where sum(sum(sum....N)) = 10

And here's the IN grammar rule.

inOperator:
expression IN (valueList | query)
    |  expression;

Here, expression is also some other rule and that is very complex. It has relatively deeper hierarchy. Now the problem is when my parser is reaching this rule it goes deep into the stack matching every token to expression part of first rule; at the end of the query it gets that this doesn't have IN operator so second rule should match.

According to general parsing rules this happens with every query token and this is useless.

What are the ways I can skip these prediction loops and easily get to second rule? In general, patterns of our query occurring of first rule is very seldom and because of that our entire structure is slowing down. For reference, I'm using Antlr 1.4.3.

1
I think you mean ANTLRWorks 1.4.3? Is this matching to ANTLR 3 or ANTLR 2?CoronA
Yes, it's ANTLRWorks 1.4.3. And the Antlr version is 3.1.3.Pranav Kevadiya

1 Answers

2
votes

Left factoring should help. Try:

inOperator:
  expression inSuffix? ;

inSuffix:
  IN (valueList | query);

This will parse expression only once and append the inSuffix if it occurs.