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