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