-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qupla.g4
207 lines (166 loc) · 4 KB
/
Qupla.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
grammar Qupla_deb;
prog:
block EOF
;
block:
(module|vari_defining|COMMENT)*
;
module:
MODULE ID INPUT DEFINE (vari_defining)+ OUTPUT DEFINE type SEPERATOR codeblock
|MODULE ID codeblock1
|vari_defining
|ERROR {print("structure ERROR")}
;
assignment:
ID ASSIGN exp SEPERATOR
|ERROR {print(" assignment ERROR")}
;
vari_defining:
ID DEFINE type SEPERATOR
|ERROR {print(" DEFINE ERROR")}
;
type:
T_BOOL
|T_STRING
|T_REAL
|ERROR {print(" type ERROR")}
;
codeblock:
BEGIN ( ((vari_defining) | (assignment)|(print) | (reading) |(ifthen) |(loop)| (return_value)|COMMENT|(sub_block ))* ) END
|ERROR {print(" codeblock ERROR")}
;
codeblock1:
BEGIN ( ((vari_defining) | (assignment)|(print) | (reading) |(ifthen) |(loop)|COMMENT|(sub_block1 )) *)END
|ERROR {print(" codeblock1 ERROR")}
;
sub_block:
BEGIN ( ((vari_defining) | (assignment)|(print) | (reading) |(ifthen) |(loop)| (return_value)|COMMENT)* ) END
|ERROR {print(" codeblock ERROR")}
;
sub_block1:
BEGIN ( ((vari_defining) | (assignment)|(print) | (reading) |(ifthen) |(loop)|COMMENT)* ) END
|ERROR {print(" s_block1 ERROR")}
;
print:
WRITE exp SEPERATOR
;
reading:
READ ID SEPERATOR
|ERROR {print(" unreadble ERROR")}
;
return_value:
RETURN exp SEPERATOR
;
ifthen:
IF exp THEN code ELSE code
|IF exp THEN code
;
code:
print
|reading
|return_value
|ifthen
|loop
|assignment
|codeblock
;
loop:
WHILE exp code
;
exp:
REAL
|INT
|BOOL
|STRING
|ID
|exp POWER exp {print(" exp power")}
|exp FACT {print(" exp fact")}
|exp MUL exp {print(" exp mul")}
|exp DIV exp {print(" exp div")}
|exp MODE exp {print(" exp mod")}
|exp PLUS exp {print(" exp plus")}
|exp MINES exp {print(" exp mines")}
|exp NOTEQ exp {print(" exp <>")}
|exp GREATEREQ exp {print(" exp <=")}
|exp LESSEQ exp {print(" exp >=")}
|exp ASSIGN exp {print(" exp =")}
|exp GREATER exp {print(" exp grt")}
|exp LESS exp {print(" exp >")}
|exp XOR exp {print(" exp xor")}
|exp OR exp {print(" exp or")}
|exp NOT exp {print(" exp not")}
|exp AND exp {print(" exp and")}
|P_O exp P_C
|callmodule
;
callmodule:
ID P_O exp (COMMA exp)* P_C
|ID P_O exp P_C
;
// ----------------
// lexer rules
// ----------------
INT:[0-9]+ ;
FLOAT:[-]?([0-9]+([.][0-9]*)?|[.][0-9]+) ;
REAL : [0-9]+ | [0][xX][0-9a-fA-F]+ | [-]?([0-9]+([.][0-9]*)?|[.][0-9]+);
T_REAL: [rR][Ee][Aa][Ll] ;
COMMENT:'%%%'.*? '%%%'|'%%'~[\r\n]* ;
STRING :'"'~[\r\n]*'"';
AND:'&'|[aA][Nn][Dd];
OR:'|'|[oO][Rr];
NOT : ([Nn][oO][Tt]);
XOR : ([xX][oO][Rr]);
P_O:[(];
P_C:[)];
B_O:('[');
B_C:[\]];
A_O:[{];
A_C:[}];
PLUS:[+];
MINES :[-];
DIV : [/];
MUL : [*];
POWER : [^];
MODE : [%];
FACT : [!];
NOTEQ : ('<>');
ASSIGN : [=];
LESS : [<];
LESSEQ : ('<=');
GREATER : [>];
GREATEREQ : ('>=');
DEFINE : (':');
COMMA : (',');
IF : [Ii][Ff];
THEN : [Tt][Hh][Ee][Nn];
ELSE: [eE][lL][Ss][eE];
BEGIN : [Bb][Ee][Gg][Ii][Nn];
END : [Ee][Nn][Dd];
WHILE : [Ww][Hh][Ii][Ll][Ee];
DO : [Dd][oO];
FOR : [Ff][oO][Rr];
BREAK : [Bb][Rr][Ee][Aa][Kk];
CONTINUE : [Cc][oO][Nn][Tt][Ii][Nn][uU][Ee];
WRITE : [Ww][Rr][Ii][Tt][Ee];
READ : [Rr][Ee][Aa][Dd];
T_DOUBLE : [dD][oO][uU][Bb][Ll][Ee];
T_LONG : [Ll][oO][Nn][Gg];
T_INT : [Ii][Nn][Tt];
T_FLOAT : [Ff][Ll][oO][Aa][Tt];
T_STRING : [sS][Tt][Rr][Ii][Nn][Gg];
T_BOOL : [Bb][Oo][Oo][Ll];
T_HEX : [Hh][Ee][xX];
VOID : [Vv][Oo][Ii][Dd];
INPUT:[Ii][Nn][Pp][Uu][Tt];
OUTPUT:[Oo][Uu][Tt][Pp][Uu][Tt];
RETURN : [Rr][Ee][Tt][uU][rR][Nn];
MODULE : [mM][oO][dD][Uu][Ll][Ee];
BOOL : [tT][rR][uU][eE] |[fF][aA][Ll][sS][eE] ;
WT : [ \r\n\t]+ -> skip;
SEPERATOR :(';');
ID : [a-zA-Z]SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?
SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?
SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?SUBID?
SUBID?SUBID?;
SUBID :[0-9a-zA-Z];
ERROR : .;