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"
39 #include "duration-convert.hh"
40 #include "change-translator.hh"
41 #include "file-results.hh"
42 #include "mudela-version.hh"
44 #include "relative-music.hh"
45 #include "transposed-music.hh"
46 #include "compressed-music.hh"
49 Mudela_version oldest_version ("1.0.7");
50 Mudela_version version ("1.0.8");
53 // needed for bison.simple's malloc() and free()
56 int const GUESS_PLET = 5;
57 int guess_plet_a[GUESS_PLET] =
77 Assignment (Assignment const&s)
79 name_p_ = new String (*s.name_p_);
80 id_p_ = s.id_p_->clone ();
84 Paper_def* current_paper = 0;
90 #define YYERROR_VERBOSE 1
92 #define YYPARSE_PARAM my_lily_parser_l
93 #define YYLEX_PARAM my_lily_parser_l
94 #define THIS ((My_lily_parser *) my_lily_parser_l)
96 #define yyerror THIS->parser_error
97 #define ARRAY_SIZE(a,s) if (a.size () != s) THIS->parser_error (_f("expecting %d arguments", s))
104 Array<Interval>* intarr;
105 Array<Musical_pitch> *pitch_arr;
106 Array<String> * strvec;
109 Simultaneous_music *chord;
114 Music_list *music_list;
119 Music_output_def * outputdef;
120 Musical_pitch * pitch;
124 Notename_table *notenametab;
128 General_script_def * script;
133 Symtables* symtables;
145 yylex (YYSTYPE *s, void * v_l)
147 My_lily_parser *pars_l = (My_lily_parser*) v_l;
148 My_lily_lexer * lex_l = pars_l->lexer_p_;
150 lex_l->lexval_l = (void*) s;
151 return lex_l->yylex ();
159 /* tokens which are not keywords */
215 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
219 %token <pitch> NOTENAME_PITCH
220 %token <id> DURATION_IDENTIFIER
221 %token <id> IDENTIFIER
222 %token <id> NOTENAME_TABLE_IDENTIFIER
223 %token <id> MUSIC_IDENTIFIER
224 %token <id> POST_REQUEST_IDENTIFIER
225 %token <id> SCRIPT_IDENTIFIER
226 %token <id> COMMAND_IDENTIFIER
227 %token <id> REAL_IDENTIFIER
228 %token <id> STRING_IDENTIFIER
229 %token <id> TRANS_IDENTIFIER
230 %token <id> INT_IDENTIFIER
231 %token <id> SCORE_IDENTIFIER
232 %token <id> MIDI_IDENTIFIER
233 %token <id> PAPER_IDENTIFIER
234 %token <id> REQUEST_IDENTIFIER
236 %token <string> DURATION RESTNAME
237 %token <string> STRING
241 %type <outputdef> output_def
242 %type <scope> mudela_header mudela_header_body
244 %type <i> open_request_parens close_request_parens
245 %type <i> open_abbrev_parens
246 %type <i> open_plet_parens close_plet_parens
247 %type <i> sub_quotes sup_quotes
248 %type <music> simple_element request_chord command_element Simple_music Composite_music
249 %type <i> abbrev_type
250 %type <i> int unsigned
252 %type <id> identifier_init simple_identifier_init block_identifier
253 %type <duration> steno_duration notemode_duration
254 %type <duration> entered_notemode_duration explicit_duration
255 %type <interval> dinterval
256 %type <intvec> intastint_list int_list
257 %type <symtables> symtables symtables_body
259 %type <pitch> explicit_musical_pitch steno_musical_pitch musical_pitch absolute_musical_pitch
260 %type <notereq> steno_notepitch
261 %type <pitch_arr> pitch_list
262 %type <midi> midi_block midi_body
263 %type <duration> duration_length
265 %type <scalar> scalar
266 %type <music> Music relative_music Sequential_music Simultaneous_music
267 %type <music> property_def translator_change
268 %type <music_list> Music_list
269 %type <paper> paper_block paper_def_body
270 %type <real> real_expression real dimension
271 %type <request> abbrev_command_req
272 %type <request> post_request structured_post_request
273 %type <pair> plet_fraction
274 %type <request> command_req verbose_command_req
275 %type <request> script_req dynamic_req
276 %type <string> string
277 %type <score> score_block score_body
278 %type <intarr> shape_array
279 %type <script> script_definition script_body mudela_script gen_script_def
280 %type <textdef> text_def finger
281 %type <string> script_abbreviation
282 %type <symbol> symboldef
283 %type <symtable> symtable symtable_body
284 %type <trans> translator_spec translator_spec_body
285 %type <tempo> tempo_request
286 %type <notenametab> notenames_body notenames_block
297 | mudela toplevel_expression {}
298 | mudela assignment { }
300 | mudela check_version { }
305 THIS->lexer_p_->set_notename_table ($1);
308 delete header_global_p;
309 header_global_p = $1;
312 score_global_array.push ($1);
315 Identifier * id = new
316 Paper_def_identifier ($1, PAPER_IDENTIFIER);
317 THIS->lexer_p_->set_identifier ("$defaultpaper", id)
320 Identifier * id = new
321 Midi_def_identifier ($1, MIDI_IDENTIFIER);
322 THIS->lexer_p_->set_identifier ("$defaultmidi", id)
328 Mudela_version ver (*$2);
329 if (!((ver >= oldest_version) && (ver <= version))) {
330 if (THIS->ignore_version_b_) {
331 THIS->here_input ().error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
333 THIS->fatal_error_i_ = 1;
334 THIS->parser_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), version.str ()));
342 NOTENAMES '{' notenames_body '}' { $$ = $3; }
349 $$ = new Notename_table;
351 | NOTENAME_TABLE_IDENTIFIER {
352 $$ = $1-> access_Notename_table(true);
354 | notenames_body STRING '=' explicit_musical_pitch {
365 THIS->lexer_p_-> scope_l_arr_.push ($$);
367 | mudela_header_body assignment ';' {
373 HEADER '{' mudela_header_body '}' {
375 THIS->lexer_p_-> scope_l_arr_.pop ();
385 THIS->remember_spot ();
387 /* cont */ '=' identifier_init {
388 THIS->lexer_p_->set_identifier (*$1, $4);
389 $4->init_b_ = THIS->init_parse_b_;
390 $4->set_spot (THIS->pop_spot ());
395 simple_identifier_init: identifier_init
404 $$ = new Score_identifier ($1, SCORE_IDENTIFIER);
408 $$ = new Notename_table_identifier ($1, NOTENAME_TABLE_IDENTIFIER);
411 $$ = new Paper_def_identifier ($1, PAPER_IDENTIFIER);
414 $$ = new Midi_def_identifier ($1, MIDI_IDENTIFIER);
418 $$ = new Symtables_identifier ($1, IDENTIFIER);
421 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
424 $$ = new Music_identifier ($1, MUSIC_IDENTIFIER);
428 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
430 | explicit_duration {
431 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
434 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
437 $$ = new String_identifier ($1, STRING_IDENTIFIER);
440 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
442 | script_definition {
443 $$ = new General_script_def_identifier ($1, SCRIPT_IDENTIFIER);
449 TRANSLATOR '{' translator_spec_body '}'
453 translator_spec_body:
455 $$ = $1->access_Translator (true);
456 $$-> set_spot (THIS->here_input ());
459 Translator* t = get_translator_l (*$2);
460 Translator_group * tg = t->access_Translator_group ();
463 THIS->parser_error (_("Need a translator group for a context"));
466 t->set_spot (THIS->here_input ());
470 | translator_spec_body STRING '=' simple_identifier_init ';' {
472 String_identifier *s = id->access_String_identifier ();
473 Real_identifier *r= id->access_Real_identifier ();
474 int_identifier *i = id->access_int_identifier ();
477 if (s) str = *s->access_String (false);
478 if (i) str = to_str (*i->access_int (false));
479 if (r) str = to_str (*r->access_Real (false));
481 THIS->parser_error (_("Wrong type for property value"));
484 $$->set_property (*$2, str);
486 | translator_spec_body NAME STRING ';' {
490 | translator_spec_body CONSISTS STRING ';' {
491 $$->access_Translator_group ()-> set_element (*$3, true);
494 | translator_spec_body ACCEPTS STRING ';' {
495 $$->access_Translator_group ()-> set_acceptor (*$3, true);
498 | translator_spec_body REMOVE STRING ';' {
499 $$->access_Translator_group ()-> set_element (*$3, false);
508 SCORE { THIS->remember_spot ();
509 THIS->error_level_i_ =0;
511 /*cont*/ '{' score_body '}' {
513 $$->set_spot (THIS->pop_spot ());
514 if (!$$->def_p_arr_.size ())
515 $$->add_output (THIS->default_paper_p ());
517 /* handle error levels. */
518 $$->errorlevel_i_ = THIS->error_level_i_;
519 THIS->error_level_i_ = 0;
527 $$ = $1->access_Score (true);
529 | score_body mudela_header {
534 $2->warning (_ ("More than one music block"));
537 | score_body output_def {
555 /* */ { $$ =new Array<int>; }
556 | intastint_list int '*' int {
557 $$->push ($2); $$->push ($4);
559 | intastint_list int {
560 $$->push ($2); $$->push (1);
569 PAPER '{' paper_def_body '}' {
571 THIS-> lexer_p_->scope_l_arr_.pop ();
582 Paper_def *p = THIS->default_paper_p ();
583 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
586 | PAPER_IDENTIFIER optional_semicolon {
587 Paper_def *p = $1->access_Paper_def (true);
588 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
591 | paper_def_body int '=' symtables { // ugh, what a syntax
592 Lookup * l = global_lookup_l->lookup_p (*$4);
593 $$->set_lookup ($2, l);
595 | paper_def_body assignment ';' {
598 | paper_def_body translator_spec {
599 $$->assign_translator ($2);
601 | paper_def_body SHAPE '=' shape_array ';' {
602 $$->shape_int_a_ = *$4;
605 | paper_def_body error {
612 real_expression { $$ = $1; }
637 $$= *$1->access_Real (false);
639 | '-' real_expression %prec UNARY_MINUS {
642 | real_expression '*' real_expression {
645 | real_expression '/' real_expression {
648 | real_expression '+' real_expression {
651 | real_expression '-' real_expression {
654 | '(' real_expression ')' {
662 $$ = new Array<Interval>;
664 | shape_array real real {
665 $$->push(Interval($2, $2 + $3));
674 '{' midi_body '}' { $$ = $3; }
677 midi_body: /* empty */ {
678 $$ = THIS->default_midi_p ();
681 $$ = $1-> access_Midi_def (true);
683 | midi_body translator_spec {
684 $$-> assign_translator ($2);
686 | midi_body tempo_request ';' {
687 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
696 TEMPO entered_notemode_duration '=' unsigned {
700 $$-> metronome_i_ = $4;
704 Music_list: /* empty */ {
720 Sequential_music: '{' Music_list '}' {
721 $$ = new Sequential_music ($2);
725 Simultaneous_music: '<' Music_list '>' {
726 $$ = new Simultaneous_music ($2);
731 request_chord { $$ = $1; }
732 | MUSIC_IDENTIFIER { $$ = $1->access_Music (true); }
741 $$->translator_type_str_ = *$2;
744 | TYPE STRING '=' STRING Music {
746 $$->translator_type_str_ = *$2;
747 $$->translator_id_str_ = *$4;
751 | TIMES int '/' int Music {
752 $$ = new Compressed_music ($2, $4, $5);
755 | Simultaneous_music { $$ = $1; }
756 | Sequential_music { $$ = $1; }
757 | TRANSPOSE musical_pitch Music {
758 $$ = new Transposed_music ($3, *$2);
762 { THIS->lexer_p_->push_note_state (); }
765 THIS->lexer_p_->pop_state ();
769 { THIS->lexer_p_->push_lyric_state (); }
773 THIS->lexer_p_->pop_state ();
775 | relative_music { $$ = $1; }
779 RELATIVE absolute_musical_pitch Music {
780 $$ = new Relative_octave_music ($3, *$2);
786 TRANSLATOR STRING '=' STRING {
787 Change_translator * t = new Change_translator;
788 t-> change_to_type_str_ = *$2;
789 t-> change_to_id_str_ = *$4;
792 $$->set_spot (THIS->here_input ());
799 PROPERTY STRING '.' STRING '=' scalar {
800 Translation_property *t = new Translation_property;
801 t-> translator_type_str_ = *$2;
805 $$->set_spot (THIS->here_input ());
813 STRING { $$ = new Scalar (*$1); delete $1; }
814 | int { $$ = new Scalar ($1); }
819 pre_requests simple_element post_requests {
820 THIS->add_requests ((Simultaneous_music*)$2);//ugh
828 $$ = new Request_chord;
829 $$-> set_spot (THIS->here_input ());
830 $1-> set_spot (THIS->here_input ());
831 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
837 | verbose_command_req ';' { $$ = $1; }
842 $$ = new Barcheck_req;
844 | COMMAND_IDENTIFIER {
845 $$ = $1->access_Request (true);
850 $$->spantype = Span_req::START;
854 $$->spantype = Span_req::STOP;
862 $$ = new Bar_req (*$2);
866 $$ = new Mark_req (*$2);
870 $$ = new Mark_req (to_str ($2));
872 | TIME_T unsigned '/' unsigned {
873 Time_signature_change_req *m = new Time_signature_change_req;
879 Break_req * b = new Break_req;
881 b-> set_spot (THIS->here_input ());
884 | SKIP duration_length {
885 Skip_req * skip_p = new Skip_req;
886 skip_p->duration_ = *$2;
894 $$ = new Cadenza_req ($2);
896 | PARTIAL duration_length {
897 $$ = new Partial_measure_req ($2->length ());
901 $$ = new Clef_change_req (*$2);
904 | KEY NOTENAME_PITCH {
905 Key_change_req *key_p= new Key_change_req;
906 key_p->pitch_arr_.push(*$2);
907 key_p->ordinary_key_b_ = true;
911 | KEYSIGNATURE pitch_list {
912 Key_change_req *key_p= new Key_change_req;
913 key_p->pitch_arr_ = *$2;
914 key_p->ordinary_key_b_ = false;
918 | GROUPING intastint_list {
919 $$ = get_grouping_req (*$2); delete $2;
925 /* something silly happened. Junk this stuff*/
926 if (!THIS->post_reqs.empty ())
928 warning ("Junking post-requests");
929 THIS->post_reqs.clear ();
932 | post_requests structured_post_request {
933 $2->set_spot (THIS->here_input ());
934 THIS->post_reqs.push ($2);
936 | post_requests close_request_parens {
937 Array<Request*>& r = *THIS->get_parens_request ($2);
938 for (int i = 0; i < r.size (); i++ )
939 r[i]->set_spot (THIS->here_input ());
940 THIS->post_reqs.concat (r);
945 structured_post_request:
951 POST_REQUEST_IDENTIFIER {
952 $$ = (Request*)$1->access_Request (true);
958 Abbreviation_req* a = new Abbreviation_req;
985 | NOTENAME_PITCH sup_quotes {
989 | NOTENAME_PITCH sub_quotes {
991 $$->octave_i_ += - $2;
995 explicit_musical_pitch:
996 MUSICAL_PITCH '{' int_list '}' {/* ugh */
999 $$ = new Musical_pitch;
1000 $$->octave_i_ = a[0];
1001 $$->notename_i_ = a[1];
1002 $$->accidental_i_ = a[2];
1009 | explicit_musical_pitch
1019 | steno_notepitch '!' {
1020 $$->forceacc_b_ = ! $$->forceacc_b_;
1026 DURATION '{' int_list '}' {
1028 Array<int> &a = *$3;
1031 $$-> durlog_i_ = a[0];
1032 $$-> dots_i_ = a[1];
1039 ABSDYNAMIC '{' unsigned '}' {
1040 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
1041 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
1044 | SPANDYNAMIC '{' int int '}' {
1045 Span_dynamic_req * sp_p = new Span_dynamic_req;
1046 sp_p->spantype = (Span_req::Spantype)$4;
1047 sp_p-> dynamic_dir_ = (Direction)$3;
1053 unsigned '/' unsigned {
1060 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1068 THIS->plet_.type_i_ = $2[1];
1069 THIS->plet_.iso_i_ = $2[0];
1070 THIS->default_duration_.plet_ = THIS->plet_;
1074 THIS->plet_.type_i_ = 1;
1075 THIS->plet_.iso_i_ = 1;
1076 THIS->default_duration_.plet_ = THIS->plet_;
1078 | TELP plet_fraction {
1080 THIS->plet_.type_i_ = $2[1];
1081 THIS->plet_.iso_i_ = $2[0];
1082 THIS->default_duration_.plet_ = THIS->plet_;
1086 close_request_parens:
1108 if (!Duration::duration_type_b ($3))
1109 THIS->parser_error (_f ("not a duration: %d", $3));
1111 THIS->parser_error (_ ("can't abbreviate"));
1113 THIS->set_abbrev_beam ($3);
1120 THIS->plet_.type_i_ = $2[1];
1121 THIS->plet_.iso_i_ = $2[0];
1122 THIS->default_duration_.plet_ = THIS->plet_;
1124 | PLET plet_fraction {
1126 THIS->plet_.type_i_ = $2[1];
1127 THIS->plet_.iso_i_ = $2[0];
1128 THIS->default_duration_.plet_ = THIS->plet_;
1132 open_request_parens:
1142 | open_abbrev_parens
1149 SCRIPT '{' script_body '}' { $$ = $3; }
1153 STRING int int int int int {
1154 Script_def *s = new Script_def;
1155 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1162 script_dir gen_script_def {
1163 Musical_script_req *m = new Musical_script_req;
1165 m->scriptdef_p_ = $2;
1166 m->set_spot (THIS->here_input ());
1168 m->dir_ = (Direction)$1;
1175 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1179 $$-> set_spot (THIS->here_input ());
1183 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1189 Text_def *t = new Text_def;
1193 $$->set_spot (THIS->here_input ());
1199 Text_def* t = new Text_def;
1201 t->text_str_ = to_str ($1);
1202 t->style_str_ = "finger";
1203 $$->set_spot (THIS->here_input ());
1207 script_abbreviation:
1208 '^' { $$ = get_scriptdef ('^'); }
1209 | '+' { $$ = get_scriptdef ('+'); }
1210 | '-' { $$ = get_scriptdef ('-'); }
1211 | '|' { $$ = get_scriptdef ('|'); }
1212 | 'o' { $$ = get_scriptdef ('o'); }
1213 | '>' { $$ = get_scriptdef ('>'); }
1215 $$ = get_scriptdef ('.');
1220 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1221 | script_definition { $$ = $1; }
1222 | script_abbreviation {
1223 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1237 | pre_requests open_request_parens {
1238 Array<Request*>& r = *THIS->get_parens_request ($2);
1239 for (int i = 0; i < r.size (); i++ )
1240 r[i]->set_spot (THIS->here_input ());
1241 THIS->pre_reqs.concat (r);
1246 absolute_musical_pitch:
1247 steno_musical_pitch {
1260 | dots '.' { $$ ++; }
1263 entered_notemode_duration:
1265 $$ = new Duration (THIS->default_duration_);
1268 $$ = new Duration (THIS->default_duration_);
1272 THIS->set_last_duration ($1);
1277 entered_notemode_duration {
1285 if (!Duration::duration_type_b ($1))
1286 THIS->parser_error (_f ("not a duration: %d", $1));
1288 $$->durlog_i_ = Duration_convert::i2_type ($1);
1289 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1292 | DURATION_IDENTIFIER {
1293 $$ = $1->access_Duration (true);
1295 | steno_duration '.' {
1298 | steno_duration '*' unsigned {
1299 $$->plet_.iso_i_ *= $3;
1301 | steno_duration '/' unsigned {
1302 $$->plet_.type_i_ *= $3;
1312 if (!Duration::duration_type_b ($2))
1313 THIS->parser_error (_f ("not a duration: %d", $2));
1315 THIS->parser_error (_ ("can't abbreviate"));
1323 steno_notepitch notemode_duration {
1324 if (!THIS->lexer_p_->note_state_b ())
1325 THIS->parser_error (_ ("have to be in Note mode for notes"));
1326 $1->duration_ = *$2;
1327 $$ = THIS->get_note_element ($1, $2);
1329 | RESTNAME notemode_duration {
1330 $$ = THIS->get_rest_element (*$1, $2);
1331 delete $1; // delete notename
1333 | MEASURES notemode_duration {
1334 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1338 Simultaneous_music*velt_p = new Request_chord;
1339 velt_p->set_spot (THIS->here_input ());
1340 velt_p->add_music (m);
1343 | STRING notemode_duration {
1344 if (!THIS->lexer_p_->lyric_state_b ())
1345 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1346 $$ = THIS->get_word_element (*$1, $2);
1356 $$ = new Array<Musical_pitch>;
1358 | pitch_list musical_pitch {
1390 $$ = *$1->access_int (false);
1399 | STRING_IDENTIFIER {
1400 $$ = $1->access_String (true);
1402 | string '+' string {
1414 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1422 $$ = $1->access_Symtables (true);
1424 | symtables_body FONT STRING {
1426 $$->font_path_ = global_path.find (*$3);
1427 if (!$$->font_path_.length_i ())
1428 THIS->here_input ().error (_f("can't open file: `%s'", $3->ch_C()));
1432 | symtables_body STRING '=' symtable {
1439 TABLE '{' symtable_body '}' { $$ = $3; }
1443 { $$ = new Symtable; }
1444 | symtable_body STRING symboldef {
1445 $$->elem (*$2) = *$3;
1452 STRING unsigned box {
1453 $$ = global_lookup_l->atom_p (*$1, $2, *$3);
1458 Box b (Interval (0,0), Interval (0,0));
1459 $$ = global_lookup_l->atom_p (*$1, $2, b);
1465 dinterval dinterval {
1466 $$ = new Box (*$1, *$2);
1472 dinterval: real real {
1473 $$ = new Interval ($1, $2);
1480 My_lily_parser::set_yydebug (bool b)
1487 My_lily_parser::do_yyparse ()
1489 yyparse ((void*)this);