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