-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcc.g4
122 lines (107 loc) · 3.14 KB
/
jcc.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
grammar jcc;
// Syntax Specification ==> Context-free Grammar
prog : main_class class_declaration*;
main_class : 'class' identifier '{' main_method '}';
main_method : 'public' 'static' 'void' 'main' '(' 'String' '[' ']' IDENTIFIER ')' '{' statement '}';
class_declaration : 'class' IDENTIFIER '{' class_inner_body '}';
class_inner_body : field_declaration* method_declaration* ;
field_declaration
: type_of_variable identifier SC;
identifier:
IDENTIFIER;
method_declaration : type_of_variable identifier '(' parameter_list ')' '{' method_inner_body '}';
method_inner_body
: field_declaration*
statement*
return_statement;
statement
: '{' statement* '}' #blockStatement
| 'do' statement 'while' '(' expression ')' SC #doWhileStatement
| 'while' '(' expression ')'
statement #whileStatement
| 'if' '(' expression ')'
statement
('else'
statement)? #ifStatement
| identifier '=' expression SC #assignStatement
| identifier '[' expression ']' '=' expression SC #assignArrayStatement
| 'System.out.println' '(' expression ')' SC #printStatement
| 'break' SC #breakStatement
| 'continue' SC #breakStatement
;
return_statement
: 'return' expression SC ;
parameter_list : parameters? ;
parameters : parameter ( ',' parameter )*;
parameter : type_of_variable identifier;
expression
: '(' expression ')' #braquetedExpression
| expression '.' 'length' '(' ')' #stringLengthExpression
| expression '.' 'charAt''(' expression ')' #stringCharAtExpression
| 'new' identifier '(' ')' #newCallExpression
| expression '.' call_a_method ('.' call_a_method)? #methodCallExpression
| expression '.' IDENTIFIER #variableCallExpression
| '-' expression #minusExpression
| 'new' 'int' '[' expression ']' #arrayCreateExpression
| expression '[' expression ']' #arraySelectExpression
| expression '.' 'length' #arrayLengthExpression
| expression '*' expression #multiplicationExpression
| expression '/' expression #divisionExpression
| expression '+' expression #sumExpression
| expression '-' expression #substractionExpression
| expression '<' expression #lessThanExpression
| expression '==' expression #equalExpression
| expression ('&&' || '||' ) expression #comparisonExpression
| '!' expression #notExpression
| integer #integerExpression
| bool #boolExpression
| cha #charExpression
| string #stringExpression
| identifier #identifierExpression
| 'this' #thisExpression;
call_a_method
: identifier '(' (expression (',' expression)*)? ')' ;
integer
: INTEGER;
bool:
BOOLEAN;
cha:
CHAR;
string:
STRING;
type_of_variable
: identifier
| int_type
| int_array_type
| char_type
| boolean_type
| string_type ;
int_type
: 'int';
int_array_type
: 'int' '['']' ;
char_type
: 'char';
boolean_type
: 'boolean';
string_type
: 'String';
// Lexical Specification ==> Regular Expressions
LINE_COMMENT
: '//' ~[\r\n]* -> skip;
COMMENT
: '/*' .*? '*/' -> skip;
INTEGER
: ('0'..'9')+ ;
BOOLEAN
: 'true'
| 'false';
CHAR
: '\'' . '\'';
STRING
: '"' (~('"'|'\\') )* '"' ;
SC : ';';
IDENTIFIER
: ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
WS
: [ \t\r\n]+ -> skip;