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>
13 #include "notename-table.hh"
15 #include "translation-property.hh"
16 #include "script-def.hh"
17 #include "symtable.hh"
19 #include "ps-lookup.hh"
20 #include "tex-lookup.hh"
22 #include "my-lily-lexer.hh"
23 #include "paper-def.hh"
24 #include "midi-def.hh"
26 #include "file-path.hh"
29 #include "parseconstruct.hh"
30 #include "dimensions.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"
43 #include "mudela-version.hh"
45 #include "relative-music.hh"
46 #include "transposed-music.hh"
49 Mudela_version oldest_version ("1.0.6");
50 Mudela_version version ("1.0.6");
53 // needed for bison.simple's malloc() and free()
56 int const GUESS_PLET = 5;
57 int guess_plet_a[GUESS_PLET] =
66 Paper_def* current_paper = 0;
72 #define YYERROR_VERBOSE 1
74 #define YYPARSE_PARAM my_lily_parser_l
75 #define YYLEX_PARAM my_lily_parser_l
76 #define THIS ((My_lily_parser *) my_lily_parser_l)
78 #define yyerror THIS->parser_error
79 #define ARRAY_SIZE(a,s) if (a.size () != s) THIS->parser_error (_f("expecting %d arguments", s))
86 Array<Interval>* intarr;
87 Array<Musical_pitch> *pitch_arr;
88 Array<String> * strvec;
91 Simultaneous_music *chord;
96 Music_list *music_list;
101 Music_output_def * outputdef;
102 Musical_pitch * pitch;
106 Notename_table *notenametab;
110 General_script_def * script;
115 Symtables* symtables;
127 yylex (YYSTYPE *s, void * v_l)
129 My_lily_parser *pars_l = (My_lily_parser*) v_l;
130 My_lily_lexer * lex_l = pars_l->lexer_p_;
132 lex_l->lexval_l = (void*) s;
133 return lex_l->yylex ();
141 /* tokens which are not keywords */
196 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
200 %token <pitch> NOTENAME_PITCH
201 %token <id> DURATION_IDENTIFIER
202 %token <id> IDENTIFIER
203 %token <id> NOTENAME_TABLE_IDENTIFIER
204 %token <id> MUSIC_IDENTIFIER
205 %token <id> POST_REQUEST_IDENTIFIER
206 %token <id> SCRIPT_IDENTIFIER
207 %token <id> COMMAND_IDENTIFIER
208 %token <id> REAL_IDENTIFIER
209 %token <id> STRING_IDENTIFIER
210 %token <id> TRANS_IDENTIFIER
211 %token <id> INT_IDENTIFIER
212 %token <id> SCORE_IDENTIFIER
213 %token <id> MIDI_IDENTIFIER
214 %token <id> PAPER_IDENTIFIER
215 %token <id> REQUEST_IDENTIFIER
217 %token <string> DURATION RESTNAME
218 %token <string> STRING
222 %type <outputdef> output_def
223 %type <header> mudela_header mudela_header_body
225 %type <i> open_request_parens close_request_parens
226 %type <i> open_abbrev_parens
227 %type <i> open_plet_parens close_plet_parens
228 %type <i> sub_quotes sup_quotes
229 %type <music> simple_element request_chord command_element
230 %type <i> abbrev_type
231 %type <i> int unsigned
233 %type <id> identifier_init simple_identifier_init
234 %type <duration> steno_duration notemode_duration
235 %type <duration> entered_notemode_duration explicit_duration
236 %type <interval> dinterval
237 %type <intvec> intastint_list int_list
238 %type <symtables> symtables symtables_body
240 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
241 %type <notereq> steno_notepitch
242 %type <pitch_arr> pitch_list
243 %type <midi> midi_block midi_body
244 %type <duration> duration_length
246 %type <scalar> scalar
247 %type <music> Music relative_music Sequential_music Simultaneous_music
248 %type <music> property_def translator_change
249 %type <music_list> Music_list
250 %type <paper> paper_block paper_def_body
251 %type <real> dim real
252 %type <real> real_mult_expression real_primary
254 %type <request> abbrev_command_req
255 %type <request> post_request structured_post_request
256 %type <pair> plet_fraction
257 %type <request> command_req verbose_command_req
258 %type <request> script_req dynamic_req
259 %type <string> string
260 %type <string> string_primary
261 %type <score> score_block score_body
262 %type <intarr> shape_array
263 %type <script> script_definition script_body mudela_script gen_script_def
264 %type <textdef> text_def finger
265 %type <string> script_abbreviation
266 %type <symbol> symboldef
267 %type <symtable> symtable symtable_body
268 %type <trans> translator_spec translator_spec_body
269 %type <tempo> tempo_request
270 %type <notenametab> notenames_body notenames_block
277 | mudela toplevel_expression {}
278 | mudela add_declaration { }
280 | mudela check_version { }
285 THIS->lexer_p_->set_notename_table ($1);
288 delete header_global_p;
289 header_global_p = $1;
292 score_global_array.push ($1);
295 Identifier * id = new
296 Paper_def_identifier ($1, PAPER_IDENTIFIER);
297 THIS->lexer_p_->set_identifier ("$defaultpaper", id)
300 Identifier * id = new
301 Midi_def_identifier ($1, MIDI_IDENTIFIER);
302 THIS->lexer_p_->set_identifier ("$defaultmidi", id)
308 Mudela_version ver (*$2);
309 if (!((ver >= oldest_version) && (ver <= version))) {
310 if (THIS->ignore_version_b_) {
311 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
313 THIS->fatal_error_i_ = 1;
314 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
322 NOTENAMES '{' notenames_body '}' { $$ = $3; }
329 $$ = new Notename_table;
331 | NOTENAME_TABLE_IDENTIFIER {
332 $$ = $1-> access_Notename_table(true);
334 | notenames_body STRING '=' explicit_musical_pitch {
346 | mudela_header_body STRING '=' string ';' {
354 HEADER '{' mudela_header_body '}' {
365 THIS->remember_spot ();
367 /* cont */ '=' identifier_init optional_semicolon {
368 THIS->lexer_p_->set_identifier (*$1, $4);
369 $4->init_b_ = THIS->init_parse_b_;
370 $4->set_spot (THIS->pop_spot ());
375 simple_identifier_init: identifier_init
379 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
383 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
386 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
389 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
392 | script_definition {
393 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
397 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
401 $$ = new Symtables_identifier ($1, IDENTIFIER);
404 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
406 | explicit_duration {
407 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
410 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
413 $$ = new String_identifier ($1, STRING_IDENTIFIER);
416 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
419 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
424 TRANSLATOR '{' translator_spec_body '}'
428 translator_spec_body:
430 $$ = $1->access_Translator (true);
431 $$-> set_spot (THIS->here_input ());
434 Translator* t = get_translator_l (*$2);
435 Translator_group * tg = t->access_Translator_group ();
438 THIS->parser_error (_("Need a translator group for a context"));
441 t->set_spot (THIS->here_input ());
445 | translator_spec_body STRING '=' simple_identifier_init ';' {
447 String_identifier *s = id->access_String_identifier ();
448 Real_identifier *r= id->access_Real_identifier ();
449 int_identifier *i = id->access_int_identifier ();
452 if (s) str = *s->access_String (false);
453 if (i) str = to_str (*i->access_int (false));
454 if (r) str = to_str (*r->access_Real (false));
456 THIS->parser_error (_("Wrong type for property value"));
459 $$->set_property (*$2, str);
461 | translator_spec_body NAME STRING ';' {
465 | translator_spec_body CONSISTS STRING ';' {
466 $$->access_Translator_group ()-> set_element (*$3, true);
469 | translator_spec_body ACCEPTS STRING ';' {
470 $$->access_Translator_group ()-> set_acceptor (*$3, true);
473 | translator_spec_body REMOVE STRING ';' {
474 $$->access_Translator_group ()-> set_element (*$3, false);
483 SCORE { THIS->remember_spot ();
484 THIS->error_level_i_ =0;
486 /*cont*/ '{' score_body '}' {
488 $$->set_spot (THIS->pop_spot ());
489 if (!$$->def_p_arr_.size ())
490 $$->add_output (THIS->default_paper_p ());
492 /* handle error levels. */
493 $$->errorlevel_i_ = THIS->error_level_i_;
494 THIS->error_level_i_ = 0;
502 $$ = $1->access_Score (true);
504 | score_body mudela_header {
509 $2->warning (_ ("More than one music block"));
512 | score_body output_def {
530 /* */ { $$ =new Array<int>; }
531 | intastint_list int '*' int {
532 $$->push ($2); $$->push ($4);
534 | intastint_list int {
535 $$->push ($2); $$->push (1);
544 PAPER '{' paper_def_body '}' {
546 THIS-> lexer_p_->scope_l_arr_.pop ();
557 Paper_def *p = THIS->default_paper_p ();
558 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
561 | PAPER_IDENTIFIER optional_semicolon {
562 Paper_def *p = $1->access_Paper_def (true);
563 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
566 | paper_def_body int '=' symtables { // ugh, what a syntax
567 Lookup * l = global_lookup_l->lookup_p (*$4);
568 $$->set_lookup ($2, l);
570 | paper_def_body STRING '=' simple_identifier_init ';' {
571 THIS->lexer_p_->set_identifier (*$2, $4);
574 | paper_def_body translator_spec {
575 $$-> assign_translator ($2);
577 | paper_def_body SHAPE '=' shape_array ';' {
578 $$->shape_int_a_ = *$4;
581 | paper_def_body error {
591 $$= *$1->access_Real (false);
601 | real '+' real_mult_expression {
604 | real '-' real_mult_expression {
609 real_mult_expression:
611 | real_mult_expression '*' real_primary {
614 | real_mult_expression '/' real_primary {
621 $$ = new Array<Interval>;
623 | shape_array dim dim {
624 $$->push(Interval($2, $2 + $3));
633 '{' midi_body '}' { $$ = $3; }
636 midi_body: /* empty */ {
637 $$ = THIS->default_midi_p ();
640 $$ = $1-> access_Midi_def (true);
642 | midi_body translator_spec {
643 $$-> assign_translator ($2);
645 | midi_body tempo_request ';' {
646 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
655 TEMPO entered_notemode_duration '=' unsigned {
659 $$-> metronome_i_ = $4;
663 Music_list: /* empty */ {
674 Sequential_music: '{' Music_list '}' {
675 $$ = new Sequential_music ($2);
679 Simultaneous_music: '<' Music_list '>' {
680 $$ = new Simultaneous_music ($2);
684 request_chord { $$ = $1; }
685 | TYPE STRING Music {
687 $$->translator_type_str_ = *$2;
690 | TYPE STRING '=' STRING Music {
692 $$->translator_type_str_ = *$2;
693 $$->translator_id_str_ = *$4;
697 | Simultaneous_music { $$ = $1; }
698 | Sequential_music { $$ = $1; }
699 | TRANSPOSE musical_pitch Music {
700 $$ = new Transposed_music ($3, *$2);
703 | MUSIC_IDENTIFIER { $$ = $1->access_Music (true); }
705 { THIS->lexer_p_->push_note_state (); }
708 THIS->lexer_p_->pop_state ();
712 { THIS->lexer_p_->push_lyric_state (); }
716 THIS->lexer_p_->pop_state ();
720 | relative_music { $$ = $1; }
724 RELATIVE absolute_musical_pitch Music {
725 $$ = new Relative_octave_music ($3, *$2);
731 TRANSLATOR STRING '=' STRING {
732 Change_translator * t = new Change_translator;
733 t-> change_to_type_str_ = *$2;
734 t-> change_to_id_str_ = *$4;
737 $$->set_spot (THIS->here_input ());
744 PROPERTY STRING '.' STRING '=' scalar {
745 Translation_property *t = new Translation_property;
746 t-> translator_type_str_ = *$2;
750 $$->set_spot (THIS->here_input ());
758 STRING { $$ = new Scalar (*$1); delete $1; }
759 | int { $$ = new Scalar ($1); }
765 pre_requests simple_element post_requests {
766 THIS->add_requests ((Simultaneous_music*)$2);//ugh
777 $$ = new Request_chord;
778 $$-> set_spot (THIS->here_input ());
779 $1-> set_spot (THIS->here_input ());
780 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
787 | verbose_command_req ';' { $$ = $1; }
792 $$ = new Barcheck_req;
794 | COMMAND_IDENTIFIER {
795 $$ = $1->access_Request (true);
801 $$ = new Bar_req (*$2);
805 $$ = new Mark_req (*$2);
809 $$ = new Mark_req (to_str ($2));
811 | TIME_T unsigned '/' unsigned {
812 Time_signature_change_req *m = new Time_signature_change_req;
818 Break_req * b = new Break_req;
820 b-> set_spot (THIS->here_input ());
823 | SKIP duration_length {
824 Skip_req * skip_p = new Skip_req;
825 skip_p->duration_ = *$2;
833 $$ = new Cadenza_req ($2);
835 | PARTIAL duration_length {
836 $$ = new Partial_measure_req ($2->length ());
840 $$ = new Clef_change_req (*$2);
843 | KEY NOTENAME_PITCH {
844 Key_change_req *key_p= new Key_change_req;
845 key_p->pitch_arr_.push(*$2);
846 key_p->ordinary_key_b_ = true;
850 | KEYSIGNATURE pitch_list {
851 Key_change_req *key_p= new Key_change_req;
852 key_p->pitch_arr_ = *$2;
853 key_p->ordinary_key_b_ = false;
857 | GROUPING intastint_list {
858 $$ = get_grouping_req (*$2); delete $2;
864 /* something silly happened. Junk this stuff*/
865 if (!THIS->post_reqs.empty ())
867 warning ("Junking post-requests");
868 THIS->post_reqs.clear ();
871 | post_requests structured_post_request {
872 $2->set_spot (THIS->here_input ());
873 THIS->post_reqs.push ($2);
875 | post_requests close_request_parens {
876 Array<Request*>& r = *THIS->get_parens_request ($2);
877 for (int i = 0; i < r.size (); i++ )
878 r[i]->set_spot (THIS->here_input ());
879 THIS->post_reqs.concat (r);
884 structured_post_request:
890 POST_REQUEST_IDENTIFIER {
891 $$ = (Request*)$1->access_Request (true);
897 Abbreviation_req* a = new Abbreviation_req;
924 | NOTENAME_PITCH sup_quotes {
928 | NOTENAME_PITCH sub_quotes {
930 $$->octave_i_ += - $2;
934 explicit_musical_pitch:
935 MUSICAL_PITCH '{' int_list '}' {/* ugh */
938 $$ = new Musical_pitch;
939 $$->octave_i_ = a[0];
940 $$->notename_i_ = a[1];
941 $$->accidental_i_ = a[2];
948 | explicit_musical_pitch
958 | steno_notepitch '!' {
959 $$->forceacc_b_ = ! $$->forceacc_b_;
965 DURATION '{' int_list '}' {
970 $$-> durlog_i_ = a[0];
978 ABSDYNAMIC '{' unsigned '}' {
979 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
980 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
983 | SPANDYNAMIC '{' int int '}' {
984 Span_dynamic_req * sp_p = new Span_dynamic_req;
985 sp_p->spantype = (Span_req::Spantype)$4;
986 sp_p-> dynamic_dir_ = (Direction)$3;
992 unsigned '/' unsigned {
999 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1007 THIS->plet_.type_i_ = $2[1];
1008 THIS->plet_.iso_i_ = $2[0];
1009 THIS->default_duration_.plet_ = THIS->plet_;
1013 THIS->plet_.type_i_ = 1;
1014 THIS->plet_.iso_i_ = 1;
1015 THIS->default_duration_.plet_ = THIS->plet_;
1017 | TELP plet_fraction {
1019 THIS->plet_.type_i_ = $2[1];
1020 THIS->plet_.iso_i_ = $2[0];
1021 THIS->default_duration_.plet_ = THIS->plet_;
1025 close_request_parens:
1047 if (!Duration::duration_type_b ($3))
1048 THIS->parser_error (_f ("not a duration: %d", $3));
1050 THIS->parser_error (_ ("can't abbreviate"));
1052 THIS->set_abbrev_beam ($3);
1059 THIS->plet_.type_i_ = $2[1];
1060 THIS->plet_.iso_i_ = $2[0];
1061 THIS->default_duration_.plet_ = THIS->plet_;
1063 | PLET plet_fraction {
1065 THIS->plet_.type_i_ = $2[1];
1066 THIS->plet_.iso_i_ = $2[0];
1067 THIS->default_duration_.plet_ = THIS->plet_;
1071 open_request_parens:
1081 | open_abbrev_parens
1088 SCRIPT '{' script_body '}' { $$ = $3; }
1092 STRING int int int int int {
1093 Script_def *s = new Script_def;
1094 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1101 script_dir gen_script_def {
1102 Musical_script_req *m = new Musical_script_req;
1104 m->scriptdef_p_ = $2;
1105 m->set_spot (THIS->here_input ());
1107 m->dir_ = (Direction)$1;
1114 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1118 $$-> set_spot (THIS->here_input ());
1122 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1128 Text_def *t = new Text_def;
1132 $$->set_spot (THIS->here_input ());
1138 Text_def* t = new Text_def;
1140 t->text_str_ = to_str ($1);
1141 t->style_str_ = "finger";
1142 $$->set_spot (THIS->here_input ());
1146 script_abbreviation:
1147 '^' { $$ = get_scriptdef ('^'); }
1148 | '+' { $$ = get_scriptdef ('+'); }
1149 | '-' { $$ = get_scriptdef ('-'); }
1150 | '|' { $$ = get_scriptdef ('|'); }
1151 | 'o' { $$ = get_scriptdef ('o'); }
1152 | '>' { $$ = get_scriptdef ('>'); }
1154 $$ = get_scriptdef ('.');
1159 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1160 | script_definition { $$ = $1; }
1161 | script_abbreviation {
1162 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1176 | pre_requests open_request_parens {
1177 Array<Request*>& r = *THIS->get_parens_request ($2);
1178 for (int i = 0; i < r.size (); i++ )
1179 r[i]->set_spot (THIS->here_input ());
1180 THIS->pre_reqs.concat (r);
1185 absolute_musical_pitch:
1186 steno_musical_pitch {
1199 | dots '.' { $$ ++; }
1202 entered_notemode_duration:
1204 $$ = new Duration (THIS->default_duration_);
1207 $$ = new Duration (THIS->default_duration_);
1211 THIS->set_last_duration ($1);
1216 entered_notemode_duration {
1224 if (!Duration::duration_type_b ($1))
1225 THIS->parser_error (_f ("not a duration: %d", $1));
1227 $$->durlog_i_ = Duration_convert::i2_type ($1);
1228 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1231 | DURATION_IDENTIFIER {
1232 $$ = $1->access_Duration (true);
1234 | steno_duration '.' {
1237 | steno_duration '*' unsigned {
1238 $$->plet_.iso_i_ *= $3;
1240 | steno_duration '/' unsigned {
1241 $$->plet_.type_i_ *= $3;
1251 if (!Duration::duration_type_b ($2))
1252 THIS->parser_error (_f ("not a duration: %d", $2));
1254 THIS->parser_error (_ ("can't abbreviate"));
1262 steno_notepitch notemode_duration {
1263 if (!THIS->lexer_p_->note_state_b ())
1264 THIS->parser_error (_ ("have to be in Note mode for notes"));
1265 $1->set_duration (*$2);
1266 $$ = THIS->get_note_element ($1, $2);
1268 | RESTNAME notemode_duration {
1269 $$ = THIS->get_rest_element (*$1, $2);
1270 delete $1; // delete notename
1272 | MEASURES notemode_duration {
1273 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1277 Simultaneous_music*velt_p = new Request_chord;
1278 velt_p->set_spot (THIS->here_input ());
1279 velt_p->add_music (m);
1282 | STRING notemode_duration {
1283 if (!THIS->lexer_p_->lyric_state_b ())
1284 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1285 $$ = THIS->get_word_element (*$1, $2);
1295 $$ = new Array<Musical_pitch>;
1297 | pitch_list musical_pitch {
1329 $$ = *$1->access_int (false);
1337 | STRING_IDENTIFIER {
1338 $$ = $1->access_String (true);
1346 | string '+' string_primary {
1353 real_primary unit { $$ = $1*$2; }
1357 unit: CM_T { $$ = 1 CM; }
1358 |IN_T { $$ = 1 INCH; }
1359 |MM_T { $$ = 1 MM; }
1360 |PT_T { $$ = 1 PT; }
1367 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1375 $$ = $1->access_Symtables (true);
1377 | symtables_body FONT STRING {
1379 $$->font_path_ = global_path.find (*$3);
1380 if (!$$->font_path_.length_i ())
1381 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1385 | symtables_body STRING '=' symtable {
1392 TABLE '{' symtable_body '}' { $$ = $3; }
1396 { $$ = new Symtable; }
1397 | symtable_body STRING symboldef {
1405 STRING unsigned box {
1406 $$ = global_lookup_l->atom_p (*$1, $2, *$3);
1411 Box b (Interval (0,0), Interval (0,0));
1412 $$ = global_lookup_l->atom_p (*$1, $2, b);
1418 dinterval dinterval {
1419 $$ = new Box (*$1, *$2);
1425 dinterval: dim dim {
1426 $$ = new Interval ($1, $2);
1433 My_lily_parser::set_yydebug (bool b)
1440 My_lily_parser::do_yyparse ()
1442 yyparse ((void*)this);