-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntactic_analyser.py
More file actions
47 lines (36 loc) · 1.69 KB
/
syntactic_analyser.py
File metadata and controls
47 lines (36 loc) · 1.69 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
from token_classifier import TToken
from non_terminals import NonTerminal, RuleLeftTokens, RuleNumberOfTokens
from action_table import ACTION_TABLE
# from semantics import Analyser
class Parser:
def __init__(self, lexical_analyser):
self.action_table = ACTION_TABLE
self.state_stack = [0]
self.lexical_analyser = lexical_analyser
# self.sem = Analyser(lexical_analyser)
def run(self):
state = 0
current_token = self.lexical_analyser.next_token()
action = self.action_table[state][current_token]
while (action!="acc"):
print(state, current_token, int(current_token), action)
if (action[0]=="s"):
state = int(action[1:])
self.state_stack.append(state)
current_token=self.lexical_analyser.next_token()
action = self.action_table[state][current_token]
elif (action[0]=="r"):
rule = int(action[1:])
amount_to_pop = RuleNumberOfTokens[rule-1]
self.state_stack = self.state_stack[:len(self.state_stack) - amount_to_pop]
temporary_state = self.state_stack[-1]
left_token = RuleLeftTokens[rule-1]
state_string = self.action_table[temporary_state][left_token]
state = int(state_string)
self.state_stack.append(state)
action = self.action_table[state][current_token]
#self.sem.parse(rule)
else:
print("Erro de sintaxe", state, current_token)
break
print("No syntactic error!")