-
Notifications
You must be signed in to change notification settings - Fork 0
/
oaf_lex.py
150 lines (135 loc) · 2.89 KB
/
oaf_lex.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
import ply.lex as lex
# Reserved words
reserved = {
'arc': 'ARC',
'bool': 'BOOL',
'brush': 'BRUSH',
'char': 'CHAR',
'circle': 'CIRCLE',
'color': 'COLOR',
'else': 'ELSE',
'false': 'FALSE',
'fd': 'FD',
'figure': 'FIGURE',
'float': 'FLOAT',
'home': 'HOME',
'if': 'IF',
'int': 'INT',
'length': 'LENGTH',
'loop': 'LOOP',
'main': 'MAIN',
'pd': 'PD',
'print': 'PRINT',
'pu': 'PU',
'read': 'READ',
'return': 'RETURN',
'rt': 'RT',
'square': 'SQUARE',
'triangle': 'TRIANGLE',
'true': 'TRUE',
'void': 'VOID',
'speed': 'SPEED',
'clear':'CLEAR'
}
# List of token names
tokens = [
# Literals
'ID', 'ICONST', 'FCONST', 'CCONST', 'STRING',
# Operators
'PLUS', 'MINUS', 'TIMES', 'DIVIDE',
# Separators
'LPAREN', 'RPAREN', 'LBRACE', 'RBRACE', 'LBRACKET', 'RBRACKET', 'SEMI', 'COMMA',
# Comparison
'LESSTHAN', 'GREATHAN', 'GREATEQUAL', 'LESSEQUAL', 'DIFFERENT', 'TWOEQUAL',
# Logical
'AND', 'OR', 'NOT',
# Assignment
'EQUAL',
] + list(reserved.values())
# Regular expression rules
# Identifiers
def t_ID(t):
r'[a-z][a-zA-Z0-9]*'
# Check for reserved words
t.type = reserved.get(t.value, 'ID')
return t
# Float
def t_FCONST(t):
r'\d+\.\d+'
t.value = float(t.value)
return t
# Integer
def t_ICONST(t):
r'\d+'
t.value = int(t.value)
return t
# Comments
def t_COMMENT(t):
r'\#.*'
# No return value, token discarded
pass
# Literals
t_STRING = r'".*"'
t_CCONST = r'\'[ -~]\''
# Operators
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
# Separators
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACE = r'\{'
t_RBRACE = r'\}'
t_LBRACKET = r'\['
t_RBRACKET = r'\]'
t_SEMI = r';'
t_COMMA = r','
# Comparison
t_LESSTHAN = r'\<'
t_GREATHAN = r'\>'
t_GREATEQUAL = r'\>='
t_LESSEQUAL = r'\<='
t_DIFFERENT = r'\<\>'
t_TWOEQUAL = r'=='
# Logical
t_AND = r'&&'
t_OR = r'\|\|'
t_NOT = r'!'
# Assignment
t_EQUAL = r'='
# 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)
t_ignore = ' \t'
# Compute column.
# input is the input text string
# token is a token instance
def find_column(input, token):
last_cr = input.rfind('\n', 0, token.lexpos)
if last_cr < 0:
last_cr = 0
column = (token.lexpos - last_cr) + 1
return column
# Error handling rule
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# # Test it out
# data = '''
# 3 + 4 * 10
# 4.8 0.0
# "" "hola"
# derp 'c'
# '''
# # Give the lexer some input
# lexer.input(data)
# # Tokenize
# while True:
# tok = lexer.token()
# if not tok: break # No more input
# print tok