-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical_analyzer.py
executable file
·162 lines (128 loc) · 4.5 KB
/
lexical_analyzer.py
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
# coding: utf-8
# Antonio Filho
# Diego Carvalho
import sys
import string
from IO import IO
from classes.token import Token
# Símbolos aceitos e palavras reservadas.
dictionary = ['(', ')', '{', '}', ',', ';', '+', '-', '*', '/', ' ', '\n', '\t']
words = ['int', 'main', 'float', 'if', 'else', 'while', 'read', 'print']
delimiters = [' ', '(', ')', '{', '}']
tokens = []
program = []
def addToken(token):
program.append(token.lexical)
tokens.append(token)
# Automato
def lexicalAnalyzer(char, file):
if char.char == "=":
if file.buffer[0].char == '=':
token = Token("==", "EQ", char.line)
addToken(token)
char = file.getNextChar()
else:
token = Token(char.char, "ATTR", char.line)
addToken(token)
elif char.char == "!":
if file.buffer[0].char == '=':
token = Token("!=", "NE", char.line)
addToken(token)
char = file.getNextChar()
elif char.char == ">":
if file.buffer[0].char == '=':
token = Token(">=", "GE", char.line)
addToken(token)
char = file.getNextChar()
else:
token = Token(char.char, "GT", char.line)
addToken(token)
elif char.char == "<":
if file.buffer[0].char == '=':
token = Token("<=", "LE", char.line)
addToken(token)
char = file.getNextChar()
else:
token = Token(char.char, "LT", char.line)
addToken(token)
elif char.char == "&":
if file.buffer[0].char == '&':
token = Token("&&", "AND", char.line)
addToken(token)
char = file.getNextChar()
elif char.char == "|":
if file.buffer[0].char == '|':
token = Token("||", "OR", char.line)
addToken(token)
char = file.getNextChar()
elif char.char == ";":
token = Token(char.char, "PCOMMA", char.line)
addToken(token)
elif char.char == ",":
token = Token(char.char, "COMMA", char.line)
addToken(token)
elif char.char == "}":
token = Token(char.char, "RBRACE", char.line)
addToken(token)
elif char.char == "{":
token = Token(char.char, "LBRACE", char.line)
addToken(token)
elif char.char == ")":
token = Token(char.char, "RBRACKET", char.line)
addToken(token)
elif char.char == "(":
token = Token(char.char, "LBRACKET", char.line)
addToken(token)
elif char.char == "+":
token = Token(char.char, "PLUS", char.line)
addToken(token)
elif char.char == "-":
token = Token(char.char, "MINUS", char.line)
addToken(token)
elif char.char == "*":
token = Token(char.char, "MULT", char.line)
addToken(token)
elif char.char == "/":
token = Token(char.char, "DIV", char.line)
addToken(token)
elif char.char in string.ascii_lowercase or char.char \
in string.ascii_uppercase:
newWord = char.char
while file.buffer[0].char in string.ascii_lowercase or file.buffer[0].char in string.ascii_uppercase or \
file.buffer[0].char in string.digits:
char = file.getNextChar()
newWord = newWord + char.char
if newWord in words:
token = Token(newWord, str.upper(newWord), char.line)
else:
token = Token(newWord, "ID", char.line)
addToken(token)
elif char.char in string.digits:
newWord = char.char
int_avaiable = True
while file.buffer[0].char in string.digits or file.buffer[0].char == ".":
char = file.getNextChar()
newWord = newWord + char.char
if char.char == ".":
char = file.getNextChar()
newWord = newWord + char.char
while file.buffer[0].char in string.digits:
char = file.getNextChar()
newWord = newWord + char.char
token = Token(newWord, "FLOAT_CONST", char.line)
int_avaiable = False
if int_avaiable:
token = Token(newWord, "INTEGER_CONST", char.line)
addToken(token)
# Get tokens out
def getOut():
return tokens, program
# Run AnalisadorLexico
def run():
file = IO('main.c')
char = file.getNextChar()
while (char.char != False):
lexicalAnalyzer(char, file)
char = file.getNextChar()
# file_out = open("out.txt", 'w')
# file_out.write(" ".join(program))