-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar_rules
64 lines (46 loc) · 2.45 KB
/
grammar_rules
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
statement_list : statement
| statement statement_list
statement : variable_declaration SEMICOLON
| func_declaration
| class_declaration
| while_statement
| for_statement
| if_statement
| output_stream SEMICOLON
| input_stream SEMICOLON
| expression
| identifier
| block
| return
while_statement : 'while' '(' expression ')' '{' statement_list '}'
for_statement : 'for' '(' variable_declaration ';' expression ';' expression ')' '{' statement_list '}'
| 'for' '(' expression ';' expression ';' expression ')' '{' statement_list '}'
if_statement : 'if' '(' expression ')' '{' statement_list '}'
('else' '{' statement '}'
block : '{' statement_list '}'
return : 'return' expression ;
class_declaration : class identifier : identifier '{' ( variable_declaration | func_declaration )* '}'
variable_declaration : 'var' identifier '->' type := expression
| 'var' identifier '->' type;
func_declaration : 'func' identifier '(' parameters? ')' -> type? '{' statement_list '}'
parameters : identifier -> type ( COMMA identifier -> type )*
arguments : expression ( COMMA expression )*
type : 'string' | 'number' | void | identifier | '(' ')' -> type?
output_stream : >> output output_stream*
output : expression
input_stream : << identifier inputStream*
expression : assignment
assignment : property_chain := assignment
| equality_expression (( == | != ) equality_expression)*
equality_expression : logical_expression ((<|>) logical_expression)*
logical_expression : clause ((<|>) clause)*
clause : term ( PLUS|MINUS ) term
term : factor ((DIV|MULT|MOD) factor)*
factor : (PLUS|MINUS)factor
| NUMBER | STRING
| property_chain
| '(' expression ')'
property_chain : (identifier_or_call) ( . property_chain )*
identifier_or_call : identifier | call
call : identifier '(' arguments? ')'
identifier : IDENTIFIER