-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlala-grammar-ebnf.txt
176 lines (156 loc) · 4.95 KB
/
lala-grammar-ebnf.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(*
Lala grammar in EBNF ISO/IEC 14977 : 1996(E).
This grammar uses pcre2 special sequence extension to ISO 14977.
pcre2 special sequence starts with "pcre2 ", then contains a valid
PCRE2 regular expression and ends with a " ".
For example, special sequence
?pcre2 [a-z] ?
represents any string that matches PCRE2 regular expression "[a-z]".
*)
(* Program *)
program = {declaration} ;
global statement = include
| declaration
;
include = INCLUDE, ID, {DOT, ID} ;
(* Declaration *)
declaration = variable
| function
| structure
| statement
;
variable = VAR, typed id, EQUAL, expression ;
function = FUNCTION, ID, parameters, type spec, statement ;
parameters = LPAREN, [variable, {COMMA, variable}, [COMMA]], RPAREN ;
structure = STRUCTURE, ID, LBRACE, {typed id}, RBRACE ;
typed id = ID, type spec ;
(* Statement *)
statement = print
| assignment
| if
| while
| do while
| function call
| return
| block
;
print = PRINT, expression ;
assignment = postfix, EQUAL, expression ; (* Compiler should force this postfix to end with one of: primary, subscript, member access *)
if = IF, expression, statement, [else] ;
else = ELSE, statement ;
while = WHILE, expression, statement ;
do while = DO, statement, WHILE, expression ;
function call = postfix ; (* Compiler should force this postfix to end with a call *)
return = RETURN [expression] ;
block = LBRACE, {declaration}, RBRACE ;
(* Expression *)
expression = or ;
or = and, {OR, and} ;
and = comparison, {AND, comparison} ;
comparison = term, [comparison op, term];
comparison op = EQUAL EQUAL
| EXCLAMATION EQUAL
| GREATER EQUAL
| LESS EQUAL
| GREATER
| LESS
;
term = factor, {term op, factor} ;
term op = PLUS
| MINUS
;
factor = prefix, {factor op, prefix} ;
factor op = STAR
| SLASH
| PRECENT
;
prefix = [MINUS | EXCLAMATION], postfix ;
postfix = primary, {postfix op} ;
postfix op = DOT, ID (* member access *)
| call
| LBRACKET, expression, RBRACKET (* subscript *)
| type cast
;
call = LPAREN, [expression, {COMMA, expression}, [COMMA]], RPAREN ;
primary = TRUE
| FALSE
| INTEGER VALUE
| FLOAT VALUE
| STRING VALUE
| READ, (BOOL | INT | FLOAT | STRING)
| ID
| array
| LPAREN expression RPAREN
;
array = LBRACKET, [expression, {COMMA, expression}, [COMMA]], RBRACKET ;
(* Type *)
type spec = COLON, type ;
type cast = COLON, castable type ;
type = castable type
| BOOL
| ID
| LBRACKET, type, RBRACKET
;
castable type = INT
| FLOAT
| STRING
;
(* Tokens *)
COLON = ":" ;
COMMA = "," ;
DOT = "." ;
EQUAL = "=" ;
EQUAL EQUAL = "==" ;
EXCLAMATION = "!" ;
EXCLAMATION EQUAL = "!=" ;
GREATER = ">" ;
GREATER EQUAL = ">=" ;
LBRACE = "{" ;
LBRACKET = "[" ;
LESS = "<" ;
LESS EQUAL = "<=" ;
LPAREN = "(" ;
MINUS = "-" ;
MINUS EQUAL = "-=" ;
PERCENT = "%" ;
PERCENT EQUAL = "%=" ;
PLUS = "+" ;
PLUS EQUAL = "+=" ;
RBRACE = "}" ;
RBRACKET = "]" ;
RPAREN = ")" ;
SLASH = "/" ;
SLASH EQUAL = "/=" ;
STAR = "*" ;
STAR EQUAL = "*=" ;
AND = "and" ;
BOOL = "bool" ;
ELSE = "else" ;
ENUM = "enum" ;
FALSE = "false" ;
FLOAT = "float" ;
FOR = "for" ;
FUNCTION = "function" ;
IF = "if" ;
IN = "in" ;
INT = "int" ;
MUTABLE = "mutable" ;
OR = "or" ;
PREDICATE = "predicate" ;
PRINT = "print" ;
READ = "read" ;
RETURN = "return" ;
STRING = "string" ;
STRUCTURE = "structure" ;
TRUE = "true" ;
VAR = "var" ;
WHILE = "while" ;
INTEGER VALUE = ?pcre2 ([1-9][0-9]*|0) ? ;
FLOAT VALUE = INTEGER VALUE, ?pcre2 \.[0-9]+ ? ;
STRING VALUE = ?pcre2 '([^'\\]|\\[trn'\\])*' ? ;
ID = ?pcre2 [a-zA-Z] ?, [?pcre2 [a-zA-Z0-9-]*[a-zA-Z0-9] ?] ;
(* Ignored *)
COMMENT = ?pcre2 \|[^\n]*\n ?
| ?pcre2 /-([^-]|-[^/])*-/ ?
;
WHITESPACE : ?pcre2 [ \r\t\n]* ? ;