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