-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammar.y
220 lines (192 loc) · 6.54 KB
/
Grammar.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
{
module Grammar where
import Tokens
}
%name parseCalc
%tokentype { Token }
%error { parseError }
%token
"+" { TokenOp _ "+" }
"-" { TokenOp _ "-" }
"*" { TokenOp _ "*" }
"/" { TokenOp _ "/" }
"**" { TokenOp _ "**" }
"<" { TokenCompareOp _ "<" }
">" { TokenCompareOp _ ">" }
"<=" { TokenCompareOp _ "<=" }
">=" { TokenCompareOp _ ">=" }
"==" { TokenCompareOp _ "==" }
"!=" { TokenCompareOp _ "!=" }
"&&" { TokenCompareOp _ "&&" }
"||" { TokenCompareOp _ "||" }
ident { TokenIdent _ $$ }
intLit { TokenIntLit _ $$ }
stringLit { TokenStringLit _ $$}
"int" { TokenInt _ }
"string" { TokenString _ }
"bool" { TokenBool _ }
"=" { TokenEquals _ }
"not" { TokenNegate _ }
"(" { TokenLeftParen _ }
")" { TokenRightParen _ }
"[" { TokenLeftBracket _ }
"]" { TokenRightBracket _ }
"{" { TokenLeftBrace _ }
"}" { TokenRightBrace _ }
";" { TokenSemiColon _ }
"," { TokenColon _}
"if" { TokenIf _ }
"else" { TokenElse _}
"true" { TokenTrue _}
"false" { TokenFalse _ }
"while" { TokenWhile _ }
"print" { TokenPrint _}
"println" { TokenPrintLine _}
"read" { TokenRead _}
"get" { TokenGet _}
"consume" { TokenConsume _}
"stream" { TokenStream _}
"length" { TokenLength _}
"null" { TokenNull _}
%nonassoc "if" "while"
%nonassoc "else"
%nonassoc "print" "println" "int" "bool" "string" "null"
%right "="
%left "&&" "||"
%right ">" "<" ">=" "<=" "==" "!="
%left "+" "-"
%left "*" "/"
%left "**"
%left "not"
%%
Program :
StatementList { Program $1 }
Statement : "if" "(" Expr ")" "{" StatementList "}" "else" "{" StatementList "}" { StatementIfElse $3 $6 $10 }
| "if" "(" Expr ")" "{" StatementList "}" { StatementIf $3 $6 }
| "while" "(" Expr ")" "{" StatementList "}" { StatementWhile $3 $6 }
| "print" "(" Expr ")" ";" { StatementPrint $3 }
| "println" "(" Expr ")" ";" { StatementPrintLine $3 }
| ident "=" Expr ";" { StatementAssign $1 $3 }
| ident "[" Expr "]" "=" Expr ";" { StatementArrayAssign $1 $3 $6}
| VarDeclr { StatementVarDeclr $1}
| ArrayDeclr { StatementArrayDeclr $1}
| "consume" "(" Expr ")" ";" { StatementConsume $3}
VarDeclr : Type ident ";" { VarDeclrOnly $1 $2 }
| Type ident "=" Expr ";" { VarDeclrAssign $1 $2 $4 }
ExprList : ExprList "," Expr { ($1 ++ [$3]) }
| Expr { [$1] }
| {- empty -} { [] }
ArrayDeclr : Type "[" "]" ident ";" { ArrayDeclrOnly $1 $4 }
| Type "[" "]" ident "=" Expr ";" { ArrayDeclrAssign $1 $4 $6 }
StatementList : Statement StatementList { ([$1] ++ $2) }
| {- empty -} { [] }
Expr : "not" Expr { ExprNot $2 }
| Expr "**" Expr { ExprOp $1 Power $3 }
| Expr "/" Expr { ExprOp $1 Divide $3 }
| Expr "*" Expr { ExprOp $1 Multiply $3 }
| Expr "+" Expr { ExprOp $1 Plus $3 }
| Expr "-" Expr { ExprOp $1 Minus $3 }
| Expr "<=" Expr { ExprCompareOp $1 LessOrEqualThan $3 }
| Expr "<" Expr { ExprCompareOp $1 LessThan $3 }
| Expr ">" Expr { ExprCompareOp $1 GreaterThan $3 }
| Expr ">=" Expr { ExprCompareOp $1 GreaterOrEqualThan $3 }
| Expr "==" Expr { ExprCompareOp $1 Equals $3 }
| Expr "!=" Expr { ExprNot (ExprCompareOp $1 Equals $3) }
| Expr "&&" Expr { ExprCompareOp $1 And $3 }
| Expr "||" Expr { ExprCompareOp $1 Or $3 }
| "(" Expr ")" { ExprExpr $2}
| "[" ExprList "]" { ExprArrayAssign $2 }
| ident "[" Expr "]" { ExprArrayValue $1 $3 }
| intLit { ExprInt $1 }
| ident { ExprIdent $1 }
| "true" { ExprBool True }
| "false" { ExprBool False }
| stringLit { ExprString $1 }
| "read" "(" ")" { ExprRead ExprNothing }
| "read" "(" Expr ")" { ExprRead $3 }
| "get" "(" ")" { ExprGet ExprNothing }
| "get" "(" Expr ")" { ExprGet $3 }
| "stream" "(" Expr ")" { ExprStream $3 }
| "length" "(" Expr ")" { ExprLength $3 }
| "null" { ExprNothing }
Type : "bool" { TypeBool }
| "int" { TypeInt }
| "string" { TypeString }
{
parseError :: [Token] -> a
parseError [] = error " Unknown parse error"
parseError (x:xs) = error ("Parse error at line:column " ++ (token_posn x))
data Program
= Program StatementList
deriving (Show, Eq)
data VarDeclr
= VarDeclrOnly Type Ident
| VarDeclrAssign Type Ident Expr
deriving (Show, Eq)
data ArrayDeclr
= ArrayDeclrOnly Type Ident
| ArrayDeclrAssign Type Ident Expr
deriving (Show,Eq)
data Statement
= Statement String
| StatementIfElse Expr StatementList StatementList
| StatementIf Expr StatementList
| StatementWhile Expr StatementList
| StatementPrint Expr
| StatementPrintLine Expr
| StatementAssign Ident Expr
| StatementArrayAssign Ident Expr Expr
| StatementVarDeclr VarDeclr
| StatementArrayDeclr ArrayDeclr
| StatementConsume Expr
| StatementError
deriving (Show, Eq)
type StatementList = [Statement]
type ExprList = [Expr]
data Expr
= ExprString String
| ExprNot Expr
| ExprOp Expr Op Expr
| ExprCompareOp Expr CompareOp Expr
| ExprInt Int
| ExprBool Bool
| ExprIdent Ident
| ExprExpr Expr
| ExprArrayAssign ExprList
| ExprArrayValue Ident Expr
| ExprRead Expr
| ExprGet Expr
| ExprStream Expr
| ExprLength Expr
| ExprEmpty
| ExprError
| ExprNothing
deriving (Show, Eq)
data Type
= TypeInt
| TypeBool
| TypeString
| TypeIntArray
| TypeBoolArray
| TypeStringArray
| TypeEmpty
deriving (Show,Eq)
data Op
= Minus
| Plus
| Multiply
| Divide
| Power
deriving (Show, Eq)
data CompareOp
= GreaterThan
| LessThan
| LessOrEqualThan
| GreaterOrEqualThan
| Equals
| And
| Or
deriving (Show, Eq)
type Ident = String
type IntegerLit = Int
}