Sunday, December 18, 2022

Automatic Semicolon Insertion

Automatic Semicolon Insertion

There are following basic rules of semicolon insertion:

  1. When a token is encountered is not allowed by any production of the grammar, then a semicolon is automatically inserted before if one or more of the following conditions is true:
    • The offending token is separated from the previous token by at least one LineTerminator.
    • The offending token is }.
    • The previous token is ) and the inserted semicolon would then be parsed as the terminating semicolon of a do-while statement
  2. When a token is allowed by some production of the grammar, but the production is a restricted production and
    • the token would be the first token for a terminal or nonterminal immediately following the annotation "[no LineTerminator here]" within the restricted production(and therefore such a token is called a restricted token)
    • the restricted token is separated from the previous token by at least one LineTerminator

However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement or if that semicolon would become one of the two semicolons in the header of a for-statement

example 01

The source

return
a + b
is transformed by automatic semicolon insertion into the following:
return;
a + b;

according to rule 2 and `return [no LineTerminator here] Expression`

No comments:

Post a Comment