-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserLogic.py
More file actions
97 lines (76 loc) · 2.81 KB
/
ParserLogic.py
File metadata and controls
97 lines (76 loc) · 2.81 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
from ast_nodes import IfStatement, BinOp, Variable, Number, Assignment
from Lexical_Analyzer import LexicalAnalyzer, Token
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.pos = 0
self.current_token = self.tokens[self.pos]
def eat(self, token_type):
if self.current_token.type == token_type:
self.pos += 1
if self.pos < len(self.tokens):
self.current_token = self.tokens[self.pos]
else:
raise Exception(f"Syntax Error: Expected {token_type}, found {self.current_token.type}")
def parse(self):
"""Entry point for the parser"""
return self.parse_if_statement()
def parse_if_statement(self):
# if ( condition ) { block } else { block }
self.eat('IF')
self.eat('LPAREN')
condition = self.parse_condition()
self.eat('RPAREN')
self.eat('LBRACE')
then_body = self.parse_block()
self.eat('RBRACE')
else_body = None
if self.current_token.type == 'ELSE':
self.eat('ELSE')
self.eat('LBRACE')
else_body = self.parse_block()
self.eat('RBRACE')
return IfStatement(condition, then_body, else_body)
def parse_condition(self):
# ID OP (ID | NUMBER)
left = Variable(self.current_token.value)
self.eat('ID')
op = self.current_token.value
self.eat('OP')
right = None
if self.current_token.type == 'ID':
right = Variable(self.current_token.value)
self.eat('ID')
elif self.current_token.type == 'NUMBER':
right = Number(int(self.current_token.value))
self.eat('NUMBER')
return BinOp(left, op, right)
def parse_block(self):
# Simple block parser: looks for assignments like 'x = 5;'
statements = []
while self.current_token.type == 'ID':
var_name = self.current_token.value
self.eat('ID')
self.eat('ASSIGN')
value_node = None
if self.current_token.type == 'NUMBER':
value_node = Number(int(self.current_token.value))
self.eat('NUMBER')
elif self.current_token.type == 'ID':
value_node = Variable(self.current_token.value)
self.eat('ID')
self.eat('SEMI')
statements.append(Assignment(var_name, value_node))
return statements
# --- Test Runner ---
if __name__ == "__main__":
code = "if (x > 10) { y = 5; } else { y = 0; }"
print(f"Testing Code: {code}")
# 1. Lexical Analysis
lexer = LexicalAnalyzer(code)
tokens = lexer.tokenize()
# 2. Syntax Analysis
parser = Parser(tokens)
ast = parser.parse()
print("\nAbstract Syntax Tree (AST):")
print(ast)