1 %{ // -*-Fundamental-*-
4 parser.yy -- YACC -> C++ parser for mudela
6 source file of the GNU LilyPond music typesetter
8 (c) 1997 Han-Wen Nienhuys <hanwen@stack.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.0");
48 Mudela_version version ("1.0.0");
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);
509 PAPER '{' paper_body '}' {
511 THIS-> lexer_p_->scope_l_arr_.pop ();
522 Paper_def *p = THIS->default_paper_p ();
523 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
526 | PAPER_IDENTIFIER optional_semicolon {
527 Paper_def *p = $1->access_Paper_def ();
528 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
531 | paper_body OUTPUT STRING ';' {
532 $$->outfile_str_ = *$3;
535 | paper_body int '=' symtables { // ugh, what a syntax
536 $$->set_lookup ($2, $4);
538 | paper_body STRING '=' simple_identifier_init optional_semicolon {
540 id->init_b_ = THIS->init_parse_b_;
541 if (id->is_type_b (Translator_identifier::static_name ()))
543 $$->assign_translator (*$2, id->access_Translator ());
547 THIS->lexer_p_->set_identifier (*$2, id);
550 | paper_body SHAPE '=' shape_array ';' {
551 $$->shape_int_a_ = *$4;
564 Real *r_p = $1->access_Real ();
566 DOUT << "Lookup real: " << *r_p << "\n";
577 | real '+' real_mult_expression {
580 | real '-' real_mult_expression {
585 real_mult_expression:
587 | real_mult_expression '*' real_primary {
590 | real_mult_expression '/' real_primary {
597 $$ = new Array<Interval>;
599 | shape_array dim dim {
600 $$->push(Interval($2, $2 + $3));
609 '{' midi_body '}' { $$ = $3; }
612 midi_body: /* empty */ {
613 $$ = THIS->default_midi_p ();
615 | midi_body STRING '=' translator_spec {
616 $$-> assign_translator (*$2, $4);
619 | midi_body OUTPUT STRING ';' {
620 $$->outfile_str_ = *$3;
623 | midi_body tempo_request ';' {
624 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
633 TEMPO entered_notemode_duration '=' unsigned {
637 $$-> metronome_i_ = $4;
650 $$->set_spot (THIS->here_input ());
661 request_chord { $$ = $1; }
662 | TYPE STRING Music {
664 $$->translator_type_str_ = *$2;
667 | TYPE STRING '=' STRING Music {
669 $$->translator_type_str_ = *$2;
670 $$->translator_id_str_ = *$4;
676 | transposed_music { $$ = $1; }
677 | MUSIC_IDENTIFIER { $$ = $1->access_Music (); }
679 { THIS->lexer_p_->push_note_state (); }
682 THIS->lexer_p_->pop_state ();
686 { THIS->lexer_p_->push_lyric_state (); }
690 THIS->lexer_p_->pop_state ();
694 | relative_music { $$ = $1; }
698 RELATIVE absolute_musical_pitch Music {
699 $$ = new Relative_octave_music ($3, *$2);
705 TRANSLATOR STRING '=' STRING {
706 Change_translator * t = new Change_translator;
707 t-> change_to_type_str_ = *$2;
708 t-> change_to_id_str_ = *$4;
711 $$->set_spot (THIS->here_input ());
718 PROPERTY STRING '.' STRING '=' scalar {
719 Translation_property *t = new Translation_property;
720 t-> translator_type_str_ = *$2;
724 $$->set_spot (THIS->here_input ());
732 STRING { $$ = new Scalar (*$1); delete $1; }
733 | int { $$ = new Scalar ($1); }
738 '<' Chord_body '>' { $$ = $2; }
744 $$-> multi_level_i_ = 0;
745 $$->set_spot (THIS->here_input ());
756 TRANSPOSE musical_pitch Music {
758 $$ -> transpose (*$2);
768 pre_requests simple_element post_requests {
769 THIS->add_requests ((Chord*)$2);//ugh
780 $$ = new Request_chord;
781 $$-> set_spot (THIS->here_input ());
782 $1-> set_spot (THIS->here_input ());
783 ((Chord*)$$) ->add ($1);//ugh
790 | verbose_command_req ';' { $$ = $1; }
795 $$ = new Barcheck_req;
797 | COMMAND_IDENTIFIER {
798 $$ = $1->access_Request ();
804 $$ = new Bar_req (*$2);
808 $$ = new Mark_req (*$2);
812 $$ = new Mark_req (to_str ($2));
814 | TIME_T unsigned '/' unsigned {
815 Time_signature_change_req *m = new Time_signature_change_req;
821 Break_req * b = new Break_req;
823 b-> set_spot (THIS->here_input ());
826 | SKIP duration_length {
827 Skip_req * skip_p = new Skip_req;
828 skip_p->duration_ = *$2;
836 $$ = new Cadenza_req ($2);
838 | PARTIAL duration_length {
839 $$ = new Partial_measure_req ($2->length ());
843 $$ = new Clef_change_req (*$2);
846 | KEY NOTENAME_PITCH {
847 Key_change_req *key_p= new Key_change_req;
848 key_p->pitch_arr_.push(*$2);
849 key_p->ordinary_key_b_ = true;
853 | ACCIDENTALS pitch_list {
854 Key_change_req *key_p= new Key_change_req;
855 key_p->pitch_arr_ = *$2;
856 key_p->ordinary_key_b_ = false;
860 | GROUPING intastint_list {
861 $$ = get_grouping_req (*$2); delete $2;
867 /* something silly happened. Junk this stuff*/
868 if (!THIS->post_reqs.empty ())
870 warning ("Junking post-requests");
871 THIS->post_reqs.clear ();
874 | post_requests structured_post_request {
875 $2->set_spot (THIS->here_input ());
876 THIS->post_reqs.push ($2);
878 | post_requests close_request_parens {
879 Array<Request*>& r = *THIS->get_parens_request ($2);
880 for (int i = 0; i < r.size (); i++ )
881 r[i]->set_spot (THIS->here_input ());
882 THIS->post_reqs.concat (r);
887 structured_post_request:
893 POST_REQUEST_IDENTIFIER {
894 $$ = (Request*)$1->access_Request ();
900 #if 1 // re-instating
901 Abbreviation_req* a = new Abbreviation_req;
904 #else // and disabling just created ugly sticky abbrev_req
905 THIS->default_abbrev_i_ = $1;
930 // $$->octave_i_ += THIS->default_octave_i_;
932 | NOTENAME_PITCH sup_quotes {
934 // $$->octave_i_ += THIS->default_octave_i_ + $2;
937 | NOTENAME_PITCH sub_quotes {
939 $$->octave_i_ += - $2;
940 // $$->octave_i_ += THIS->default_octave_i_ - $2;
944 explicit_musical_pitch:
945 MUSICAL_PITCH '{' int int int '}' {/* ugh */
946 $$ = new Musical_pitch;
948 $$->notename_i_ = $4;
949 $$->accidental_i_ = $5;
954 | explicit_musical_pitch
964 | steno_notepitch '!' {
965 $$->forceacc_b_ = ! $$->forceacc_b_;
971 DURATION '{' int unsigned '}' {
979 ABSDYNAMIC '{' unsigned '}' {
980 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
981 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
984 | SPANDYNAMIC '{' int int '}' {
985 Span_dynamic_req * sp_p = new Span_dynamic_req;
986 sp_p->spantype = (Span_req::Spantype)$4;
987 sp_p-> dynamic_dir_ = (Direction)$3;
993 unsigned '/' unsigned {
1000 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1008 THIS->plet_.type_i_ = $2[1];
1009 THIS->plet_.iso_i_ = $2[0];
1010 THIS->default_duration_.plet_ = THIS->plet_;
1014 THIS->plet_.type_i_ = 1;
1015 THIS->plet_.iso_i_ = 1;
1016 THIS->default_duration_.plet_ = THIS->plet_;
1018 | TELP plet_fraction {
1020 THIS->plet_.type_i_ = $2[1];
1021 THIS->plet_.iso_i_ = $2[0];
1022 THIS->default_duration_.plet_ = THIS->plet_;
1026 close_request_parens:
1048 if (!Duration::duration_type_b ($3))
1049 THIS->parser_error (_f ("not a duration: %d", $3));
1051 THIS->parser_error (_ ("can't abbreviate"));
1053 THIS->set_abbrev_beam ($3);
1060 THIS->plet_.type_i_ = $2[1];
1061 THIS->plet_.iso_i_ = $2[0];
1062 THIS->default_duration_.plet_ = THIS->plet_;
1064 | PLET plet_fraction {
1066 THIS->plet_.type_i_ = $2[1];
1067 THIS->plet_.iso_i_ = $2[0];
1068 THIS->default_duration_.plet_ = THIS->plet_;
1072 open_request_parens:
1082 | open_abbrev_parens
1089 SCRIPT '{' script_body '}' { $$ = $3; }
1093 STRING int int int int int {
1094 Script_def *s = new Script_def;
1095 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1102 script_dir gen_script_def {
1103 Musical_script_req *m = new Musical_script_req;
1105 m->scriptdef_p_ = $2;
1106 m->set_spot (THIS->here_input ());
1108 m->dir_ = (Direction)$1;
1115 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1119 $$-> set_spot (THIS->here_input ());
1123 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1129 Text_def *t = new Text_def;
1133 $$->set_spot (THIS->here_input ());
1139 Text_def* t = new Text_def;
1141 t->text_str_ = to_str ($1);
1142 t->style_str_ = "finger";
1143 $$->set_spot (THIS->here_input ());
1147 script_abbreviation:
1148 '^' { $$ = get_scriptdef ('^'); }
1149 | '+' { $$ = get_scriptdef ('+'); }
1150 | '-' { $$ = get_scriptdef ('-'); }
1151 | '|' { $$ = get_scriptdef ('|'); }
1152 | 'o' { $$ = get_scriptdef ('o'); }
1153 | '>' { $$ = get_scriptdef ('>'); }
1155 $$ = get_scriptdef ('.');
1160 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1161 | script_definition { $$ = $1; }
1162 | script_abbreviation {
1163 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1177 | pre_requests open_request_parens {
1178 Array<Request*>& r = *THIS->get_parens_request ($2);
1179 for (int i = 0; i < r.size (); i++ )
1180 r[i]->set_spot (THIS->here_input ());
1181 THIS->pre_reqs.concat (r);
1186 absolute_musical_pitch:
1187 steno_musical_pitch {
1189 // $$->octave_i_ -= THIS->default_octave_i_;
1201 | dots '.' { $$ ++; }
1204 entered_notemode_duration:
1206 $$ = new Duration (THIS->default_duration_);
1209 $$ = new Duration (THIS->default_duration_);
1213 THIS->set_last_duration ($1);
1218 entered_notemode_duration {
1226 if (!Duration::duration_type_b ($1))
1227 THIS->parser_error (_f ("not a duration: %d", $1));
1229 $$->durlog_i_ = Duration_convert::i2_type ($1);
1230 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1233 | DURATION_IDENTIFIER {
1234 $$ = $1->access_Duration ();
1236 | steno_duration '.' {
1239 | steno_duration '*' unsigned {
1240 $$->plet_.iso_i_ *= $3;
1242 | steno_duration '/' unsigned {
1243 $$->plet_.type_i_ *= $3;
1253 if (!Duration::duration_type_b ($2))
1254 THIS->parser_error (_f ("not a duration: %d", $2));
1256 THIS->parser_error (_ ("can't abbreviate"));
1264 steno_notepitch notemode_duration {
1265 if (!THIS->lexer_p_->note_state_b ())
1266 THIS->parser_error (_ ("have to be in Note mode for notes"));
1267 $1->set_duration (*$2);
1268 int durlog_i = $2->durlog_i_;
1269 $$ = THIS->get_note_element ($1, $2);
1271 | RESTNAME notemode_duration {
1272 $$ = THIS->get_rest_element (*$1, $2);
1273 delete $1; // delete notename
1275 | MEASURES notemode_duration {
1276 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1280 Chord*velt_p = new Request_chord;
1281 velt_p->set_spot (THIS->here_input ());
1286 | STRING notemode_duration {
1287 if (!THIS->lexer_p_->lyric_state_b ())
1288 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1289 $$ = THIS->get_word_element (*$1, $2);
1299 $$ = new Array<Musical_pitch>;
1301 | pitch_list musical_pitch {
1323 int *i_p = $1->access_int ();
1333 | STRING_IDENTIFIER {
1334 String *s_p = $1->access_String ();
1337 DOUT << "Lookup string: " << *s_p << "\n";
1345 | string '+' string_primary {
1352 real_primary unit { $$ = $1*$2; }
1356 unit: CM_T { $$ = 1 CM; }
1357 |IN_T { $$ = 1 INCH; }
1358 |MM_T { $$ = 1 MM; }
1359 |PT_T { $$ = 1 PT; }
1366 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1374 $$ = $1->access_Lookup ();
1376 | symtables_body FONT STRING {
1378 $$->font_path_ = global_path.find (*$3);
1379 if (!$$->font_path_.length_i ())
1380 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1384 | symtables_body STRING '=' symtable {
1391 TABLE '{' symtable_body '}' { $$ = $3; }
1395 { $$ = new Symtable; }
1396 | symtable_body STRING symboldef {
1405 $$ = new Atom (*$1, *$2);
1410 Box b (Interval (0,0), Interval (0,0));
1411 $$ = new Atom (*$1, b);
1417 dinterval dinterval {
1418 $$ = new Box (*$1, *$2);
1424 dinterval: dim dim {
1425 $$ = new Interval ($1, $2);
1432 My_lily_parser::set_yydebug (bool b)
1439 My_lily_parser::do_yyparse ()
1441 yyparse ((void*)this);