14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
def __init__(self, value, duration=.5, chord_type="major"):
self.value = value
self.duration = duration
self.chord_type = chord_type
def __repr__(self):
return "Chord %s %s %s" % (self.value, self.duration, self.chord_type)
def parse(score):
# Tokenize (lex)
tokens = (
"NOTE_LENGTH",
"BASENOTE",
"ACCIDENTAL",
|
>
>
>
>
>
>
>
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
def __init__(self, value, duration=.5, chord_type="major"):
self.value = value
self.duration = duration
self.chord_type = chord_type
def __repr__(self):
return "Chord %s %s %s" % (self.value, self.duration, self.chord_type)
class Rest():
def __init__(self, duration=.25):
self.duration = duration
def __repr__(self):
return "Rest node %s" % self.duration
def parse(score):
# Tokenize (lex)
tokens = (
"NOTE_LENGTH",
"BASENOTE",
"ACCIDENTAL",
|
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# Parse (yacc)
def p_pitch_list(p):
'''score : score note
score : score chord
'''
p[0] = p[1] + [p[2]]
def p_score(p):
'''score : note
score : chord
'''
p[0] = [p[1]]
def p_note(p):
'''note : pitch
'''
|
|
>
|
>
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# Parse (yacc)
def p_pitch_list(p):
'''score : score note
| score chord
| score rest
'''
p[0] = p[1] + [p[2]]
def p_score(p):
'''score : note
| chord
| rest
'''
p[0] = [p[1]]
def p_note(p):
'''note : pitch
'''
|
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
p[0] = p[1]
def p_pitch(p):
'''pitch : BASENOTE
'''
p[0] = Note(p[1])
def p_error(p):
print "Syntax error at '%s' of element type %s" % (p.value, p.type)
yacc.yacc()
#print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD")
return yacc.parse(score)
|
>
>
>
>
>
>
>
>
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
p[0] = p[1]
def p_pitch(p):
'''pitch : BASENOTE
'''
p[0] = Note(p[1])
def p_rest(p):
''' rest : REST
| REST NOTE_LENGTH
'''
p[0] = Rest()
if len(p) > 2:
p[0].duration = p[2]
def p_error(p):
print "Syntax error at '%s' of element type %s" % (p.value, p.type)
yacc.yacc()
#print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD")
return yacc.parse(score)
|