-
Notifications
You must be signed in to change notification settings - Fork 1
/
mylexer.py
148 lines (114 loc) · 2.57 KB
/
mylexer.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
#!/usr/bin/python
import sys
sys.path.append("../..")
from ply import *
if "cmm" not in sys.argv[0]:
print ("usage : cmm inputfile")
raise SystemExit
'''
palavras reservadas
bool break for false if int return string true void while
'''
reserved = {
'bool' : 'BOOL',
'break' : 'BREAK',
'for' : 'FOR',
'false' : 'FALSE',
'if' : 'IF',
'else' : 'ELSE',
'int' : 'INT',
'return': 'RETURN',
'string': 'STRING',
'true' : 'TRUE',
'while' : 'WHILE',
'write' : 'WRITE',
'read' : 'READ'
}
tokens = ['NAME', 'NUMBER', 'NORMALSTRING', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'ASSIGN',
'RPAREN', 'LPAREN', 'RCOLC', 'LCOLC', 'RBRACE', 'LBRACE', 'COMMA', 'SEMICOLON', 'OR', 'AND', 'EXPLAMATION', 'INTERROGATION', 'COLON',
'EQUALS', 'DIFF', 'MENOR', 'MAIOR', 'MENOREQUALS', 'MAIOREQUALS', 'SUMEQUALS', 'MINUSEQUALS', 'TIMESEQUALS', 'DIVIDEEQUALS', 'MOD'
]+ list(reserved.values())
'''
tokens e simbolos
( ) [ ] { } , ; + - * / == != > >= < <= || && ! = += -= *= /= %= ? :
'''
t_ignore = ' \t'
t_RPAREN = r'\)'
t_LPAREN = r'\('
t_RCOLC = r'\]'
t_LCOLC = r'\['
t_RBRACE = r'\}'
t_LBRACE = r'\{'
t_COMMA = r','
t_SEMICOLON = r';'
t_OR = r'\|\|'
t_AND = r'&&'
t_EXPLAMATION = r'!'
t_INTERROGATION = r'\?'
t_COLON = r':'
t_EQUALS = r'=='
t_DIFF = r'!='
t_MENOR = r'<'
t_MAIOR = r'>'
t_MENOREQUALS = r'<='
t_MAIOREQUALS = r'>='
t_SUMEQUALS = r'\+='
t_MINUSEQUALS = r'-='
t_TIMESEQUALS = r'\*='
t_DIVIDEEQUALS = r'/='
t_MOD = r'%='
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_ASSIGN = r'='
#t_ASPAS = r'\"'
'''
t_BOOL = r'bool'
t_BREAK = r'break'
t_FOR = r'for'
t_FALSE = r'false'
t_IF = r'if'
t_INT = r'int'
t_RETURN = r'return'
t_STRING = r'string'
t_TRUE = r'true'
t_VOID = r'void'
t_WHILE = r'while'
t_PROCEDURE = r'proc'
t_FUNCTION = r'func'
'''
def t_NAME(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
if t.value in reserved:# Check for reserved words
t.type = reserved[ t.value ]
# print(t)
return t
def t_NORMALSTRING(t):
r'\"([^\\\n]|(\\.))*?\"'
# print(t)
return t
def t_NUMBER(t):
r'\d+'
# print(t)
t.value = int(t.value)
return t
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# A string containing ignored characters (spaces and tabs)
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
def t_COMMENT_MONOLINE(t):
r'//.*'
pass
# No return value. Token discarded
def t_ccode_comment(t):
r'(/\*(.|\n)*?\*/)|(//.*)'
pass
if __name__ == '__main__':
lex.runmain()
lex.lex()