-
let [x, y] = [0, 0];
console.log(93.8 * 2 ** x + y);
-
import x from "x"
console.log(93.8 * {x} << x.r[z])
-
x / {[x]: `${x}`}[x]
-
class C {
f({a}) {return ([a,f]) => C}
}
- The expression
-2**2
- Will ultimately evaluate to 4 if negation has higher precedence than exponentiation:
Exp = Exp "+" Term | Term
Term = Term "*" Factor | Factor
Factor = Primary "**" Factor | Primary
Primary = "-" Primary | num | id | "(" Exp ")"
- Will ultimately evaluate to -4 if negation has lower precedence than exponentiation:
Exp = "-"? (Exp "+" Term | Term)
Term = Term "*" Factor | Factor
Factor = Primary "**" Factor | Primary
Primary = num | id | "(" Exp ")"
- Will be a syntax error if negation has the same precedence as exponentiation:
Exp = Exp "+" Term | Term
Term = Term "*" Factor | Factor
Factor = Primary "**" Factor | "-"? Primary
Primary = num | id | "(" Exp ")"
- Here is a grammar for Astro++ that enforces the appearance of break statements only within while loops. (In practice, most people don't actually capture this in the grammar, but it's a good exercise to actually do it!)
Astro {
Program = StmtNoBreak+
StmtNoBreak = Assignment
| PrintStmt
| IfStmtNoBreak
| WhileStmt
Stmt = Assignment
| PrintStmt
| IfStmt
| WhileStmt
| BreakStmt
Assignment = id "=" Exp ";"
PrintStmt = print Exp ";"
BreakStmt = break ";"
WhileStmt = while Exp Block
IfStmtNoBreak = if Exp BlockNoBreak else BlockNoBreak
| if Exp BlockNoBreak else IfStmtNoBreak
| if Exp BlockNoBreak
IfStmt = if Exp Block else Block
| if Exp Block else IfStmt
| if Exp Block
BlockNoBreak = "{" StmtNoBreak* "}"
Block = "{" Stmt* "}"
Exp = Exp1 ("<=" | "<" | "==" | "!=" | ">=" | ">") Exp1 --relational
| Exp1
Exp1 = Exp1 ("+" | "-") Term --additive
| Term
Term = Term ("*" | "/" | "%") Factor --multiplicative
| Factor
Factor = Primary "**" Factor --exponentiation
| "-" Primary --negation
| Primary
Primary = id "(" ListOf<Exp, ","> ")" --call
| numeral --num
| id --id
| "(" Exp ")" --parens
numeral = digit+ ("." digit+)? (("E" | "e") ("+" | "-")? digit+)?
print = "print" ~idchar
while = "while" ~idchar
if = "if" ~idchar
else = "else" ~idchar
break = "break" ~idchar
keyword = print | while | if | else | break
idchar = letter | digit | "_"
id = ~keyword letter idchar*
space += "//" (~"\n" any)* --comment
}