-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.y
121 lines (100 loc) · 2.94 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
{
-- Details of this implementation can be found at:
-- https://www.haskell.org/happy/doc/html/sec-using.html
module Parser where
import Data.Char
}
%name parser
%tokentype { Token }
%error { parseError }
%token
true { TokenTrue }
false { TokenFalse }
num { TokenNum $$ }
'+' { TokenPlus }
'*' { TokenMult }
'(' { TokenOpen }
')' { TokenClose }
',' { TokenComma }
and { TokenAnd }
or { TokenOr }
if { TokenIf }
then { TokenThen }
else { TokenElse }
first { TokenFirst }
second { TokenSecond }
%%
Exp : true { BTrue }
| false { BFalse }
| num { Num $1 }
| Exp '+' Exp { Add $1 $3 }
| Exp '*' Exp { Mult $1 $3 }
| Exp and Exp { And $1 $3 }
| Exp or Exp { Or $1 $3 }
| if Exp then Exp else Exp { If $2 $4 $6 }
| '(' Exp ',' Exp ')' { Pair $2 $4 }
| first Exp { First $2 }
| second Exp { Second $2 }
-- Start of Lexer coding
---------------------------------
{
parseError :: [Token] -> a
parseError _ = error "Syntax error: sequência de caracteres inválida!"
-- Abstract syntax tree
data Expr = BTrue
| BFalse
| Num Int
| Add Expr Expr
| Mult Expr Expr
| And Expr Expr
| Or Expr Expr
| If Expr Expr Expr
| Pair Expr Expr
| First Expr
| Second Expr
deriving (Show, Eq)
-- Tokens allowed in the language
data Token = TokenTrue
| TokenFalse
| TokenNum Int
| TokenPlus
| TokenAnd
| TokenOr
| TokenIf
| TokenMult
| TokenThen
| TokenElse
| TokenOpen
| TokenClose
| TokenComma
| TokenFirst
| TokenSecond
deriving Show
-- Lexical analyzer (reads code and converts to a list of tokens)
lexer :: String -> [Token]
lexer [] = []
lexer (c:cs)
| isSpace c = lexer cs
| isAlpha c = lexKeyWord (c:cs)
| isDigit c = lexNum (c:cs)
lexer ('+':cs) = TokenPlus : lexer cs
lexer ('*':cs) = TokenMult : lexer cs
lexer ('(':cs) = TokenOpen : lexer cs
lexer (')':cs) = TokenClose : lexer cs
lexer (',':cs) = TokenComma : lexer cs
lexer _ = error "Lexical error: caracter inválido!"
-- Read a boolean token
lexKeyWord cs = case span isAlpha cs of -- if true then 1 else 0
("true", rest) -> TokenTrue : lexer rest
("false", rest) -> TokenFalse : lexer rest
("if", rest) -> TokenIf : lexer rest
("then", rest) -> TokenThen : lexer rest
("else", rest) -> TokenElse : lexer rest
("and", rest) -> TokenAnd : lexer rest
("or", rest) -> TokenOr : lexer rest
("first", rest) -> TokenFirst : lexer rest
("second", rest) -> TokenSecond : lexer rest
-- Read a numeric token
lexNum cs = case span isDigit cs of
(num, rest) -> TokenNum (read num) : lexer rest
}