[antlr-interest] Runtime rule multiplicity

Terence Parr parrt at cs.usfca.edu
Fri May 12 14:28:07 PDT 2006


On May 12, 2006, at 12:59 PM, Matthew Mastracci wrote:

> Is it possible in ANTLR3 to generate rules that have a multiplicity
> determined at runtime?  For instance, in a grammer where parameters  
> and
> functions are somewhat ambiguous (ie: not delimited in any way),  
> you may
> have something like this:
>
> func1 1 func2 2
>
> At run-time, it would be determined that func1 has two parameters: "1"
> and "func2 2".  For another run it might be determined that func1 has
> three parameters and func2 has none.

This is a highly context sensitive issue if you try to resolve with  
the parser. I.e., context-free grammars tend not to be so nice ;)   
You can use semantic predicates to look for only a certain number of  
parameters:

( {count<=max}?=> ID)?

I *think* that will work.

> So far, it looks like the only solution to this problem is to run the
> lexer to get a list of tokens and generate a hand-rolled parser to
> process it.
>
> After reading through the ANTLR blogs, I suppose it might also be  
> possible to use a gated semantic predicate to  continue the rule  
> iff there are remaining parameters, or the optional subrules stuff,  
> but I'm not entirely sure what the syntax of these would look  
> like.  Are there any examples?

The above might work. ;)

Ter


More information about the antlr-interest mailing list