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