-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.lark
150 lines (110 loc) · 3.47 KB
/
grammar.lark
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
// -------------------------- BASIC PROGRAM STRUCTURE --------------------------
program: const_decl* var_decl* function_declaration* statement*
code_block: statement | _LBRACE statement* _RBRACE
// -------------------------- VARIABLES & CONSTANTS --------------------------
const_decl: _DEFINE IDENTIFIER exp
?var_decl: type declarations _SEMI
declarations: declaration (_COMMA declaration)*
?declaration: declaration_and_assignment
| array_declaration
| scalar_declaration
declaration_and_assignment: IDENTIFIER _ASSIGN exp
array_declaration: IDENTIFIER _LSQUARE exp _RSQUARE
scalar_declaration: IDENTIFIER
?statement: assign_statement _SEMI
| control_statement
| io_statement _SEMI
| function_call _SEMI
| _SEMI
?assign_statement: scalar_assignment
| IDENTIFIER _LSQUARE exp _RSQUARE _ASSIGN exp -> array_assignment
scalar_assignment: IDENTIFIER _ASSIGN exp
// -------------------------- FUNCTIONS --------------------------
function_declaration: _FUNCDEF type IDENTIFIER _LPAR function_parameters_declaration? _RPAR code_block
?function_parameters_declaration: parameter_declaration (_COMMA parameter_declaration)*
?parameter_declaration: type IDENTIFIER -> scalar_parameter
| type IDENTIFIER _LSQUARE _RSQUARE -> array_parameter
function_call: IDENTIFIER _LPAR function_parameters? _RPAR
?function_parameters: exp (_COMMA exp)*
// -------------------------- CONTROL FLOW --------------------------
?control_statement: if_construct
| while_construct
| do_while_construct _SEMI
| for_construct
| return_stmt
?if_construct: if_statement
| if_statement _ELSE code_block -> if_else_statement
if_statement: _IF _LPAR exp _RPAR code_block
while_construct: _WHILE _LPAR exp _RPAR code_block
do_while_construct: _DO code_block _WHILE _LPAR exp _RPAR
for_construct: _FOR _LPAR scalar_assignment _SEMI exp _SEMI assign_statement _RPAR code_block
return_stmt: _RETURN
// -------------------------- IO --------------------------
?io_statement: read_stmt | write_stmt
read_stmt: _READ _LPAR IDENTIFIER _RPAR
write_stmt: _WRITE _LPAR exp _RPAR
// -------------------------- EXPRESSIONS --------------------------
?exp: INTEGER -> intexpr
| IDENTIFIER -> scalar_expr
| IDENTIFIER _LSQUARE exp _RSQUARE -> array_expr
| _LNOT exp -> lnot_expr
| exp (PLUS|MINUS|DIV|MUL|MOD|EQ|NEQ|GT|LT|GTE|LTE|LAND|LOR|BAND|BOR|BXOR) exp -> binexpr
| _LPAR exp _RPAR
| MINUS exp -> neg_expr
?type: INT
// ----------- TOKENS ---------------
// Operands
_LBRACE: "{"
_RBRACE: "}"
_LSQUARE: "["
_RSQUARE: "]"
_LPAR: "("
_RPAR: ")"
_SEMI: ";"
PLUS: "+"
MINUS: "-"
DIV: "/"
MUL: "*"
MOD: "%"
EQ: "=="
NEQ: "!="
GT: ">"
LT: "<"
GTE: ">="
LTE: "<="
LAND: "&&"
LOR: "||"
BAND: "&"
BOR: "|"
BXOR: "^"
_LNOT: "!"
// TODO: shift operations
// Variables and constants declaration and assignment
INT: "int"
_DEFINE: "define"
_ASSIGN: "="
IDENTIFIER: /[a-z_][a-z0-9_]*/i
// Function declaration
_FUNCDEF: "def"
// Control flow
_IF: "if"
_ELSE: "else"
_DO: "do"
_WHILE: "while"
_FOR: "for"
_RETURN: "return"
// IO
_READ: "read"
_WRITE: "write"
// Other basic tokens
INTEGER: /[0-9]+/
_COMMA: ","
COMMENT_START: "/*"
COMMENT_END: "*/"
COMMENT: COMMENT_START /.*/ COMMENT_END
COMMENT_SR: "//" /.*/
// -------------------------- PARSER CONFIGURATION --------------------------
%import common.WS
%ignore WS
%ignore COMMENT
%ignore COMMENT_SR