-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
263 lines (200 loc) · 6.36 KB
/
parser.py
File metadata and controls
263 lines (200 loc) · 6.36 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# coding: utf-8
"""
This module implements the parser for OOPS. Its main entry point is
the function ''parse'' which takes a stream of tokens and returns
the abstract syntax tree of the program or raises an InvalidSyntaxError.
All parsing functions do have one invariant: They expect that the very next
token in the stream is the first token of the syntax element they parse
and they consume all tokens of the element that they are parsing.
"""
from errors import UnexpectedToken
import declarations
from declarations import *
from expressions import *
from statements import *
from lexer import Token
class AST(object):
"""
The abstract syntax tree.
"""
def __init__(self, main_class):
self.main_class = main_class
def generate_code(self):
pass
def context_analysis(self):
pass
def __str__(self):
return str(self.main_class)
def parse(stream):
# Every simple OOPS/0 program starts with the declartion of the
# main class
main = class_decl(stream)
stream.token('EOF')
return AST(main)
def class_decl(stream):
"""
This functions parses a class declaration
"""
stream.keyword('CLASS')
# Read the name of the class
decl = ClassDeclaration(stream.identifier())
stream.keyword('IS')
# read member (vars or methods) of the class
while(stream.seek() != 'END'):
decl.add_member(member_decl(stream))
stream.keyword('END')
stream.keyword('CLASS')
return decl
def member_decl(stream):
"""
This class parses the declaration of a class member, which
can either be a method or a variable
"""
# check if we parse a attribute or a method
token = stream.seek()
if token == 'METHOD':
return method_decl(stream)
else:
vars = var_decl(stream)
stream.token('SEMICOLON')
return vars
def method_decl(stream):
"""
Parse a method declaration.
"""
stream.keyword('METHOD')
method = MethodDeclaration(stream.identifier())
# parse the local variable declarations
stream.keyword('IS')
while(stream.seek().id != 'BEGIN'):
method.add_var(var_decl(stream))
stream.token('SEMICOLON')
stream.keyword('BEGIN')
while(stream.seek().id != 'END'):
method.add_statement(statement(stream))
stream.keyword('END')
stream.keyword('METHOD')
return method
def var_decl(stream):
"""
Parse a variable declaration.
It returns a list of variable declarations since
more than one variable with one type may be declared
in one rush
"""
var_decls = []
var_decls.append(VariableDeclaration(stream.identifier()))
while stream.seek() != "COLON":
stream.token('COMMA')
var_decls.append(VariableDeclaration(stream.indentifier()))
stream.token('COLON')
typename = stream.identifier()
# add the typename to the variable declarations
for decl in var_decls:
decl.type = typename
return var_decls
def statement(stream):
"""
Parse a statement and return the approiate statement type
"""
t = stream.seek()
# call the right function for the statement
# statements is a dict containing them
try:
return statements[t.id](stream)
except KeyError:
e = member_access(stream)
if stream.seek() == 'BECOMES':
stream.next()
stmt = Assignment(e, expression(stream))
else:
stmt = CallStatement(e)
stream.token('SEMICOLON')
return stmt
def read_stmt(stream):
"""
Parse a read statement
"""
stream.keyword('READ')
stmt = ReadStatement(member_access(stream))
stream.token('SEMICOLON')
return stmt
def write_stmt(stream):
stream.keyword('WRITE')
stmt = WriteStatement(expression(stream))
stream.token('SEMICOLON')
def if_stmt(stream):
stream.keyword('IF')
stmt = IfStatement(relation(stream))
stream.keyword('THEN')
while(stream.seek() != 'END'):
stmt.add_to_if(statement(stream))
stream.keyword('END')
stream.keyword('IF')
def while_stmt(stream):
stream.keyword('WHILE')
stmt = WhileStatement(relation(stream))
stream.keyword('DO')
while(stream.seek() != 'END'):
stmt.add(statement(stream))
stream.keyword('END')
stream.keyword('WHILE')
statements = {'READ' : read_stmt, 'WRITE' : write_stmt,
'IF' : if_stmt, 'WHILE' : while_stmt}
def relation(stream):
lhs = expression(stream)
next_token = stream.seek()
if next_token in ('EQ', 'NEQ', 'GT', "GTEQ", "LT",
"LTEQ"):
op = stream.next()
rhs = expression(stream)
return BinaryExpression(lhs, op, rhs)
else:
return lhs
def expression(stream):
e = term(stream)
while stream.seek() in ('PLUS', 'MINUS'):
op = stream.next()
e = BinaryExpression(e, op, expression(stream))
return e
def term(stream):
e = factor(stream)
while stream.seek() in ('TIMES', 'DIV', 'MOD'):
op = stream.next()
e = BinaryExpression(e, op, factor(stream))
return e
def factor(stream):
if stream.seek() == 'MINUS':
return UnaryExpression(stream.next(), factor(stream))
else:
return member_access(stream)
def member_access(stream):
e = literal(stream)
while(stream.seek() == 'PERIOD'):
stream.next()
e = AccessExpression(e, VarOrCallExpression(stream.identifier()))
return e
def literal(stream):
next = stream.next()
if next == 'NUMBER':
return LiteralExpression(next, declarations.intType)
elif next == 'NULL':
return LiteralExpression(0, declarations.nullType)
elif next == 'SELF':
return VarOrCallExpression(Token('IDENT', next.position, '_self'))
elif next == 'NEW':
return NewExpression(stream.identifier())
elif next == 'LPAREN':
exp = expression(stream)
stream.token('RPAREN')
return exp
elif next == 'IDENT':
return VarOrCallExpression(next)
else:
raise UnexpectedToken(next)
if __name__ == '__main__':
from utils import *
from lexer import lex
f = open("/home/tobias/uebersetzerbau/OOPSC-0/Examples/code.oops").read()
stream = Stream(unicode(f, 'utf8'))
parse(TokenStream(lex(stream)))