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.1");
48 Mudela_version version ("1.0.2");
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
82 Array<Interval>* intarr;
83 Array<Musical_pitch> *pitch_arr;
84 Array<String> * strvec;
87 Simultaneous_music *chord;
92 Music_list *music_list;
99 Music_output_def * outputdef;
100 Musical_pitch * pitch;
107 General_script_def * script;
112 Symtables * symtables;
124 yylex (YYSTYPE *s, void * v_l)
126 My_lily_parser *pars_l = (My_lily_parser*) v_l;
127 My_lily_lexer * lex_l = pars_l->lexer_p_;
129 lex_l->lexval_l = (void*) s;
130 return lex_l->yylex ();
138 /* tokens which are not keywords */
198 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
202 %token <pitch> NOTENAME_PITCH
203 %token <id> DURATION_IDENTIFIER
204 %token <id> IDENTIFIER
206 %token <id> MUSIC_IDENTIFIER
207 %token <id> POST_REQUEST_IDENTIFIER
208 %token <id> SCRIPT_IDENTIFIER
209 %token <id> COMMAND_IDENTIFIER
210 %token <id> REAL_IDENTIFIER
211 %token <id> STRING_IDENTIFIER
212 %token <id> TRANS_IDENTIFIER
213 %token <id> INT_IDENTIFIER
214 %token <id> SCORE_IDENTIFIER
215 %token <id> MIDI_IDENTIFIER
216 %token <id> PAPER_IDENTIFIER
217 %token <id> REQUEST_IDENTIFIER
219 %token <string> DURATION RESTNAME
220 %token <string> STRING
224 %type <outputdef> output_def
225 %type <header> mudela_header mudela_header_body
227 %type <i> open_request_parens close_request_parens
228 %type <i> open_abbrev_parens
229 %type <i> open_plet_parens close_plet_parens
230 %type <i> sub_quotes sup_quotes
231 %type <music> simple_element request_chord command_element
232 %type <i> abbrev_type
233 %type <i> int unsigned
235 %type <id> identifier_init simple_identifier_init
236 %type <duration> steno_duration notemode_duration
237 %type <duration> entered_notemode_duration explicit_duration
238 %type <interval> dinterval
239 %type <intvec> intastint_list
240 %type <lookup> symtables symtables_body
242 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
243 %type <notereq> steno_notepitch
244 %type <pitch_arr> pitch_list
245 %type <midi> midi_block midi_body
246 %type <duration> duration_length
248 %type <scalar> scalar
249 %type <music> Music transposed_music relative_music Sequential_music Simultaneous_music
250 %type <music> property_def translator_change
251 %type <music_list> Music_list
252 %type <paper> paper_block paper_body
253 %type <real> dim real
254 %type <real> real_mult_expression real_primary
256 %type <request> abbrev_command_req
257 %type <request> post_request structured_post_request
258 %type <pair> plet_fraction
259 %type <request> command_req verbose_command_req
260 %type <request> script_req dynamic_req
261 %type <string> string
262 %type <string> string_primary
263 %type <score> score_block score_body
264 %type <intarr> shape_array
265 %type <script> script_definition script_body mudela_script gen_script_def
266 %type <textdef> text_def finger
267 %type <string> script_abbreviation
268 %type <symbol> symboldef
269 %type <symtable> symtable symtable_body
270 %type <trans> translator_spec translator_spec_body
271 %type <tempo> tempo_request
279 | mudela mudela_header {
280 delete header_global_p;
281 header_global_p = $2;
283 | mudela score_block {
284 score_global_array.push ($2);
286 | mudela add_declaration { }
288 | mudela check_version { }
289 | mudela add_notenames { }
294 Mudela_version ver (*$2);
295 if (!((ver >= oldest_version) && (ver <= version))) {
296 if (THIS->ignore_version_b_) {
297 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
299 THIS->fatal_error_i_ = 1;
300 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
307 NOTENAMES '{' notenames_body '}'
312 | notenames_body CLEAR {
313 THIS->clear_notenames ();
315 | notenames_body STRING '=' explicit_musical_pitch {
316 THIS->add_notename (*$2, *$4);
326 | mudela_header_body STRING '=' string ';' {
334 HEADER '{' mudela_header_body '}' {
346 THIS->remember_spot ();
348 /* cont */ '=' identifier_init optional_semicolon {
349 THIS->lexer_p_->set_identifier (*$1, $4);
350 $4->init_b_ = THIS->init_parse_b_;
351 $4->set_spot (THIS->pop_spot ());
357 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
361 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
364 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
367 | script_definition {
368 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
372 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
376 $$ = new Lookup_identifier ($1, IDENTIFIER);
380 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
382 | explicit_duration {
383 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
385 | simple_identifier_init
388 simple_identifier_init:
390 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
393 $$ = new String_identifier ($1, STRING_IDENTIFIER);
396 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
399 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
404 TRANSLATOR '{' translator_spec_body '}'
408 translator_spec_body:
410 $$ = $1->access_Translator ();
411 $$-> set_spot (THIS->here_input ());
414 Translator* t = get_translator_l (*$2);
416 t->set_spot (THIS->here_input ());
420 | translator_spec_body STRING '=' simple_identifier_init ';' {
422 String_identifier *s = id->access_String_identifier ();
423 Real_identifier *r= id->access_Real_identifier ();
424 int_identifier *i = id->access_int_identifier ();
427 if (s) str = *s->access_String (false);
428 if (i) str = to_str (*i->access_int (false));
429 if (r) str = to_str (*r->access_Real (false));
431 THIS->parser_error (_("Wrong type for property value"));
434 $$->set_property (*$2, str);
436 | translator_spec_body CONSISTS STRING ';' {
437 $$->group_l ()->consists_str_arr_.push (*$3);
440 | translator_spec_body ACCEPTS STRING ';' {
441 $$->group_l ()->accepts_str_arr_.push (*$3);
450 SCORE { THIS->remember_spot ();
451 THIS->error_level_i_ =0;
453 /*cont*/ '{' score_body '}' {
455 $$->set_spot (THIS->pop_spot ());
456 if (!$$->def_p_arr_.size ())
457 $$->add_output (THIS->default_paper_p ());
459 /* handle error levels. */
460 $$->errorlevel_i_ = THIS->error_level_i_;
461 THIS->error_level_i_ = 0;
469 $$ = $1->access_Score ();
471 | score_body mudela_header {
476 $2->warning (_ ("More than one music block"));
479 | score_body output_def {
497 /* */ { $$ =new Array<int>; }
498 | intastint_list int '*' int {
499 $$->push ($2); $$->push ($4);
501 | intastint_list int {
502 $$->push ($2); $$->push (1);
511 PAPER '{' paper_body '}' {
513 THIS-> lexer_p_->scope_l_arr_.pop ();
524 Paper_def *p = THIS->default_paper_p ();
525 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
528 | PAPER_IDENTIFIER optional_semicolon {
529 Paper_def *p = $1->access_Paper_def ();
530 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
533 | paper_body OUTPUT STRING ';' {
534 $$->outfile_str_ = *$3;
537 | paper_body int '=' symtables { // ugh, what a syntax
538 $$->set_lookup ($2, $4);
540 | paper_body STRING '=' simple_identifier_init optional_semicolon {
542 id->init_b_ = THIS->init_parse_b_;
543 if (id->is_type_b (Translator_identifier::static_name ()))
545 $$->assign_translator (*$2, id->access_Translator ());
549 THIS->lexer_p_->set_identifier (*$2, id);
552 | paper_body SHAPE '=' shape_array ';' {
553 $$->shape_int_a_ = *$4;
566 Real *r_p = $1->access_Real ();
568 DOUT << "Lookup real: " << *r_p << "\n";
579 | real '+' real_mult_expression {
582 | real '-' real_mult_expression {
587 real_mult_expression:
589 | real_mult_expression '*' real_primary {
592 | real_mult_expression '/' real_primary {
599 $$ = new Array<Interval>;
601 | shape_array dim dim {
602 $$->push(Interval($2, $2 + $3));
611 '{' midi_body '}' { $$ = $3; }
614 midi_body: /* empty */ {
615 $$ = THIS->default_midi_p ();
617 | midi_body STRING '=' translator_spec {
618 $$-> assign_translator (*$2, $4);
621 | midi_body OUTPUT STRING ';' {
622 $$->outfile_str_ = *$3;
625 | midi_body tempo_request ';' {
626 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
635 TEMPO entered_notemode_duration '=' unsigned {
639 $$-> metronome_i_ = $4;
643 Music_list: /* empty */ {
654 Sequential_music: '{' Music_list '}' {
655 $$ = new Sequential_music ($2);
659 Simultaneous_music: '<' Music_list '>' {
660 $$ = new Simultaneous_music ($2);
664 request_chord { $$ = $1; }
665 | TYPE STRING Music {
667 $$->translator_type_str_ = *$2;
670 | TYPE STRING '=' STRING Music {
672 $$->translator_type_str_ = *$2;
673 $$->translator_id_str_ = *$4;
677 | Simultaneous_music { $$ = $1; }
678 | Sequential_music { $$ = $1; }
679 | transposed_music { $$ = $1; }
680 | MUSIC_IDENTIFIER { $$ = $1->access_Music (); }
682 { THIS->lexer_p_->push_note_state (); }
685 THIS->lexer_p_->pop_state ();
689 { THIS->lexer_p_->push_lyric_state (); }
693 THIS->lexer_p_->pop_state ();
697 | relative_music { $$ = $1; }
701 RELATIVE absolute_musical_pitch Music {
702 $$ = new Relative_octave_music ($3, *$2);
708 TRANSLATOR STRING '=' STRING {
709 Change_translator * t = new Change_translator;
710 t-> change_to_type_str_ = *$2;
711 t-> change_to_id_str_ = *$4;
714 $$->set_spot (THIS->here_input ());
721 PROPERTY STRING '.' STRING '=' scalar {
722 Translation_property *t = new Translation_property;
723 t-> translator_type_str_ = *$2;
727 $$->set_spot (THIS->here_input ());
735 STRING { $$ = new Scalar (*$1); delete $1; }
736 | int { $$ = new Scalar ($1); }
741 TRANSPOSE musical_pitch Music {
743 $$ -> transpose (*$2);
750 pre_requests simple_element post_requests {
751 THIS->add_requests ((Simultaneous_music*)$2);//ugh
762 $$ = new Request_chord;
763 $$-> set_spot (THIS->here_input ());
764 $1-> set_spot (THIS->here_input ());
765 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
772 | verbose_command_req ';' { $$ = $1; }
777 $$ = new Barcheck_req;
779 | COMMAND_IDENTIFIER {
780 $$ = $1->access_Request ();
786 $$ = new Bar_req (*$2);
790 $$ = new Mark_req (*$2);
794 $$ = new Mark_req (to_str ($2));
796 | TIME_T unsigned '/' unsigned {
797 Time_signature_change_req *m = new Time_signature_change_req;
803 Break_req * b = new Break_req;
805 b-> set_spot (THIS->here_input ());
808 | SKIP duration_length {
809 Skip_req * skip_p = new Skip_req;
810 skip_p->duration_ = *$2;
818 $$ = new Cadenza_req ($2);
820 | PARTIAL duration_length {
821 $$ = new Partial_measure_req ($2->length ());
825 $$ = new Clef_change_req (*$2);
828 | KEY NOTENAME_PITCH {
829 Key_change_req *key_p= new Key_change_req;
830 key_p->pitch_arr_.push(*$2);
831 key_p->ordinary_key_b_ = true;
835 | KEYSIGNATURE pitch_list {
836 Key_change_req *key_p= new Key_change_req;
837 key_p->pitch_arr_ = *$2;
838 key_p->ordinary_key_b_ = false;
842 | GROUPING intastint_list {
843 $$ = get_grouping_req (*$2); delete $2;
849 /* something silly happened. Junk this stuff*/
850 if (!THIS->post_reqs.empty ())
852 warning ("Junking post-requests");
853 THIS->post_reqs.clear ();
856 | post_requests structured_post_request {
857 $2->set_spot (THIS->here_input ());
858 THIS->post_reqs.push ($2);
860 | post_requests close_request_parens {
861 Array<Request*>& r = *THIS->get_parens_request ($2);
862 for (int i = 0; i < r.size (); i++ )
863 r[i]->set_spot (THIS->here_input ());
864 THIS->post_reqs.concat (r);
869 structured_post_request:
875 POST_REQUEST_IDENTIFIER {
876 $$ = (Request*)$1->access_Request ();
882 Abbreviation_req* a = new Abbreviation_req;
909 | NOTENAME_PITCH sup_quotes {
913 | NOTENAME_PITCH sub_quotes {
915 $$->octave_i_ += - $2;
919 explicit_musical_pitch:
920 MUSICAL_PITCH '{' int int int '}' {/* ugh */
921 $$ = new Musical_pitch;
923 $$->notename_i_ = $4;
924 $$->accidental_i_ = $5;
929 | explicit_musical_pitch
939 | steno_notepitch '!' {
940 $$->forceacc_b_ = ! $$->forceacc_b_;
946 DURATION '{' int unsigned '}' {
954 ABSDYNAMIC '{' unsigned '}' {
955 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
956 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
959 | SPANDYNAMIC '{' int int '}' {
960 Span_dynamic_req * sp_p = new Span_dynamic_req;
961 sp_p->spantype = (Span_req::Spantype)$4;
962 sp_p-> dynamic_dir_ = (Direction)$3;
968 unsigned '/' unsigned {
975 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
983 THIS->plet_.type_i_ = $2[1];
984 THIS->plet_.iso_i_ = $2[0];
985 THIS->default_duration_.plet_ = THIS->plet_;
989 THIS->plet_.type_i_ = 1;
990 THIS->plet_.iso_i_ = 1;
991 THIS->default_duration_.plet_ = THIS->plet_;
993 | TELP plet_fraction {
995 THIS->plet_.type_i_ = $2[1];
996 THIS->plet_.iso_i_ = $2[0];
997 THIS->default_duration_.plet_ = THIS->plet_;
1001 close_request_parens:
1023 if (!Duration::duration_type_b ($3))
1024 THIS->parser_error (_f ("not a duration: %d", $3));
1026 THIS->parser_error (_ ("can't abbreviate"));
1028 THIS->set_abbrev_beam ($3);
1035 THIS->plet_.type_i_ = $2[1];
1036 THIS->plet_.iso_i_ = $2[0];
1037 THIS->default_duration_.plet_ = THIS->plet_;
1039 | PLET plet_fraction {
1041 THIS->plet_.type_i_ = $2[1];
1042 THIS->plet_.iso_i_ = $2[0];
1043 THIS->default_duration_.plet_ = THIS->plet_;
1047 open_request_parens:
1057 | open_abbrev_parens
1064 SCRIPT '{' script_body '}' { $$ = $3; }
1068 STRING int int int int int {
1069 Script_def *s = new Script_def;
1070 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1077 script_dir gen_script_def {
1078 Musical_script_req *m = new Musical_script_req;
1080 m->scriptdef_p_ = $2;
1081 m->set_spot (THIS->here_input ());
1083 m->dir_ = (Direction)$1;
1090 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1094 $$-> set_spot (THIS->here_input ());
1098 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1104 Text_def *t = new Text_def;
1108 $$->set_spot (THIS->here_input ());
1114 Text_def* t = new Text_def;
1116 t->text_str_ = to_str ($1);
1117 t->style_str_ = "finger";
1118 $$->set_spot (THIS->here_input ());
1122 script_abbreviation:
1123 '^' { $$ = get_scriptdef ('^'); }
1124 | '+' { $$ = get_scriptdef ('+'); }
1125 | '-' { $$ = get_scriptdef ('-'); }
1126 | '|' { $$ = get_scriptdef ('|'); }
1127 | 'o' { $$ = get_scriptdef ('o'); }
1128 | '>' { $$ = get_scriptdef ('>'); }
1130 $$ = get_scriptdef ('.');
1135 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1136 | script_definition { $$ = $1; }
1137 | script_abbreviation {
1138 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1152 | pre_requests open_request_parens {
1153 Array<Request*>& r = *THIS->get_parens_request ($2);
1154 for (int i = 0; i < r.size (); i++ )
1155 r[i]->set_spot (THIS->here_input ());
1156 THIS->pre_reqs.concat (r);
1161 absolute_musical_pitch:
1162 steno_musical_pitch {
1175 | dots '.' { $$ ++; }
1178 entered_notemode_duration:
1180 $$ = new Duration (THIS->default_duration_);
1183 $$ = new Duration (THIS->default_duration_);
1187 THIS->set_last_duration ($1);
1192 entered_notemode_duration {
1200 if (!Duration::duration_type_b ($1))
1201 THIS->parser_error (_f ("not a duration: %d", $1));
1203 $$->durlog_i_ = Duration_convert::i2_type ($1);
1204 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1207 | DURATION_IDENTIFIER {
1208 $$ = $1->access_Duration ();
1210 | steno_duration '.' {
1213 | steno_duration '*' unsigned {
1214 $$->plet_.iso_i_ *= $3;
1216 | steno_duration '/' unsigned {
1217 $$->plet_.type_i_ *= $3;
1227 if (!Duration::duration_type_b ($2))
1228 THIS->parser_error (_f ("not a duration: %d", $2));
1230 THIS->parser_error (_ ("can't abbreviate"));
1238 steno_notepitch notemode_duration {
1239 if (!THIS->lexer_p_->note_state_b ())
1240 THIS->parser_error (_ ("have to be in Note mode for notes"));
1241 $1->set_duration (*$2);
1242 int durlog_i = $2->durlog_i_;
1243 $$ = THIS->get_note_element ($1, $2);
1245 | RESTNAME notemode_duration {
1246 $$ = THIS->get_rest_element (*$1, $2);
1247 delete $1; // delete notename
1249 | MEASURES notemode_duration {
1250 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1254 Simultaneous_music*velt_p = new Request_chord;
1255 velt_p->set_spot (THIS->here_input ());
1256 velt_p->add_music (m);
1260 | STRING notemode_duration {
1261 if (!THIS->lexer_p_->lyric_state_b ())
1262 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1263 $$ = THIS->get_word_element (*$1, $2);
1273 $$ = new Array<Musical_pitch>;
1275 | pitch_list musical_pitch {
1297 int *i_p = $1->access_int ();
1307 | STRING_IDENTIFIER {
1308 String *s_p = $1->access_String ();
1311 DOUT << "Lookup string: " << *s_p << "\n";
1319 | string '+' string_primary {
1326 real_primary unit { $$ = $1*$2; }
1330 unit: CM_T { $$ = 1 CM; }
1331 |IN_T { $$ = 1 INCH; }
1332 |MM_T { $$ = 1 MM; }
1333 |PT_T { $$ = 1 PT; }
1340 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1348 $$ = $1->access_Lookup ();
1350 | symtables_body FONT STRING {
1352 $$->font_path_ = global_path.find (*$3);
1353 if (!$$->font_path_.length_i ())
1354 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1358 | symtables_body STRING '=' symtable {
1365 TABLE '{' symtable_body '}' { $$ = $3; }
1369 { $$ = new Symtable; }
1370 | symtable_body STRING symboldef {
1379 $$ = new Atom (*$1, *$2);
1384 Box b (Interval (0,0), Interval (0,0));
1385 $$ = new Atom (*$1, b);
1391 dinterval dinterval {
1392 $$ = new Box (*$1, *$2);
1398 dinterval: dim dim {
1399 $$ = new Interval ($1, $2);
1406 My_lily_parser::set_yydebug (bool b)
1413 My_lily_parser::do_yyparse ()
1415 yyparse ((void*)this);