]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
release: 1.0.10
[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
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                 $$ = $3;
704                 $$ -> transpose (*$2);
705                 delete $2;
706         }
707         | MUSIC_IDENTIFIER { $$ = $1->access_Music (); }
708         | NOTES
709                 { THIS->lexer_p_->push_note_state (); }
710         Music
711                 { $$ = $3;
712                   THIS->lexer_p_->pop_state ();
713                 }
714
715         | LYRIC
716                 { THIS->lexer_p_->push_lyric_state (); }
717         Music
718                 {
719                   $$ = $3;
720                   THIS->lexer_p_->pop_state ();
721                 }
722         | property_def
723         | translator_change
724         | relative_music        { $$ = $1; }
725         ;
726
727 relative_music:
728         RELATIVE absolute_musical_pitch Music {
729                 $$ = new Relative_octave_music ($3, *$2);
730                 delete $2;
731         }
732         ;
733
734 translator_change:
735         TRANSLATOR STRING '=' STRING  {
736                 Change_translator * t = new Change_translator;
737                 t-> change_to_type_str_ = *$2;
738                 t-> change_to_id_str_ = *$4;
739
740                 $$ = t;
741                 $$->set_spot (THIS->here_input ());
742                 delete $2;
743                 delete $4;
744         }
745         ;
746
747 property_def:
748         PROPERTY STRING '.' STRING '=' scalar   {
749                 Translation_property *t = new Translation_property;
750                 t-> translator_type_str_ = *$2;
751                 t-> var_str_ = *$4;
752                 t-> value_ = *$6;
753                 $$ = t;
754                 $$->set_spot (THIS->here_input ());
755                 delete $2;
756                 delete $4;
757                 delete $6;
758         }
759         ;
760
761 scalar:
762         STRING          { $$ = new Scalar (*$1); delete $1; }
763         | int           { $$ = new Scalar ($1); }
764         ;
765
766
767
768 request_chord:
769         pre_requests simple_element post_requests       {
770                 THIS->add_requests ((Simultaneous_music*)$2);//ugh
771                 $$ = $2;
772         }
773         | command_element
774         ;
775
776
777
778
779 command_element:
780         command_req {
781                 $$ = new Request_chord;
782                 $$-> set_spot (THIS->here_input ());
783                 $1-> set_spot (THIS->here_input ());
784                 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
785
786         }
787         ;
788
789 command_req:
790         abbrev_command_req
791         | verbose_command_req ';'       { $$ = $1; }
792         ;
793
794 abbrev_command_req:
795         '|'                             {
796                 $$ = new Barcheck_req;
797         }
798         | COMMAND_IDENTIFIER    {
799                 $$ = $1->access_Request ();
800         }
801         ;
802
803 verbose_command_req:
804         BAR STRING                      {
805                 $$ = new Bar_req (*$2);
806                 delete $2;
807         }
808         | MARK STRING {
809                 $$ = new Mark_req (*$2);
810                 delete $2;
811         }
812         | MARK unsigned {
813                 $$ = new Mark_req (to_str ($2));
814         }
815         | TIME_T unsigned '/' unsigned  {
816                 Time_signature_change_req *m = new Time_signature_change_req;
817                 m->beats_i_ = $2;
818                 m->one_beat_i_=$4;
819                 $$ = m;
820         }
821         | PENALTY '=' int       {
822                 Break_req * b = new Break_req;
823                 b->penalty_i_ = $3;
824                 b-> set_spot (THIS->here_input ());
825                 $$ = b;
826         }
827         | SKIP duration_length {
828                 Skip_req * skip_p = new Skip_req;
829                 skip_p->duration_ = *$2;
830                 delete $2;
831                 $$ = skip_p;
832         }
833         | tempo_request {
834                 $$ = $1;
835         }
836         | CADENZA unsigned      {
837                 $$ = new Cadenza_req ($2);
838         }
839         | PARTIAL duration_length       {
840                 $$ = new Partial_measure_req ($2->length ());
841                 delete $2;
842         }
843         | CLEF STRING {
844                 $$ = new Clef_change_req (*$2);
845                 delete $2;
846         }
847         | KEY NOTENAME_PITCH    {
848                 Key_change_req *key_p= new Key_change_req;
849                 key_p->pitch_arr_.push(*$2);
850                 key_p->ordinary_key_b_ = true;
851                 $$ = key_p;
852                 delete $2;
853         }
854         | KEYSIGNATURE pitch_list       {
855                 Key_change_req *key_p= new Key_change_req;
856                 key_p->pitch_arr_ = *$2;
857                 key_p->ordinary_key_b_ = false;
858                 $$ = key_p;
859                 delete $2;
860         }
861         | GROUPING intastint_list {
862                 $$ = get_grouping_req (*$2); delete $2;
863         }
864         ;
865
866 post_requests:
867         {
868                 /* something silly happened.  Junk this stuff*/
869                 if (!THIS->post_reqs.empty ())
870                 {
871                         warning ("Junking post-requests");
872                         THIS->post_reqs.clear ();
873                 }
874         }
875         | post_requests structured_post_request {
876                 $2->set_spot (THIS->here_input ());
877                 THIS->post_reqs.push ($2);
878         }
879         | post_requests close_request_parens    {
880                 Array<Request*>& r = *THIS->get_parens_request ($2);
881                 for (int i = 0; i < r.size (); i++ )
882                         r[i]->set_spot (THIS->here_input ());
883                 THIS->post_reqs.concat (r);
884                 delete &r;
885         }
886         ;
887
888 structured_post_request:
889         script_req
890         | post_request
891         ;
892
893 post_request:
894         POST_REQUEST_IDENTIFIER {
895                 $$ = (Request*)$1->access_Request ();
896         }
897         | dynamic_req {
898                 $$ = $1;
899         }
900         | abbrev_type   {
901                 Abbreviation_req* a = new Abbreviation_req;
902                 a->type_i_ = $1;
903                 $$ = a;
904         }
905         ;
906
907 sup_quotes:
908         '\'' {
909                 $$ = 1;
910         }
911         | sup_quotes '\'' {
912                 $$ ++;
913         }
914         ;
915 sub_quotes:
916         ',' {
917                 $$ = 1;
918         }
919         | sub_quotes ',' {
920                 $$ ++ ;
921         }
922         ;
923
924 steno_musical_pitch:
925         NOTENAME_PITCH  {
926                 $$ = $1;
927         }
928         | NOTENAME_PITCH sup_quotes     {
929                 $$ = $1;
930                 $$->octave_i_ +=  $2;
931         }
932         | NOTENAME_PITCH sub_quotes      {
933                 $$ = $1;
934                 $$->octave_i_ += - $2;
935         }
936         ;
937
938 explicit_musical_pitch:
939         MUSICAL_PITCH '{' int_list '}'  {/* ugh */
940                 Array<int> &a = *$3;
941                 ARRAY_SIZE(a,3);
942                 $$ = new Musical_pitch;
943                 $$->octave_i_ = a[0];
944                 $$->notename_i_ = a[1];
945                 $$->accidental_i_ = a[2];
946                 delete &a;
947         }
948         ;
949
950 musical_pitch:
951         steno_musical_pitch
952         | explicit_musical_pitch
953         ;
954
955 steno_notepitch:
956         musical_pitch   {
957                 $$ = new Note_req;
958                 
959                 $$->pitch_ = *$1;
960                 delete $1;
961         }
962         | steno_notepitch  '!'          {
963                 $$->forceacc_b_ = ! $$->forceacc_b_;
964         }
965         ;
966
967
968 explicit_duration:
969         DURATION '{' int_list '}'       {
970                 $$ = new Duration;
971                 Array<int> &a = *$3;
972                 ARRAY_SIZE(a,2);
973                         
974                 $$-> durlog_i_ = a[0];
975                 $$-> dots_i_ = a[1];
976
977                 delete &a;              
978         }
979         ;
980
981 dynamic_req:
982         ABSDYNAMIC '{' unsigned '}'     {
983                 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
984                 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
985                 $$ =ad_p;
986         }
987         | SPANDYNAMIC '{' int int '}' {
988                 Span_dynamic_req * sp_p = new Span_dynamic_req;
989                 sp_p->spantype = (Span_req::Spantype)$4;
990                 sp_p-> dynamic_dir_  = (Direction)$3;
991                 $$ = sp_p;
992         }
993         ;
994
995 plet_fraction:
996         unsigned '/' unsigned {
997                 $$[0] = $1;
998                 $$[1] = $3;
999         }
1000         |
1001         '/' unsigned {
1002                 int num = $2 >? 1;
1003                 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1004                 $$[1] = num;
1005         }
1006         ;
1007
1008 close_plet_parens:
1009         ']' plet_fraction {
1010                 $$ = MAEBTELP;
1011                 THIS->plet_.type_i_ = $2[1];
1012                 THIS->plet_.iso_i_ = $2[0];
1013                 THIS->default_duration_.plet_ = THIS->plet_;
1014         }
1015         | TELP {
1016                 $$ = TELP;
1017                 THIS->plet_.type_i_ = 1;
1018                 THIS->plet_.iso_i_ = 1;
1019                 THIS->default_duration_.plet_ = THIS->plet_;
1020         }
1021         | TELP plet_fraction {
1022                 $$ = TELP;
1023                 THIS->plet_.type_i_ = $2[1];
1024                 THIS->plet_.iso_i_ = $2[0];
1025                 THIS->default_duration_.plet_ = THIS->plet_;
1026         }
1027         ;
1028
1029 close_request_parens:
1030         '~'     {
1031                 $$ = '~';
1032         }
1033         | '('   {
1034                 $$='(';
1035         }
1036         | ']'   {
1037                 $$ = ']';
1038         }
1039         | E_SMALLER {
1040                 $$ = '<';
1041         }
1042         | E_BIGGER {
1043                 $$ = '>';
1044         }
1045         | close_plet_parens
1046         ;
1047
1048 open_abbrev_parens:
1049         '[' ':' unsigned {
1050                 $$ = '[';
1051                 if (!Duration::duration_type_b ($3))
1052                         THIS->parser_error (_f ("not a duration: %d", $3));
1053                 else if ($3 < 8)
1054                         THIS->parser_error (_ ("can't abbreviate"));
1055                 else
1056                         THIS->set_abbrev_beam ($3);
1057         }
1058         ;
1059
1060 open_plet_parens:
1061         '[' plet_fraction {
1062                 $$ = BEAMPLET;
1063                 THIS->plet_.type_i_ = $2[1];
1064                 THIS->plet_.iso_i_ = $2[0];
1065                 THIS->default_duration_.plet_ = THIS->plet_;
1066         }
1067         | PLET plet_fraction {
1068                 $$ = PLET;
1069                 THIS->plet_.type_i_ = $2[1];
1070                 THIS->plet_.iso_i_ = $2[0];
1071                 THIS->default_duration_.plet_ = THIS->plet_;
1072         }
1073         ;
1074
1075 open_request_parens:
1076         E_EXCLAMATION   {
1077                 $$ = '!';
1078         }
1079         | ')'   {
1080                 $$=')';
1081         }
1082         | '['   {
1083                 $$='[';
1084         }
1085         | open_abbrev_parens
1086         | open_plet_parens
1087         ;
1088
1089
1090
1091 script_definition:
1092         SCRIPT '{' script_body '}'      { $$ = $3; }
1093         ;
1094
1095 script_body:
1096         STRING int int int int int              {
1097                 Script_def *s = new Script_def;
1098                 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1099                 $$  = s;
1100                 delete $1;
1101         }
1102         ;
1103
1104 script_req:
1105         script_dir gen_script_def       {
1106                 Musical_script_req *m = new Musical_script_req;
1107                 $$ = m;
1108                 m->scriptdef_p_ = $2;
1109                 m->set_spot (THIS->here_input ());
1110                 if (!m->dir_)
1111                   m->dir_  = (Direction)$1;
1112         }
1113         ;
1114
1115 gen_script_def:
1116         text_def        { 
1117                 $$ = $1;
1118                 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1119         }
1120         | mudela_script { 
1121                 $$ = $1;
1122                 $$-> set_spot (THIS->here_input ());
1123         }
1124         | finger {
1125                 $$ = $1;
1126                 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1127         }
1128         ;
1129
1130 text_def:
1131         string {
1132                 Text_def *t  = new Text_def;
1133                 $$ = t;
1134                 t->text_str_ = *$1;
1135                 delete $1;
1136                 $$->set_spot (THIS->here_input ());
1137         }
1138         ;
1139
1140 finger:
1141          DIGIT {
1142                 Text_def* t  = new Text_def;
1143                 $$ = t;
1144                 t->text_str_ = to_str ($1);
1145                 t->style_str_ = "finger";
1146                 $$->set_spot (THIS->here_input ());
1147         }
1148         ;
1149
1150 script_abbreviation:
1151         '^'             { $$ = get_scriptdef ('^'); }
1152         | '+'           { $$ = get_scriptdef ('+'); }
1153         | '-'           { $$ = get_scriptdef ('-'); }
1154         | '|'           { $$ = get_scriptdef ('|'); }
1155         | 'o'           { $$ = get_scriptdef ('o'); }
1156         | '>'           { $$ = get_scriptdef ('>'); }
1157         | '.'           {
1158                 $$ = get_scriptdef ('.');
1159         }
1160         ;
1161
1162 mudela_script:
1163         SCRIPT_IDENTIFIER               { $$ = $1->access_General_script_def (true); }
1164         | script_definition             { $$ = $1; }
1165         | script_abbreviation           {
1166                 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1167                 delete $1;
1168         }
1169         ;
1170
1171 script_dir:
1172         '_'     { $$ = -1; }
1173         | '^'   { $$ = 1; }
1174         | '-'   { $$ = 0; }
1175         ;
1176
1177 pre_requests:
1178         {
1179         }
1180         | pre_requests open_request_parens {
1181                 Array<Request*>& r = *THIS->get_parens_request ($2);
1182                 for (int i = 0; i < r.size (); i++ )
1183                         r[i]->set_spot (THIS->here_input ());
1184                 THIS->pre_reqs.concat (r);
1185                 delete &r;
1186         }
1187         ;
1188
1189 absolute_musical_pitch:
1190         steno_musical_pitch     {
1191                 $$ = $1;
1192         }
1193         ;
1194
1195 duration_length:
1196         steno_duration {
1197                 $$ = $1;
1198         }
1199         ;
1200
1201 dots:
1202         '.'             { $$ = 1; }
1203         | dots '.'      { $$ ++; }
1204         ;
1205
1206 entered_notemode_duration:
1207         /* */           {
1208                 $$ = new Duration (THIS->default_duration_);
1209         }
1210         | dots          {
1211                 $$ = new Duration (THIS->default_duration_);
1212                 $$->dots_i_  = $1;
1213         }
1214         | steno_duration        {
1215                 THIS->set_last_duration ($1);
1216         }
1217         ;
1218
1219 notemode_duration:
1220         entered_notemode_duration {
1221                 $$ = $1;
1222         }
1223         ;
1224
1225 steno_duration:
1226         unsigned                {
1227                 $$ = new Duration;
1228                 if (!Duration::duration_type_b ($1))
1229                         THIS->parser_error (_f ("not a duration: %d", $1));
1230                 else {
1231                         $$->durlog_i_ = Duration_convert::i2_type ($1);
1232                         $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1233                      }
1234         }
1235         | DURATION_IDENTIFIER   {
1236                 $$ = $1->access_Duration ();
1237         }
1238         | steno_duration '.'    {
1239                 $$->dots_i_ ++;
1240         }
1241         | steno_duration '*' unsigned  {
1242                 $$->plet_.iso_i_ *= $3;
1243         }
1244         | steno_duration '/' unsigned {
1245                 $$->plet_.type_i_ *= $3;
1246         }
1247         ;
1248
1249
1250 abbrev_type: 
1251         ':'     {
1252                 $$ =0;
1253         }
1254         | ':' unsigned {
1255                 if (!Duration::duration_type_b ($2))
1256                         THIS->parser_error (_f ("not a duration: %d", $2));
1257                 else if ($2 < 8)
1258                         THIS->parser_error (_ ("can't abbreviate"));
1259                 $$ = $2;
1260         }
1261         ;
1262
1263
1264
1265 simple_element:
1266         steno_notepitch notemode_duration  {
1267                 if (!THIS->lexer_p_->note_state_b ())
1268                         THIS->parser_error (_ ("have to be in Note mode for notes"));
1269                 $1->set_duration (*$2);
1270                 int durlog_i = $2->durlog_i_;
1271                 $$ = THIS->get_note_element ($1, $2);
1272         }
1273         | RESTNAME notemode_duration            {
1274                 $$ = THIS->get_rest_element (*$1, $2);
1275                 delete $1;  // delete notename
1276         }
1277         | MEASURES notemode_duration    {
1278                 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1279                 m->duration_ = *$2;
1280                 delete $2;
1281
1282                 Simultaneous_music*velt_p = new Request_chord;
1283                 velt_p->set_spot (THIS->here_input ());
1284                 velt_p->add_music (m);
1285                 $$ = velt_p;
1286
1287         }
1288         | STRING notemode_duration                      {
1289                 if (!THIS->lexer_p_->lyric_state_b ())
1290                         THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1291                 $$ = THIS->get_word_element (*$1, $2);
1292                 delete $1;
1293         }
1294         ;
1295
1296
1297 /*
1298         UTILITIES
1299  */
1300 pitch_list:                     {
1301                 $$ = new Array<Musical_pitch>;
1302         }
1303         | pitch_list musical_pitch      {
1304                 $$->push (*$2);
1305                 delete $2;
1306         }
1307         ;
1308
1309
1310 int_list:
1311         /**/                    {
1312                 $$ = new Array<int>
1313         }
1314         | int_list int          {
1315                 $$->push ($2);          
1316         }
1317         ;
1318
1319 unsigned:
1320         UNSIGNED        {
1321                 $$ = $1;
1322         }
1323         | DIGIT {
1324                 $$ = $1;
1325         };
1326
1327 int:
1328         unsigned {
1329                 $$ = $1;
1330         }
1331         | '-' unsigned {
1332                 $$ = -$2;
1333         }
1334         | INT_IDENTIFIER        {
1335                 int *i_p = $1->access_int ();
1336                 $$ = *i_p;
1337                 delete i_p;
1338         }
1339         ;
1340
1341 string_primary:
1342         STRING          {
1343                 $$ = $1;
1344         }
1345         | STRING_IDENTIFIER     {
1346                 String *s_p = $1->access_String ();
1347
1348                 $$ = s_p;
1349                 DOUT << "Lookup string: " << *s_p << "\n";
1350         }
1351         ;
1352
1353 string:
1354         string_primary {
1355                 $$ = $1;
1356         }
1357         | string '+' string_primary {
1358                 *$$ += *$3;
1359                 delete $3;
1360         }
1361         ;
1362
1363 dim:
1364         real_primary unit       { $$ = $1*$2; }
1365         ;
1366
1367
1368 unit:   CM_T            { $$ = 1 CM; }
1369         |IN_T           { $$ = 1 INCH; }
1370         |MM_T           { $$ = 1 MM; }
1371         |PT_T           { $$ = 1 PT; }
1372         ;
1373
1374 /*
1375         symbol tables
1376 */
1377 symtables:
1378         SYMBOLTABLES '{' symtables_body '}'     { $$ = $3; }
1379         ;
1380
1381 symtables_body:
1382                         {
1383                 $$ = new Lookup;
1384         }
1385         | IDENTIFIER            {
1386                 $$ = $1->access_Lookup ();
1387         }
1388         | symtables_body FONT STRING            {
1389                 $$->font_ = *$3;
1390                 $$->font_path_ = global_path.find (*$3);
1391                 if  (!$$->font_path_.length_i ())
1392                         THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1393
1394                 delete $3;
1395         }
1396         | symtables_body STRING '=' symtable            {
1397                 $$->add (*$2, $4);
1398                 delete $2;
1399         }
1400         ;
1401
1402 symtable:
1403         TABLE '{' symtable_body '}' { $$ = $3; }
1404         ;
1405
1406 symtable_body:
1407                                 { $$ = new Symtable; }
1408         | symtable_body STRING  symboldef {
1409                 $$->add (*$2, *$3);
1410                 delete $2;
1411                 delete $3;
1412         }
1413         ;
1414
1415 symboldef:
1416         STRING  box             {
1417                 $$ = new Atom (*$1, *$2);
1418                 delete $1;
1419                 delete $2;
1420         }
1421         | STRING {
1422                 Box b (Interval (0,0), Interval (0,0));
1423                 $$ = new Atom (*$1, b);
1424                 delete $1;
1425         }
1426         ;
1427
1428 box:
1429         dinterval dinterval     {
1430                 $$ = new Box (*$1, *$2);
1431                 delete $1;
1432                 delete $2;
1433         }
1434         ;
1435
1436 dinterval: dim  dim             {
1437                 $$ = new Interval ($1, $2);
1438         }
1439         ;
1440
1441 %%
1442
1443 void
1444 My_lily_parser::set_yydebug (bool b)
1445 {
1446 #ifdef YYDEBUG
1447         yydebug = b;
1448 #endif
1449 }
1450 void
1451 My_lily_parser::do_yyparse ()
1452 {
1453         yyparse ((void*)this);
1454 }
1455