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"
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"
44 #include "transposed-music.hh"
47 Mudela_version oldest_version ("1.0.6");
48 Mudela_version version ("1.0.6");
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;
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 */
195 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
199 %token <pitch> NOTENAME_PITCH
200 %token <id> DURATION_IDENTIFIER
201 %token <id> IDENTIFIER
202 %token <id> NOTENAME_TABLE_IDENTIFIER
203 %token <id> MUSIC_IDENTIFIER
204 %token <id> POST_REQUEST_IDENTIFIER
205 %token <id> SCRIPT_IDENTIFIER
206 %token <id> COMMAND_IDENTIFIER
207 %token <id> REAL_IDENTIFIER
208 %token <id> STRING_IDENTIFIER
209 %token <id> TRANS_IDENTIFIER
210 %token <id> INT_IDENTIFIER
211 %token <id> SCORE_IDENTIFIER
212 %token <id> MIDI_IDENTIFIER
213 %token <id> PAPER_IDENTIFIER
214 %token <id> REQUEST_IDENTIFIER
216 %token <string> DURATION RESTNAME
217 %token <string> STRING
221 %type <outputdef> output_def
222 %type <header> mudela_header mudela_header_body
224 %type <i> open_request_parens close_request_parens
225 %type <i> open_abbrev_parens
226 %type <i> open_plet_parens close_plet_parens
227 %type <i> sub_quotes sup_quotes
228 %type <music> simple_element request_chord command_element
229 %type <i> abbrev_type
230 %type <i> int unsigned
232 %type <id> identifier_init simple_identifier_init
233 %type <duration> steno_duration notemode_duration
234 %type <duration> entered_notemode_duration explicit_duration
235 %type <interval> dinterval
236 %type <intvec> intastint_list int_list
237 %type <lookup> symtables symtables_body
239 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
240 %type <notereq> steno_notepitch
241 %type <pitch_arr> pitch_list
242 %type <midi> midi_block midi_body
243 %type <duration> duration_length
245 %type <scalar> scalar
246 %type <music> Music relative_music Sequential_music Simultaneous_music
247 %type <music> property_def translator_change
248 %type <music_list> Music_list
249 %type <paper> paper_block paper_def_body
250 %type <real> dim real
251 %type <real> real_mult_expression real_primary
253 %type <request> abbrev_command_req
254 %type <request> post_request structured_post_request
255 %type <pair> plet_fraction
256 %type <request> command_req verbose_command_req
257 %type <request> script_req dynamic_req
258 %type <string> string
259 %type <string> string_primary
260 %type <score> score_block score_body
261 %type <intarr> shape_array
262 %type <script> script_definition script_body mudela_script gen_script_def
263 %type <textdef> text_def finger
264 %type <string> script_abbreviation
265 %type <symbol> symboldef
266 %type <symtable> symtable symtable_body
267 %type <trans> translator_spec translator_spec_body
268 %type <tempo> tempo_request
269 %type <notenametab> notenames_body notenames_block
276 | mudela toplevel_expression {}
277 | mudela add_declaration { }
279 | mudela check_version { }
284 THIS->lexer_p_->set_notename_table ($1);
287 delete header_global_p;
288 header_global_p = $1;
291 score_global_array.push ($1);
294 Identifier * id = new
295 Paper_def_identifier ($1, PAPER_IDENTIFIER);
296 THIS->lexer_p_->set_identifier ("$defaultpaper", id)
299 Identifier * id = new
300 Midi_def_identifier ($1, MIDI_IDENTIFIER);
301 THIS->lexer_p_->set_identifier ("$defaultmidi", id)
307 Mudela_version ver (*$2);
308 if (!((ver >= oldest_version) && (ver <= version))) {
309 if (THIS->ignore_version_b_) {
310 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
312 THIS->fatal_error_i_ = 1;
313 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
321 NOTENAMES '{' notenames_body '}' { $$ = $3; }
328 $$ = new Notename_table;
330 | NOTENAME_TABLE_IDENTIFIER {
331 $$ = $1-> access_Notename_table();
333 | notenames_body STRING '=' explicit_musical_pitch {
345 | mudela_header_body STRING '=' string ';' {
353 HEADER '{' mudela_header_body '}' {
364 THIS->remember_spot ();
366 /* cont */ '=' identifier_init optional_semicolon {
367 THIS->lexer_p_->set_identifier (*$1, $4);
368 $4->init_b_ = THIS->init_parse_b_;
369 $4->set_spot (THIS->pop_spot ());
372 simple_identifier_init: identifier_init
376 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
380 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
383 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
386 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
389 | script_definition {
390 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
394 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
398 $$ = new Lookup_identifier ($1, IDENTIFIER);
402 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
404 | explicit_duration {
405 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
408 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
411 $$ = new String_identifier ($1, STRING_IDENTIFIER);
414 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
417 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
422 TRANSLATOR '{' translator_spec_body '}'
426 translator_spec_body:
428 $$ = $1->access_Translator ();
429 $$-> set_spot (THIS->here_input ());
432 Translator* t = get_translator_l (*$2);
433 Translator_group * tg = t->access_Translator_group ();
436 THIS->parser_error (_("Need a translator group for a context"));
439 t->set_spot (THIS->here_input ());
443 | translator_spec_body STRING '=' simple_identifier_init ';' {
445 String_identifier *s = id->access_String_identifier ();
446 Real_identifier *r= id->access_Real_identifier ();
447 int_identifier *i = id->access_int_identifier ();
450 if (s) str = *s->access_String (false);
451 if (i) str = to_str (*i->access_int (false));
452 if (r) str = to_str (*r->access_Real (false));
454 THIS->parser_error (_("Wrong type for property value"));
457 $$->set_property (*$2, str);
459 | translator_spec_body STRING ';' {
463 | translator_spec_body CONSISTS STRING ';' {
464 $$->access_Translator_group ()-> set_element (*$3, true);
467 | translator_spec_body ACCEPTS STRING ';' {
468 $$->access_Translator_group ()-> set_acceptor (*$3, true);
471 | translator_spec_body REMOVE STRING ';' {
472 $$->access_Translator_group ()-> set_element (*$3, false);
481 SCORE { THIS->remember_spot ();
482 THIS->error_level_i_ =0;
484 /*cont*/ '{' score_body '}' {
486 $$->set_spot (THIS->pop_spot ());
487 if (!$$->def_p_arr_.size ())
488 $$->add_output (THIS->default_paper_p ());
490 /* handle error levels. */
491 $$->errorlevel_i_ = THIS->error_level_i_;
492 THIS->error_level_i_ = 0;
500 $$ = $1->access_Score ();
502 | score_body mudela_header {
507 $2->warning (_ ("More than one music block"));
510 | score_body output_def {
528 /* */ { $$ =new Array<int>; }
529 | intastint_list int '*' int {
530 $$->push ($2); $$->push ($4);
532 | intastint_list int {
533 $$->push ($2); $$->push (1);
542 PAPER '{' paper_def_body '}' {
544 THIS-> lexer_p_->scope_l_arr_.pop ();
555 Paper_def *p = THIS->default_paper_p ();
556 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
559 | PAPER_IDENTIFIER optional_semicolon {
560 Paper_def *p = $1->access_Paper_def ();
561 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
564 | paper_def_body int '=' symtables { // ugh, what a syntax
565 $$->set_lookup ($2, $4);
567 | paper_def_body STRING '=' simple_identifier_init ';' {
568 THIS->lexer_p_->set_identifier (*$2, $4);
571 | paper_def_body translator_spec {
572 $$-> assign_translator ($2);
574 | paper_def_body SHAPE '=' shape_array ';' {
575 $$->shape_int_a_ = *$4;
578 | paper_def_body error {
588 Real *r_p = $1->access_Real ();
590 DOUT << "Lookup real: " << *r_p << "\n";
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 ();
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 (); }
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 ();
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 ();
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 ();
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 int durlog_i = $2->durlog_i_;
1267 $$ = THIS->get_note_element ($1, $2);
1269 | RESTNAME notemode_duration {
1270 $$ = THIS->get_rest_element (*$1, $2);
1271 delete $1; // delete notename
1273 | MEASURES notemode_duration {
1274 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1278 Simultaneous_music*velt_p = new Request_chord;
1279 velt_p->set_spot (THIS->here_input ());
1280 velt_p->add_music (m);
1283 | STRING notemode_duration {
1284 if (!THIS->lexer_p_->lyric_state_b ())
1285 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1286 $$ = THIS->get_word_element (*$1, $2);
1296 $$ = new Array<Musical_pitch>;
1298 | pitch_list musical_pitch {
1330 int *i_p = $1->access_int ();
1340 | STRING_IDENTIFIER {
1341 String *s_p = $1->access_String ();
1344 DOUT << "Lookup string: " << *s_p << "\n";
1352 | string '+' string_primary {
1359 real_primary unit { $$ = $1*$2; }
1363 unit: CM_T { $$ = 1 CM; }
1364 |IN_T { $$ = 1 INCH; }
1365 |MM_T { $$ = 1 MM; }
1366 |PT_T { $$ = 1 PT; }
1373 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1381 $$ = $1->access_Lookup ();
1383 | symtables_body FONT STRING {
1385 $$->font_path_ = global_path.find (*$3);
1386 if (!$$->font_path_.length_i ())
1387 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1391 | symtables_body STRING '=' symtable {
1398 TABLE '{' symtable_body '}' { $$ = $3; }
1402 { $$ = new Symtable; }
1403 | symtable_body STRING symboldef {
1412 $$ = new Atom (*$1, *$2);
1417 Box b (Interval (0,0), Interval (0,0));
1418 $$ = new Atom (*$1, b);
1424 dinterval dinterval {
1425 $$ = new Box (*$1, *$2);
1431 dinterval: dim dim {
1432 $$ = new Interval ($1, $2);
1439 My_lily_parser::set_yydebug (bool b)
1446 My_lily_parser::do_yyparse ()
1448 yyparse ((void*)this);