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