-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lexer.x
54 lines (50 loc) · 2.88 KB
/
Lexer.x
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
{
module Lexer where
}
%wrapper "basic"
$white = [\ \t\n\r\b\v\f\a\\\'\"]
$alpha = [_A-Za-z]
$digit = [0-9]
tokens :-
$white+ ; -- ignorar caracteres brancos
"//".* ; -- ignorar comentários na mesma linha
"/*"([^\*]|[\r\n]|("*"+([^\*\/]|[\r\n])))*"*"+"/" ; -- ignorar comentários multi linha
"+" { \_ -> PLUS }
"-" { \_ -> MINUS }
"*" { \_ -> MULT }
"/" { \_ -> DIV }
"%" { \_ -> MOD }
"=" { \_ -> ASSIGN }
">" { \_ -> GREATER }
"<" { \_ -> LESS }
">=" { \_ -> GREATERorEQUAL }
"<=" { \_ -> LESSorEQUAL }
"!=" { \_ -> DIFF }
"==" { \_ -> EQUAL }
"(" { \_ -> LPAREN }
")" { \_ -> RPAREN }
"{" { \_ -> LBRACE }
"}" { \_ -> RBRACE }
";" { \_ -> SEMICOLON }
"," { \_ -> COMMA }
"!" { \_ -> NOT }
"&&" { \_ -> AND}
"||" { \_ -> OR }
"true" { \_ -> TRUE }
"false" { \_ -> FALSE }
"bool" { \_ -> BOOL }
"int" { \_ -> INT }
"print_int" { \_ -> PRINTint }
if { \_ -> IF }
else { \_ -> ELSE }
return { \_ -> RETURN }
break { \_ -> BREAK }
continue { \_ -> CONTINUE }
while { \_ -> WHILE }
for { \_ -> FOR }
$alpha($alpha|$digit)* { \s -> ID s }
$digit+ { \s -> NUM (read s) }
{
data Token = ID String | NUM Int | GREATER | LESS | GREATERorEQUAL | LESSorEQUAL | DIFF | EQUAL | ASSIGN | IF | ELSE | WHILE | INT | BOOL | PLUS | DIV | MINUS | MOD | MULT | SEMICOLON | COMMA | RPAREN | LPAREN | RBRACK | LBRACK | RBRACE | LBRACE | RETURN | PRINTint | NOT | AND | OR | BREAK | CONTINUE | FOR | TRUE | FALSE
deriving (Eq, Show)
}