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