-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminijava.g4
More file actions
66 lines (51 loc) · 2.33 KB
/
Copy pathminijava.g4
File metadata and controls
66 lines (51 loc) · 2.33 KB
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
grammar minijava;
goal : mainClass classDeclaration* ;
mainClass : 'class' identifier '{' 'public' 'static' 'void' 'main' '(' 'String' '[]' identifier ')' '{' ( varDeclaration )* (statement)* '}' '}' ;
identifier : [a-zA-Z][a-zA-Z_0-9]* ;
WS : [ \t\r\n]+ -> skip ;
MULTILINE_COMMENT: '/*' .*? '*/' -> skip ;
LINE_COMMENT: '//' .*? '\n' -> skip ;
INTEGER : '0'|[1-9][0-9]* ;
classDeclaration : 'class' identifier ( 'extends' identifier ) ? '{' ( varDeclaration )* ( methodDeclaration )* '}' ;
varDeclaration : type identifier ';' ;
methodDeclaration : 'public' type identifier '(' ( type identifier ( ',' type identifier )* ) ? ')' '{' ( varDeclaration )* ( statement )* 'return' expression ';' '}' ;
type : 'int' '['']'| 'boolean'| 'int' | 'char' | 'String' | identifier;
statement : '{' statement* '}'
| 'while' '(' expression ')' statement
| 'System.out.println' '(' expression ')' ';'
| 'if' '(' expression ')' statement ('else' statement)?
| 'break' ';'
| 'continue' ';'
| 'return' expression ';'
| 'do' statement 'while' '(' expression ')' ';'
| identifier '[' expression ']' '=' expression ';'
| identifier '=' expression ';'
| expression ';'
;
expression : INTEGER expressionPrim
| 'true' expressionPrim
| 'false' expressionPrim
| identifier expressionPrim
| 'this' expressionPrim
| 'new' 'int' '[' expression ']' expressionPrim
| 'new' identifier '(' ')' expressionPrim
| '!' expression expressionPrim
| '(' expression ')' expressionPrim
| INTEGER # integerLitExpression
| 'true' # boolLitExpression
| 'false' # boolLitExpression
| identifier # identifierExpression
| 'this'
| 'new' type '[' expression ']' # arrayInstantiationExpression
| 'new' identifier '(' ')' # objectInstantiationExpression
| '!' expression # notExpression
| '(' expression ')'
| '"'(ESC_SEQ| ~( '\\' | '"' ))* '"' # stringExpression
| '\''(ESC_SEQ| ~( '\'' | '\\' )) '\'' # characterExpression
;
fragment ESC_SEQ: '\\'('b'| 't'| 'n'| 'f'| 'r'| '"'| '\''| '\\')| UNICODE_ESC| OCTAL_ESC ;
expressionPrim : ( ( '&&' | '<' | '+' | '-' | '*' | '>' ) expression expressionPrim ) ?
| ( '[' expression ']' expressionPrim ) ? # arrayAccessExpression
| ( '.' 'length' expressionPrim ) ? # dotlengthExpression
| ('.' identifier'(' ( expression ( ',' expression )* )? ')' expressionPrim ) ?
;