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
%{
#include "t.tab.hpp"

int yylex(YYSTYPE *lvalp);

#include <QTextStream>

int tokenPos = -1;

void yyerror(char const *s)
{
    QTextStream(stderr) << "Error:" << s;
}

%}

%token DECLARE
%token FOO
%token BAR

%defines
%debug
%error-verbose
%glr-parser
%verbose
%pure-parser

%expect 1

%%
Root: Prolog

Prolog: FirstProlog SecondProlog

FirstProlog: /* empty */
| FirstProlog A

SecondProlog: /* empty */
| SecondProlog B

A: DECLARE FOO
B: DECLARE BAR
%%

int yylex(YYSTYPE *)
{
    static QList<int> tokens;
    static int pos = -1;

    if(tokens.isEmpty())
    {
        tokens.append(DECLARE);
        tokens.append(FOO);
        tokens.append(DECLARE);
        tokens.append(BAR);
        tokens.append(0);
    }

    ++pos;
    return tokens.at(pos);
}

int main()
{
    yydebug = 1;
    return yyparse();
}

_______________________________________________
[email protected] http://lists.gnu.org/mailman/listinfo/help-bison

Reply via email to