Repository navigation
Expand file tree
/
Copy pathparser.py
More file actions
153 lines (152 loc) · 6 KB
/
Copy pathparser.py
File metadata and controls
153 lines (152 loc) · 6 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
from tokens import TokenType
from ast_nodes import *
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.pos = 0
def current(self):
return self.tokens[self.pos]
def eat(self, token_type):
if self.current().type == token_type:
self.pos += 1
return True
return False
def expect(self, token_type):
if self.current().type == token_type:
token = self.current()
self.pos += 1
return token
raise SyntaxError(f"Expected {token_type}, got {self.current().type}")
def parse_program(self):
stmts = []
while self.current().type != TokenType.EOF:
stmts.append(self.parse_statement())
return Program(stmts)
def parse_statement(self):
token = self.current()
if token.type in [TokenType.INT, TokenType.FLOAT, TokenType.INTR]:
return self.parse_declaration()
elif token.type == TokenType.PRINTF:
return self.parse_printf()
elif token.type == TokenType.SCANF:
return self.parse_scanf()
elif token.type == TokenType.SWITCH:
return self.parse_switch()
elif token.type == TokenType.IDENTIFIER:
return self.parse_assignment()
elif token.type == Token.Type.TUER:
return self.parse_assignment()
else:
return self.parse_tuer()
def parse_declaration(self):
var_type = self.current().type.name.lower()
self.pos += 1
name = self.expect(TokenType.IDENTIFIER).value
self.expect(TokenType.ASSIGN)
value = self.parse_expression()
self.expect(TokenType.SEMICOLON)
return Declaration(var_type, name, value)
def parse_assignment(self):
name = self.expect(TokenType.IDENTIFIER).value
self.expect(TokenType.ASSIGN)
value = self.parse_expression()
self.expect(TokenType.SEMICOLON)
return Declaration(None, name, value)
def parse_printf(self):
self.eat(TokenType.PRINTF)
self.expect(TokenType.LPAREN)
format_str = self.expect(TokenType.STRING).value
args = []
if self.current().type == TokenType.COMMA:
self.eat(TokenType.COMMA)
args.append(self.parse_expression())
while self.current().type == TokenType.COMMA:
self.eat(TokenType.COMMA)
args.append(self.parse_expression())
self.expect(TokenType.RPAREN)
self.expect(TokenType.SEMICOLON)
return Printf(format_str, args)
def parse_scanf(self):
self.eat(TokenType.SCANF)
self.expect(TokenType.LPAREN)
format_str = self.expect(TokenType.STRING).value
args = []
if self.current().type == TokenType.COMMA:
self.eat(TokenType.COMMA)
args.append(self.expect(TokenType.IDENTIFIER).value)
while self.current().type == TokenType.COMMA:
self.eat(TokenType.COMMA)
args.append(self.expect(TokenType.IDENTIFIER).value)
self.expect(TokenType.RPAREN)
self.expect(TokenType.SEMICOLON)
return Scanf(format_str, args)
def parse_switch(self):
self.eat(TokenType.SWITCH)
self.expect(TokenType.LPAREN)
expr = self.parse_expression()
self.expect(TokenType.RPAREN)
self.expect(TokenType.LBRACE)
cases = []
default = None
while self.current().type != TokenType.RBRACE:
if self.current().type == TokenType.CASE:
self.eat(TokenType.CASE)
value_token = self.expect(TokenType.NUMBER)
self.expect(TokenType.COLON)
body = []
while self.current().type not in [TokenType.CASE, TokenType.DEFAULT, TokenType.RBRACE]:
body.append(self.parse_statement())
cases.append(Case(value_token.value, body))
elif self.current().type == TokenType.DEFAULT:
self.eat(TokenType.DEFAULT)
self.expect(TokenType.COLON)
body = []
while self.current().type not in [TokenType.CASE, TokenType.DEFAULT, TokenType.RBRACE]:
body.append(self.parse_statement())
default = body
else:
raise SyntaxError("Expected case or default")
self.expect(TokenType.RBRACE)
return Switch(expr, cases, default)
def parse_expression(self):
return self.parse_add_sub()
def parse_add_sub(self):
left = self.parse_mul_div()
while self.current().type in [TokenType.PLUS, TokenType.MINUS]:
op = self.current()
self.pos += 1
right = self.parse_mul_div()
left = BinaryOp(left, op.type.name, right)
return left
def parse_mul_div(self):
left = self.parse_primary()
while self.current().type in [TokenType.MULTIPLY, TokenType.DIVIDE]:
op = self.current()
self.pos += 1
right = self.parse_primary()
left = BinaryOp(left, op.type.name, right)
return left
def parse_primary(self):
token = self.current()
if token.type in [TokenType.NUMBER, TokenType.FLOAT_NUMBER]:
self.pos += 1
return Number(token.value)
elif token.type == TokenType.IDENTIFIER:
self.pos += 1
return Variable(token.value)
elif token.type == TokenType.LPAREN:
self.pos += 1
expr = self.parse_expression()
self.expect(TokenType.RPAREN)
return expr
def parse_tuer(self):
self.eat(TokenType.TUER)
self.expect(TokenType.LPAREN)
format_str = self.expect(TokenType.STRING).value
self.expect(TokenType.COMMA)
var_name = self.expect(TokenType.IDENTIFIER).value
self.expect(TokenType.RPAREN)
self.expect(TokenType.SEMICOLON)
return ('tuer', format_str, var_name)
else:
raise SyntaxError(f"Unexpected token: {token}")