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