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