help-bison
[Top][All Lists]
Advanced

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

Re: resolving shift/reduce in ActionScript3


From: Hans Aberg
Subject: Re: resolving shift/reduce in ActionScript3
Date: Wed, 26 Nov 2008 15:25:42 +0100

On 26 Nov 2008, at 14:43, Matthias Kramm wrote:

I'm writing a parser for ActionScript3, and I'm stuck
with the following problem.

In ActionScript, you can omit the semicolon (;). Furthermore, function
pointers are allowed.

I have the following (simplified) grammar:

PROGRAM : EXPRESSION
        | PROGRAM ';' EXPRESSION
        | PROGRAM EXPRESSION //omit ;

EXPRESSION : T_IDENTIFIER '(' EXPRESSION ')' // functioncall
           | T_IDENTIFIER                    // variable
           | '(' EXPRESSION ')'
           | EXPRESSION "++"
           | EXPRESSION "--"
           | EXPRESSION '+' EXPRESSION
           | EXPRESSION '-' EXPRESSION
           | EXPRESSION '/' EXPRESSION
             (etc.)

Now, when parsing the code

    function ( var++ )

it's naturally unclear to the parser whether to treat this as
"function(var++);" or "function;var++;"

Interestingly enough, bison seems to choose the latter (from the
-r report):

state 1
    4 EXPRESSION: T_IDENTIFIER . '(' EXPRESSION ')'
5 | T_IDENTIFIER . [$end, T_IDENTIFIER, ';', '(', ')', "++", "--", '+', '-', '/']
    '('  shift, and go to state 5
    '('       [reduce using rule 5 (EXPRESSION)]
    $default   reduce using rule 5 (EXPRESSION)

Is there some way to tell bison to prefer shifting to reducing in this
case?

If one should use Bison token precedence, then T_IDENTIFIER needs to be expanded in the grammar, so that there is a token immediately before the parsing "." in the shift-reduce conflict above.

But if your identifiers have been defined and pu on a look-up table which the lexer can read, them change the rule above to
  EXPRESSION:
    FUNCTION '(' EXPRESSION ')'
where the lexer returns the token FUNCTION.

(I'd rather not go down the GLR road just yet. I know I could add a
 second '(' token and make the lexer generate that instead of the
"normal" '(' whenever at the "start" of an expression- but I was hoping
 there might be an easier way I'm missing)

Much can be done without GLR with such tweaking, even Yaccify C++:
http://www.parashift.com/c++-faq-lite/compiler- dependencies.html#faq-38.11

  Hans






reply via email to

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