-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyLanguage.g4
95 lines (68 loc) · 1.74 KB
/
myLanguage.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
grammar myLanguage;
/*
Parser rules
*/
program: 'mainclass' ID '{' 'public' 'static' 'void' 'main' '(' ')' comp_stmt '}';
comp_stmt: '{' stmt+ '}';
stmt: assign_stmt
| for_stmt
| while_stmt
| if_stmt
| comp_stmt
| declaration
| null_stmt
| println_stmt
;
declaration: type ID (',' ID)* ';';
type: 'int' # TypeInt
| 'float' # TypeFloat
;
null_stmt: ';';
println_stmt : 'println' '(' expr ')' ';';
assign_stmt: assign_expr ';';
assign_expr: ID '=' expr;
bool_expr: expr c_op expr;
expr: assign_expr
| rval
;
for_stmt: 'for' '(' opassign_expr';' opbool_expr';' opassign_expr ')' stmt;
opassign_expr: assign_expr
|
;
opbool_expr: bool_expr # OpBoolPresent
| # OpBoolAbsent
;
while_stmt: 'while' '(' bool_expr ')' stmt;
if_stmt: 'if' '(' bool_expr ')' stmt # PlainIf
| 'if' '(' bool_expr ')' stmt 'else' stmt # IfElse
;
c_op: '=='
| '<'
| '<='
| '>'
| '>='
| '!='
;
rval: term # RvalTerm
| rval '+' term # RvalPlus
| rval '-' term # RvalMinus
;
term: factor # TermFactor
| term '*' factor # TermMultFactor
| term '/' factor # TermDivFactor
;
factor: '(' expr ')' # FactorExpr
| '-' factor # FactorNegative
| ID # FactorID
| INT # FactorInt
| FLOAT # FactorFloat
;
/*
Lexer rules
*/
fragment DIGIT : [0-9];
fragment LETTER : [A-Za-z];
ID: LETTER (LETTER | DIGIT | '_')*;
INT: '0' | [1-9] DIGIT*;
FLOAT: INT '.' DIGIT*;
WS : [ \t\r\n]+ -> skip;