[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Grammar failing single lookahead
From: |
Frans Englich |
Subject: |
Re: Grammar failing single lookahead |
Date: |
Wed, 30 May 2007 14:12:38 +0200 |
User-agent: |
KMail/1.9.1 |
On Wednesday 30 May 2007 12:55, Tim Van Holder wrote:
> Frans Englich wrote:
> > The intent of the grammar is to enforce that declarations appear in a
> > certain order(FirstProlog, SecondProlog), that the declarations are
> > optional, and they take the form of "declare <statically known keyword>".
> >
> > This is something that naturally leads to a shift/reduce conflict. As far
> > as I know, this can be solved in two ways:
> >
> > * Tokenize "declare foo" into one token, instead of two(the conflict goes
> > away).
> > * Generate a GLR parser. The conflict stays, but the parser survives it.
> >
> > For the user, I think the latter is a better alternative since it will
> > yield better error reporting(since the token granularity is higher).
> >
> > But is it solvable in some other way? For instance, can the grammar be
> > rewritten in some way such that it can be parsed as LR(1)? I doubt it.
>
> Try moving semantic checks like this out of the actual grammar
> definition; often conflicts like this can be avoided by doing so.
> Simply allow a sequence of any type of declaration, and do the
> are-they-in-an-acceptable-order check on the AST. You could do this in
> actions during the parse, or as part of a dedicated semantic check phase
> on the full AST once it's constructed by yyparse().
Yeah. It's of course one of those design decisions one can ponder: to what
degree one lets semantics be imposed by the grammar, or to what degree
business logic should sit in the parser. Of course, that would start with
deciding on what is "semantics" and what is physical grammar.
In my case, I let the grammar flag it, since it naturally imposed the physical
order. But indeed, moving it into a logical check is probably the easiest way
to solve the problem I posed.
Thanks,
Frans