-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalProject.g4
executable file
·181 lines (131 loc) · 3.65 KB
/
finalProject.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
grammar finalProject;
//Tree Expressions
program : moduleDeclarations otherModules driverModule otherModules;
moduleDeclarations : moduleDeclaration moduleDeclarations | ;
moduleDeclaration : DECLARE MODULE ID SEMICOL;
otherModules : module otherModules | ;
driverModule : DEF DRIVER PROGRAM ENDDEF moduleDef;
module : DEF MODULE ID ENDDEF TAKES INPUT SQBO input_plist SQBC SEMICOL ret moduleDef;
ret : RETURNS SQBO output_plist SQBC SEMICOL | ;
input_plist : input_plist COMMA ID COLON dataType | ID COLON dataType;
output_plist : output_plist COMMA ID COLON dataType | ID COLON type;
dataType : INTEGER | REAL | BOOLEAN | ARRAY SQBO range SQBC OF type;
type : INTEGER | REAL | BOOLEAN;
moduleDef : START statements END;
statements : statement statements | ;
statement : ioStmt | simpleStmt | declareStmt | conditionalStmt | iterativeStmt ;
ioStmt : GET_VALUE BO ID BC SEMICOL | PRINT BO var BC SEMICOL;
var : ID whichId | NUM | RNUM ;
whichId : SQBO ID SQBC | ;
simpleStmt : assignmentStmt | moduleReuseStmt;
assignmentStmt : ID whichStmt;
whichStmt : lvalueIDStmt | lvalueARRStmt;
lvalueIDStmt : ASSIGNOP expression SEMICOL;
lvalueARRStmt : SQBO index SQBC ASSIGNOP expression SEMICOL;
index : NUM | ID;
moduleReuseStmt : optional USE MODULE ID WITH PARAMETERS idList SEMICOL;
optional : SQBO idList SQBC ASSIGNOP | ;
idList : idList COMMA ID | ID;
expression : arithmeticExpr | booleanExpr;
arithmeticExpr : arithmeticExpr op term | term;
term : term op factor | factor;
factor : BO arithmeticExpr BC | var;
op : PLUSE | MINUS | MUL | DIV;
booleanExpr : booleanExpr logicalOp booleanExpr| arithmeticExpr relationalOp arithmeticExpr | BO booleanExpr BC;
logicalOp : AND | OR;
relationalOp : LT | LE | GT | GE | EQ | NE;
declareStmt : DECLARE idList COLON dataType SEMICOL;
conditionalStmt : SWITCH BO ID BC START caseStmt myDefault END;
caseStmt: CASE value COLON statements BREAK SEMICOL caseStmt;
value : NUM | TRUE | FALSE;
myDefault : DEFAULT COLON statements BREAK SEMICOL | ;
iterativeStmt : FOR BO ID IN range BC START statements END | WHILE BO booleanExpr BC START statements END;
range : NUM RANGEOP NUM;
//Keywords
INTEGER : 'integer';
REAL :'real';
BOOLEAN :'boolean';
OF : 'of';
ARRAY : 'array';
START : 'start';
END : 'end';
DECLARE : 'declare';
MODULE : 'module';
DRIVER : 'driver';
PROGRAM : 'program';
GET_VALUE : 'get_value';
PRINT : 'print';
USE : 'use';
WITH : 'with';
PARAMETERS : 'parameters';
TRUE : 'true';
FALSE : 'false';
TAKES : 'takes';
INPUT : 'input';
RETURNS : 'returns';
AND : 'and';
OR : 'or';
FOR : 'for';
IN : 'in';
SWITCH : 'switch';
CASE : 'case';
BREAK : 'break';
DEFAULT : 'default';
WHILE : 'while';
//Symbols
PLUSE : '+';
MINUS : '-';
MUL : '*';
DIV : '/';
LT : '<';
LE : '<=';
GE : '>=';
GT : '>';
EQ : '==';
NE : '!=';
DEF : '<<';
ENDDEF : '>>';
COLON : ':';
RANGEOP : '..';
SEMICOL : ';';
COMMA : ',';
ASSIGNOP : ':=';
SQBO : '[';
SQBC : ']';
BO : '(';
BC : ')';
COMMENTMAR : '**' ~[\r\n]* -> skip;
//BLOCK_COMMENT: '/*' .*? '*/' -> skip;
WHITESPACE : ( ' '| '\t'| '\r'| '\n') -> skip;
ID: [_A-Za-z]+[_A-Za-z0-9]*;
NUM : [0-9]+;
RNUM: (NUM'.'NUM);
//<<driver program>>
//start
// declare num,k:integer;
// declare A:array[1..0]of integer;
// num:=5;
// for(k in 1..10)
// start
// A[k]:=(num - k)*(num-k);
// print(A[k]);
// end
//end
//declare module a;
//
//<<module a >> takes input [a:integer,b:boolean];returns [c:real];
//start
//
//end
//
//<<driver program>>
//start
// declare num,k:integer;
// declare A:array[1..0]of integer;
// num:=5;
// for(k in 1..10)
// start
// A[k]:=(num - k)*(num-k);
// print(A[k]);
// end
//end