]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
release: 1.1.67
[lilypond.git] / lily / parser.yy
1 %{ // -*-Fundamental-*-
2
3 /*
4   parser.yy -- Bison/C++ parser for mudela
5
6   source file of the GNU LilyPond music typesetter
7
8   (c) 1997 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9            Jan Nieuwenhuizen <janneke@gnu.org>
10 */
11
12 #include <iostream.h>
13 #include "lily-guile.hh"
14 #include "notename-table.hh"
15 #include "scalar.hh"
16 #include "translation-property.hh"
17 #include "lookup.hh"
18 #include "misc.hh"
19 #include "my-lily-lexer.hh"
20 #include "paper-def.hh"
21 #include "midi-def.hh"
22 #include "main.hh"
23 #include "file-path.hh"
24 #include "keyword.hh"
25 #include "debug.hh"
26 #include "dimensions.hh"
27 #include "identifier.hh"
28 #include "command-request.hh"
29 #include "musical-request.hh"
30 #include "my-lily-parser.hh"
31 #include "context-specced-music.hh"
32 #include "translator-group.hh"
33 #include "score.hh"
34 #include "music-list.hh"
35 #include "duration-convert.hh"
36 #include "change-translator.hh"
37 #include "file-results.hh"
38 #include "scope.hh"
39 #include "relative-music.hh"
40 #include "lyric-combine-music.hh"
41 #include "transposed-music.hh"
42 #include "time-scaled-music.hh"
43 #include "repeated-music.hh"
44 #include "mudela-version.hh"
45 #include "grace-music.hh"
46
47 // mmm
48 Mudela_version oldest_version ("1.1.52");
49
50
51
52 void
53 print_mudela_versions (ostream &os)
54 {
55   os << "Oldest supported input version:   " << oldest_version.str () << endl;
56 }
57 // needed for bison.simple's malloc() and free()
58 #include <malloc.h>
59
60 #ifndef NDEBUG
61 #define YYDEBUG 1
62 #endif
63
64 #define YYERROR_VERBOSE 1
65
66 #define YYPARSE_PARAM my_lily_parser_l
67 #define YYLEX_PARAM my_lily_parser_l
68 #define THIS ((My_lily_parser *) my_lily_parser_l)
69
70 #define yyerror THIS->parser_error
71 #define ARRAY_SIZE(a,s)   if (a.size () != s) THIS->parser_error (_f("expecting %d arguments", s))
72
73 %}
74
75
76 %union {
77     Array<Real>* realarr;
78     Array<Musical_pitch> *pitch_arr;
79     Link_array<Request> *reqvec;
80     Array<String> * strvec;
81     Array<int> *intvec;
82     Notename_table *chordmodifiertab;
83     Duration *duration;
84     Identifier *id;
85     Music *music;
86     Music_list *music_list;
87     Score *score;
88     Scope *scope;
89     Interval *interval;
90     Musical_req* musreq;
91     Music_output_def * outputdef;
92     Musical_pitch * pitch;
93     Midi_def* midi;
94     Moment *moment;
95     Notename_table *notenametab;
96     Paper_def *paper;
97     Real real;
98     Request * request;
99     Scalar *scalar;
100
101     String *string;
102     Tempo_req *tempo;
103     Translator* trans;
104     char c;
105     int i;
106     int ii[10];
107 }
108 %{
109
110 int
111 yylex (YYSTYPE *s,  void * v_l)
112 {
113         My_lily_parser   *pars_l = (My_lily_parser*) v_l;
114         My_lily_lexer * lex_l = pars_l->lexer_p_;
115
116         lex_l->lexval_l = (void*) s;
117         return lex_l->yylex ();
118 }
119
120
121 %}
122
123 %pure_parser
124
125 /* tokens which are not keywords */
126
127 %token TEXTSCRIPT
128 %token ACCEPTS
129 %token ALTERNATIVE
130 %token BAR
131 %token BREATHE
132 %token CADENZA
133 %token CHORDMODIFIERS
134 %token CHORDS
135 %token HYPHEN
136 %token CLEF
137 %token CM_T
138 %token CONSISTS
139 %token SEQUENTIAL
140 %token SIMULTANEOUS
141 %token CONSISTSEND
142 %token DURATION
143 %token EXTENDER
144 %token FONT
145 %token GRACE
146 %token HEADER
147 %token IN_T
148 %token INVALID
149 %token KEY
150 %token KEYSIGNATURE
151 %token LYRICS
152 %token MARK
153 %token MEASURES
154 %token MIDI
155 %token MM_T
156 %token MUSICAL_PITCH
157 %token NAME
158 %token NOTENAMES
159 %token NOTES
160 %token PAPER
161 %token PARTIAL
162 %token PENALTY
163 %token PROPERTY
164 %token PT_T
165 %token RELATIVE
166 %token REMOVE
167 %token REPEAT
168 %token REPETITIONS
169 %token ADDLYRICS
170 %token SCM_T
171 %token SCMFILE
172 %token SCORE
173 %token SCRIPT
174 %token SHAPE
175 %token SKIP
176 %token SPANREQUEST
177 %token TEMPO
178 %token TIME_T
179 %token TIMES
180 %token TRANSLATOR
181 %token TRANSPOSE
182 %token TYPE
183 %token CONTEXT
184 %token VERSION
185
186 /* escaped */
187 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR CHORD_MINUS CHORD_CARET 
188
189 %type <i>       exclamations questions
190 %token <i>      DIGIT
191 %token <pitch>  NOTENAME_PITCH
192 %token <pitch>  TONICNAME_PITCH
193 %token <pitch>  CHORDMODIFIER_PITCH
194 %token <id>     DURATION_IDENTIFIER
195 %token <id>     IDENTIFIER
196 %token <id>     NOTENAME_TABLE_IDENTIFIER
197 %token <id>     MUSIC_IDENTIFIER
198 %token <id>     REQUEST_IDENTIFIER
199 %token <id>     REAL_IDENTIFIER
200 %token <id>     STRING_IDENTIFIER
201 %token <id>     TRANS_IDENTIFIER
202 %token <id>     INT_IDENTIFIER
203 %token <id>     SCORE_IDENTIFIER
204 %token <id>     MIDI_IDENTIFIER
205 %token <id>     PAPER_IDENTIFIER
206 %token <real>   REAL
207 %token <string> DURATION RESTNAME
208 %token <string> STRING
209 %token <i>      UNSIGNED
210
211
212 %type <outputdef> output_def
213 %type <scope>   mudela_header mudela_header_body
214 %type <request> open_request_parens close_request_parens open_request close_request
215 %type <request> request_with_dir request_that_take_dir verbose_request
216 %type <i>       sub_quotes sup_quotes
217 %type <music>   simple_element  request_chord command_element Simple_music  Composite_music 
218 %type <music>   Alternative_music Repeated_music
219 %type <i>       abbrev_type
220 %type <i>       int unsigned
221 %type <i>       script_dir
222 %type <i>       optional_modality
223 %type <id>      identifier_init  
224 %type <duration> steno_duration optional_notemode_duration
225 %type <duration> entered_notemode_duration explicit_duration
226 %type <intvec>   int_list
227 %type <reqvec>  pre_requests post_requests
228 %type <request> gen_text_def
229 %type <pitch>   explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
230 %type <pitch>   steno_tonic_pitch
231
232 %type <pitch_arr>       pitch_list
233 %type <music>   chord
234 %type <pitch_arr>       chord_additions chord_substractions chord_notes chord_step
235 %type <pitch>   chord_note chord_inversion
236 %type <midi>    midi_block midi_body
237 %type <duration>        duration_length
238
239 %type <scalar>  scalar
240 %type <music>   Music Sequential_music Simultaneous_music Music_sequence
241 %type <music>   relative_music re_rhythmed_music
242 %type <music>   property_def translator_change
243 %type <music_list> Music_list
244 %type <paper>   paper_block paper_def_body
245 %type <real>    real real_with_dimension
246 %type <request> abbrev_command_req
247 %type <request> post_request 
248 %type <request> command_req verbose_command_req
249 %type <request> extender_req
250 %type <request> hyphen_req
251 %type <string>  string
252 %type <score>   score_block score_body
253 %type <realarr> real_array
254
255 %type <string>  script_abbreviation
256 %type <trans>   translator_spec_block translator_spec_body
257 %type <tempo>   tempo_request
258 %type <notenametab> notenames_body notenames_block chordmodifiers_block
259
260
261
262 %left '-' '+'
263 %left '*' '/'
264 %left UNARY_MINUS
265
266 %%
267
268 mudela: /* empty */
269         | mudela toplevel_expression {}
270         | mudela assignment { }
271         | mudela error
272         | mudela INVALID        {
273                 THIS->error_level_i_  =1;
274         }
275         ;
276
277 toplevel_expression:
278         notenames_block                 {
279                 THIS->lexer_p_->set_notename_table ($1);
280         }
281         | chordmodifiers_block                  {
282                 THIS->lexer_p_->set_chordmodifier_table ($1);
283         }
284         | mudela_header {
285                 delete header_global_p;
286                 header_global_p = $1;
287         }
288         | score_block {
289                 score_global_array.push ($1);
290         }
291         | paper_block {
292                 Identifier * id = new
293                         Paper_def_identifier ($1, PAPER_IDENTIFIER);
294                 THIS->lexer_p_->set_identifier ("$defaultpaper", id)
295         }
296         | midi_block {
297                 Identifier * id = new
298                         Midi_def_identifier ($1, MIDI_IDENTIFIER);
299                 THIS->lexer_p_->set_identifier ("$defaultmidi", id)
300         }
301         | embedded_scm { 
302         }
303         ;
304
305 embedded_scm:
306         SCMFILE STRING semicolon {
307                 read_lily_scm_file (*$2);
308                 delete $2;
309         }
310         | SCM_T STRING semicolon {
311                 if (THIS->lexer_p_->main_input_b_ && safe_global_b)
312                         error (_("Cannot evaluate Scheme in safe mode"));
313                 gh_eval_str ($2->ch_l ());
314                 delete $2;
315         };
316
317
318 chordmodifiers_block:
319         CHORDMODIFIERS '{' notenames_body '}'  {  $$ = $3; }
320         ;
321
322
323 notenames_block:
324         NOTENAMES '{' notenames_body '}'  {  $$ = $3; }
325         ;
326
327
328
329 notenames_body:
330         /**/    {
331                 $$ = new Notename_table;
332         }
333         | NOTENAME_TABLE_IDENTIFIER     {
334                 $$ = $1-> access_content_Notename_table(true);
335         }
336         | notenames_body STRING '=' explicit_musical_pitch {
337                 (*$$)[*$2] = *$4;
338
339                 delete $4;
340                 delete $2;
341         }
342         ;
343
344 mudela_header_body:
345         {
346                 $$ = new Scope;
347                 THIS->lexer_p_-> scope_l_arr_.push ($$);
348         }
349         | mudela_header_body assignment semicolon { 
350
351         }
352         ;
353
354 mudela_header:
355         HEADER '{' mudela_header_body '}'       {
356                 $$ = $3;
357                 THIS->lexer_p_-> scope_l_arr_.pop ();           
358         }
359         ;
360
361
362 /*
363         DECLARATIONS
364 */
365 assignment:
366         STRING {
367                 THIS->remember_spot ();
368         }
369         /* cont */ '=' identifier_init  {
370             THIS->lexer_p_->set_identifier (*$1, $4);
371             $4->init_b_ = THIS->init_parse_b_;
372             $4->set_spot (THIS->pop_spot ());
373         }
374         ;
375
376
377
378 identifier_init:
379         score_block {
380                 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
381
382         }
383         | chordmodifiers_block {
384                 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
385         }
386         | notenames_block {
387                 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
388         }
389         | paper_block {
390                 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
391         }
392         | midi_block {
393                 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
394
395         }
396         | translator_spec_block {
397                 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
398         }
399         | Music  {
400                 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
401         }
402
403         | post_request {
404                 $$ = new Request_identifier ($1, REQUEST_IDENTIFIER);
405         }
406         | explicit_duration {
407                 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
408         }
409         | real {
410                 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
411         }
412         | string {
413                 $$ = new String_identifier ($1, STRING_IDENTIFIER);
414         }
415         | int   {
416                 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
417         }
418         ;
419
420 translator_spec_block:
421         TRANSLATOR '{' translator_spec_body '}'
422                 {
423                 $$ = $3;
424         }
425         ;
426
427 translator_spec_body:
428         TRANS_IDENTIFIER        {
429                 $$ = $1->access_content_Translator (true);
430                 $$-> set_spot (THIS->here_input ());
431         }
432         | TYPE STRING semicolon {
433                 Translator* t = get_translator_l (*$2);
434                 Translator_group * tg = dynamic_cast<Translator_group*> (t);
435
436                 if (!tg)
437                         THIS->parser_error (_("Need a translator group for a context"));
438                 
439                 t = t->clone ();
440                 t->set_spot (THIS->here_input ());
441                 $$ = t;
442                 delete $2;
443         }
444         | translator_spec_body STRING '=' identifier_init semicolon     { 
445                 Identifier* id = $4;
446                 String_identifier *s = dynamic_cast<String_identifier*> (id);
447                 Real_identifier *r= dynamic_cast<Real_identifier*>(id);
448                 int_identifier *i = dynamic_cast<int_identifier*> (id);
449         
450                 String str;
451                 if (s) str = *s->access_content_String (false); 
452                 if (i) str = to_str (*i->access_content_int (false));
453                 if (r) str = to_str (*r->access_content_Real (false));
454                 if (!s && !i && !r)
455                         THIS->parser_error (_("Wrong type for property value"));
456
457                 delete $4;
458                 /* ugh*/
459                 Translator_group * tr = dynamic_cast<Translator_group*>($$);
460                 tr->set_property (*$2, str);
461         }
462         | translator_spec_body NAME STRING semicolon {
463                 $$->type_str_ = *$3;
464                 delete $3;
465         }
466         | translator_spec_body CONSISTS STRING semicolon {
467                 dynamic_cast<Translator_group*> ($$)-> set_element (*$3, true);
468                 delete $3;
469         }
470         | translator_spec_body CONSISTSEND STRING semicolon {
471                 dynamic_cast<Translator_group*> ($$)-> set_element (*$3, true);
472                 delete $3;
473         }
474         | translator_spec_body ACCEPTS STRING semicolon {
475                 dynamic_cast<Translator_group*> ($$)-> set_acceptor (*$3, true);
476                 delete $3;
477         }
478         | translator_spec_body REMOVE STRING semicolon {
479                 dynamic_cast<Translator_group*> ($$)-> set_element (*$3, false);
480                 delete $3;
481         }
482         ;
483
484 /*
485         SCORE
486 */
487 score_block:
488         SCORE { 
489         }
490         /*cont*/ '{' score_body '}'     {
491                 $$ = $4;
492                 if (!$$->def_p_arr_.size ())
493                         $$->add_output (THIS->default_paper_p ());
494         }
495         ;
496
497 score_body:             {
498                 $$ = new Score;
499                 $$->set_spot (THIS->here_input ());
500         }
501         | SCORE_IDENTIFIER {
502                 $$ = $1->access_content_Score (true);
503         }
504         | score_body mudela_header      {
505                 $$->header_p_ = $2;
506         }
507         | score_body Music      {
508                 if ($$->music_p_)
509                         $2->warning (_ ("More than one music block"));  
510                 $$->music_p_ = $2;
511         }
512         | score_body output_def {
513                 $$->add_output ($2);
514         }
515         | score_body error {
516
517         }
518         ;
519
520 output_def:
521         paper_block {
522                 $$ = $1;
523         }
524         |  midi_block           {
525                 $$= $1;
526         }
527         ;
528
529
530 /*
531         PAPER
532 */
533 paper_block:
534         PAPER '{' paper_def_body '}'    { 
535                 $$ = $3;
536                 THIS-> lexer_p_->scope_l_arr_.pop ();
537         }
538         ;
539
540
541 paper_def_body:
542         /* empty */                     {
543                 Paper_def *p = THIS->default_paper_p ();
544                 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
545                 $$ = p;
546         }
547         | PAPER_IDENTIFIER      {
548                 Paper_def *p = $1->access_content_Paper_def (true);
549                 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
550                 $$ = p;
551         }
552         | paper_def_body int '=' FONT STRING            { // ugh, what a syntax
553                 Lookup * l = new Lookup;
554                 l->font_name_ = *$5;
555                 delete $5;
556                 $$->set_lookup ($2, l);
557         }
558         | paper_def_body assignment semicolon {
559
560         }
561         | paper_def_body translator_spec_block {
562                 $$->assign_translator ($2);
563         }
564         | paper_def_body SHAPE real_array  semicolon {
565                 /*
566                         URG URG.
567                 */
568                 if ($3->size () % 2)
569                         warning ("Need even number of args for shape array");
570
571                 for (int i=0; i < $3->size ();  i+=2)
572                 {
573                         Real l = $3->elem (i);
574                         $$->shape_int_a_.push (Interval (l,
575                                                          l + $3->elem (i+1)));
576                 }
577                 delete $3;
578         }
579         | paper_def_body error {
580
581         }
582         ;
583
584
585
586 real_with_dimension:
587         REAL CM_T       {
588                 $$ = $1 CM;
589         }
590         | REAL PT_T     {
591                 $$ = $1 PT;
592         }
593         | REAL IN_T     {
594                 $$ = $1 INCH;
595         }
596         | REAL MM_T     {
597                 $$ = $1 MM;
598         }
599         ;
600
601 real:
602         REAL            {
603                 $$ = $1;
604         }
605         | real_with_dimension
606         | REAL_IDENTIFIER               {
607                 $$= *$1->access_content_Real (false);
608         }
609         | '-'  real %prec UNARY_MINUS {
610                 $$ = -$2;
611         }
612         | real '*' real {
613                 $$ = $1 * $3;
614         }
615         | real '/' real {
616                 $$ = $1 / $3;
617         }
618         | real '+' real {
619                 $$ = $1  + $3;
620         }
621         | real '-' real {
622                 $$ = $1 - $3;
623         }
624         | '(' real ')'  {
625                 $$ = $2;
626         }
627         ;
628                 
629
630 real_array:
631         real {
632                 $$ = new Array<Real>;
633                 $$->push ($1);
634         }
635         |  /* empty */ {
636                 $$ = new Array<Real>;
637         }
638         | real_array ',' real {
639                 $$->push($3);
640         }
641         ;
642
643 /*
644         MIDI
645 */
646 midi_block:
647         MIDI
648
649         '{' midi_body '}'       { $$ = $3; }
650         ;
651
652 midi_body: /* empty */          {
653                 $$ = THIS->default_midi_p ();
654         }
655         | MIDI_IDENTIFIER       {
656                 $$ = $1-> access_content_Midi_def (true);
657         }
658         | midi_body assignment semicolon {
659
660         }
661         | midi_body translator_spec_block       {
662                 $$-> assign_translator ($2);
663         }
664         | midi_body tempo_request semicolon {
665                 $$->set_tempo ($2->dur_.length_mom (), $2->metronome_i_);
666                 delete $2;
667         }
668         | midi_body error {
669
670         }
671         ;
672
673 tempo_request:
674         TEMPO steno_duration '=' unsigned       {
675                 $$ = new Tempo_req;
676                 $$->dur_ = *$2;
677                 delete $2;
678                 $$-> metronome_i_ = $4;
679         }
680         ;
681
682 Music_list: /* empty */ {
683                 $$ = new Music_list;
684                 $$->set_spot (THIS->here_input ());
685         }
686         | Music_list Music {
687                 $$->add_music ($2);
688         }
689         | Music_list error {
690         }
691         ;
692
693
694 Music:
695         Simple_music
696         | Composite_music
697         ;
698
699 Alternative_music:
700         /* empty */ {
701                 $$ = 0;
702         }
703         | ALTERNATIVE Music_sequence {
704                 $$ = $2;
705                 $2->set_spot (THIS->here_input ());
706         }
707         ;
708
709
710
711
712 Repeated_music:
713         REPEAT STRING unsigned Music Alternative_music
714         {
715                 Music_sequence* m = dynamic_cast <Music_sequence*> ($5);
716                 if (m && $3 < m->length_i ())
717                         $5->warning ("More alternatives than repeats. Junking excess alternatives.");
718
719                 Repeated_music * r = new Repeated_music ($4, $3 >? 1, m);
720                 $$ = r;
721                 r->fold_b_ = (*$2 == "fold");
722                 r->volta_fold_b_ =  (*$2 == "volta");
723                 delete $2;
724                 r->set_spot ($4->spot  ());
725         }
726         ;
727
728 Music_sequence: '{' Music_list '}'      {
729                 $$ = new Music_sequence ($2);
730                 $$->set_spot ($2->spot ());
731         }
732         ;
733
734 Sequential_music:
735         SEQUENTIAL '{' Music_list '}'           {
736                 $$ = new Sequential_music ($3);
737                 $$->set_spot ($3->spot ());
738         }
739         | '{' Music_list '}'            {
740                 $$ = new Sequential_music ($2);
741                 $$->set_spot ($2->spot ());
742         }
743         ;
744
745 Simultaneous_music:
746         SIMULTANEOUS '{' Music_list '}'{
747                 $$ = new Simultaneous_music ($3);
748                 $$->set_spot ($3->spot ());
749         }
750         | '<' Music_list '>'    {
751                 $$ = new Simultaneous_music ($2);
752                 $$->set_spot ($2->spot ());
753         }
754         ;
755
756 Simple_music:
757         request_chord           { $$ = $1; }
758         | MUSIC_IDENTIFIER { $$ = $1->access_content_Music (true); }
759         | property_def
760         | translator_change
761         | Simple_music '*' unsigned '/' unsigned        {
762                 $$ = $1;
763                 $$->compress (Moment($3, $5 ));
764         }
765         | Simple_music '*' unsigned              {
766                 $$ = $1;
767                 $$->compress (Moment ($3, 1));
768         }
769         ;
770
771
772 Composite_music:
773         CONTEXT STRING Music    {
774                 Context_specced_music *csm =  new Context_specced_music ($3);
775
776                 csm->translator_type_str_ = *$2;
777                 csm->translator_id_str_ = "";
778                 delete $2;
779
780                 $$ = csm;
781         }
782         | GRACE Music {
783                 $$ = new Grace_music ($2);
784         }
785         | CONTEXT STRING '=' STRING Music {
786                 Context_specced_music *csm =  new Context_specced_music ($5);
787
788                 csm->translator_type_str_ = *$2;
789                 csm->translator_id_str_ = *$4;
790                 delete $2;
791                 delete $4;
792
793                 $$ = csm;
794         }
795         | TIMES {
796                 THIS->remember_spot ();
797         }
798         /* CONTINUED */ 
799                 unsigned '/' unsigned Music     
800
801         {
802                 $$ = new Time_scaled_music ($3, $5, $6);
803                 $$->set_spot (THIS->pop_spot ());
804         }
805         | Repeated_music                { $$ = $1; }
806         | Simultaneous_music            { $$ = $1; }
807         | Sequential_music              { $$ = $1; }
808         | TRANSPOSE musical_pitch Music {
809                 $$ = new Transposed_music ($3, *$2);
810                 delete $2;
811         }
812         | TRANSPOSE steno_tonic_pitch Music {
813                 $$ = new Transposed_music ($3, *$2);
814                 delete $2;
815         }
816         | NOTES
817                 { THIS->lexer_p_->push_note_state (); }
818         Music
819                 { $$ = $3;
820                   THIS->lexer_p_->pop_state ();
821                 }
822         | CHORDS
823                 { THIS->lexer_p_->push_chord_state (); }
824         Music
825                 {
826                   $$ = $3;
827                   THIS->lexer_p_->pop_state ();
828         }
829         | LYRICS
830                 { THIS->lexer_p_->push_lyric_state (); }
831         Music
832                 {
833                   $$ = $3;
834                   THIS->lexer_p_->pop_state ();
835         }
836         | relative_music        { $$ = $1; }
837         | re_rhythmed_music     { $$ = $1; } 
838         ;
839
840 relative_music:
841         RELATIVE absolute_musical_pitch Music {
842                 $$ = new Relative_octave_music ($3, *$2);
843                 delete $2;
844         }
845         ;
846
847 re_rhythmed_music:
848         ADDLYRICS Music Music {
849                 Lyric_combine_music * l = new Lyric_combine_music ($2, $3);
850                 $$ = l;
851         }
852         ;
853
854 translator_change:
855         TRANSLATOR STRING '=' STRING  {
856                 Change_translator * t = new Change_translator;
857                 t-> change_to_type_str_ = *$2;
858                 t-> change_to_id_str_ = *$4;
859
860                 $$ = t;
861                 $$->set_spot (THIS->here_input ());
862                 delete $2;
863                 delete $4;
864         }
865         ;
866
867 property_def:
868         PROPERTY STRING '.' STRING '=' scalar {
869                 Translation_property *t = new Translation_property;
870
871                 t-> var_str_ = *$4;
872                 t-> value_ = *$6;
873
874                 Context_specced_music *csm = new Context_specced_music (t);
875                 $$ = csm;
876                 $$->set_spot (THIS->here_input ());
877
878                 csm-> translator_type_str_ = *$2;
879
880                 delete $2;
881                 delete $4;
882                 delete $6;
883         }
884         ;
885
886 scalar:
887         string          { $$ = new Scalar (*$1); delete $1; }
888         | int           { $$ = new Scalar ($1); }
889         ;
890
891
892 request_chord:
893         pre_requests simple_element post_requests       {
894                 Music_sequence *l = dynamic_cast<Music_sequence*>($2);
895                 for (int i=0; i < $1->size(); i++)
896                         l->add_music ($1->elem(i));
897                 for (int i=0; i < $3->size(); i++)
898                         l->add_music ($3->elem(i));
899                 $$ = $2;
900                 
901         }
902         | command_element
903         ;
904
905 command_element:
906         command_req {
907                 $$ = new Request_chord;
908                 $$-> set_spot (THIS->here_input ());
909                 $1-> set_spot (THIS->here_input ());
910                 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
911         }
912         ;
913
914 command_req:
915         abbrev_command_req
916         | verbose_command_req semicolon { $$ = $1; }
917         ;
918
919 abbrev_command_req:
920         extender_req {
921                 $$ = $1;
922         }
923         | hyphen_req {
924                 $$ = $1;
925         }
926         | '|'                           {
927                 $$ = new Barcheck_req;
928         }
929         | '~'   {
930                 $$ = new Tie_req;
931         }
932         | '['           {
933                 Span_req*b= new Span_req;
934                 b->span_dir_ = START;
935                 b->span_type_str_ = "beam";
936                 $$ =b;
937         }
938         | '[' ':' unsigned {
939                 if (!Duration::duration_type_b ($3))
940                   THIS->parser_error (_f ("not a duration: %d", $3));
941                 else if ($3 < 8)
942                   THIS->parser_error (_ ("can't abbreviate"));
943                 else
944                   THIS->set_abbrev_beam ($3);
945
946                 Chord_tremolo_req* a = new Chord_tremolo_req;
947                 a->span_dir_ = START;
948                 a->type_i_ = THIS->abbrev_beam_type_i_;
949                 $$=a;
950         }
951         | ']'           {
952                 if (!THIS->abbrev_beam_type_i_)
953                   {
954                      Span_req*b= new Span_req;
955                      b->span_dir_ = STOP;
956                      b->span_type_str_ = "beam";
957                      $$ = b;
958                    }
959                 else
960                   {
961                     Chord_tremolo_req* a = new Chord_tremolo_req;
962                     a->span_dir_ = STOP;
963                     a->type_i_ = THIS->abbrev_beam_type_i_;
964                     THIS->set_abbrev_beam (0);
965                     $$ = a;
966                   }
967         }
968         | BREATHE {
969                 $$ = new Breathing_sign_req;
970         }
971         ;
972
973
974 verbose_command_req:
975         BAR STRING                      {
976                 $$ = new Bar_req (*$2);
977                 delete $2;
978         }
979         | MARK STRING {
980                 $$ = new Mark_req (*$2);
981                 delete $2;
982         }
983         | MARK unsigned {
984                 $$ = new Mark_req (to_str ($2));
985         }
986         | TIME_T unsigned '/' unsigned  {
987                 Time_signature_change_req *m = new Time_signature_change_req;
988                 m->beats_i_ = $2;
989                 m->one_beat_i_=$4;
990                 $$ = m;
991         }
992         | PENALTY int   {
993                 Break_req * b = new Break_req;
994                 b->penalty_i_ = $2;
995                 b-> set_spot (THIS->here_input ());
996                 $$ = b;
997         }
998         | SKIP duration_length {
999                 Skip_req * skip_p = new Skip_req;
1000                 skip_p->duration_ = *$2;
1001                 delete $2;
1002                 $$ = skip_p;
1003         }
1004         | tempo_request {
1005                 $$ = $1;
1006         }
1007         | CADENZA unsigned      {
1008                 $$ = new Cadenza_req ($2);
1009         }
1010         | PARTIAL duration_length       {
1011                 $$ = new Partial_measure_req ($2->length_mom ());
1012                 delete $2;
1013         }
1014         | CLEF STRING {
1015                 $$ = new Clef_change_req (*$2);
1016                 delete $2;
1017         }
1018         | KEY NOTENAME_PITCH optional_modality  {
1019                 Key_change_req *key_p= new Key_change_req;
1020                 key_p->key_.pitch_arr_.push (*$2);
1021                 key_p->key_.ordinary_key_b_ = true;
1022                 key_p->key_.modality_i_ = $3;
1023                 $$ = key_p;
1024                 delete $2;
1025         }
1026         | KEYSIGNATURE pitch_list {
1027                 Key_change_req *key_p= new Key_change_req;
1028                 key_p->key_.pitch_arr_ = *$2;
1029                 key_p->key_.ordinary_key_b_ = false;
1030                 $$ = key_p;
1031                 delete $2;
1032         }
1033
1034         ;
1035
1036 post_requests:
1037         {
1038                 $$ = new Link_array<Request>;
1039         }
1040         | post_requests post_request {
1041                 $2->set_spot (THIS->here_input ());
1042                 $$->push ($2);
1043         }
1044         ;
1045
1046 post_request:
1047         verbose_request
1048         | request_with_dir
1049         | close_request
1050         ;
1051
1052
1053 request_that_take_dir:
1054         gen_text_def
1055         | verbose_request
1056         | script_abbreviation {
1057                 Identifier*i = THIS->lexer_p_->lookup_identifier ("dash-" + *$1);
1058                 Articulation_req *a = new Articulation_req;
1059                 a->articulation_str_ = *i->access_content_String (false);
1060                 delete $1;
1061                 $$ = a;
1062         }
1063         ;
1064
1065 request_with_dir:
1066         script_dir request_that_take_dir        {
1067                 if (Script_req * gs = dynamic_cast<Script_req*> ($2))
1068                         gs->dir_ = Direction ($1);
1069                 else if ($1)
1070                         $2->warning ("Can't specify direction for this request");
1071                 $$ = $2;
1072         }
1073         ;
1074         
1075 verbose_request:
1076         REQUEST_IDENTIFIER      {
1077                 $$ = (Request*)$1->access_content_Request (true);
1078                 $$->set_spot (THIS->here_input ());
1079         }
1080         | TEXTSCRIPT STRING STRING      {
1081                 Text_script_req *ts_p = new Text_script_req;
1082                 ts_p-> text_str_ = *$2;
1083                 ts_p-> style_str_ = *$3;
1084                 ts_p->set_spot (THIS->here_input ());
1085                 delete $3;
1086                 delete $2;
1087                 $$ = ts_p;
1088         }
1089         | SPANREQUEST int STRING {
1090                 Span_req * sp_p = new Span_req;
1091                 sp_p-> span_dir_  = Direction($2);
1092                 sp_p->span_type_str_ = *$3;
1093                 sp_p->set_spot (THIS->here_input ());
1094                 $$ = sp_p;
1095         }
1096         | abbrev_type   {
1097                 Tremolo_req* a = new Tremolo_req;
1098                 a->set_spot (THIS->here_input ());
1099                 a->type_i_ = $1;
1100                 $$ = a;
1101         }
1102         | SCRIPT STRING         { 
1103                 Articulation_req * a = new Articulation_req;
1104                 a->articulation_str_ = *$2;
1105                 a->set_spot (THIS->here_input ());
1106                 $$ = a;
1107                 delete $2;
1108         }
1109         ;
1110
1111 optional_modality:
1112         /* empty */     {
1113                 $$ = 0;
1114         }
1115         | int   {
1116                 $$ = $1;
1117         }
1118         ;
1119
1120 sup_quotes:
1121         '\'' {
1122                 $$ = 1;
1123         }
1124         | sup_quotes '\'' {
1125                 $$ ++;
1126         }
1127         ;
1128
1129 sub_quotes:
1130         ',' {
1131                 $$ = 1;
1132         }
1133         | sub_quotes ',' {
1134                 $$ ++ ;
1135         }
1136         ;
1137
1138 steno_musical_pitch:
1139         NOTENAME_PITCH  {
1140                 $$ = $1;
1141         }
1142         | NOTENAME_PITCH sup_quotes     {
1143                 $$ = $1;
1144                 $$->octave_i_ +=  $2;
1145         }
1146         | NOTENAME_PITCH sub_quotes      {
1147                 $$ = $1;
1148                 $$->octave_i_ += - $2;
1149         }
1150         ;
1151
1152 steno_tonic_pitch:
1153         TONICNAME_PITCH {
1154                 $$ = $1;
1155         }
1156         | TONICNAME_PITCH sup_quotes    {
1157                 $$ = $1;
1158                 $$->octave_i_ +=  $2;
1159         }
1160         | TONICNAME_PITCH sub_quotes     {
1161                 $$ = $1;
1162                 $$->octave_i_ += - $2;
1163         }
1164         ;
1165
1166 explicit_musical_pitch:
1167         MUSICAL_PITCH '{' int_list '}'  {/* ugh */
1168                 Array<int> &a = *$3;
1169                 ARRAY_SIZE(a,3);
1170                 $$ = new Musical_pitch;
1171                 $$->octave_i_ = a[0];
1172                 $$->notename_i_ = a[1];
1173                 $$->accidental_i_ = a[2];
1174                 delete &a;
1175         }
1176         ;
1177
1178 musical_pitch:
1179         steno_musical_pitch {
1180                 $$ = $1;
1181                 THIS->set_last_pitch ($1);
1182         }
1183         | explicit_musical_pitch {
1184                 $$ = $1;
1185                 THIS->set_last_pitch ($1);
1186         }
1187         ;
1188
1189 explicit_duration:
1190         DURATION '{' int_list '}'       {
1191                 $$ = new Duration;
1192                 Array<int> &a = *$3;
1193                 ARRAY_SIZE(a,2);
1194                         
1195                 $$-> durlog_i_ = a[0];
1196                 $$-> dots_i_ = a[1];
1197
1198                 delete &a;              
1199         }
1200         ;
1201
1202 extender_req:
1203         EXTENDER {
1204                 if (!THIS->lexer_p_->lyric_state_b ())
1205                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1206                 $$ = new Extender_req;
1207         }
1208         ;
1209
1210 hyphen_req:
1211         HYPHEN {
1212                 if (!THIS->lexer_p_->lyric_state_b ())
1213                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1214                 $$ = new Hyphen_req;
1215         }
1216         ;
1217
1218 close_request:
1219         close_request_parens {
1220                 $$ = $1;
1221                 dynamic_cast<Span_req*> ($$)->span_dir_ = START;
1222         }
1223         
1224 close_request_parens:
1225         '('     {
1226                 Span_req* s= new Span_req;
1227                 $$ = s;
1228                 s->span_type_str_ = "slur";
1229         }
1230         | E_SMALLER {
1231                 Span_req*s =new Span_req;
1232                 $$ = s;
1233                 s->span_type_str_ = "crescendo";
1234         }
1235         | E_BIGGER {
1236                 Span_req*s =new Span_req;
1237                 $$ = s;
1238                 s->span_type_str_ = "decrescendo";
1239         }
1240         ;
1241
1242
1243 open_request:
1244         open_request_parens {
1245                 $$ = $1;
1246                 dynamic_cast<Span_req*> ($$)->span_dir_ = STOP;
1247         }
1248         ;
1249
1250 open_request_parens:
1251         E_EXCLAMATION   {
1252                 Span_req *s =  new Span_req;
1253                 s->span_type_str_ = "crescendo";
1254                 $$ = s;
1255         }
1256         | ')'   {
1257                 Span_req* s= new Span_req;
1258                 $$ = s;
1259                 s->span_type_str_ = "slur";
1260         }
1261         ;
1262
1263 gen_text_def:
1264         string {
1265                 Text_script_req *t  = new Text_script_req;
1266                 $$ = t;
1267                 t->text_str_ = *$1;
1268                 delete $1;
1269                 $$->set_spot (THIS->here_input ());
1270         }
1271         | DIGIT {
1272                 Text_script_req* t  = new Text_script_req;
1273                 $$ = t;
1274                 t->text_str_ = to_str ($1);
1275                 t->style_str_ = "finger";
1276                 $$->set_spot (THIS->here_input ());
1277         }
1278         ;
1279
1280 script_abbreviation:
1281         '^'             {
1282                 $$ = new String ("hat");
1283         }
1284         | '+'           {
1285                 $$ = new String ("plus");
1286         }
1287         | '-'           {
1288                 $$ = new String ("dash");
1289         }
1290         | '|'           {
1291                 $$ = new String ("bar");
1292         }
1293         | '>'           {
1294                 $$ = new String ("larger");
1295         }
1296         | '.'           {
1297                 $$ = new String ("dot");
1298         }
1299         ;
1300
1301
1302 script_dir:
1303         '_'     { $$ = DOWN; }
1304         | '^'   { $$ = UP; }
1305         | '-'   { $$ = CENTER; }
1306         ;
1307
1308 pre_requests:
1309         {
1310                 $$ = new Link_array<Request>;
1311         }
1312         | pre_requests open_request {
1313                 $$->push ($2);
1314         }
1315         ;
1316
1317 absolute_musical_pitch:
1318         steno_musical_pitch     {
1319                 $$ = $1;
1320         }
1321         ;
1322
1323 duration_length:
1324         steno_duration {
1325                 $$ = $1;
1326         }
1327         | duration_length '*' unsigned {
1328                 $$->plet_.iso_i_ *= $3;
1329         }
1330         | duration_length '/' unsigned {
1331                 $$->plet_.type_i_ *= $3;
1332         }
1333         ;
1334
1335 entered_notemode_duration:
1336         steno_duration  {
1337                 THIS->set_last_duration ($1);
1338         }
1339         ;
1340
1341 optional_notemode_duration:
1342         {
1343                 $$ = new Duration (THIS->default_duration_);
1344         }
1345         | entered_notemode_duration {
1346                 $$ = $1;
1347         }
1348         ;
1349
1350 steno_duration:
1351         unsigned                {
1352                 $$ = new Duration;
1353                 if (!Duration::duration_type_b ($1))
1354                         THIS->parser_error (_f ("not a duration: %d", $1));
1355                 else {
1356                         $$->durlog_i_ = Duration_convert::i2_type ($1);
1357                      }
1358         }
1359         | DURATION_IDENTIFIER   {
1360                 $$ = $1->access_content_Duration (true);
1361         }
1362         | steno_duration '.'    {
1363                 $$->dots_i_ ++;
1364         }
1365         ;
1366
1367
1368 abbrev_type: 
1369         ':'     {
1370                 $$ =0;
1371         }
1372         | ':' unsigned {
1373                 if (!Duration::duration_type_b ($2))
1374                         THIS->parser_error (_f ("not a duration: %d", $2));
1375                 else if ($2 < 8)
1376                         THIS->parser_error (_ ("can't abbreviate"));
1377                 $$ = $2;
1378         }
1379         ;
1380
1381
1382 simple_element:
1383         musical_pitch exclamations questions optional_notemode_duration {
1384                 if (!THIS->lexer_p_->note_state_b ())
1385                         THIS->parser_error (_ ("have to be in Note mode for notes"));
1386
1387
1388                 Note_req *n = new Note_req;
1389                 
1390                 n->pitch_ = *$1;
1391                 delete $1;
1392                 n->duration_ = *$4;
1393                 delete $4;
1394                 if (THIS->abbrev_beam_type_i_)
1395                   {
1396                     if (n->duration_.plet_b ())
1397                       THIS->parser_error (_ ("can't abbreviate tuplet"));
1398                     else
1399                       n->duration_.set_plet (1, 2);
1400                   }
1401                 n->cautionary_b_ = $3 % 2;
1402                 n->forceacc_b_ = $2 % 2 || n->cautionary_b_;
1403
1404                 Simultaneous_music*v = new Request_chord;
1405                 v->set_spot (THIS->here_input ());
1406                 n->set_spot (THIS->here_input ());
1407
1408                 v->add_music (n);
1409
1410                 $$ = v;
1411         }
1412         | RESTNAME optional_notemode_duration           {
1413                 $$ = THIS->get_rest_element (*$1, $2);
1414                 delete $1;  // delete notename
1415         }
1416         | MEASURES optional_notemode_duration   {
1417                 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1418                 m->duration_ = *$2;
1419                 delete $2;
1420
1421                 Simultaneous_music*velt_p = new Request_chord;
1422                 velt_p->set_spot (THIS->here_input ());
1423                 velt_p->add_music (m);
1424                 $$ = velt_p;
1425         }
1426         | REPETITIONS optional_notemode_duration        {
1427                 Repetitions_req* r = new Repetitions_req;
1428                 r->duration_ = *$2;
1429                 delete $2;
1430
1431                 Simultaneous_music*velt_p = new Request_chord;
1432                 velt_p->set_spot (THIS->here_input ());
1433                 velt_p->add_music (r);
1434                 $$ = velt_p;
1435         }
1436         | STRING optional_notemode_duration     {
1437                 if (!THIS->lexer_p_->lyric_state_b ())
1438                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1439                 $$ = THIS->get_word_element (*$1, $2);
1440                 delete $1;
1441         }
1442         | chord {
1443                 if (!THIS->lexer_p_->chord_state_b ())
1444                         THIS->parser_error (_ ("have to be in Chord mode for chords"));
1445                 $$ = $1;
1446         }
1447         ;
1448
1449 chord:
1450         steno_tonic_pitch optional_notemode_duration chord_additions chord_substractions chord_inversion {
1451                 $$ = THIS->get_chord (*$1, $3, $4, $5, *$2);
1452         };
1453
1454 chord_additions: 
1455         {
1456                 $$ = new Array<Musical_pitch>;
1457         } 
1458         | CHORD_MINUS chord_notes {
1459                 $$ = $2;
1460         }
1461         ;
1462
1463 chord_notes:
1464         chord_step {
1465                 $$ = $1
1466         }
1467         | chord_notes '.' chord_step {
1468                 $$ = $1;
1469                 $$->concat (*$3);
1470         }
1471         ;
1472
1473 chord_substractions: 
1474         {
1475                 $$ = new Array<Musical_pitch>;
1476         } 
1477         | CHORD_CARET chord_notes {
1478                 $$ = $2;
1479         }
1480         ;
1481
1482
1483 chord_inversion:
1484         {
1485                 $$ = 0;
1486         }
1487         | '/' steno_tonic_pitch {
1488                 $$ = $2
1489         }
1490         ;
1491
1492 chord_step:
1493         chord_note {
1494                 $$ = new Array<Musical_pitch>;
1495                 $$->push (*$1);
1496         }
1497         | CHORDMODIFIER_PITCH {
1498                 $$ = new Array<Musical_pitch>;
1499                 $$->push (*$1);
1500         }
1501         | CHORDMODIFIER_PITCH chord_note {
1502                 $$ = new Array<Musical_pitch>;
1503                 $$->push (*$1);
1504                 $$->push (*$2);
1505         }
1506         ;
1507
1508 chord_note:
1509         unsigned {
1510                 $$ = new Musical_pitch;
1511                 $$->notename_i_ = ($1 - 1) % 7;
1512                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1513                 $$->accidental_i_ = 0;
1514         } 
1515         | unsigned '+' {
1516                 $$ = new Musical_pitch;
1517                 $$->notename_i_ = ($1 - 1) % 7;
1518                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1519                 $$->accidental_i_ = 1;
1520         }
1521         | unsigned CHORD_MINUS {
1522                 $$ = new Musical_pitch;
1523                 $$->notename_i_ = ($1 - 1) % 7;
1524                 $$->octave_i_ = $1 > 7 ? 1 : 0;
1525                 $$->accidental_i_ = -1;
1526         }
1527         ;
1528
1529 /*
1530         UTILITIES
1531  */
1532 pitch_list:                     {
1533                 $$ = new Array<Musical_pitch>;
1534         }
1535         | pitch_list musical_pitch      {
1536                 $$->push (*$2);
1537                 delete $2;
1538         }
1539         ;
1540
1541
1542 int_list:
1543         /**/                    {
1544                 $$ = new Array<int>
1545         }
1546         | int_list int          {
1547                 $$->push ($2);          
1548         }
1549         ;
1550
1551 unsigned:
1552         UNSIGNED        {
1553                 $$ = $1;
1554         }
1555         | DIGIT         {
1556                 $$ = $1;
1557         }
1558         ;
1559
1560 int:
1561         unsigned {
1562                 $$ = $1;
1563         }
1564         | '-' unsigned {
1565                 $$ = -$2;
1566         }
1567         | INT_IDENTIFIER        {
1568                 $$ = *$1->access_content_int (false);
1569         }
1570         ;
1571
1572
1573 string:
1574         STRING          {
1575                 $$ = $1;
1576         }
1577         | STRING_IDENTIFIER     {
1578                 $$ = $1->access_content_String (true);
1579         }
1580         | string '+' string {
1581                 *$$ += *$3;
1582                 delete $3;
1583         }
1584         ;
1585
1586
1587 exclamations:
1588                 { $$ = 0; }
1589         | exclamations '!'      { $$ ++; }
1590         ;
1591
1592 questions:
1593                 { $$ = 0; }
1594         | questions '?' { $$ ++; }
1595         ;
1596
1597
1598 semicolon:
1599         ';'
1600         ;
1601 %%
1602
1603 void
1604 My_lily_parser::set_yydebug (bool b)
1605 {
1606 #ifdef YYDEBUG
1607         yydebug = b;
1608 #endif
1609 }
1610 void
1611 My_lily_parser::do_yyparse ()
1612 {
1613         yyparse ((void*)this);
1614 }
1615
1616