Pranav Kevadiya
Pranav Kevadiya

Reputation: 539

Antlr prediction analysis

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.

Upvotes: 0

Views: 134

Answers (1)

CoronA
CoronA

Reputation: 8075

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.

Upvotes: 2

Related Questions