-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexparse_bla.py
More file actions
164 lines (116 loc) · 3.72 KB
/
lexparse_bla.py
File metadata and controls
164 lines (116 loc) · 3.72 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
from ply import lex
import os
import sys
from ply import yacc
tokens = ["ID", "BINARY_LITERAL", "WHITESPACE", "COMMENT"]
literals = ["A", "S", "M", "D", "=", "(", ")"]
lexerror = ""
t_BINARY_LITERAL = r'[-+]?[0-9]+'
start = "Program"
p_errors_list = []
l_errors_list = []
syntree = []
s_table = []
s_errors_list = []
def t_WHITESPACE(t):
r'\s*(\p{P})?\s'
t.lexer.lineno += t.value.count("\n") # line number tracking for exception handling
return t
def t_COMMENT(t):
r'/\*(.|[\r\n])*?\*/|(//.*)'
# regex allows for /* */ and // comments but also allows extra *s in a multiline /* */ comment
t.lexer.lineno += t.value.count("\n") # implement line number tracking for exception handling
return t
def t_ID(t):
r'[_a-z][_a-z0-9]*'
return t
def t_error(t):
l_errors_list.append("lexical error on line %s" % t.lexer.lineno)
t.lexer.skip(1)
def p_program_statements(p):
"""Program : Statements"""
p[0] = ("Program", p[1])
def p_statements(p):
"""Statements : Statements Statement
| Statement"""
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]
def p_statement(p):
"""Statement : ID '=' expression"""
p[0] = ("=", ["ID," + p[1], p[3]])
if p[1] in s_table:
s_errors_list.append("semantic error on line %s" % p.lineno(1))
else:
s_table.append(p[1])
def p_expression_plus(p):
"""expression : expression 'A' term"""
p[0] = ("A", [p[1], p[3]])
if "ID" in p[3]:
if p[3] not in s_table:
s_errors_list.append("semantic error on line %s" % p.lineno(2))
def p_expression_minus(p):
"""expression : expression 'S' term"""
p[0] = ("S", [p[1], p[3]])
if "ID" in p[3]:
if p[3] not in s_table:
s_errors_list.append("semantic error on line %s" % p.lineno(2))
def p_expression_term(p):
"""expression : term"""
p[0] = p[1]
def p_term_multiply(p):
"""term : term 'M' factor"""
p[0] = ("M", [p[1], p[3]])
if "ID" in p[3]:
if p[3] not in s_table:
s_errors_list.append("semantic error on line %s" % p.lineno(2))
def p_term_divide(p):
"""term : term 'D' factor"""
p[0] = ("D", [p[1], p[3]])
if "ID" in p[3]:
if p[3] not in s_table:
s_errors_list.append("semantic error on line %s" % p.lineno(2))
def p_term_factor(p):
"""term : factor"""
p[0] = p[1]
def p_factor_expression(p):
"""factor : '(' expression ')'"""
p[0] = p[2]
def p_factor_binary(p):
"""factor : BINARY_LITERAL"""
p[0] = "BINARY_LITERAL," + p[1]
def p_factor_id(p):
"""factor : ID"""
p[0] = "ID," + p[1]
def p_comment(p):
"""factor : COMMENT"""
#endrules
def p_error(p):
global flag_for_error
flag_for_error = 1
if p is not None:
if p.type=="WHITESPACE" or p.type=="COMMENT":
parser.errok()
elif len(p_errors_list)>0:
for item in p_errors_list:
if str(p.lineno) not in item:
errors_list.append("parse error on line %s" % p.lineno)
parser.errok()
else:
p_errors_list.append("parse error on line %s" % p.lineno)
parser.errok()
def generate_tree(source):
result = parser.parse(source)
return result
lexer = lex.lex()
parser = yacc.yacc()
def main():
if len(sys.argv) == 2:
infilename = sys.argv[1]
if os.path.isfile(infilename):
infile = open(infilename, "r")
lexer.input(infile.read())
infile.seek(0)
syntree = generate_tree(infile.read())
main()