1 %{ // -*-Fundamental-*-
4 parser.yy -- Bison/C++ parser for mudela
6 source file of the GNU LilyPond music typesetter
8 (c) 1997 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 Jan Nieuwenhuizen <janneke@gnu.org>
15 #include "translation-property.hh"
16 #include "script-def.hh"
17 #include "symtable.hh"
20 #include "my-lily-lexer.hh"
21 #include "paper-def.hh"
22 #include "midi-def.hh"
24 #include "file-path.hh"
27 #include "parseconstruct.hh"
28 #include "dimension.hh"
29 #include "identifier.hh"
30 #include "command-request.hh"
31 #include "musical-request.hh"
32 #include "my-lily-parser.hh"
33 #include "text-def.hh"
34 #include "translator-group.hh"
36 #include "music-list.hh"
38 #include "duration-convert.hh"
39 #include "change-translator.hh"
40 #include "file-results.hh"
41 #include "mudela-version.hh"
43 #include "relative-music.hh"
47 Mudela_version oldest_version ("1.0.3");
48 Mudela_version version ("1.0.3");
51 // needed for bison.simple's malloc() and free()
54 int const GUESS_PLET = 5;
55 int guess_plet_a[GUESS_PLET] =
64 Paper_def* current_paper = 0;
70 #define YYERROR_VERBOSE 1
72 #define YYPARSE_PARAM my_lily_parser_l
73 #define YYLEX_PARAM my_lily_parser_l
74 #define THIS ((My_lily_parser *) my_lily_parser_l)
76 #define yyerror THIS->parser_error
77 #define ARRAY_SIZE(a,s) if (a.size () != s) THIS->parser_error (_f("expecting %d arguments", s))
84 Array<Interval>* intarr;
85 Array<Musical_pitch> *pitch_arr;
86 Array<String> * strvec;
89 Simultaneous_music *chord;
94 Music_list *music_list;
101 Music_output_def * outputdef;
102 Musical_pitch * pitch;
109 General_script_def * script;
114 Symtables * symtables;
126 yylex (YYSTYPE *s, void * v_l)
128 My_lily_parser *pars_l = (My_lily_parser*) v_l;
129 My_lily_lexer * lex_l = pars_l->lexer_p_;
131 lex_l->lexval_l = (void*) s;
132 return lex_l->yylex ();
140 /* tokens which are not keywords */
200 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
204 %token <pitch> NOTENAME_PITCH
205 %token <id> DURATION_IDENTIFIER
206 %token <id> IDENTIFIER
208 %token <id> MUSIC_IDENTIFIER
209 %token <id> POST_REQUEST_IDENTIFIER
210 %token <id> SCRIPT_IDENTIFIER
211 %token <id> COMMAND_IDENTIFIER
212 %token <id> REAL_IDENTIFIER
213 %token <id> STRING_IDENTIFIER
214 %token <id> TRANS_IDENTIFIER
215 %token <id> INT_IDENTIFIER
216 %token <id> SCORE_IDENTIFIER
217 %token <id> MIDI_IDENTIFIER
218 %token <id> PAPER_IDENTIFIER
219 %token <id> REQUEST_IDENTIFIER
221 %token <string> DURATION RESTNAME
222 %token <string> STRING
226 %type <outputdef> output_def
227 %type <header> mudela_header mudela_header_body
229 %type <i> open_request_parens close_request_parens
230 %type <i> open_abbrev_parens
231 %type <i> open_plet_parens close_plet_parens
232 %type <i> sub_quotes sup_quotes
233 %type <music> simple_element request_chord command_element
234 %type <i> abbrev_type
235 %type <i> int unsigned
237 %type <id> identifier_init simple_identifier_init
238 %type <duration> steno_duration notemode_duration
239 %type <duration> entered_notemode_duration explicit_duration
240 %type <interval> dinterval
241 %type <intvec> intastint_list int_list
242 %type <lookup> symtables symtables_body
244 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
245 %type <notereq> steno_notepitch
246 %type <pitch_arr> pitch_list
247 %type <midi> midi_block midi_body
248 %type <duration> duration_length
250 %type <scalar> scalar
251 %type <music> Music transposed_music relative_music Sequential_music Simultaneous_music
252 %type <music> property_def translator_change
253 %type <music_list> Music_list
254 %type <paper> paper_block paper_body
255 %type <real> dim real
256 %type <real> real_mult_expression real_primary
258 %type <request> abbrev_command_req
259 %type <request> post_request structured_post_request
260 %type <pair> plet_fraction
261 %type <request> command_req verbose_command_req
262 %type <request> script_req dynamic_req
263 %type <string> string
264 %type <string> string_primary
265 %type <score> score_block score_body
266 %type <intarr> shape_array
267 %type <script> script_definition script_body mudela_script gen_script_def
268 %type <textdef> text_def finger
269 %type <string> script_abbreviation
270 %type <symbol> symboldef
271 %type <symtable> symtable symtable_body
272 %type <trans> translator_spec translator_spec_body
273 %type <tempo> tempo_request
281 | mudela mudela_header {
282 delete header_global_p;
283 header_global_p = $2;
285 | mudela score_block {
286 score_global_array.push ($2);
288 | mudela add_declaration { }
290 | mudela check_version { }
291 | mudela add_notenames { }
296 Mudela_version ver (*$2);
297 if (!((ver >= oldest_version) && (ver <= version))) {
298 if (THIS->ignore_version_b_) {
299 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
301 THIS->fatal_error_i_ = 1;
302 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
309 NOTENAMES '{' notenames_body '}'
314 | notenames_body CLEAR {
315 THIS->clear_notenames ();
317 | notenames_body STRING '=' explicit_musical_pitch {
318 THIS->add_notename (*$2, *$4);
328 | mudela_header_body STRING '=' string ';' {
336 HEADER '{' mudela_header_body '}' {
348 THIS->remember_spot ();
350 /* cont */ '=' identifier_init optional_semicolon {
351 THIS->lexer_p_->set_identifier (*$1, $4);
352 $4->init_b_ = THIS->init_parse_b_;
353 $4->set_spot (THIS->pop_spot ());
359 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
363 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
366 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
369 | script_definition {
370 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
374 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
378 $$ = new Lookup_identifier ($1, IDENTIFIER);
382 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
384 | explicit_duration {
385 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
387 | simple_identifier_init
390 simple_identifier_init:
392 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
395 $$ = new String_identifier ($1, STRING_IDENTIFIER);
398 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
401 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
406 TRANSLATOR '{' translator_spec_body '}'
410 translator_spec_body:
412 $$ = $1->access_Translator ();
413 $$-> set_spot (THIS->here_input ());
416 Translator* t = get_translator_l (*$2);
418 t->set_spot (THIS->here_input ());
422 | translator_spec_body STRING '=' simple_identifier_init ';' {
424 String_identifier *s = id->access_String_identifier ();
425 Real_identifier *r= id->access_Real_identifier ();
426 int_identifier *i = id->access_int_identifier ();
429 if (s) str = *s->access_String (false);
430 if (i) str = to_str (*i->access_int (false));
431 if (r) str = to_str (*r->access_Real (false));
433 THIS->parser_error (_("Wrong type for property value"));
436 $$->set_property (*$2, str);
438 | translator_spec_body CONSISTS STRING ';' {
439 $$->group_l ()->consists_str_arr_.push (*$3);
442 | translator_spec_body ACCEPTS STRING ';' {
443 $$->group_l ()->accepts_str_arr_.push (*$3);
452 SCORE { THIS->remember_spot ();
453 THIS->error_level_i_ =0;
455 /*cont*/ '{' score_body '}' {
457 $$->set_spot (THIS->pop_spot ());
458 if (!$$->def_p_arr_.size ())
459 $$->add_output (THIS->default_paper_p ());
461 /* handle error levels. */
462 $$->errorlevel_i_ = THIS->error_level_i_;
463 THIS->error_level_i_ = 0;
471 $$ = $1->access_Score ();
473 | score_body mudela_header {
478 $2->warning (_ ("More than one music block"));
481 | score_body output_def {
499 /* */ { $$ =new Array<int>; }
500 | intastint_list int '*' int {
501 $$->push ($2); $$->push ($4);
503 | intastint_list int {
504 $$->push ($2); $$->push (1);
513 PAPER '{' paper_body '}' {
515 THIS-> lexer_p_->scope_l_arr_.pop ();
526 Paper_def *p = THIS->default_paper_p ();
527 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
530 | PAPER_IDENTIFIER optional_semicolon {
531 Paper_def *p = $1->access_Paper_def ();
532 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
535 | paper_body OUTPUT STRING ';' {
536 $$->outfile_str_ = *$3;
539 | paper_body int '=' symtables { // ugh, what a syntax
540 $$->set_lookup ($2, $4);
542 | paper_body STRING '=' simple_identifier_init optional_semicolon {
544 id->init_b_ = THIS->init_parse_b_;
545 if (id->is_type_b (Translator_identifier::static_name ()))
547 $$->assign_translator (*$2, id->access_Translator ());
551 THIS->lexer_p_->set_identifier (*$2, id);
554 | paper_body SHAPE '=' shape_array ';' {
555 $$->shape_int_a_ = *$4;
568 Real *r_p = $1->access_Real ();
570 DOUT << "Lookup real: " << *r_p << "\n";
581 | real '+' real_mult_expression {
584 | real '-' real_mult_expression {
589 real_mult_expression:
591 | real_mult_expression '*' real_primary {
594 | real_mult_expression '/' real_primary {
601 $$ = new Array<Interval>;
603 | shape_array dim dim {
604 $$->push(Interval($2, $2 + $3));
613 '{' midi_body '}' { $$ = $3; }
616 midi_body: /* empty */ {
617 $$ = THIS->default_midi_p ();
620 $$ = $1-> access_Midi_def ();
622 | midi_body STRING '=' translator_spec {
623 $$-> assign_translator (*$2, $4);
626 | midi_body OUTPUT STRING ';' {
627 $$->outfile_str_ = *$3;
630 | midi_body tempo_request ';' {
631 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
640 TEMPO entered_notemode_duration '=' unsigned {
644 $$-> metronome_i_ = $4;
648 Music_list: /* empty */ {
659 Sequential_music: '{' Music_list '}' {
660 $$ = new Sequential_music ($2);
664 Simultaneous_music: '<' Music_list '>' {
665 $$ = new Simultaneous_music ($2);
669 request_chord { $$ = $1; }
670 | TYPE STRING Music {
672 $$->translator_type_str_ = *$2;
675 | TYPE STRING '=' STRING Music {
677 $$->translator_type_str_ = *$2;
678 $$->translator_id_str_ = *$4;
682 | Simultaneous_music { $$ = $1; }
683 | Sequential_music { $$ = $1; }
684 | transposed_music { $$ = $1; }
685 | MUSIC_IDENTIFIER { $$ = $1->access_Music (); }
687 { THIS->lexer_p_->push_note_state (); }
690 THIS->lexer_p_->pop_state ();
694 { THIS->lexer_p_->push_lyric_state (); }
698 THIS->lexer_p_->pop_state ();
702 | relative_music { $$ = $1; }
706 RELATIVE absolute_musical_pitch Music {
707 $$ = new Relative_octave_music ($3, *$2);
713 TRANSLATOR STRING '=' STRING {
714 Change_translator * t = new Change_translator;
715 t-> change_to_type_str_ = *$2;
716 t-> change_to_id_str_ = *$4;
719 $$->set_spot (THIS->here_input ());
726 PROPERTY STRING '.' STRING '=' scalar {
727 Translation_property *t = new Translation_property;
728 t-> translator_type_str_ = *$2;
732 $$->set_spot (THIS->here_input ());
740 STRING { $$ = new Scalar (*$1); delete $1; }
741 | int { $$ = new Scalar ($1); }
746 TRANSPOSE musical_pitch Music {
748 $$ -> transpose (*$2);
755 pre_requests simple_element post_requests {
756 THIS->add_requests ((Simultaneous_music*)$2);//ugh
767 $$ = new Request_chord;
768 $$-> set_spot (THIS->here_input ());
769 $1-> set_spot (THIS->here_input ());
770 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
777 | verbose_command_req ';' { $$ = $1; }
782 $$ = new Barcheck_req;
784 | COMMAND_IDENTIFIER {
785 $$ = $1->access_Request ();
791 $$ = new Bar_req (*$2);
795 $$ = new Mark_req (*$2);
799 $$ = new Mark_req (to_str ($2));
801 | TIME_T unsigned '/' unsigned {
802 Time_signature_change_req *m = new Time_signature_change_req;
808 Break_req * b = new Break_req;
810 b-> set_spot (THIS->here_input ());
813 | SKIP duration_length {
814 Skip_req * skip_p = new Skip_req;
815 skip_p->duration_ = *$2;
823 $$ = new Cadenza_req ($2);
825 | PARTIAL duration_length {
826 $$ = new Partial_measure_req ($2->length ());
830 $$ = new Clef_change_req (*$2);
833 | KEY NOTENAME_PITCH {
834 Key_change_req *key_p= new Key_change_req;
835 key_p->pitch_arr_.push(*$2);
836 key_p->ordinary_key_b_ = true;
840 | KEYSIGNATURE pitch_list {
841 Key_change_req *key_p= new Key_change_req;
842 key_p->pitch_arr_ = *$2;
843 key_p->ordinary_key_b_ = false;
847 | GROUPING intastint_list {
848 $$ = get_grouping_req (*$2); delete $2;
854 /* something silly happened. Junk this stuff*/
855 if (!THIS->post_reqs.empty ())
857 warning ("Junking post-requests");
858 THIS->post_reqs.clear ();
861 | post_requests structured_post_request {
862 $2->set_spot (THIS->here_input ());
863 THIS->post_reqs.push ($2);
865 | post_requests close_request_parens {
866 Array<Request*>& r = *THIS->get_parens_request ($2);
867 for (int i = 0; i < r.size (); i++ )
868 r[i]->set_spot (THIS->here_input ());
869 THIS->post_reqs.concat (r);
874 structured_post_request:
880 POST_REQUEST_IDENTIFIER {
881 $$ = (Request*)$1->access_Request ();
887 Abbreviation_req* a = new Abbreviation_req;
914 | NOTENAME_PITCH sup_quotes {
918 | NOTENAME_PITCH sub_quotes {
920 $$->octave_i_ += - $2;
924 explicit_musical_pitch:
925 MUSICAL_PITCH '{' int_list '}' {/* ugh */
928 $$ = new Musical_pitch;
929 $$->octave_i_ = a[0];
930 $$->notename_i_ = a[1];
931 $$->accidental_i_ = a[2];
938 | explicit_musical_pitch
948 | steno_notepitch '!' {
949 $$->forceacc_b_ = ! $$->forceacc_b_;
955 DURATION '{' int_list '}' {
960 $$-> durlog_i_ = a[0];
968 ABSDYNAMIC '{' unsigned '}' {
969 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
970 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
973 | SPANDYNAMIC '{' int int '}' {
974 Span_dynamic_req * sp_p = new Span_dynamic_req;
975 sp_p->spantype = (Span_req::Spantype)$4;
976 sp_p-> dynamic_dir_ = (Direction)$3;
982 unsigned '/' unsigned {
989 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
997 THIS->plet_.type_i_ = $2[1];
998 THIS->plet_.iso_i_ = $2[0];
999 THIS->default_duration_.plet_ = THIS->plet_;
1003 THIS->plet_.type_i_ = 1;
1004 THIS->plet_.iso_i_ = 1;
1005 THIS->default_duration_.plet_ = THIS->plet_;
1007 | TELP plet_fraction {
1009 THIS->plet_.type_i_ = $2[1];
1010 THIS->plet_.iso_i_ = $2[0];
1011 THIS->default_duration_.plet_ = THIS->plet_;
1015 close_request_parens:
1037 if (!Duration::duration_type_b ($3))
1038 THIS->parser_error (_f ("not a duration: %d", $3));
1040 THIS->parser_error (_ ("can't abbreviate"));
1042 THIS->set_abbrev_beam ($3);
1049 THIS->plet_.type_i_ = $2[1];
1050 THIS->plet_.iso_i_ = $2[0];
1051 THIS->default_duration_.plet_ = THIS->plet_;
1053 | PLET plet_fraction {
1055 THIS->plet_.type_i_ = $2[1];
1056 THIS->plet_.iso_i_ = $2[0];
1057 THIS->default_duration_.plet_ = THIS->plet_;
1061 open_request_parens:
1071 | open_abbrev_parens
1078 SCRIPT '{' script_body '}' { $$ = $3; }
1082 STRING int int int int int {
1083 Script_def *s = new Script_def;
1084 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1091 script_dir gen_script_def {
1092 Musical_script_req *m = new Musical_script_req;
1094 m->scriptdef_p_ = $2;
1095 m->set_spot (THIS->here_input ());
1097 m->dir_ = (Direction)$1;
1104 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1108 $$-> set_spot (THIS->here_input ());
1112 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1118 Text_def *t = new Text_def;
1122 $$->set_spot (THIS->here_input ());
1128 Text_def* t = new Text_def;
1130 t->text_str_ = to_str ($1);
1131 t->style_str_ = "finger";
1132 $$->set_spot (THIS->here_input ());
1136 script_abbreviation:
1137 '^' { $$ = get_scriptdef ('^'); }
1138 | '+' { $$ = get_scriptdef ('+'); }
1139 | '-' { $$ = get_scriptdef ('-'); }
1140 | '|' { $$ = get_scriptdef ('|'); }
1141 | 'o' { $$ = get_scriptdef ('o'); }
1142 | '>' { $$ = get_scriptdef ('>'); }
1144 $$ = get_scriptdef ('.');
1149 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1150 | script_definition { $$ = $1; }
1151 | script_abbreviation {
1152 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1166 | pre_requests open_request_parens {
1167 Array<Request*>& r = *THIS->get_parens_request ($2);
1168 for (int i = 0; i < r.size (); i++ )
1169 r[i]->set_spot (THIS->here_input ());
1170 THIS->pre_reqs.concat (r);
1175 absolute_musical_pitch:
1176 steno_musical_pitch {
1189 | dots '.' { $$ ++; }
1192 entered_notemode_duration:
1194 $$ = new Duration (THIS->default_duration_);
1197 $$ = new Duration (THIS->default_duration_);
1201 THIS->set_last_duration ($1);
1206 entered_notemode_duration {
1214 if (!Duration::duration_type_b ($1))
1215 THIS->parser_error (_f ("not a duration: %d", $1));
1217 $$->durlog_i_ = Duration_convert::i2_type ($1);
1218 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1221 | DURATION_IDENTIFIER {
1222 $$ = $1->access_Duration ();
1224 | steno_duration '.' {
1227 | steno_duration '*' unsigned {
1228 $$->plet_.iso_i_ *= $3;
1230 | steno_duration '/' unsigned {
1231 $$->plet_.type_i_ *= $3;
1241 if (!Duration::duration_type_b ($2))
1242 THIS->parser_error (_f ("not a duration: %d", $2));
1244 THIS->parser_error (_ ("can't abbreviate"));
1252 steno_notepitch notemode_duration {
1253 if (!THIS->lexer_p_->note_state_b ())
1254 THIS->parser_error (_ ("have to be in Note mode for notes"));
1255 $1->set_duration (*$2);
1256 int durlog_i = $2->durlog_i_;
1257 $$ = THIS->get_note_element ($1, $2);
1259 | RESTNAME notemode_duration {
1260 $$ = THIS->get_rest_element (*$1, $2);
1261 delete $1; // delete notename
1263 | MEASURES notemode_duration {
1264 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1268 Simultaneous_music*velt_p = new Request_chord;
1269 velt_p->set_spot (THIS->here_input ());
1270 velt_p->add_music (m);
1274 | STRING notemode_duration {
1275 if (!THIS->lexer_p_->lyric_state_b ())
1276 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1277 $$ = THIS->get_word_element (*$1, $2);
1287 $$ = new Array<Musical_pitch>;
1289 | pitch_list musical_pitch {
1321 int *i_p = $1->access_int ();
1331 | STRING_IDENTIFIER {
1332 String *s_p = $1->access_String ();
1335 DOUT << "Lookup string: " << *s_p << "\n";
1343 | string '+' string_primary {
1350 real_primary unit { $$ = $1*$2; }
1354 unit: CM_T { $$ = 1 CM; }
1355 |IN_T { $$ = 1 INCH; }
1356 |MM_T { $$ = 1 MM; }
1357 |PT_T { $$ = 1 PT; }
1364 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1372 $$ = $1->access_Lookup ();
1374 | symtables_body FONT STRING {
1376 $$->font_path_ = global_path.find (*$3);
1377 if (!$$->font_path_.length_i ())
1378 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1382 | symtables_body STRING '=' symtable {
1389 TABLE '{' symtable_body '}' { $$ = $3; }
1393 { $$ = new Symtable; }
1394 | symtable_body STRING symboldef {
1403 $$ = new Atom (*$1, *$2);
1408 Box b (Interval (0,0), Interval (0,0));
1409 $$ = new Atom (*$1, b);
1415 dinterval dinterval {
1416 $$ = new Box (*$1, *$2);
1422 dinterval: dim dim {
1423 $$ = new Interval ($1, $2);
1430 My_lily_parser::set_yydebug (bool b)
1437 My_lily_parser::do_yyparse ()
1439 yyparse ((void*)this);