@@ -16,10 +16,17 @@ 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", @@ -66,17 +73,19 @@ # Parse (yacc) def p_pitch_list(p): '''score : score note - score : score chord + | score chord + | score rest ''' p[0] = p[1] + [p[2]] def p_score(p): '''score : note - score : chord + | chord + | rest ''' p[0] = [p[1]] def p_note(p): @@ -129,12 +138,20 @@ 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)