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