-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.ebnf
49 lines (42 loc) · 1.41 KB
/
grammar.ebnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* A context-free grammar for the Monkey programming language.
*
* Go to https://www.bottlecaps.de/rr/ui to view a syntax diagram of the
* grammar.
*/
Program ::= Stmt*
Stmt ::= Let | Return | ExprStmt
Let ::= 'let' Id '=' Expr ';'
Return ::= 'return' Expr ';'
ExprStmt ::= Expr ';'?
Expr ::= Equality
Equality ::= Comparison ( '==' | '!=' ) Comparison | Comparison
Comparison ::= Term ( '<' | '>' ) Term | Term
Term ::= Term ( '+' | '-' ) Factor | Factor
Factor ::= Factor ( '*' | '/' ) Unary | Unary
Unary ::= ( '!' | '-' ) Unary | Operator
Operator ::= Operator ( Call | Index ) | Primary
Call ::= '(' ')' | '(' ExprList ')'
Index ::= '[' Expr ']'
Primary ::= Identifier
| Number
| Boolean
| String
| Array
| Hash
| If
| Function
| '(' Expr ')'
Identifier ::= [a-zA-Z_]+
Number ::= [0-9]+
Boolean ::= 'true' | 'false'
String ::= '"' [^"]* '"'
Array ::= '[' ']' | '[' ExprList ']'
Hash ::= '{' '}' | '{' KeyValueList '}'
If ::= 'if' '(' Expr ')' Block ( 'else' Block )?
Function ::= 'fn' Params Block
ExprList ::= ExprList ',' Expr | Expr
KeyValueList ::= KeyValueList ',' KeyValue | KeyValue
KeyValue ::= Expr ':' Expr
Params ::= '(' ')' | '(' ParamList ')'
ParamList ::= ParamList ',' Id | Id
Block ::= '{' Stmt* '}'