grammatica-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Grammatica-users] Lookahead feature?


From: Per Cederberg
Subject: Re: [Grammatica-users] Lookahead feature?
Date: Sun, 04 Sep 2005 22:43:12 +0200

Well, the nicer way to rewrite the grammar would be like
this I think:

Goal = a+ AorB;
AorB = A | B ;
A    = b ;
B    = c ;

The root problem isn't solvable by an LL parser, as the
number of needed look-ahead tokens is possibly infinite. In
all other cases Grammatica should be able to correctly
evaluate the options and create look-ahead lists of required
length. So the LOOKAHEAD() option of JavaCC wouldn't do the
trick, but it would create an incorrect parser. Also, this
stuff is not needed with Grammatica, as all the look-ahead
handling is taken care of automatically.

Possibly one could create a parser that automatically
performs a rewrite such as the one above, and then
transforms the resulting parse back to what you'd expect.
But it might be a bit tricky to implement. Or you can do
similar transforms yourself.

The issue also goes away if you use an LR parser generator.
But then again, those come with another set of issues that
you might not want to handle... :)

Cheers,

/Per

On fri, 2005-09-02 at 16:54 +0200, Thomas Moschny wrote:
> Hi!
> 
> while working on a grammar, I am struggling with this sort of productions:
> 
> Goal = A | B ;
> A    = a+ b ;
> B    = a+ c ;
> 
> It is clear that this grammar is not LL(k), and thus grammatica tells me that 
> there is an inherent ambiguity in production 'Goal'. The normal way to deal 
> with this problem would be to do left-factoring, I think:
> 
> Goal  = a+ (ATail|BTail) ;
> ATail = b;
> BTail = c;
> 
> But, this is ugly, isn't it? In fact, there is no ambiguity in the original 
> grammar. Imho, the left-factoring makes the grammar bigger and less readable. 
> Maybe not in this example, but there are more complicated cases.
> 
> It would be nice, if one could give grammatica a hint that it's ok to search 
> for the right production in such cases  - some sort of marker or something 
> similar to the LOOKAHEAD(..) feature of JavaCC:
> 
> Goal = lookahead(A) A | B ;
> (If A would match, use the first subrule, don't bother with the second.)
> 
> Any thoughts or comments on this?
> 
> Regards,
> Thomas
> _______________________________________________
> Grammatica-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/grammatica-users





reply via email to

[Prev in Thread] Current Thread [Next in Thread]