-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLanguageV1Code.g4
158 lines (136 loc) · 4.66 KB
/
MyLanguageV1Code.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// A simple syntax-directed translator for a simple language
grammar MyLanguageV1Code;
// Root non-terminal symbol
// A program is a bunch of declarations followed by a bunch of statements
// The Java code outputs the necessary NASM code around these declarations
program :
{System.out.println("%include \"asm_io.inc\"");
System.out.println("segment .data"); }
declaration*
{System.out.println("segment .text");
System.out.println("\tglobal asm_main");
System.out.println("asm_main:");
System.out.println("\tenter 0,0");
System.out.println("\tpusha"); }
statement*
{System.out.println("\tpopa");
System.out.println("\tmov eax,0");
System.out.println("\tleave");
System.out.println("\tret"); }
;
// Parse rule for variable declarations
declaration :
{int a,b; }
INT a=NAME ASSIGN b=INTEGER SEMICOLON
{System.out.println("\t"+$a.text + " dd " + $b.text);}
;
// Parse rule for statements
statement :
ifstmt
| dostmt
| printstmt
| aprintstmt
| assignstmt
;
// Parse rule for do-while statements
dostmt :
DO
{
System.out.println("loop:");
}
statement*
WHILE LPAREN a=identifier NOTEQUAL b=integer RPAREN
{
System.out.println("\tcmp dword "+$a.toString+","+$b.toString);
System.out.println("\tjne loop");
}
;
// Parse rule for if statements
ifstmt :
{int a,b;}
{String label;}
IF LPAREN a=identifier EQUAL b=integer RPAREN
{System.out.println("tcmp dword "+$a.toString+","+$b.toString);
label = "label_"+Integer.toString($IF.index);
System.out.println("tjnz "+label); }
statement*
{ System.out.println(label+":"); }
ENDIF
;
// Parse rule for print statements
printstmt :
PRINT term SEMICOLON
{System.out.println("\tmov eax, "+$term.toString);
System.out.println("\tcall print_int");
System.out.println("\tcall print_nl");}
;
aprintstmt :
PRINT term COMMA
{System.out.println("\tmov eax, "+$term.toString);
System.out.println("\tcall print_int");
System.out.println("\tcall print_nl");}
|
term COMMA
{System.out.println("\tmov eax, "+$term.toString);
System.out.println("\tcall print_int");
System.out.println("\tcall print_nl");}
|
term SEMICOLON
{System.out.println("\tmov eax, "+$term.toString);
System.out.println("\tcall print_int");
System.out.println("\tcall print_nl");}
;
// Parse rule for assignment statements
assignstmt :
{int a; }
a=NAME ASSIGN expression SEMICOLON
{System.out.println("\tmov ["+$a.text+"], eax");}
;
// Parse rule for expressions
expression :
{int a,b; }
a=term
{System.out.println("\tmov eax,"+$a.toString);}
|
a=term PLUS b=term
{System.out.println("\tmov eax,"+$a.toString);}
{System.out.println("\tadd eax,"+$b.toString);}
|
a=term MINUS b=term
{System.out.println("\tmov eax,"+$a.toString);}
{System.out.println("\tsub eax,"+$b.toString);}
;
// Parse rule for terms
term returns [String toString] :
identifier {$toString = $identifier.toString;}
| integer {$toString = $integer.toString;}
;
// Parse rule for identifiers
identifier returns [String toString]: NAME {$toString = "["+$NAME.text+"]";} ;
// Parse rule for numbers
integer returns [String toString]: INTEGER {$toString = $INTEGER.text;} ;
// Reserved Keywords
////////////////////////////////
IF: 'if';
ENDIF: 'endif';
PRINT: 'print';
INT: 'int';
DO: 'do';
WHILE: 'while';
// Operators
PLUS: '+';
MINUS: '-';
EQUAL: '==';
ASSIGN: '=';
NOTEQUAL: '!=';
// Semicolon and parentheses
SEMICOLON: ';';
LPAREN: '(';
RPAREN: ')';
COMMA: ',';
// Integers
INTEGER: [0-9][0-9]*;
// Variable names
NAME: [a-z]+;
// Ignore all white spaces
WS: [ \t\r\n]+ -> skip ;