help-bison
[Top][All Lists]
Advanced

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

ECMAScript: Automatic Semicolon Insertion


From: Simon Richter
Subject: ECMAScript: Automatic Semicolon Insertion
Date: Tue, 6 Dec 2016 22:23:23 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

I'm still building an ECMAScript parser, it's mostly running, except for
the Automatic Semicolon Insertion rule in the language: if a token stream
is invalid, but can be made valid by inserting a semicolon in front of the
current token, pretend that a semicolon is there.

For example inside a function:

    function f() {
        i = 5
    }

the semicolon at the end of the assignment is implied, because a closing
brace is unexpected there. Similarly,

    var foo = 4
    var bar = 5

is legal, because the "var" token after the 4 is unexpected. At the same
time, if I make the semicolon optional, I'm in shift/reduce hell as the
next token could be unary or binary minus (as well as some other things).

Is there a way to create a last-resort alternative that always loses any
conflict, so I can do something similar to

    variable_statement:
        "var" variable_declaration_list ";"     %prec WIN |
        "var" variable_declaration_list         %prec LOSE;

The expected result would be that anything that can be used to continue a
variable_declaration_list should go into it, anything that cannot be used
would terminate it, and a semicolon would terminate it nonetheless.

Alternatively, can I somehow use YYBACKUP or the error symbol here, by
allowing error productions instead of a semicolon, and inserting a
semicolon token as the next lookahead?

   Simon



reply via email to

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