[antlr-interest] Working around the LALL(k) vs. LL(k) problem ?

mzukowski at bco.com mzukowski at bco.com
Wed Mar 20 10:38:40 PST 2002


Note that then your syn pred (variableDecl)=> is parsing through the entire
variableDecl rule to completion.  Oftentimes you know the shortest prefix to
disambiguate with, which is the rationale behind my (IDENTIFIER
IDENTIFIER)=> suggestion.  But then as you debug you need to keep in mind
possible changes to the prefixes involved.

Monty

> -----Original Message-----
> From: Matthew Ford [mailto:Matthew.Ford at forward.com.au]
> Sent: Wednesday, March 20, 2002 10:32 AM
> To: antlr-interest at yahoogroups.com
> Subject: Re: [antlr-interest] Working around the LALL(k) vs. LL(k)
> problem?
> 
> 
> Randall try
> 
> 
> statement
>           : (variableDecl) => variableDecl
>           | expression;
> 
> This will look ahead as far as it needs to  to determine if 
> the input is
> variableDecl when Antlr sees (variableDecl)=>
> and only take that branch if the input matches.  This is one of the
> strengths of Antlr.
> 
> matthew
> ----- Original Message -----
> From: "Randall Nortman" <antlr-list at wonderclown.com>
> To: <antlr-interest at yahoogroups.com>
> Sent: Thursday, March 21, 2002 4:49 AM
> Subject: [antlr-interest] Working around the LALL(k) vs. 
> LL(k) problem?
> 
> 
> > I'm working on my first ANTLR grammar, and I keep getting bitten by
> > the fact that ANTLR uses linear approximation of LL(k).  
> I'm wondering
> > if there are any general strategies for avoiding or working around
> > this problem.  Can predicates be used?  As an example, consider the
> > following relatively simple grammar, which allows both simple
> > arithmetic expressions and variable declarations:
> >
> > ----- Begin Grammar
> > block: ((statement)? ";")*; // Possibly empty statements, 
> delimited by ";"
> >
> > statement: variableDecl | expression;
> >
> > variableDecl: typeSpec IDENTIFIER (":=" expression)?; // Optional
> initializer
> >
> > typeSpec: IDENTIFIER;
> >
> > expression: term ( ( "+" | "-" ) term)*;
> >
> > term: factor ( ( "*" | "/" ) factor)*;
> >
> > factor: IDENTIFIER | LITERAL | ("(" expression ")");
> > ----- End Grammar
> >
> > I get nondeterminism on the 'statement' rule, because the 
> parser can't
> > tell the difference between a variableDecl and an expression for an
> > input of (IDENTIFIER IDENTIFIER ";").  Clearly, that should 
> be parsed
> > as a variable declaration, because an expression requires 
> IDENTIFIERs
> > to be separated by operators.  But because of linear approximation,
> > ANTLR doesn't see that.  What's the best way to fix this problem?
> >
> > On a related note, I'm designing both the syntax of this 
> language and
> > the parser together, using ANTLR along the way to catch 
> problems in my
> > syntax.  However, I feel like I spend as much time trying to figure
> > out why ANTLR doesn't like a rule as I do developing the syntax
> > itself.  I never know if an error message from ANTLR is because my
> > syntax is bad or because I'm just not representing it in a way that
> > ANTLR likes.  Might there be a better tool for developing 
> the grammar
> > independent of the parser?  (I want to make sure I have a regular
> > grammar, which can be parsed without referencing any semantic
> > information such as symbol tables, so the tool should be able to
> > validate this.)
> >
> > Any assistance or insight would be greatly appreciated.
> >
> > Randall Nortman
> >
> >
> >
> > 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/ 



 

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



More information about the antlr-interest mailing list