-
Notifications
You must be signed in to change notification settings - Fork 2
/
MiniJava.g4
125 lines (108 loc) · 4.03 KB
/
MiniJava.g4
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
grammar MiniJava;
goal : mainClass ( classDeclaration )* EOF
;
mainClass : 'class' Identifier '{' 'public' 'static' 'void' 'main' '(' 'String' '[' ']' Identifier ')' '{' statement '}' '}'
#mainclass
;
classDeclaration : 'class' Identifier ('extends' Identifier)? '{' (varDeclaration)* (methodDeclaration)* '}'
#dec_class
;
varDeclaration : mtype Identifier ';'
#dec_var
;
methodDeclaration : 'public' mtype Identifier '(' ( mtype Identifier ( ',' mtype Identifier )* )? ')' '{' ( varDeclaration )* ( statement )* 'return' expression ';' '}'
#dec_method
;
//methodDeclaration : 'public' mtype Identifier '(' parameters ')' '{' (varDeclaration)* (statement)* 'return' expression ';' '}'
// #dec_method
// ;
//parameters : (parameterDeclaration (',' parameterDeclaration)*)?
// ;
//parameterDeclaration : mtype Identifier
// ;
mtype : 'int' '[' ']'
| 'boolean'
| 'int'
| Identifier
;
statement : '{' (statement)* '}'
#state_lrparents
| 'if' '(' expression ')' statement 'else' statement
#state_if
| 'while' '(' expression ')' statement
#state_while
| 'System.out.println' '(' expression ')' ';'
#state_print
| Identifier '=' expression ';'
#state_assign
| Identifier '[' expression ']' '=' expression ';'
#state_array_assign
;
expression : //expression ('&&' | '<' | '+' | '-' | '*') expression
//#expr_op
expression '&&' expression
#expr_op_and
| expression '<' expression
#expr_op_less
| expression '+' expression
#expr_op_plus
| expression '-' expression
#expr_op_minus
| expression '*' expression
#expr_op_multi
| expression '[' expression ']'
#expr_array
| expression '.' 'length'
#expr_length
| expression '.' Identifier '(' (expression (',' expression)* )? ')'
#expr_method_calling
| Integer
#expr_int
| Boolean
#expr_bool
| Identifier
#expr_id
| 'this'
#expr_this
| 'new' 'int' '[' expression ']'
#expr_int_array
| 'new' Identifier '(' ')'
#expr_new_array
| '!' expression
#expr_not
| '(' expression ')'
#expr_lrparents
| expression ('&&' | '<' | '+' | '-' | '*')
{self.notifyErrorListeners('Error: missing the RHS of the operator')}
#err_miss_RHS
| ('&&' | '<' | '+' | '-' | '*') expression
{self.notifyErrorListeners('Error: missing the LHS of the operator')}
#err_miss_LHS
| '(' expression ')' ')'
{self.notifyErrorListeners("Error: too many ')'s")}
#err_many_rparents
| '(' '(' expression ')'
{self.notifyErrorListeners("Error: too many '('s")}
#err_many_lparents
| '(' expression
{self.notifyErrorListeners('Error: need right parent closing')}
#err_rparent_closing
| expression ')'
{self.notifyErrorListeners('Error: need left parent closing')}
#err_lparent_closing
;
Boolean : 'true'
| 'false'
;
Identifier : [a-zA-Z_][a-zA-Z0-9_]*
| [0-9]+[a-zA-Z_][a-zA-Z0-9_]*
{self.notifyErrorListeners('Error: identifier starting with digit')}
;
Integer : [0-9]+
;
WS : [ \t\r\n]+ -> skip
;
LineComment : '//' .*? ('\r')? '\n' -> skip
;
Comment : '/*' .*? '*/' -> skip
;