-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyparser.py
90 lines (64 loc) · 1.6 KB
/
pyparser.py
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
import configparser
import ply.lex as lex
import ply.yacc as yacc
# Read the config file
config = configparser.ConfigParser()
config.read("parser.cfg")
# Lexer definition from config
tokens = config.get("lexer", "tokens").split(", ")
t_NAME = r"[a-zA-Z_][a-zA-Z0-9_]*"
t_NUMBER = r"\d+"
t_PLUS = r"\+"
t_MINUS = r"-"
t_TIMES = r"\*"
t_DIVIDE = r"/"
t_LPAREN = r"\("
t_RPAREN = r"\)"
t_SEMI = r";"
t_COMMA = r","
t_EQUAL = r"="
t_INT = r"\bint\b"
t_VOID = r"\bvoid\b"
# Ignored characters
t_ignore = " \t"
def t_newline(t):
r"\n+"
t.lexer.lineno += len(t.value)
def t_error(t):
"""Generated docstring placeholder."""
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# Define parsing rules from config (simplified example)
def p_declaration(p):
"""declaration : type_specifier NAME SEMI
| type_specifier NAME EQUAL NUMBER SEMI"""
if len(p) == 4:
p[0] = f"{p[1]} {p[2]};"
else:
p[0] = f"{p[1]} {p[2]} = {p[4]};"
def p_type_specifier(p):
"""type_specifier : INT
| VOID"""
p[0] = p[1]
# Error handling rule
def p_error(p):
"""Generated docstring placeholder."""
print("Syntax error at '%s'" % p.value if p else "Syntax error at EOF")
# Build the parser
parser = yacc.yacc()
# Test the parser
def parse_code(code):
"""Generated docstring placeholder."""
lexer.input(code)
return parser.parse(lexer=lexer)
# Example code for parsing
if __name__ == "__main__":
code = """
int x;
int y = 10;
void foo;
"""
result = parse_code(code)
print("Parsed code:\n", result)