[antlr-interest] More ANTLR meta-syntax questions

Brian Smith brian-l-smith at uiowa.edu
Tue May 14 01:13:32 PDT 2002

jsrs701 wrote:

>--- In antlr-interest at y..., mzukowski at y... wrote:
>>ANTLR requires the parenthesis.  It makes it easier to associate 
>actions with the proper construct, e.g. inside or outside of the ()+ 
>Hi Brian,
>Since you're new to ANTLR, that might not make much sense without an 
>example, though it's a very good answer.
>A: ( B )+ ;
>That seems redundant, sure.  But what about when you add the actions 
>that Monty mentioned?
>A: ( B { /* do something with this particular B */ } )+
>   { /* do something with the group of matched B's */ }
> ;
>How would you do that without the parentheses?
Thank you for your answers. I realize that they help when usiong the 
"options { ... }" construct, actions, and predicates. It just seems odd 
to me that they are required even when not using these constructs. But, 
I will deal with it.

Actually, I have a few other questions about how ANTLR works and why 
certain things are the way they are. I read through the FAQ and didn't 
see answers to these, but I apologize in advance if I missed them somewhere:

(1) Why does ANTLR bother with seperating the lexical rules from the 
parsing rules? In particular, it requires that lexical rules start with 
an uppercase letter and parser rules start with a lowercase letter, and 
the rules are defined in seperate sections. Is it just an enforced 
(historical) convention or is there a philisophical reason for it? I 
have found that even with my first grammer (a modified OCL grammar), I 
wanted to move rules between the parser and the lexer fairly freely but 
each time I was forced to do a search and replace on the grammar file to 
change letter case, even though the rule body was exactly the same. 
Also, I have found that the ANTLR syntax makes the distinction between 
lexer and parser seem almost arbitrary since they are both specified 
with EBNF.

(2) In ANTLR, you have to specify a lookahead distince (k) for the 
parser. My OCL grammar is LL(1) for all rules except one, which requires 
2 tokens of lookahead. So, I changed my grammar to be specified as 
LL(2). Does this affect the performance of my LL(1) rules? Is there a 
runtime performance penalty to saying k=2 for rules where k=1 is 
sufficient? If not, is it (theoretically) possible for ANTLR to compute 
the minimum value of k needed for my grammar to be unambiguous?

(3) I know that I have to use a syntactic predicate for "infinite" 
lookahead. But, if I only need a larger-than-k finite lookahead (say, 
two tokens when k=1), is there any advantage to using a syntactic 
predicate instead of increasing the value of k (e.g. performance)? I 
would rather increase k since my grammar is then more readable

(4) I know that ANTLR handles grammars that are "approximately" LL(k), 
and that LL(k) is a subset of LALR(1). But, is LL(k)+syntactic 
predicates still a subset of LALR(1)? That is, are there LALR(1), 
LALR(k), or LR(k) grammars that cannot be specified in ANTLR's 
LL(k)+syntactic predicates?

Thanks a lot,


Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

More information about the antlr-interest mailing list