-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclarations.py
More file actions
173 lines (141 loc) · 4.17 KB
/
Copy pathdeclarations.py
File metadata and controls
173 lines (141 loc) · 4.17 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
from enum import Enum
from typing import Union, List
class ActionSymbol(Enum):
ptoken = "ptoken",
pid = "pid",
starr = "starr",
stvar = "stvar",
stfunc = "stfunc",
st_param_var = "st_param_var",
st_param_arr = "st_param_arr",
pop_exp = "pop_exp",
break_val = "break_val",
save = "save",
jpf_save = "jpf_save",
jp = "jp",
jpf_save_i = "jpf_save_i",
save_i = "save_i",
return_result = "return_result",
determine_arr = "determine_arr",
assign = "assign",
assign_arr = "assign_arr",
compare = "compare",
addop = "addop",
mult = "mult",
start_args = "start_args",
start_func = "start_func",
call_func = "call_func",
init_program = "init_program",
end_func = "end_func",
return_from_func = "return_from_func",
determine_id = "determine_id",
pnum = "pnum"
class Nonterminal(Enum):
PROGRAM = "Program"
DECLARATION_LIST = "Declaration-list"
DECLARATION = "Declaration"
DECLARATION_INITIAL = "Declaration-initial"
DECLARATION_PRIME = "Declaration-prime"
VAR_DECLARATION_PRIME = "Var-declaration-prime"
FUN_DECLARATION_PRIME = "Fun-declaration-prime"
TYPE_SPECIFIER = "Type-specifier"
PARAMS = "Params"
PARAM_LIST = "Param-list"
PARAM = "Param"
PARAM_PRIME = "Param-prime"
COMPOUND_STMT = "Compound-stmt"
STATEMENT_LIST = "Statement-list"
STATEMENT = "Statement"
EXPRESSION_STMT = "Expression-stmt"
SELECTION_STMT = "Selection-stmt"
ELSE_STMT = "Else-stmt"
ITERATION_STMT = "Iteration-stmt"
RETURN_STMT = "Return-stmt"
RETURN_STMT_PRIME = "Return-stmt-prime"
EXPRESSION = "Expression"
B = "B"
H = "H"
SIMPLE_EXPRESSION_ZEGOND = "Simple-expression-zegond"
SIMPLE_EXPRESSION_PRIME = "Simple-expression-prime"
C = "C"
RELOP = "Relop"
ADDITIVE_EXPRESSION = "Additive-expression"
ADDITIVE_EXPRESSION_PRIME = "Additive-expression-prime"
ADDITIVE_EXPRESSION_ZEGOND = "Additive-expression-zegond"
D = "D"
ADDOP = "Addop"
TERM = "Term"
TERM_PRIME = "Term-prime"
TERM_ZEGOND = "Term-zegond"
G = "G"
FACTOR = "Factor"
VAR_CALL_PRIME = "Var-call-prime"
VAR_PRIME = "Var-prime"
FACTOR_PRIME = "Factor-prime"
FACTOR_ZEGOND = "Factor-zegond"
ARGS = "Args"
ARG_LIST = "Arg-list"
ARG_LIST_PRIME = "Arg-list-prime"
class TokenType(Enum):
NUM = "NUM"
KEYWORD = "KEYWORD"
ID = "ID"
SYMBOL = "SYMBOL"
EOF = "EOF"
class TransitionType(Enum):
T = "TERMINAL"
NT = "NON-TERMINAL"
class Token:
type: TokenType
lexeme: str
line_num: int
def __init__(self, type: TokenType, lexeme, line_num):
self.type = type
self.lexeme = lexeme
self.line_num = line_num
def __str__(self):
if self.lexeme == "$":
return self.lexeme
return f"({self.type.value}, {self.lexeme})"
class T_ID:
type: TokenType
lexeme: str
def __init__(self, type: TokenType, lexeme):
self.type = type
self.lexeme = lexeme
def __eq__(self, other):
return isinstance(other, T_ID) and self.type == other.type and self.lexeme == other.lexeme
class State:
nonterminal: Nonterminal
state: int
def __init__(self, nonterminal, state):
self.nonterminal = nonterminal
self.state = state
class Transition:
dest_state: int
identifier: Union[str, Nonterminal, TokenType, T_ID, ActionSymbol]
def __init__(self, dest_state: int, identifier):
self.dest_state = dest_state
self.identifier = identifier
class NTerminalInfo:
first: List[str]
follow: List[str]
def __init__(self, first, follow):
self.first = first
self.follow = follow
class Syntax_Error:
line_num: int
text: str
def __init__(self, line_num, text):
self.text = text
self.line_num = line_num
def __str__(self):
return f'#{self.line_num} : syntax error, {self.text}'
class Semantic_Error:
line_num: int
text: str
def __init__(self, line_num, text):
self.text = text
self.line_num = line_num
def __str__(self):
return f'#{self.line_num} : Semantic Error! {self.text}'