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