help-bison
[Top][All Lists]
Advanced

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

Re: Shift reduce errors due to embedded actions


From: Hans Aberg
Subject: Re: Shift reduce errors due to embedded actions
Date: Wed, 10 Oct 2001 20:58:55 +0200

At 20:20 +0200 2001/10/10, Axel Kittenberger wrote:
>> LR(n) grammars can be rewritten as LR(1) grammars, but I do not know how
>> that works when out when having to take actios into account.
>
>Oh, this is possible?

And even LR(0) if each sentence is assumed to be followed by an end marker;
see J.E.Hopcroft & J.D.Ullman, "Introduction to Automata Theory, Languages
and Computation", Addison-Wesley (1979).

>I had the problem with normal class declarations:
>cork[5] = 3;
>
>Set the 5th element of the array cork to 3.
>
>against having the decleration:
>cork[5] a;
>
>which means create an array of the -type- cork which is 5 elements in size.
>
>I thought this could not be expressen in LR(1), as when I'm having (variable
>| type) '[', the bracket is the one look ahead, I don't yet know if the token
>'cork' should be should be evaluated as type or as variable, while a LR(n)
>parser could theoretically do this.

Note that LR(n) grammars are still context independent, and I get the
feeling you try to plug in a context dependent grammar. The usual way out
of it is to have a look-up table with the type of each name, and then let
the lexer return that type to the Bison generated parser.

The stuff above is probably known to C compiler writers (check in say the
newsgroups comp.compilers, comp.lang.c.moderated, or some).

But here is an idea:

If
  cork[5] = 3;
appears first, it is an error, as "cork" has not been declared. So the
lexer checks the lookup table, and finds that "cork" is not there, and
returns say "undeclared_name". If the name is on the table, it will tell
its type, say "array_name". Then the grammar may look something like
  declaration:
    type_name "[" number "]" undeclared_name ";" { /* action */ }
    ...

  assignment:
    array_name "[" number "]" "=" ...

At least the C++ ANSI standard (can be bought at ANSI for $18 or something)
has an appendix with the C++ grammar, where one can get ideas on how to
create such grammars.

  Hans Aberg





reply via email to

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