[antlr-interest] ANTLR Optional statements

Tom Moog tmoog at polhode.com
Wed Apr 10 15:30:03 PDT 2002


Part 1:

The extra EOF in the follow set is a general problem.
When antlr2 or pccts analyzes a grammar and finds an
orphan rule they assume that it is a start rule.  Since
every start rule should have EOF in the follow-set they
add one.  They should probably warn the user, but they
don't.

There is a warning about this in the "Notes For New Users
of pccts" and why pccts has a "-info o" option which
lists all orphan rules.


Part 2

If antlr2 doesn't see class_head, then obviously it will
compute the follow set improperly.  Sounds like a bug
to me.  My hypothesis is that it is skipping the block
following the syntax predicate in computing first and follow
sets.  This is correct for first sets, but incorrect for 
follow sets.


On Wed, 10 Apr 2002, Ric Klaren wrote:

> Hi,
> 
> I'll post the solution for the problem in question also to the group..
> 
> In context there is a calling rule:
> 
> external_declaration:
> (  ( (TYPEDEF | template_head )? class_head LCURLY) =>
>       ( template_head )? declaration
>    | .. more ..
> .	
> 
> > > class_head
> > >    :
> > >     (  STRUCT
> > >     |  UNION
> > >     |  CLASS
> > >     )
> > >     (ID ( base_clause)? )?
> > >    ;
> 
> The problem here was the fact that class_head was not called inside the
> grammar it was an unreferenced rule. As a result ANTLR set the followset to
> EOF. In stead of to EOF and LCURLY.
> 
> If class_head had been used inside template_head/declaration rules the
> problem would not have occured.
> 
> Which leads to the following rule of thumb:
> 
> Do not use loose (unreferenced) rules inside predicates.
> 
> ANTLR should probably give a warning for unreferenced rules especially if
> they occur inside predicates. (Or would this have been caught by hoisting?
> I'm not familiar with PCCTS's hoisting)
> 
> Cheers,
> 
> Ric
> --
> -----+++++*****************************************************+++++++++-------
>     ---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722  ----
> -----+++++*****************************************************+++++++++-------
> Wit is cultured insolence. --- Aristotle
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 
> 
> 


 

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



More information about the antlr-interest mailing list