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