-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRAMMAR.txt
100 lines (82 loc) · 2.62 KB
/
GRAMMAR.txt
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Notation:
UPPERCASE WORDS are terminals
"+" etc are also some simple terminals, usually operators
lower-case-words are non-terminals
I use the EBNF notation:
| -- means alternative;
parentheses ( ) -- are used for grouping,
? after an item means it is optional
+ after an item means "one or more of" and
* means "zero or more of".
I used C++-style //... comments to, well, comment on some parts of the grammar. They are not part of the grammar itself.
Terminals (besides the operators in quotes):
ARRAY: "array"
ID: identifier, defined below
DOTDOT: ".."
LBRAK: "["
RBRAK: "]"
SEMI: ";"
TUPLE: "tuple"
LOCAL: "local"
GLOBAL: "global"
DEFUN: "defun"
LPAR: "("
RPAR: ")"
COMMA: ","
END: "end"
WHILE: "while"
DO: "do"
IF: "if"
THEN: "then"
ELSIF: "elsif"
ELSE: "else"
FOREACH: "foreach"
FOR: "for"
IN: "in"
DOT: "."
INT: integer literal, defined below
RETURN: "return"
PRINT: "print"
Grammar:
start symbol: input
input = ( statement | decl | defn )*
decl = ARRAY ID LBRAK expr DOTDOT expr RBRAK ( ID "=" expr )? SEMI //XXX
// XXX no more "tuple declarations", this is gone: | TUPLE ID "=" expr ( COMMA expr)+ SEMI
| LOCAL ID ("=" expr)? SEMI //XXX init ALSO optional
| GLOBAL ID ("=" expr)? SEMI //XXX init optional
// function definition; no way to specify types of parameters (yet?)
def = DEFUN ID LPAR ID ( COMMA ID )* RPAR body END DEFUN
body = ( statement | decl )* // no nested function definitions
statement = lhs "=" expr SEMI // assignment
| lhs "<->" lhs SEMI // exchange
| WHILE bool-expr DO statement* END WHILE
| IF bool-expr THEN statement*
(ELSIF bool-expr THEN statement*)* // XXX any number of statements allowed
(ELSE statement*)? END IF // XXX any number of statements allowed
| FOREACH ID IN (range | array-id) DO statement* END FOR
| RETURN expr SEMI
| PRINT expr SEMI
// New. Forgot to define it.
array-id = ID
// New. Forgot to define it.
range = expr DOTDOT expr // XXX
bool-expr = expr bool-op expr
bool-op = "<" | ">" | "==" | "!=" | "<=" | ">="
// left hand side of an assignment
lhs = lhs-item ( COMMA lhs-item )*
lhs-item =
| ID // variable
| ID DOT INT // tuple component reference
| ID LBRAK expr RBRAK // array element reference
expr = // XXX *ascending* order of precedence: from least important to most important
| expr COMMA expr // tuple constuctor
| expr ( "+" | "-" ) expr
| expr ( "*" | "/" ) expr
// XXX there is no more unary minus!!
| LPAR expr RPAR
| ID
| ID expr // function call, right-associative
| ID DOT INT // tuple reference
| ID LBRAK expr RBRAK // array element reference
// XXX this was missing!
| INT