forked from lapprand/AST-Minijava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniJavaGrammar.g4
More file actions
46 lines (34 loc) · 1.24 KB
/
MiniJavaGrammar.g4
File metadata and controls
46 lines (34 loc) · 1.24 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
grammar MiniJavaGrammar;
goal : mainClass ( classDeclaration )* EOF;
mainClass : 'class' IDENTIFIER '{' 'public' 'static' 'void' 'main'
'(' 'String' '[' ']' IDENTIFIER ')'
'{' statement '}' '}';
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' | IDENTIFIER ;
statement : '{'(statement)*'}' |
'if' '('expression')' statement 'else' statement |
'while''('expression')'statement |
'System.out.println' '('expression')'';' |
IDENTIFIER '=' expression';' |
IDENTIFIER '['expression']' '=' expression ';';
expression : expression OP expression |
expression '['expression']'|
expression '.' 'length'|
expression '.' IDENTIFIER '('(expression (','expression)*)?')'|
'true' |
'false' |
IDENTIFIER |
'this' |
'new' 'int' '[' expression ']' |
'new' IDENTIFIER '('')' |
'!'expression |
'(' expression ')'|
INTEGER;
INTEGER : ('-')?[0-9]+;
IDENTIFIER : ([a-zA-Z] | '_' | [0-9])+;
OP : ('&&'|'<'|'+'|'-'|'*');
SPACE : [ \r\n\t]+ -> skip;