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