-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlala-grammar.txt
169 lines (154 loc) · 4.6 KB
/
lala-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
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
grammar: Lala
version: 0.0.2
# Parser
program : global-statement* ;
global-statement: include
| declaration
;
include : IMPORT ID (DOT ID)* ;
# Declaration
declaration : variable
| function
| structure
| statement
;
variable : VAR typed-id init EQUAL expression ;
function : FUNCTION ID parameters type-spec statement ;
parameters : LPAREN (variable (COMMA variable)* COMMA?)? RPAREN ;
structure : STRUCTURE ID LBRACE member* RBRACE ;
member : predicate
| typed-id
;
predicate : PREDICATE expression ;
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 : and ;
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
| PERCENT
;
prefix : (MINUS | EXCLAMATION)? postfix ;
postfix : primary postfixop* ;
postfixop : DOT ID
| call
| LBRACKET expression RBRACKET
| 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 ;
# Utility
type-spec : COLON type ;
type-cast : COLON castable-type ;
type : MUTABLE? immutable-type ;
immutable-type : castable-type
| BOOL
| ID
| LBRACKET type RBRACKET
;
castable-type : INT
| FLOAT
| STRING
;
# Lexer
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 : ( ([1-9][0-9]*) | 0) ;
FLOAT_VALUE : INTEGER_VALUE (\.[0-9]+) ;
STRING_VALUE : \' ( [^\'\\] | \\ [trn\'\\] )* \' ;
ID : [a-zA-Z] ([a-zA-Z0-9\-]* [a-zA-Z0-9])? ;
# Ignore
COMMENT : \| [^\n]* \n
| /- ([^-] | - [^/])* -/
;
WHITESPACE : [ \r\t\n]* ;