1 %{ // -*-Fundamental-*-
4 parser.yy -- YACC -> C++ parser for mudela
6 source file of the GNU LilyPond music typesetter
8 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9 Jan Nieuwenhuizen <jan@digicash.com>
15 #define MUDELA_VERSION "0.1.10"
18 #include "translation-property.hh"
19 #include "script-def.hh"
20 #include "symtable.hh"
23 #include "my-lily-lexer.hh"
24 #include "paper-def.hh"
25 #include "midi-def.hh"
29 #include "parseconstruct.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"
38 #include "music-list.hh"
40 #include "duration-convert.hh"
41 #include "change-translator.hh"
42 #include "file-results.hh"
44 // needed for bison.simple's malloc() and free()
47 int const GUESS_PLET = 5;
48 int guess_plet_a[GUESS_PLET] =
61 #define YYERROR_VERBOSE 1
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)
67 #define yyerror THIS->parser_error
73 Array<Interval>* intarr;
74 Array<Melodic_req*> *melreqvec;/* should clean up naming */
75 Array<String> * strvec;
83 Music_list *musiclist;
90 Music_output_def * outputdef;
97 General_script_def * script;
102 Symtables * symtables;
114 yylex (YYSTYPE *s, void * v_l)
116 My_lily_parser *pars_l = (My_lily_parser*) v_l;
117 My_lily_lexer * lex_l = pars_l->lexer_p_;
119 lex_l->lexval_l = (void*) s;
120 return lex_l->yylex ();
128 /* tokens which are not keywords */
153 %token MELODIC_REQUEST
184 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
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
205 %token <string> DURATION RESTNAME
206 %token <string> STRING
208 %token <i> POST_QUOTES
209 %token <i> PRE_QUOTES
211 %type <outputdef> output_def
212 %type <header> mudela_header mudela_header_body
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
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
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
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
263 | mudela mudela_header {
264 delete THIS->default_header_p_ ;
265 THIS->default_header_p_ = $2;
267 | mudela score_block {
268 score_global_array.push ($2);
270 | mudela add_declaration { }
272 | mudela check_version { }
273 | mudela add_notenames { }
278 if (String (*$2) != MUDELA_VERSION) {
279 if (THIS->ignore_version_b_) {
280 THIS->here_input ().error ("Incorrect mudela version");
282 THIS->fatal_error_i_ = 1;
283 THIS->parser_error ("Incorrect mudela version");
290 NOTENAMES '{' notenames_body '}'
295 | notenames_body CLEAR {
296 THIS->clear_notenames ();
298 | notenames_body STRING '=' melodic_request {
299 THIS->add_notename (*$2, $4);
308 | mudela_header_body STRING '=' concat_strings ';' {
316 HEADER '{' mudela_header_body '}' {
326 | concat_strings STRING {
337 THIS->remember_spot ();
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 ());
348 $$ = new Score_id ($1, SCORE_IDENTIFIER);
352 $$ = new Paper_def_id ($1, PAPER_IDENTIFIER);
356 $$ = new Midi_def_id ($1, MIDI_IDENTIFIER);
359 | script_definition {
360 $$ = new Script_id ($1, SCRIPT_IDENTIFIER);
364 $$ = new Music_id ($1, MUSIC_IDENTIFIER);
368 $$ = new Lookup_id ($1, IDENTIFIER);
372 $$ = new Real_id (new Real ($1), REAL_IDENTIFIER);
376 $$ = new Int_id (new int ($1), INT_IDENTIFIER);
379 $$ = new Request_id ($1, POST_REQUEST_IDENTIFIER);
382 $$ = new Request_id ($1, MELODIC_REQUEST_IDENTIFIER);
386 $$ = new Translator_id ($1, TRANS_IDENTIFIER);
388 | explicit_duration {
389 $$ = new Duration_id ($1, DURATION_IDENTIFIER);
396 TRANSLATOR '{' translator_spec_body '}'
400 translator_spec_body:
402 $$ = $1->translator ();
403 $$-> set_spot (THIS->here_input ());
406 $$ = get_translator_l (*$2)->clone ();
407 $$->set_spot (THIS->here_input ());
410 | translator_spec_body STRING '=' scalar ';' {
411 $$-> set_property (*$2, *$4);
415 | translator_spec_body CONSISTS STRING ';' {
416 $$->group_l ()->consists_str_arr_.push (*$3);
419 | translator_spec_body ACCEPTS STRING ';' {
420 $$->group_l ()->accepts_str_arr_.push (*$3);
429 SCORE { THIS->remember_spot ();
430 THIS->error_level_i_ =0;
432 /*cont*/ '{' score_body '}' {
434 $$->set_spot (THIS->pop_spot ());
435 if (!$$->def_p_arr_.size ())
436 $$->add (THIS->default_paper ());
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_);
452 | score_body mudela_header {
457 $2->warning ("More than one music block");
460 | score_body output_def {
478 /* */ { $$ =new Array<int>; }
479 | intastint_list int '*' int {
480 $$->push ($2); $$->push ($4);
490 '{' paper_body '}' { $$ = $3; }
495 $$ = THIS->default_paper ();
498 $$ = $1->paperdef ();
500 | paper_body OUTPUT STRING ';' {
501 $$->outfile_str_ = *$3;
504 | paper_body symtables { $$->set ($2); }
505 | paper_body STRING '=' dim ';' {
506 $$->set_var (*$2, $4);
508 | paper_body STRING '=' real ';' {
509 $$->set_var (*$2, $4);
511 | paper_body STRING '=' translator_spec {
512 $$-> assign_translator (*$2, $4);
515 | paper_body SHAPE '=' shape_array ';' {
516 $$->shape_int_a_ = *$4;
526 $$ = new Array<Interval>;
528 | shape_array dim dim {
529 $$->push(Interval($2, $2 + $3));
538 '{' midi_body '}' { $$ = $3; }
541 midi_body: /* empty */ {
542 $$ = THIS->default_midi ();
544 | midi_body STRING '=' translator_spec {
545 $$-> assign_translator (*$2, $4);
548 | midi_body OUTPUT STRING ';' {
549 $$->outfile_str_ = *$3;
552 | midi_body tempo_request ';' {
553 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
562 TEMPO entered_notemode_duration '=' unsigned {
566 $$-> metronome_i_ = $4;
583 $$->set_spot (THIS->here_input ());
591 full_element { $$ = $1; }
592 | TYPE STRING Music {
594 $$->translator_type_str_ = *$2;
597 | TYPE STRING '=' STRING Music {
599 $$->translator_type_str_ = *$2;
600 $$->translator_id_str_ = *$4;
606 | transposed_music { $$ = $1; }
607 | MUSIC_IDENTIFIER { $$ = $1->music (); }
608 | MUSIC_IDENTIFIER ';' { $$ = $1->music (); }
610 { THIS->lexer_p_->push_note_state (); }
612 { $$=$3; THIS->lexer_p_->pop_state (); }
615 { THIS->lexer_p_->push_lyric_state (); }
617 { $$ = $3; THIS->lexer_p_->pop_state (); }
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;
629 $$->set_spot (THIS->here_input ());
636 PROPERTY STRING '.' STRING '=' scalar {
637 Translation_property *t = new Translation_property;
638 t-> translator_type_str_ = *$2;
642 $$->set_spot (THIS->here_input ());
650 STRING { $$ = new Scalar (*$1); delete $1; }
651 | int { $$ = new Scalar ($1); }
656 '<' Chord_body '>' { $$ = $2; }
657 | MULTI unsigned Chord {
659 $$->multi_level_i_=$2;
666 $$-> multi_level_i_ = 1;
667 $$->set_spot (THIS->here_input ());
675 TRANSPOSE steno_melodic_req Music {
677 $$ -> transpose ($2);
688 pre_requests simple_element post_requests {
689 THIS->add_requests ((Chord*)$2);//ugh
693 | voice_command ';' { $$ = 0; }
703 $$ = new Request_chord;
704 $$-> set_spot (THIS->here_input ());
705 $1-> set_spot (THIS->here_input ());
706 ((Chord*)$$) ->add ($1);//ugh
713 | verbose_command_req ';' { $$ = $1; }
718 $$ = new Barcheck_req;
720 | COMMAND_IDENTIFIER {
727 $$ = new Bar_req (*$2);
731 Break_force_req * f = new Break_force_req;
732 f-> set_spot (THIS->here_input ());
735 | METER unsigned '/' unsigned {
736 Meter_change_req *m = new Meter_change_req;
740 | SKIP duration_length {
741 Skip_req * skip_p = new Skip_req;
742 skip_p->duration_.set_plet ($2->num (),
752 $$ = new Cadenza_req ($2);
754 | PARTIAL duration_length {
755 $$ = new Partial_measure_req (*$2);
759 $$ = new Clef_change_req (*$2);
763 Key_change_req *key_p= new Key_change_req;
764 key_p->melodic_p_arr_ = *$2;
768 | GROUPING intastint_list {
769 $$ = get_grouping_req (*$2); delete $2;
775 /* something silly happened. Junk this stuff*/
776 if (!THIS->post_reqs.empty ())
778 warning ("Junking post-requests");
779 THIS->post_reqs.clear ();
782 | post_requests structured_post_request {
783 $2->set_spot (THIS->here_input ());
784 THIS->post_reqs.push ($2);
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);
795 structured_post_request:
801 POST_REQUEST_IDENTIFIER {
802 $$ = (Request*)$1->request ();
808 Abbreviation_req* a = new Abbreviation_req;
821 $$ = $1->clone ()->musical ()->melodic ();
822 $$->octave_i_ += THIS->default_octave_i_;
824 | steno_melodic_req POST_QUOTES {
825 $$-> octave_i_ += $2;
827 | PRE_QUOTES steno_melodic_req {
829 $2-> octave_i_ -= $1;
836 * (Melodic_req *) $$ = *$1;
839 | steno_note_req '!' {
840 $$->forceacc_b_ = ! $$->forceacc_b_;
842 /* have to duration here. */
846 MELODIC_REQUEST '{' int int int '}' {/* ugh */
847 $$ = new Melodic_req;
849 $$->notename_i_ = $4;
850 $$->accidental_i_ = $5;
855 DURATION '{' int unsigned '}' {
863 ABSDYNAMIC '{' unsigned '}' {
864 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
865 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
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;
877 unsigned '/' unsigned {
884 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
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];
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;
904 | TELP plet_fraction {
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];
913 close_request_parens:
929 | close_plet_parens {
936 if (!Duration::duration_type_b ($3))
937 THIS->parser_error (String ("Not a duration: ") + $3);
939 THIS->parser_error ("Can't abbreviate");
941 THIS->set_abbrev_beam ($3);
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];
953 | PLET plet_fraction {
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];
972 | open_abbrev_parens {
981 SCRIPT '{' script_body '}' { $$ = $3; }
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);
994 script_dir gen_script_def {
995 Musical_script_req *m = new Musical_script_req;
997 m->scriptdef_p_ = $2;
998 m->set_spot (THIS->here_input ());
1000 m->dir_ = (Direction)$1;
1007 ((Text_def*) $$)->align_i_ = CENTER; /* UGH */
1011 $$-> set_spot (THIS->here_input ());
1015 ((Text_def*)$$)->align_i_ = CENTER;
1021 Text_def *t = new Text_def;
1025 t->style_str_ = THIS->textstyle_str_;
1026 $$->set_spot (THIS->here_input ());
1032 Text_def* t = new Text_def;
1034 t->text_str_ = String ($1);
1035 t->style_str_ = "finger";
1036 $$->set_spot (THIS->here_input ());
1040 script_abbreviation:
1041 '^' { $$ = get_scriptdef ('^'); }
1042 | '+' { $$ = get_scriptdef ('+'); }
1043 | '-' { $$ = get_scriptdef ('-'); }
1044 | '|' { $$ = get_scriptdef ('|'); }
1045 | 'o' { $$ = get_scriptdef ('o'); }
1046 | '>' { $$ = get_scriptdef ('>'); }
1048 $$ = get_scriptdef ('.');
1053 SCRIPT_IDENTIFIER { $$ = $1->script (); }
1054 | script_definition { $$ = $1; }
1055 | script_abbreviation {
1056 $$ = THIS->lexer_p_->lookup_identifier (*$1)->script ();
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);
1081 THIS->set_duration_mode (*$2);
1084 | DURATION entered_notemode_duration {
1085 THIS->set_default_duration ($2);
1090 This is weird, but default_octave_i_
1091 is used in steno_note_req too
1093 c' -> default_octave_i_ == 1
1095 /* why can't we have \oct 0 iso \oct{c'}*/
1096 THIS->default_octave_i_ = 1; }
1099 THIS->default_octave_i_ = $3->octave_i_;
1102 | TEXTSTYLE STRING {
1103 THIS->textstyle_str_ = *$2;
1110 $$ = new Moment (0,1);
1112 | duration_length explicit_steno_duration {
1113 *$$ += $2->length ();
1119 | dots '.' { $$ ++; }
1122 entered_notemode_duration:
1124 $$ = new Duration (THIS->default_duration_);
1125 // get sticky plet part too
1126 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1129 $$ = new Duration (THIS->default_duration_);
1130 // get sticky plet part too
1131 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1134 | explicit_steno_duration {
1135 THIS->set_last_duration ($1);
1141 entered_notemode_duration {
1146 explicit_steno_duration:
1149 if (!Duration::duration_type_b ($1))
1150 THIS->parser_error ("Not a duration:" + String ($1));
1152 $$->durlog_i_ = Duration_convert::i2_type ($1);
1153 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1156 | DURATION_IDENTIFIER {
1157 $$ = $1->duration ();
1159 | explicit_steno_duration '.' {
1162 | explicit_steno_duration '*' unsigned {
1163 $$->plet_.iso_i_ = $3; /* ugh. should do *= */
1165 | explicit_steno_duration '/' unsigned {
1166 $$->plet_.type_i_ = $3; /* ugh. should do *= iso = */
1176 if (!Duration::duration_type_b ($2))
1177 THIS->parser_error ("Not a duration:" + String ($2));
1179 THIS->parser_error ("Can't abbreviate");
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);
1193 | RESTNAME notemode_duration {
1194 $$ = THIS->get_rest_element (*$1, $2);
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);
1212 $$ = new Array<Melodic_req*>;
1214 | pitch_list NOTENAME_ID {
1215 $$->push ($2->clone ()->musical ()->melodic ());
1235 int *i_p = $1->intid ();
1246 Real *r_p = $1->real ();
1255 real unit { $$ = $1*$2; }
1259 unit: CM_T { $$ = 1 CM; }
1260 |IN_T { $$ = 1 INCH; }
1261 |MM_T { $$ = 1 MM; }
1262 |PT_T { $$ = 1 PT; }
1269 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1279 | symtables_body TEXID STRING {
1280 $$->texsetting = *$3;
1283 | symtables_body STRING '=' symtable {
1290 TABLE '{' symtable_body '}' { $$ = $3; }
1294 { $$ = new Symtable; }
1295 | symtable_body STRING symboldef {
1304 $$ = new Atom (*$1, *$2);
1309 Box b (Interval (0,0), Interval (0,0));
1310 $$ = new Atom (*$1, b);
1316 dinterval dinterval {
1317 $$ = new Box (*$1, *$2);
1323 dinterval: dim dim {
1324 $$ = new Interval ($1, $2);
1331 My_lily_parser::set_yydebug (bool b)
1338 My_lily_parser::do_yyparse ()
1340 yyparse ((void*)this);
1344 My_lily_parser::default_paper ()
1346 Identifier *id = lexer_p_->lookup_identifier ("default_paper");
1347 return id ? id->paperdef () : new Paper_def ;
1351 My_lily_parser::default_midi ()
1353 Identifier *id = lexer_p_->lookup_identifier ("default_midi");
1354 return id ? id->mididef () : new Midi_def ;