forked from shay2025/Compiler-for-C0-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.y
222 lines (188 loc) · 8.25 KB
/
Parser.y
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{
module Parser where
import Lexer
}
%name parser -- nome da função de parsing
%tokentype { Token }
%error { parseError }
%token -- definição dos valores de terminais
num { NUM $$ }
id { ID $$ }
'+' { PLUS }
'-' { MINUS }
'*' { MULT }
'%' { MOD }
'/' { DIV }
'(' { LPAREN }
')' { RPAREN }
'{' { LBRACE }
'}' { RBRACE }
';' { SEMICOLON }
',' { COMMA }
'=' { ASSIGN }
'>' { GREATER }
'<' { LESS }
'>=' { GREATERorEQUAL }
'<=' { LESSorEQUAL }
'!=' { DIFF }
'==' { EQUAL }
'!' { NOT }
'&&' { AND }
'||' { OR }
'true' { TRUE }
'false' { FALSE }
'bool' { BOOL }
'int' { INT }
if { IF }
else { ELSE }
return { RETURN }
while { WHILE }
for { FOR }
break { BREAK }
continue { CONTINUE }
'print_int' { PRINTint }
-- precedencia mais baixa
%nonassoc '<' '>' '==' '!=' '<=' '>=' '&&' '||'
%left '+' '-'
%left '*' '/'
%left '%'
%nonassoc OUTERTHEN
%nonassoc else
-- precedencia mais alta
%%
-- Functions
nextFun : Fun { [$1] }
| Fun nextFun { $1 : $2 }
Fun : Tp id '(' Arg ')' '{' nextStmt '}' { FunDef $2 $1 $4 $7 }
| Tp id '(' ')' '{' nextStmt '}' { FunDef $2 $1 [] $6 }
-- Functions arguments
Arg : Tp id { [($2, $1)] }
| Tp id ',' Arg { ($2, $1) : $4 }
-- Statements
Stmt : Simple ';' { $1 }
| Declr ';' { $1 }
| Increment ';' { $1 }
| whileStmt { $1 }
| forStmt { $1 }
| '{' nextStmt '}' { Block $2 }
| return Exp ';' { Return $2 }
| 'print_int' '(' Exp ')' ';' { PrintINT $3 }
| if '(' Exp ')' Stmt else Stmt { IfElse $3 $5 $7 }
| if '(' Exp ')' Stmt %prec OUTERTHEN { If $3 $5 }
nextStmt : Stmt { [$1] }
| Stmt nextStmt { $1 : $2 }
StmtBC : Simple ';' { $1 }
| Declr ';' { $1 }
| Increment ';' { $1 }
| if '(' Exp ')' StmtBC else StmtBC { IfElse $3 $5 $7 }
| if '(' Exp ')' StmtBC %prec OUTERTHEN { If $3 $5 }
| whileStmt { $1 }
| '{' '}' { Skip }
| '{' nextStmtBC '}' { Block $2 }
| return Exp ';' { Return $2 }
| 'print_int' '(' Exp ')' ';' { PrintINT $3 }
| break ';' { Break }
| continue ';' { Continue }
| forStmt { $1 }
nextStmtBC : StmtBC { [$1] }
| StmtBC nextStmtBC { $1 : $2 }
whileStmt : while '(' Exp ')' StmtBC { While $3 $5 }
forStmt : for '(' Simple ';' Exp ';' Increment ')' StmtBC { For $3 $5 $7 $9 }
| for '(' ';' Exp ';' ')' StmtBC { For Skip $4 Skip $7 }
| for '(' Simple ';' Exp ';' ')' StmtBC { For $3 $5 Skip $8 }
| for '(' ';' Exp ';' Increment ')' StmtBC { For Skip $4 $6 $8 }
-- Assignments and declarations
Simple : id '=' Exp { Assign $1 $3 }
Declr : Tp id { Decl ($2, $1) }
Increment : id '+' '+' { Incr $1 }
| id '-' '-' { Decr $1 }
-- Types allowed
Tp : 'int' { TyInt }
| 'bool' { TyBool }
-- elements of the function call
ArgFunCall : Exp { [$1] }
| Exp ',' ArgFunCall { $1 : $3 }
-- terms
Term : num { Num $1 }
| id { Var $1 }
| '(' Exp ')' { $2 }
| boolean { $1 }
| '!' '(' Exp ')' { Not $3 }
-- Expressions
Exp : Term { $1 }
| Exp '+' Exp { OpBin Plus $1 $3 }
| Exp '-' Exp { OpBin Minus $1 $3 }
| Exp '*' Exp { OpBin Times $1 $3 }
| Exp '/' Exp { OpBin Div $1 $3 }
| Exp '%' Exp { OpBin Mod $1 $3 }
| Exp '||' Exp { OpRel Or $1 $3 }
| Exp '&&' Exp { OpRel And $1 $3 }
| Exp '>' Exp { OpRel Gt $1 $3 }
| Exp '<' Exp { OpRel Lt $1 $3 }
| Exp '>=' Exp { OpRel Gteq $1 $3 }
| Exp '<=' Exp { OpRel Lteq $1 $3 }
| Exp '==' Exp { OpRel Eq $1 $3 }
| Exp '!=' Exp { OpRel Diff $1 $3 }
| id '(' ')' { FunCall $1 [] }
| id '(' ArgFunCall ')' { FunCall $1 $3 }
-- Boolean values
boolean : 'true' { BoolVal ValTrue }
| 'false' { BoolVal ValFalse }
{
type Ident = String
data Type = TyInt
| TyBool
deriving (Show, Eq)
data Boolean = ValTrue
| ValFalse
deriving (Show, Eq)
-- statements
data Stmt = Assign Ident Exp -- ident = exp
| IfElse Exp Stmt Stmt -- if/then/else
| If Exp Stmt -- if/then/else
| While Exp Stmt -- while
| Block [Stmt] -- bloco de instruções
| Decl (Ident, Type) -- declaração de variáveis
| Return Exp -- valor de retorno da função
| Skip -- comando vazio
| PrintINT Exp -- comando print para INT
| Break
| Continue
| For Stmt Exp Stmt Stmt --for
| Incr Ident
| Decr Ident
deriving Show
-- functions
data Fun = FunDef Ident Type ArgList [Stmt]
deriving Show
-- functions arguments
type ArgList = [(Ident, Type)]
-- expressions
data Exp = Var Ident -- x, y, z, etc.
| Num Int -- 123, etc.
| BoolVal Boolean -- 'true', 'false'
| OpBin BinOp Exp Exp -- e1+e2, e1*e2, ...
| OpRel RelOp Exp Exp -- e1<e2, e1!=e2, ...
| Not Exp -- NOT operator
| FunCall Ident [Exp] -- function call and its arguments
deriving (Eq, Show)
-- arithmetic operators
data BinOp = Plus
| Minus
| Times
| Div
| Mod
deriving (Eq, Show)
-- relational operators
data RelOp = Lteq
| Lt
| Gt
| Gteq
| Diff
| Eq
| And
| Or
deriving (Eq, Show)
parseError :: [Token] -> a
parseError toks = error "parse error"
}