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.1");
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;
92 Music_list *musiclist;
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> VOICE_IDENTIFIER
208 %token <id> POST_REQUEST_IDENTIFIER
209 %token <id> SCRIPT_IDENTIFIER
210 %token <id> COMMAND_IDENTIFIER
211 %token <id> REAL_IDENTIFIER
212 %token <id> STRING_IDENTIFIER
213 %token <id> TRANS_IDENTIFIER
214 %token <id> INT_IDENTIFIER
215 %token <id> SCORE_IDENTIFIER
216 %token <id> MIDI_IDENTIFIER
217 %token <id> PAPER_IDENTIFIER
218 %token <id> REQUEST_IDENTIFIER
220 %token <string> DURATION RESTNAME
221 %token <string> STRING
225 %type <outputdef> output_def
226 %type <header> mudela_header mudela_header_body
228 %type <i> open_request_parens close_request_parens
229 %type <i> open_abbrev_parens
230 %type <i> open_plet_parens close_plet_parens
231 %type <i> sub_quotes sup_quotes
232 %type <music> simple_element request_chord command_element
233 %type <i> abbrev_type
234 %type <i> int unsigned
236 %type <id> identifier_init simple_identifier_init
237 %type <duration> steno_duration notemode_duration
238 %type <duration> entered_notemode_duration explicit_duration
239 %type <interval> dinterval
240 %type <intvec> intastint_list
241 %type <lookup> symtables symtables_body
243 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
244 %type <notereq> steno_notepitch
245 %type <pitch_arr> pitch_list
246 %type <midi> midi_block midi_body
247 %type <duration> duration_length
249 %type <scalar> scalar
250 %type <music> Music transposed_music relative_music
251 %type <music> property_def translator_change
252 %type <musiclist> Voice Voice_body
253 %type <chord> Chord Chord_body
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
274 %type <string> concat_strings
282 | mudela mudela_header {
283 delete header_global_p;
284 header_global_p = $2;
286 | mudela score_block {
287 score_global_array.push ($2);
289 | mudela add_declaration { }
291 | mudela check_version { }
292 | mudela add_notenames { }
297 Mudela_version ver (*$2);
298 if (!((ver >= oldest_version) && (ver <= version))) {
299 if (THIS->ignore_version_b_) {
300 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
302 THIS->fatal_error_i_ = 1;
303 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
310 NOTENAMES '{' notenames_body '}'
315 | notenames_body CLEAR {
316 THIS->clear_notenames ();
318 | notenames_body STRING '=' explicit_musical_pitch {
319 THIS->add_notename (*$2, *$4);
329 | mudela_header_body STRING '=' concat_strings ';' {
337 HEADER '{' mudela_header_body '}' {
347 | concat_strings STRING {
358 THIS->remember_spot ();
360 /* cont */ '=' identifier_init optional_semicolon {
361 THIS->lexer_p_->set_identifier (*$1, $4);
362 $4->init_b_ = THIS->init_parse_b_;
363 $4->set_spot (THIS->pop_spot ());
369 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
373 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
376 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
379 | script_definition {
380 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
384 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
388 $$ = new Lookup_identifier ($1, IDENTIFIER);
392 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
394 | explicit_duration {
395 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
397 | simple_identifier_init
400 simple_identifier_init:
402 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
405 $$ = new String_identifier ($1, STRING_IDENTIFIER);
408 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
411 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
416 TRANSLATOR '{' translator_spec_body '}'
420 translator_spec_body:
422 $$ = $1->access_Translator ();
423 $$-> set_spot (THIS->here_input ());
426 Translator* t = get_translator_l (*$2);
428 t->set_spot (THIS->here_input ());
432 | translator_spec_body STRING '=' scalar ';' {
433 $$-> set_property (*$2, *$4);
437 | translator_spec_body CONSISTS STRING ';' {
438 $$->group_l ()->consists_str_arr_.push (*$3);
441 | translator_spec_body ACCEPTS STRING ';' {
442 $$->group_l ()->accepts_str_arr_.push (*$3);
451 SCORE { THIS->remember_spot ();
452 THIS->error_level_i_ =0;
454 /*cont*/ '{' score_body '}' {
456 $$->set_spot (THIS->pop_spot ());
457 if (!$$->def_p_arr_.size ())
458 $$->add_output (THIS->default_paper_p ());
460 /* handle error levels. */
461 $$->errorlevel_i_ = THIS->error_level_i_;
462 THIS->error_level_i_ = 0;
470 $$ = $1->access_Score ();
472 | score_body mudela_header {
477 $2->warning (_ ("More than one music block"));
480 | score_body output_def {
498 /* */ { $$ =new Array<int>; }
499 | intastint_list int '*' int {
500 $$->push ($2); $$->push ($4);
502 | intastint_list int {
503 $$->push ($2); $$->push (1);
512 PAPER '{' paper_body '}' {
514 THIS-> lexer_p_->scope_l_arr_.pop ();
525 Paper_def *p = THIS->default_paper_p ();
526 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
529 | PAPER_IDENTIFIER optional_semicolon {
530 Paper_def *p = $1->access_Paper_def ();
531 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
534 | paper_body OUTPUT STRING ';' {
535 $$->outfile_str_ = *$3;
538 | paper_body int '=' symtables { // ugh, what a syntax
539 $$->set_lookup ($2, $4);
541 | paper_body STRING '=' simple_identifier_init optional_semicolon {
543 id->init_b_ = THIS->init_parse_b_;
544 if (id->is_type_b (Translator_identifier::static_name ()))
546 $$->assign_translator (*$2, id->access_Translator ());
550 THIS->lexer_p_->set_identifier (*$2, id);
553 | paper_body SHAPE '=' shape_array ';' {
554 $$->shape_int_a_ = *$4;
567 Real *r_p = $1->access_Real ();
569 DOUT << "Lookup real: " << *r_p << "\n";
580 | real '+' real_mult_expression {
583 | real '-' real_mult_expression {
588 real_mult_expression:
590 | real_mult_expression '*' real_primary {
593 | real_mult_expression '/' real_primary {
600 $$ = new Array<Interval>;
602 | shape_array dim dim {
603 $$->push(Interval($2, $2 + $3));
612 '{' midi_body '}' { $$ = $3; }
615 midi_body: /* empty */ {
616 $$ = THIS->default_midi_p ();
618 | midi_body STRING '=' translator_spec {
619 $$-> assign_translator (*$2, $4);
622 | midi_body OUTPUT STRING ';' {
623 $$->outfile_str_ = *$3;
626 | midi_body tempo_request ';' {
627 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
636 TEMPO entered_notemode_duration '=' unsigned {
640 $$-> metronome_i_ = $4;
653 $$->set_spot (THIS->here_input ());
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;
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 '<' Chord_body '>' { $$ = $2; }
747 $$-> multi_level_i_ = 0;
748 $$->set_spot (THIS->here_input ());
759 TRANSPOSE musical_pitch Music {
761 $$ -> transpose (*$2);
771 pre_requests simple_element post_requests {
772 THIS->add_requests ((Chord*)$2);//ugh
783 $$ = new Request_chord;
784 $$-> set_spot (THIS->here_input ());
785 $1-> set_spot (THIS->here_input ());
786 ((Chord*)$$) ->add ($1);//ugh
793 | verbose_command_req ';' { $$ = $1; }
798 $$ = new Barcheck_req;
800 | COMMAND_IDENTIFIER {
801 $$ = $1->access_Request ();
807 $$ = new Bar_req (*$2);
811 $$ = new Mark_req (*$2);
815 $$ = new Mark_req (to_str ($2));
817 | TIME_T unsigned '/' unsigned {
818 Time_signature_change_req *m = new Time_signature_change_req;
824 Break_req * b = new Break_req;
826 b-> set_spot (THIS->here_input ());
829 | SKIP duration_length {
830 Skip_req * skip_p = new Skip_req;
831 skip_p->duration_ = *$2;
839 $$ = new Cadenza_req ($2);
841 | PARTIAL duration_length {
842 $$ = new Partial_measure_req ($2->length ());
846 $$ = new Clef_change_req (*$2);
849 | KEY NOTENAME_PITCH {
850 Key_change_req *key_p= new Key_change_req;
851 key_p->pitch_arr_.push(*$2);
852 key_p->ordinary_key_b_ = true;
856 | KEYSIGNATURE pitch_list {
857 Key_change_req *key_p= new Key_change_req;
858 key_p->pitch_arr_ = *$2;
859 key_p->ordinary_key_b_ = false;
863 | GROUPING intastint_list {
864 $$ = get_grouping_req (*$2); delete $2;
870 /* something silly happened. Junk this stuff*/
871 if (!THIS->post_reqs.empty ())
873 warning ("Junking post-requests");
874 THIS->post_reqs.clear ();
877 | post_requests structured_post_request {
878 $2->set_spot (THIS->here_input ());
879 THIS->post_reqs.push ($2);
881 | post_requests close_request_parens {
882 Array<Request*>& r = *THIS->get_parens_request ($2);
883 for (int i = 0; i < r.size (); i++ )
884 r[i]->set_spot (THIS->here_input ());
885 THIS->post_reqs.concat (r);
890 structured_post_request:
896 POST_REQUEST_IDENTIFIER {
897 $$ = (Request*)$1->access_Request ();
903 #if 1 // re-instating
904 Abbreviation_req* a = new Abbreviation_req;
907 #else // and disabling just created ugly sticky abbrev_req
908 THIS->default_abbrev_i_ = $1;
933 // $$->octave_i_ += THIS->default_octave_i_;
935 | NOTENAME_PITCH sup_quotes {
937 // $$->octave_i_ += THIS->default_octave_i_ + $2;
940 | NOTENAME_PITCH sub_quotes {
942 $$->octave_i_ += - $2;
943 // $$->octave_i_ += THIS->default_octave_i_ - $2;
947 explicit_musical_pitch:
948 MUSICAL_PITCH '{' int int int '}' {/* ugh */
949 $$ = new Musical_pitch;
951 $$->notename_i_ = $4;
952 $$->accidental_i_ = $5;
957 | explicit_musical_pitch
967 | steno_notepitch '!' {
968 $$->forceacc_b_ = ! $$->forceacc_b_;
974 DURATION '{' int unsigned '}' {
982 ABSDYNAMIC '{' unsigned '}' {
983 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
984 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
987 | SPANDYNAMIC '{' int int '}' {
988 Span_dynamic_req * sp_p = new Span_dynamic_req;
989 sp_p->spantype = (Span_req::Spantype)$4;
990 sp_p-> dynamic_dir_ = (Direction)$3;
996 unsigned '/' unsigned {
1003 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1011 THIS->plet_.type_i_ = $2[1];
1012 THIS->plet_.iso_i_ = $2[0];
1013 THIS->default_duration_.plet_ = THIS->plet_;
1017 THIS->plet_.type_i_ = 1;
1018 THIS->plet_.iso_i_ = 1;
1019 THIS->default_duration_.plet_ = THIS->plet_;
1021 | TELP plet_fraction {
1023 THIS->plet_.type_i_ = $2[1];
1024 THIS->plet_.iso_i_ = $2[0];
1025 THIS->default_duration_.plet_ = THIS->plet_;
1029 close_request_parens:
1051 if (!Duration::duration_type_b ($3))
1052 THIS->parser_error (_f ("not a duration: %d", $3));
1054 THIS->parser_error (_ ("can't abbreviate"));
1056 THIS->set_abbrev_beam ($3);
1063 THIS->plet_.type_i_ = $2[1];
1064 THIS->plet_.iso_i_ = $2[0];
1065 THIS->default_duration_.plet_ = THIS->plet_;
1067 | PLET plet_fraction {
1069 THIS->plet_.type_i_ = $2[1];
1070 THIS->plet_.iso_i_ = $2[0];
1071 THIS->default_duration_.plet_ = THIS->plet_;
1075 open_request_parens:
1085 | open_abbrev_parens
1092 SCRIPT '{' script_body '}' { $$ = $3; }
1096 STRING int int int int int {
1097 Script_def *s = new Script_def;
1098 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1105 script_dir gen_script_def {
1106 Musical_script_req *m = new Musical_script_req;
1108 m->scriptdef_p_ = $2;
1109 m->set_spot (THIS->here_input ());
1111 m->dir_ = (Direction)$1;
1118 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1122 $$-> set_spot (THIS->here_input ());
1126 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1132 Text_def *t = new Text_def;
1136 $$->set_spot (THIS->here_input ());
1142 Text_def* t = new Text_def;
1144 t->text_str_ = to_str ($1);
1145 t->style_str_ = "finger";
1146 $$->set_spot (THIS->here_input ());
1150 script_abbreviation:
1151 '^' { $$ = get_scriptdef ('^'); }
1152 | '+' { $$ = get_scriptdef ('+'); }
1153 | '-' { $$ = get_scriptdef ('-'); }
1154 | '|' { $$ = get_scriptdef ('|'); }
1155 | 'o' { $$ = get_scriptdef ('o'); }
1156 | '>' { $$ = get_scriptdef ('>'); }
1158 $$ = get_scriptdef ('.');
1163 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1164 | script_definition { $$ = $1; }
1165 | script_abbreviation {
1166 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1180 | pre_requests open_request_parens {
1181 Array<Request*>& r = *THIS->get_parens_request ($2);
1182 for (int i = 0; i < r.size (); i++ )
1183 r[i]->set_spot (THIS->here_input ());
1184 THIS->pre_reqs.concat (r);
1189 absolute_musical_pitch:
1190 steno_musical_pitch {
1192 // $$->octave_i_ -= THIS->default_octave_i_;
1204 | dots '.' { $$ ++; }
1207 entered_notemode_duration:
1209 $$ = new Duration (THIS->default_duration_);
1212 $$ = new Duration (THIS->default_duration_);
1216 THIS->set_last_duration ($1);
1221 entered_notemode_duration {
1229 if (!Duration::duration_type_b ($1))
1230 THIS->parser_error (_f ("not a duration: %d", $1));
1232 $$->durlog_i_ = Duration_convert::i2_type ($1);
1233 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1236 | DURATION_IDENTIFIER {
1237 $$ = $1->access_Duration ();
1239 | steno_duration '.' {
1242 | steno_duration '*' unsigned {
1243 $$->plet_.iso_i_ *= $3;
1245 | steno_duration '/' unsigned {
1246 $$->plet_.type_i_ *= $3;
1256 if (!Duration::duration_type_b ($2))
1257 THIS->parser_error (_f ("not a duration: %d", $2));
1259 THIS->parser_error (_ ("can't abbreviate"));
1267 steno_notepitch notemode_duration {
1268 if (!THIS->lexer_p_->note_state_b ())
1269 THIS->parser_error (_ ("have to be in Note mode for notes"));
1270 $1->set_duration (*$2);
1271 int durlog_i = $2->durlog_i_;
1272 $$ = THIS->get_note_element ($1, $2);
1274 | RESTNAME notemode_duration {
1275 $$ = THIS->get_rest_element (*$1, $2);
1276 delete $1; // delete notename
1278 | MEASURES notemode_duration {
1279 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1283 Chord*velt_p = new Request_chord;
1284 velt_p->set_spot (THIS->here_input ());
1289 | STRING notemode_duration {
1290 if (!THIS->lexer_p_->lyric_state_b ())
1291 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1292 $$ = THIS->get_word_element (*$1, $2);
1302 $$ = new Array<Musical_pitch>;
1304 | pitch_list musical_pitch {
1326 int *i_p = $1->access_int ();
1336 | STRING_IDENTIFIER {
1337 String *s_p = $1->access_String ();
1340 DOUT << "Lookup string: " << *s_p << "\n";
1348 | string '+' string_primary {
1355 real_primary unit { $$ = $1*$2; }
1359 unit: CM_T { $$ = 1 CM; }
1360 |IN_T { $$ = 1 INCH; }
1361 |MM_T { $$ = 1 MM; }
1362 |PT_T { $$ = 1 PT; }
1369 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1377 $$ = $1->access_Lookup ();
1379 | symtables_body FONT STRING {
1381 $$->font_path_ = global_path.find (*$3);
1382 if (!$$->font_path_.length_i ())
1383 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1387 | symtables_body STRING '=' symtable {
1394 TABLE '{' symtable_body '}' { $$ = $3; }
1398 { $$ = new Symtable; }
1399 | symtable_body STRING symboldef {
1408 $$ = new Atom (*$1, *$2);
1413 Box b (Interval (0,0), Interval (0,0));
1414 $$ = new Atom (*$1, b);
1420 dinterval dinterval {
1421 $$ = new Box (*$1, *$2);
1427 dinterval: dim dim {
1428 $$ = new Interval ($1, $2);
1435 My_lily_parser::set_yydebug (bool b)
1442 My_lily_parser::do_yyparse ()
1444 yyparse ((void*)this);