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 "dimension.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 */
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 <symtables> 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(true);
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 Symtables_identifier ($1, IDENTIFIER);
401 $$ = new Request_identifier ($1, POST_REQUEST_IDENTIFIER);
403 | explicit_duration {
404 $$ = new Duration_identifier ($1, DURATION_IDENTIFIER);
407 $$ = new Real_identifier (new Real ($1), REAL_IDENTIFIER);
410 $$ = new String_identifier ($1, STRING_IDENTIFIER);
413 $$ = new int_identifier (new int ($1), INT_IDENTIFIER);
416 $$ = new Translator_identifier ($1, TRANS_IDENTIFIER);
421 TRANSLATOR '{' translator_spec_body '}'
425 translator_spec_body:
427 $$ = $1->access_Translator (true);
428 $$-> set_spot (THIS->here_input ());
431 Translator* t = get_translator_l (*$2);
432 Translator_group * tg = t->access_Translator_group ();
435 THIS->parser_error (_("Need a translator group for a context"));
438 t->set_spot (THIS->here_input ());
442 | translator_spec_body STRING '=' simple_identifier_init ';' {
444 String_identifier *s = id->access_String_identifier ();
445 Real_identifier *r= id->access_Real_identifier ();
446 int_identifier *i = id->access_int_identifier ();
449 if (s) str = *s->access_String (false);
450 if (i) str = to_str (*i->access_int (false));
451 if (r) str = to_str (*r->access_Real (false));
453 THIS->parser_error (_("Wrong type for property value"));
456 $$->set_property (*$2, str);
458 | translator_spec_body STRING ';' {
462 | translator_spec_body CONSISTS STRING ';' {
463 $$->access_Translator_group ()-> set_element (*$3, true);
466 | translator_spec_body ACCEPTS STRING ';' {
467 $$->access_Translator_group ()-> set_acceptor (*$3, true);
470 | translator_spec_body REMOVE STRING ';' {
471 $$->access_Translator_group ()-> set_element (*$3, false);
480 SCORE { THIS->remember_spot ();
481 THIS->error_level_i_ =0;
483 /*cont*/ '{' score_body '}' {
485 $$->set_spot (THIS->pop_spot ());
486 if (!$$->def_p_arr_.size ())
487 $$->add_output (THIS->default_paper_p ());
489 /* handle error levels. */
490 $$->errorlevel_i_ = THIS->error_level_i_;
491 THIS->error_level_i_ = 0;
499 $$ = $1->access_Score (true);
501 | score_body mudela_header {
506 $2->warning (_ ("More than one music block"));
509 | score_body output_def {
527 /* */ { $$ =new Array<int>; }
528 | intastint_list int '*' int {
529 $$->push ($2); $$->push ($4);
531 | intastint_list int {
532 $$->push ($2); $$->push (1);
541 PAPER '{' paper_def_body '}' {
543 THIS-> lexer_p_->scope_l_arr_.pop ();
554 Paper_def *p = THIS->default_paper_p ();
555 THIS-> lexer_p_-> scope_l_arr_.push (p->scope_p_);
558 | PAPER_IDENTIFIER optional_semicolon {
559 Paper_def *p = $1->access_Paper_def (true);
560 THIS->lexer_p_->scope_l_arr_.push (p->scope_p_);
563 | paper_def_body int '=' symtables { // ugh, what a syntax
564 Lookup * l = ps_output_global_b ? new Ps_lookup (*$4)
565 : new Tex_lookup (*$4);
566 $$->set_lookup ($2, l);
568 | paper_def_body STRING '=' simple_identifier_init ';' {
569 THIS->lexer_p_->set_identifier (*$2, $4);
572 | paper_def_body translator_spec {
573 $$-> assign_translator ($2);
575 | paper_def_body SHAPE '=' shape_array ';' {
576 $$->shape_int_a_ = *$4;
579 | paper_def_body error {
589 $$= *$1->access_Real (false);
599 | real '+' real_mult_expression {
602 | real '-' real_mult_expression {
607 real_mult_expression:
609 | real_mult_expression '*' real_primary {
612 | real_mult_expression '/' real_primary {
619 $$ = new Array<Interval>;
621 | shape_array dim dim {
622 $$->push(Interval($2, $2 + $3));
631 '{' midi_body '}' { $$ = $3; }
634 midi_body: /* empty */ {
635 $$ = THIS->default_midi_p ();
638 $$ = $1-> access_Midi_def (true);
640 | midi_body translator_spec {
641 $$-> assign_translator ($2);
643 | midi_body tempo_request ';' {
644 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
653 TEMPO entered_notemode_duration '=' unsigned {
657 $$-> metronome_i_ = $4;
661 Music_list: /* empty */ {
672 Sequential_music: '{' Music_list '}' {
673 $$ = new Sequential_music ($2);
677 Simultaneous_music: '<' Music_list '>' {
678 $$ = new Simultaneous_music ($2);
682 request_chord { $$ = $1; }
683 | TYPE STRING Music {
685 $$->translator_type_str_ = *$2;
688 | TYPE STRING '=' STRING Music {
690 $$->translator_type_str_ = *$2;
691 $$->translator_id_str_ = *$4;
695 | Simultaneous_music { $$ = $1; }
696 | Sequential_music { $$ = $1; }
697 | TRANSPOSE musical_pitch Music {
698 $$ = new Transposed_music ($3, *$2);
701 | MUSIC_IDENTIFIER { $$ = $1->access_Music (true); }
703 { THIS->lexer_p_->push_note_state (); }
706 THIS->lexer_p_->pop_state ();
710 { THIS->lexer_p_->push_lyric_state (); }
714 THIS->lexer_p_->pop_state ();
718 | relative_music { $$ = $1; }
722 RELATIVE absolute_musical_pitch Music {
723 $$ = new Relative_octave_music ($3, *$2);
729 TRANSLATOR STRING '=' STRING {
730 Change_translator * t = new Change_translator;
731 t-> change_to_type_str_ = *$2;
732 t-> change_to_id_str_ = *$4;
735 $$->set_spot (THIS->here_input ());
742 PROPERTY STRING '.' STRING '=' scalar {
743 Translation_property *t = new Translation_property;
744 t-> translator_type_str_ = *$2;
748 $$->set_spot (THIS->here_input ());
756 STRING { $$ = new Scalar (*$1); delete $1; }
757 | int { $$ = new Scalar ($1); }
763 pre_requests simple_element post_requests {
764 THIS->add_requests ((Simultaneous_music*)$2);//ugh
775 $$ = new Request_chord;
776 $$-> set_spot (THIS->here_input ());
777 $1-> set_spot (THIS->here_input ());
778 ((Simultaneous_music*)$$) ->add_music ($1);//ugh
785 | verbose_command_req ';' { $$ = $1; }
790 $$ = new Barcheck_req;
792 | COMMAND_IDENTIFIER {
793 $$ = $1->access_Request (true);
799 $$ = new Bar_req (*$2);
803 $$ = new Mark_req (*$2);
807 $$ = new Mark_req (to_str ($2));
809 | TIME_T unsigned '/' unsigned {
810 Time_signature_change_req *m = new Time_signature_change_req;
816 Break_req * b = new Break_req;
818 b-> set_spot (THIS->here_input ());
821 | SKIP duration_length {
822 Skip_req * skip_p = new Skip_req;
823 skip_p->duration_ = *$2;
831 $$ = new Cadenza_req ($2);
833 | PARTIAL duration_length {
834 $$ = new Partial_measure_req ($2->length ());
838 $$ = new Clef_change_req (*$2);
841 | KEY NOTENAME_PITCH {
842 Key_change_req *key_p= new Key_change_req;
843 key_p->pitch_arr_.push(*$2);
844 key_p->ordinary_key_b_ = true;
848 | KEYSIGNATURE pitch_list {
849 Key_change_req *key_p= new Key_change_req;
850 key_p->pitch_arr_ = *$2;
851 key_p->ordinary_key_b_ = false;
855 | GROUPING intastint_list {
856 $$ = get_grouping_req (*$2); delete $2;
862 /* something silly happened. Junk this stuff*/
863 if (!THIS->post_reqs.empty ())
865 warning ("Junking post-requests");
866 THIS->post_reqs.clear ();
869 | post_requests structured_post_request {
870 $2->set_spot (THIS->here_input ());
871 THIS->post_reqs.push ($2);
873 | post_requests close_request_parens {
874 Array<Request*>& r = *THIS->get_parens_request ($2);
875 for (int i = 0; i < r.size (); i++ )
876 r[i]->set_spot (THIS->here_input ());
877 THIS->post_reqs.concat (r);
882 structured_post_request:
888 POST_REQUEST_IDENTIFIER {
889 $$ = (Request*)$1->access_Request (true);
895 Abbreviation_req* a = new Abbreviation_req;
922 | NOTENAME_PITCH sup_quotes {
926 | NOTENAME_PITCH sub_quotes {
928 $$->octave_i_ += - $2;
932 explicit_musical_pitch:
933 MUSICAL_PITCH '{' int_list '}' {/* ugh */
936 $$ = new Musical_pitch;
937 $$->octave_i_ = a[0];
938 $$->notename_i_ = a[1];
939 $$->accidental_i_ = a[2];
946 | explicit_musical_pitch
956 | steno_notepitch '!' {
957 $$->forceacc_b_ = ! $$->forceacc_b_;
963 DURATION '{' int_list '}' {
968 $$-> durlog_i_ = a[0];
976 ABSDYNAMIC '{' unsigned '}' {
977 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
978 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
981 | SPANDYNAMIC '{' int int '}' {
982 Span_dynamic_req * sp_p = new Span_dynamic_req;
983 sp_p->spantype = (Span_req::Spantype)$4;
984 sp_p-> dynamic_dir_ = (Direction)$3;
990 unsigned '/' unsigned {
997 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
1005 THIS->plet_.type_i_ = $2[1];
1006 THIS->plet_.iso_i_ = $2[0];
1007 THIS->default_duration_.plet_ = THIS->plet_;
1011 THIS->plet_.type_i_ = 1;
1012 THIS->plet_.iso_i_ = 1;
1013 THIS->default_duration_.plet_ = THIS->plet_;
1015 | TELP plet_fraction {
1017 THIS->plet_.type_i_ = $2[1];
1018 THIS->plet_.iso_i_ = $2[0];
1019 THIS->default_duration_.plet_ = THIS->plet_;
1023 close_request_parens:
1045 if (!Duration::duration_type_b ($3))
1046 THIS->parser_error (_f ("not a duration: %d", $3));
1048 THIS->parser_error (_ ("can't abbreviate"));
1050 THIS->set_abbrev_beam ($3);
1057 THIS->plet_.type_i_ = $2[1];
1058 THIS->plet_.iso_i_ = $2[0];
1059 THIS->default_duration_.plet_ = THIS->plet_;
1061 | PLET plet_fraction {
1063 THIS->plet_.type_i_ = $2[1];
1064 THIS->plet_.iso_i_ = $2[0];
1065 THIS->default_duration_.plet_ = THIS->plet_;
1069 open_request_parens:
1079 | open_abbrev_parens
1086 SCRIPT '{' script_body '}' { $$ = $3; }
1090 STRING int int int int int {
1091 Script_def *s = new Script_def;
1092 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1099 script_dir gen_script_def {
1100 Musical_script_req *m = new Musical_script_req;
1102 m->scriptdef_p_ = $2;
1103 m->set_spot (THIS->here_input ());
1105 m->dir_ = (Direction)$1;
1112 ((Text_def*) $$)->align_dir_ = LEFT; /* UGH */
1116 $$-> set_spot (THIS->here_input ());
1120 ((Text_def*)$$)->align_dir_ = RIGHT; /* UGH */
1126 Text_def *t = new Text_def;
1130 $$->set_spot (THIS->here_input ());
1136 Text_def* t = new Text_def;
1138 t->text_str_ = to_str ($1);
1139 t->style_str_ = "finger";
1140 $$->set_spot (THIS->here_input ());
1144 script_abbreviation:
1145 '^' { $$ = get_scriptdef ('^'); }
1146 | '+' { $$ = get_scriptdef ('+'); }
1147 | '-' { $$ = get_scriptdef ('-'); }
1148 | '|' { $$ = get_scriptdef ('|'); }
1149 | 'o' { $$ = get_scriptdef ('o'); }
1150 | '>' { $$ = get_scriptdef ('>'); }
1152 $$ = get_scriptdef ('.');
1157 SCRIPT_IDENTIFIER { $$ = $1->access_General_script_def (true); }
1158 | script_definition { $$ = $1; }
1159 | script_abbreviation {
1160 $$ = THIS->lexer_p_->lookup_identifier (*$1)->access_General_script_def (true);
1174 | pre_requests open_request_parens {
1175 Array<Request*>& r = *THIS->get_parens_request ($2);
1176 for (int i = 0; i < r.size (); i++ )
1177 r[i]->set_spot (THIS->here_input ());
1178 THIS->pre_reqs.concat (r);
1183 absolute_musical_pitch:
1184 steno_musical_pitch {
1197 | dots '.' { $$ ++; }
1200 entered_notemode_duration:
1202 $$ = new Duration (THIS->default_duration_);
1205 $$ = new Duration (THIS->default_duration_);
1209 THIS->set_last_duration ($1);
1214 entered_notemode_duration {
1222 if (!Duration::duration_type_b ($1))
1223 THIS->parser_error (_f ("not a duration: %d", $1));
1225 $$->durlog_i_ = Duration_convert::i2_type ($1);
1226 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1229 | DURATION_IDENTIFIER {
1230 $$ = $1->access_Duration (true);
1232 | steno_duration '.' {
1235 | steno_duration '*' unsigned {
1236 $$->plet_.iso_i_ *= $3;
1238 | steno_duration '/' unsigned {
1239 $$->plet_.type_i_ *= $3;
1249 if (!Duration::duration_type_b ($2))
1250 THIS->parser_error (_f ("not a duration: %d", $2));
1252 THIS->parser_error (_ ("can't abbreviate"));
1260 steno_notepitch notemode_duration {
1261 if (!THIS->lexer_p_->note_state_b ())
1262 THIS->parser_error (_ ("have to be in Note mode for notes"));
1263 $1->set_duration (*$2);
1264 int durlog_i = $2->durlog_i_;
1265 $$ = THIS->get_note_element ($1, $2);
1267 | RESTNAME notemode_duration {
1268 $$ = THIS->get_rest_element (*$1, $2);
1269 delete $1; // delete notename
1271 | MEASURES notemode_duration {
1272 Multi_measure_rest_req* m = new Multi_measure_rest_req;
1276 Simultaneous_music*velt_p = new Request_chord;
1277 velt_p->set_spot (THIS->here_input ());
1278 velt_p->add_music (m);
1281 | STRING notemode_duration {
1282 if (!THIS->lexer_p_->lyric_state_b ())
1283 THIS->parser_error (_ ("have to be in Lyric mode for lyrics"));
1284 $$ = THIS->get_word_element (*$1, $2);
1294 $$ = new Array<Musical_pitch>;
1296 | pitch_list musical_pitch {
1328 $$ = *$1->access_int (false);
1336 | STRING_IDENTIFIER {
1337 $$ = $1->access_String (true);
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_Symtables (true);
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);