-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
146 lines (130 loc) · 4.02 KB
/
parser.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
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
import ply.yacc as yacc
from lexer import tokens, lexer
precedence = (
('left', 'BTS', 'BP'),
('left', 'TWICE', 'RV'),
('left', 'IKON'),
('left', 'BB', 'JYP', 'SJ', 'SKZ')
)
def p_program(p):
'''program : ANNYEONG ID LPAREN RPAREN block'''
p[0] = ('program', p[2], p[5])
def p_block(p):
'''block : LBRACE statements RBRACE'''
p[0] = p[2]
def p_statements(p):
'''statements : statement
| statements statement'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]
def p_statement(p):
'''statement : assignment
| fighting
| dance
| comeback
| saranghae
| record
| playback'''
p[0] = p[1]
def p_record(p):
'''record : RECORD LPAREN ID RPAREN KPOP'''
p[0] = ('record', p[3])
def p_playback(p):
'''playback : PLAYBACK LPAREN expression RPAREN KPOP'''
p[0] = ('playback', p[3])
def p_assignment(p):
'''assignment : IDOL ID OP expression KPOP'''
p[0] = ('assign', p[2], p[4])
def p_fighting(p):
'''fighting : FIGHTING LPAREN expression RPAREN block
| FIGHTING LPAREN expression RPAREN block DAEBAK block'''
if len(p) == 6:
p[0] = ('fighting', p[3], p[5], None)
else:
p[0] = ('fighting', p[3], p[5], p[7])
def p_dance(p):
'''dance : DANCE LPAREN expression RPAREN block'''
p[0] = ('dance', p[3], p[5])
def p_comeback(p):
'''comeback : COMEBACK LPAREN assignment expression KPOP expression RPAREN block'''
p[0] = ('comeback', p[3], p[4], p[6], p[8])
def p_saranghae(p):
'''saranghae : SARANGHAE LPAREN expression RPAREN KPOP'''
p[0] = ('saranghae', p[3])
def p_expression(p):
'''expression : ID
| NUMBER
| STRING
| BOOLEAN
| SHINEE NUMBER
| GG STRING
| EXO BOOLEAN
| expression IKON expression
| expression BTS expression
| expression BP expression
| expression TWICE expression
| expression RV expression
| expression SJ expression
| expression BB expression
| expression JYP expression
| expression SKZ expression'''
if len(p) == 2:
if p.slice[1].type == 'BOOLEAN':
p[0] = ('boolean', p[1])
elif p.slice[1].type == 'NUMBER':
p[0] = ('number', p[1])
elif p.slice[1].type == 'STRING':
p[0] = ('string', p[1])
else:
p[0] = ('id', p[1])
elif len(p) == 3:
if p[1] == 'shinee':
p[0] = ('number', p[2])
elif p[1] == 'gg':
p[0] = ('string', p[2])
elif p[1] == 'exo':
p[0] = ('boolean', p[2])
elif len(p) == 4:
p[0] = (p[2], p[1], p[3])
def p_error(p):
if p:
print(f"SyntaxError: Unexpected token '{p.value}' of type {p.type} at line {p.lineno}, position {p.lexpos}")
else:
print("SyntaxError: Unexpected end of input")
parser = yacc.yacc()
# Test the parser
if __name__ == "__main__":
data = '''
annyeong main() {
idol x = shinee 10 kpop
idol y = gg "Hello, K-pop!" kpop
idol z = exo true kpop
fighting(x ikon shinee 5) {
saranghae(x bts 10) kpop
} daebak {
saranghae(y) kpop
}
dance(x bp shinee 0) {
saranghae(x bts 20) kpop
idol x = x bp shinee 1 kpop
}
comeback(idol i = shinee 0 kpop i bp shinee 5 kpop i bts shinee 1) {
saranghae(i) kpop
}
fighting(x bb shinee 5) {
saranghae(x bts shinee 10) kpop
} daebak {
saranghae(x) kpop
}
idol name = gg "" kpop
saranghae("What's your name? ") kpop
record(name) kpop
playback("Hello, ") kpop
playback(name) kpop
playback("! Welcome to K-pop Programming!\\n") kpop
}
'''
result = parser.parse(data, lexer=lexer)
print(result)