[antlr-interest] faster expression parsing

Terence Parr parrt at cs.usfca.edu
Tue Mar 18 18:57:59 PDT 2008


On Mar 17, 2008, at 10:10 PM, Austin Hastings wrote:

> Terence Parr wrote:
>> So, I should have been working on something else but got to  
>> thinking about how annoying it is specifying expressions in  
>> recursive descent parsers.  You have to have a new rule for each  
>> precedence level.  This is also very slow. Just to match 34 it has  
>> to descend about 15 method calls.
>
> I totally agree. (See thread at: http://www.antlr.org/ 
> pipermail/antlr-interest/2007-December/025112.html)

Yep, i remember. also some macro requests.

>> I built a prototype single-rule (plus primary and suffix) operator  
>> matching thingie today.  I should be able to generate this from  
>> some metameta syntax in antlr.  Try it out...it's amazing (v3.1  
>> required due to tree bulding bug fix).
>
> Closer. I want the syntax built in to Antlr. Make expression sub- 
> grammars some kind of callable thing.
>
> string_expr:
>    @expression_subgrammar { operators = '+', '.', 'x', 'trim'(),  
> 'ltrim'(), 'rtrim'()  ; }
>    ;

Yep, my plan is to make it easy to specify.  I must admit though that  
I can't hide primary and suffix operator rules so I'd only be hiding  
this rule:

/** This could be autogenerated if you give me primary and suffix and  
precedence levels */
e[int p]
      :   (primary->primary)
          (   {prec[input.LA(1)]>=p}?=>     bop r=e[nextp(p)] -> ^ 
(bop $e $r)
          |   {postprec[input.LA(1)]>=p}?=> suffix[$e.tree]   ->  
{$suffix.tree}
          )*
      ;

We'd just need to say something like you request.  I also thought  
that just using an option would be ok...or, use the left-recursive rule.

e	:	e '*' e
	|	e '-' e
	|	e '+' e
	|	'-' e
	|	e '.' ID
	|	e '[' e ']'
	|	e '(' e (',' e)* ')'
	|	INT
	|	ID
	;

That's nice 'cause it's explicit like a yacc grammar would be.  I'd  
recognize this pattern and build what i sent before.  Only issue is  
precedence.  Order would work sort of but probably not  
perfectly...for example a+b.x should not match as (a+b).x.

Ter


More information about the antlr-interest mailing list