[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Grammar failing single lookahead
From: |
Frans Englich |
Subject: |
Grammar failing single lookahead |
Date: |
Wed, 30 May 2007 12:12:24 +0200 |
User-agent: |
KMail/1.9.1 |
See attached grammar. Its essence is this:
-----------------------------------------
%token DECLARE
%token FOO
%token BAR
Root: Prolog
Prolog: FirstProlog SecondProlog
FirstProlog: /* empty */
| FirstProlog A
SecondProlog: /* empty */
| SecondProlog B
A: DECLARE FOO
B: DECLARE BAR
-----------------------------------------
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.
Cheers,
Frans
withglr.ypp
Description: Text Data
- Grammar failing single lookahead,
Frans Englich <=