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 <jan@digicash.com>
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"
26 #include "parseconstruct.hh"
28 #include "identifier.hh"
29 #include "command-request.hh"
30 #include "musical-request.hh"
31 #include "my-lily-parser.hh"
32 #include "text-def.hh"
33 #include "translator-group.hh"
35 #include "music-list.hh"
37 #include "duration-convert.hh"
38 #include "change-translator.hh"
39 #include "file-results.hh"
40 #include "mudela-version.hh"
43 Mudela_version oldest_version ("0.1.8");
44 Mudela_version version ("0.1.13");
47 // needed for bison.simple's malloc() and free()
50 int const GUESS_PLET = 5;
51 int guess_plet_a[GUESS_PLET] =
60 Paper_def* current_paper = 0;
66 #define YYERROR_VERBOSE 1
68 #define YYPARSE_PARAM my_lily_parser_l
69 #define YYLEX_PARAM my_lily_parser_l
70 #define THIS ((My_lily_parser *) my_lily_parser_l)
72 #define yyerror THIS->parser_error
78 Array<Interval>* intarr;
79 Array<Melodic_req*> *melreqvec;/* should clean up naming */
80 Array<String> * strvec;
88 Music_list *musiclist;
95 Music_output_def * outputdef;
102 General_script_def * script;
107 Symtables * symtables;
119 yylex (YYSTYPE *s, void * v_l)
121 My_lily_parser *pars_l = (My_lily_parser*) v_l;
122 My_lily_lexer * lex_l = pars_l->lexer_p_;
124 lex_l->lexval_l = (void*) s;
125 return lex_l->yylex ();
133 /* tokens which are not keywords */
158 %token MELODIC_REQUEST
190 %token E_EXCLAMATION E_SMALLER E_BIGGER E_CHAR
194 %token <melreq> NOTENAME_ID
195 %token <id> DURATION_IDENTIFIER
196 %token <id> IDENTIFIER
197 %token <id> MELODIC_REQUEST_IDENTIFIER
198 %token <id> MUSIC_IDENTIFIER
199 %token <id> VOICE_IDENTIFIER
200 %token <id> POST_REQUEST_IDENTIFIER
201 %token <id> SCRIPT_IDENTIFIER
202 %token <id> COMMAND_IDENTIFIER
203 %token <id> REAL_IDENTIFIER
204 %token <id> TRANS_IDENTIFIER
205 %token <id> INT_IDENTIFIER
206 %token <id> SCORE_IDENTIFIER
207 %token <id> MIDI_IDENTIFIER
208 %token <id> PAPER_IDENTIFIER
209 %token <id> REQUEST_IDENTIFIER
211 %token <string> DURATION RESTNAME
212 %token <string> STRING
214 %token <i> POST_QUOTES
215 %token <i> PRE_QUOTES
217 %type <outputdef> output_def
218 %type <header> mudela_header mudela_header_body
220 %type <i> open_request_parens close_request_parens
221 %type <i> open_abbrev_parens
222 %type <i> open_plet_parens close_plet_parens
223 %type <i> post_quotes pre_quotes
224 %type <music> simple_element music_elt full_element lyrics_elt command_elt
225 %type <i> abbrev_type
226 %type <i> int unsigned
228 %type <id> identifier_init
229 %type <duration> explicit_steno_duration notemode_duration
230 %type <duration> entered_notemode_duration explicit_duration
231 %type <interval> dinterval
232 %type <intvec> intastint_list
233 %type <lookup> symtables symtables_body
234 %type <melreq> melodic_request steno_melodic_req
235 %type <notereq> steno_note_req
236 %type <melreqvec> pitch_list
237 %type <midi> midi_block midi_body
238 %type <moment> duration_length
240 %type <scalar> scalar
241 %type <music> Music transposed_music
242 %type <music> property_def translator_change
243 %type <musiclist> Voice Voice_body
244 %type <chord> Chord Chord_body
245 %type <paper> paper_block paper_body
246 %type <real> dim real real_expression
247 %type <real> real_add_expression real_mult_expression real_primary
249 %type <request> abbrev_command_req
250 %type <request> post_request structured_post_request
251 %type <pair> plet_fraction
252 %type <request> command_req verbose_command_req
253 %type <request> script_req dynamic_req
254 %type <score> score_block score_body
255 %type <intarr> shape_array
256 %type <script> script_definition script_body mudela_script gen_script_def
257 %type <textdef> text_def finger
258 %type <string> script_abbreviation
259 %type <symbol> symboldef
260 %type <symtable> symtable symtable_body
261 %type <trans> translator_spec translator_spec_body
262 %type <tempo> tempo_request
263 %type <string> concat_strings
271 | mudela mudela_header {
272 delete THIS->default_header_p_ ;
273 THIS->default_header_p_ = $2;
275 | mudela score_block {
276 score_global_array.push ($2);
278 | mudela add_declaration { }
280 | mudela check_version { }
281 | mudela add_notenames { }
286 Mudela_version ver (*$2);
287 if (!((ver >= oldest_version) && (ver <= version))) {
288 if (THIS->ignore_version_b_) {
289 THIS->here_input ().error ("Incorrect mudela version");
291 THIS->fatal_error_i_ = 1;
292 THIS->parser_error ("Incorrect mudela version");
299 NOTENAMES '{' notenames_body '}'
304 | notenames_body CLEAR {
305 THIS->clear_notenames ();
307 | notenames_body STRING '=' melodic_request {
308 THIS->add_notename (*$2, $4);
317 | mudela_header_body STRING '=' concat_strings ';' {
325 HEADER '{' mudela_header_body '}' {
335 | concat_strings STRING {
346 THIS->remember_spot ();
348 /* cont */ '=' identifier_init {
349 THIS->lexer_p_->set_identifier (*$1, $4);
350 $4->init_b_ = THIS->init_parse_b_;
351 $4->set_spot (THIS->pop_spot ());
357 $$ = new Score_id ($1, SCORE_IDENTIFIER);
361 $$ = new Paper_def_id ($1, PAPER_IDENTIFIER);
365 $$ = new Midi_def_id ($1, MIDI_IDENTIFIER);
368 | script_definition {
369 $$ = new Script_id ($1, SCRIPT_IDENTIFIER);
373 $$ = new Music_id ($1, MUSIC_IDENTIFIER);
377 $$ = new Lookup_id ($1, IDENTIFIER);
381 $$ = new Real_id (new Real ($1), REAL_IDENTIFIER);
384 $$ = new Int_id (new int ($1), INT_IDENTIFIER);
387 $$ = new Request_id ($1, POST_REQUEST_IDENTIFIER);
390 $$ = new Request_id ($1, MELODIC_REQUEST_IDENTIFIER);
394 $$ = new Translator_id ($1, TRANS_IDENTIFIER);
396 | explicit_duration {
397 $$ = new Duration_id ($1, DURATION_IDENTIFIER);
402 TRANSLATOR '{' translator_spec_body '}'
406 translator_spec_body:
408 $$ = $1->translator ();
409 $$-> set_spot (THIS->here_input ());
412 $$ = get_translator_l (*$2)->clone ();
413 $$->set_spot (THIS->here_input ());
416 | translator_spec_body STRING '=' scalar ';' {
417 $$-> set_property (*$2, *$4);
421 | translator_spec_body CONSISTS STRING ';' {
422 $$->group_l ()->consists_str_arr_.push (*$3);
425 | translator_spec_body ACCEPTS STRING ';' {
426 $$->group_l ()->accepts_str_arr_.push (*$3);
435 SCORE { THIS->remember_spot ();
436 THIS->error_level_i_ =0;
438 /*cont*/ '{' score_body '}' {
440 $$->set_spot (THIS->pop_spot ());
441 if (!$$->def_p_arr_.size ())
442 $$->add (THIS->default_paper ());
444 /* handle error levels. */
445 $$->errorlevel_i_ = THIS->error_level_i_;
446 THIS->error_level_i_ = 0;
447 if (!$$->header_p_ && THIS->default_header_p_)
448 $$->header_p_ = new Header (*THIS->default_header_p_);
458 | score_body mudela_header {
463 $2->warning ("More than one music block");
466 | score_body output_def {
484 /* */ { $$ =new Array<int>; }
485 | intastint_list int '*' int {
486 $$->push ($2); $$->push ($4);
496 '{' paper_body '}' { $$ = $3; }
506 $$ = THIS->default_paper ();
508 | PAPER_IDENTIFIER optional_semicolon {
509 $$ = $1->paperdef ();
511 | paper_body OUTPUT STRING ';' {
512 $$->outfile_str_ = *$3;
515 | paper_body symtables { $$->set ($2); }
516 | paper_body STRING '=' real_expression ';' {
517 $$->set_var (*$2, $4);
521 | paper_body STRING '=' translator_spec {
522 $$-> assign_translator (*$2, $4);
525 | paper_body SHAPE '=' shape_array ';' {
526 $$->shape_int_a_ = *$4;
539 // $$ = THIS->default_paper ()->get_var (*$1);
540 $$ = current_paper->get_var (*$1);
542 | '(' real_expression ')' {
553 | real_add_expression '+' real_mult_expression {
556 | real_add_expression '-' real_mult_expression {
561 real_mult_expression:
563 | real_mult_expression '*' real_primary {
566 | real_mult_expression '/' real_primary {
573 $$ = new Array<Interval>;
575 | shape_array dim dim {
576 $$->push(Interval($2, $2 + $3));
585 '{' midi_body '}' { $$ = $3; }
588 midi_body: /* empty */ {
589 $$ = THIS->default_midi ();
591 | midi_body STRING '=' translator_spec {
592 $$-> assign_translator (*$2, $4);
595 | midi_body OUTPUT STRING ';' {
596 $$->outfile_str_ = *$3;
599 | midi_body tempo_request ';' {
600 $$->set_tempo ($2->dur_.length (), $2->metronome_i_);
609 TEMPO entered_notemode_duration '=' unsigned {
613 $$-> metronome_i_ = $4;
630 $$->set_spot (THIS->here_input ());
638 full_element { $$ = $1; }
639 | TYPE STRING Music {
641 $$->translator_type_str_ = *$2;
644 | TYPE STRING '=' STRING Music {
646 $$->translator_type_str_ = *$2;
647 $$->translator_id_str_ = *$4;
653 | transposed_music { $$ = $1; }
654 | MUSIC_IDENTIFIER { $$ = $1->music (); }
655 | MUSIC_IDENTIFIER ';' { $$ = $1->music (); }
657 { THIS->lexer_p_->push_note_state (); }
659 { $$=$3; THIS->lexer_p_->pop_state (); }
662 { THIS->lexer_p_->push_lyric_state (); }
664 { $$ = $3; THIS->lexer_p_->pop_state (); }
670 TRANSLATOR STRING '=' STRING {
671 Change_translator * t = new Change_translator;
672 t-> change_to_type_str_ = *$2;
673 t-> change_to_id_str_ = *$4;
676 $$->set_spot (THIS->here_input ());
683 PROPERTY STRING '.' STRING '=' scalar {
684 Translation_property *t = new Translation_property;
685 t-> translator_type_str_ = *$2;
689 $$->set_spot (THIS->here_input ());
697 STRING { $$ = new Scalar (*$1); delete $1; }
698 | int { $$ = new Scalar ($1); }
703 '<' Chord_body '>' { $$ = $2; }
704 | MULTI unsigned Chord {
706 $$->multi_level_i_=$2;
713 $$-> multi_level_i_ = 1;
714 $$->set_spot (THIS->here_input ());
722 TRANSPOSE steno_melodic_req Music {
724 $$ -> transpose ($2);
735 pre_requests simple_element post_requests {
736 THIS->add_requests ((Chord*)$2);//ugh
740 | voice_command ';' { $$ = 0; }
750 $$ = new Request_chord;
751 $$-> set_spot (THIS->here_input ());
752 $1-> set_spot (THIS->here_input ());
753 ((Chord*)$$) ->add ($1);//ugh
760 | verbose_command_req ';' { $$ = $1; }
765 $$ = new Barcheck_req;
767 | COMMAND_IDENTIFIER {
774 $$ = new Bar_req (*$2);
778 Break_req * b = new Break_req;
780 b-> set_spot (THIS->here_input ());
783 | METER unsigned '/' unsigned {
784 Meter_change_req *m = new Meter_change_req;
788 | SKIP duration_length {
789 Skip_req * skip_p = new Skip_req;
790 skip_p->duration_.set_plet ($2->num (),
800 $$ = new Cadenza_req ($2);
802 | PARTIAL duration_length {
803 $$ = new Partial_measure_req (*$2);
807 $$ = new Clef_change_req (*$2);
811 Key_change_req *key_p= new Key_change_req;
812 key_p->melodic_p_arr_ = *$2;
816 | GROUPING intastint_list {
817 $$ = get_grouping_req (*$2); delete $2;
823 /* something silly happened. Junk this stuff*/
824 if (!THIS->post_reqs.empty ())
826 warning ("Junking post-requests");
827 THIS->post_reqs.clear ();
830 | post_requests structured_post_request {
831 $2->set_spot (THIS->here_input ());
832 THIS->post_reqs.push ($2);
834 | post_requests close_request_parens {
835 Array<Request*>& r = *THIS->get_parens_request ($2);
836 for (int i = 0; i < r.size (); i++ )
837 r[i]->set_spot (THIS->here_input ());
838 THIS->post_reqs.concat (r);
843 structured_post_request:
849 POST_REQUEST_IDENTIFIER {
850 $$ = (Request*)$1->request ();
856 Abbreviation_req* a = new Abbreviation_req;
867 | pre_quotes PRE_QUOTES {
878 | post_quotes POST_QUOTES {
887 Whitespace mustn't be stripped. So what?
888 Python is cool, and there even the amount *and* type of whitespace
889 is significant. So this is not uncool per se, maybe context-free
890 just sucks for humans.
895 Melodic_req* m = $1->clone ()->musical ()->melodic ();
896 $$ = THIS->get_melodic_req (m, 0 + THIS->default_octave_i_);
898 | NOTENAME_ID post_quotes {
899 Melodic_req* m = $1->clone ()->musical ()->melodic ();
900 $$ = THIS->get_melodic_req (m, $2 + THIS->default_octave_i_);
902 | pre_quotes NOTENAME_ID {
903 Melodic_req* m = $2->clone ()->musical ()->melodic ();
904 $$ = THIS->get_melodic_req (m, -$1 + THIS->default_octave_i_);
911 *(Melodic_req *) $$ = *$1;
914 | steno_note_req '!' {
915 $$->forceacc_b_ = ! $$->forceacc_b_;
917 /* have to duration here. */
921 MELODIC_REQUEST '{' int int int '}' {/* ugh */
922 $$ = new Melodic_req;
924 $$->notename_i_ = $4;
925 $$->accidental_i_ = $5;
930 DURATION '{' int unsigned '}' {
938 ABSDYNAMIC '{' unsigned '}' {
939 Absolute_dynamic_req *ad_p = new Absolute_dynamic_req;
940 ad_p ->loudness_ = (Dynamic_req::Loudness)$3;
943 | SPANDYNAMIC '{' int int '}' {
944 Span_dynamic_req * sp_p = new Span_dynamic_req;
945 sp_p->spantype = (Span_req::Spantype)$4;
946 sp_p-> dynamic_dir_ = (Direction)$3;
952 unsigned '/' unsigned {
959 $$[0] = guess_plet_a[(num <? GUESS_PLET) - 1];
967 THIS->plet_.type_i_ = $2[1];
968 THIS->plet_.iso_i_ = $2[0];
969 THIS->default_duration_.plet_.type_i_ = $2[1];
970 THIS->default_duration_.plet_.iso_i_ = $2[0];
974 THIS->plet_.type_i_ = 1;
975 THIS->plet_.iso_i_ = 1;
976 THIS->default_duration_.plet_.iso_i_ = 1;
977 THIS->default_duration_.plet_.type_i_ = 1;
979 | TELP plet_fraction {
981 THIS->plet_.type_i_ = $2[1];
982 THIS->plet_.iso_i_ = $2[0];
983 THIS->default_duration_.plet_.type_i_ = $2[1];
984 THIS->default_duration_.plet_.iso_i_ = $2[0];
988 close_request_parens:
1010 if (!Duration::duration_type_b ($3))
1011 THIS->parser_error (String ("Not a duration: ") + $3);
1013 THIS->parser_error ("Can't abbreviate");
1015 THIS->set_abbrev_beam ($3);
1022 THIS->plet_.type_i_ = $2[1];
1023 THIS->plet_.iso_i_ = $2[0];
1024 THIS->default_duration_.plet_.type_i_ = $2[1];
1025 THIS->default_duration_.plet_.iso_i_ = $2[0];
1027 | PLET plet_fraction {
1029 THIS->plet_.type_i_ = $2[1];
1030 THIS->plet_.iso_i_ = $2[0];
1031 THIS->default_duration_.plet_.type_i_ = $2[1];
1032 THIS->default_duration_.plet_.iso_i_ = $2[0];
1036 open_request_parens:
1046 | open_abbrev_parens
1053 SCRIPT '{' script_body '}' { $$ = $3; }
1057 STRING int int int int int {
1058 Script_def *s = new Script_def;
1059 s->set_from_input (*$1,$2, $3,$4,$5, $6);
1066 script_dir gen_script_def {
1067 Musical_script_req *m = new Musical_script_req;
1069 m->scriptdef_p_ = $2;
1070 m->set_spot (THIS->here_input ());
1072 m->dir_ = (Direction)$1;
1079 ((Text_def*) $$)->align_i_ = CENTER; /* UGH */
1083 $$-> set_spot (THIS->here_input ());
1087 ((Text_def*)$$)->align_i_ = CENTER;
1093 Text_def *t = new Text_def;
1097 t->style_str_ = THIS->textstyle_str_;
1098 $$->set_spot (THIS->here_input ());
1104 Text_def* t = new Text_def;
1106 t->text_str_ = String ($1);
1107 t->style_str_ = "finger";
1108 $$->set_spot (THIS->here_input ());
1112 script_abbreviation:
1113 '^' { $$ = get_scriptdef ('^'); }
1114 | '+' { $$ = get_scriptdef ('+'); }
1115 | '-' { $$ = get_scriptdef ('-'); }
1116 | '|' { $$ = get_scriptdef ('|'); }
1117 | 'o' { $$ = get_scriptdef ('o'); }
1118 | '>' { $$ = get_scriptdef ('>'); }
1120 $$ = get_scriptdef ('.');
1125 SCRIPT_IDENTIFIER { $$ = $1->script (); }
1126 | script_definition { $$ = $1; }
1127 | script_abbreviation {
1128 $$ = THIS->lexer_p_->lookup_identifier (*$1)->script ();
1142 | pre_requests open_request_parens {
1143 Array<Request*>& r = *THIS->get_parens_request ($2);
1144 for (int i = 0; i < r.size (); i++ )
1145 r[i]->set_spot (THIS->here_input ());
1146 THIS->pre_reqs.concat (r);
1153 THIS->set_duration_mode (*$2);
1156 | DURATION entered_notemode_duration {
1157 THIS->set_default_duration ($2);
1162 This is weird, but default_octave_i_
1163 is used in steno_note_req too
1165 c' -> default_octave_i_ == 1
1167 /* why can't we have \oct 0 iso \oct{c'}*/
1168 THIS->default_octave_i_ = 1; }
1171 THIS->default_octave_i_ = $3->octave_i_;
1175 THIS->set_octave_mode (*$2);
1178 | TEXTSTYLE STRING {
1179 THIS->textstyle_str_ = *$2;
1186 $$ = new Moment (0,1);
1188 | duration_length explicit_steno_duration {
1189 *$$ += $2->length ();
1195 | dots '.' { $$ ++; }
1198 entered_notemode_duration:
1200 $$ = new Duration (THIS->default_duration_);
1201 // get sticky plet part too
1202 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1205 $$ = new Duration (THIS->default_duration_);
1206 // get sticky plet part too
1207 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1210 | explicit_steno_duration {
1211 THIS->set_last_duration ($1);
1217 entered_notemode_duration {
1222 explicit_steno_duration:
1225 if (!Duration::duration_type_b ($1))
1226 THIS->parser_error ("Not a duration:" + String ($1));
1228 $$->durlog_i_ = Duration_convert::i2_type ($1);
1229 $$->set_plet (THIS->plet_.iso_i_, THIS->plet_.type_i_);
1232 | DURATION_IDENTIFIER {
1233 $$ = $1->duration ();
1235 | explicit_steno_duration '.' {
1238 | explicit_steno_duration '*' unsigned {
1239 $$->plet_.iso_i_ = $3; /* ugh. should do *= */
1241 | explicit_steno_duration '/' unsigned {
1242 $$->plet_.type_i_ = $3; /* ugh. should do *= iso = */
1252 if (!Duration::duration_type_b ($2))
1253 THIS->parser_error ("Not a duration:" + String ($2));
1255 THIS->parser_error ("Can't abbreviate");
1262 steno_note_req 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 Chord*velt_p = new Request_chord;
1279 velt_p->set_spot (THIS->here_input ());
1287 text_def notemode_duration {
1288 /* this sux! text-def should be feature of lyric-engraver. */
1289 if (!THIS->lexer_p_->lyric_state_b ())
1290 THIS->parser_error ("Have to be in Lyric mode for lyrics");
1291 $$ = THIS->get_word_element ($1, $2);
1299 $$ = new Array<Melodic_req*>;
1301 | pitch_list NOTENAME_ID {
1302 $$->push ($2->clone ()->musical ()->melodic ());
1322 int *i_p = $1->intid ();
1333 Real *r_p = $1->real ();
1342 real unit { $$ = $1*$2; }
1346 unit: CM_T { $$ = 1 CM; }
1347 |IN_T { $$ = 1 INCH; }
1348 |MM_T { $$ = 1 MM; }
1349 |PT_T { $$ = 1 PT; }
1356 SYMBOLTABLES '{' symtables_body '}' { $$ = $3; }
1366 | symtables_body TEXID STRING {
1367 $$->texsetting = *$3;
1370 | symtables_body STRING '=' symtable {
1377 TABLE '{' symtable_body '}' { $$ = $3; }
1381 { $$ = new Symtable; }
1382 | symtable_body STRING symboldef {
1391 $$ = new Atom (*$1, *$2);
1396 Box b (Interval (0,0), Interval (0,0));
1397 $$ = new Atom (*$1, b);
1403 dinterval dinterval {
1404 $$ = new Box (*$1, *$2);
1410 dinterval: dim dim {
1411 $$ = new Interval ($1, $2);
1418 My_lily_parser::set_yydebug (bool b)
1425 My_lily_parser::do_yyparse ()
1427 yyparse ((void*)this);
1431 My_lily_parser::default_paper ()
1433 Identifier *id = lexer_p_->lookup_identifier ("default_paper");
1434 return id ? id->paperdef () : new Paper_def ;
1438 My_lily_parser::default_midi ()
1440 Identifier *id = lexer_p_->lookup_identifier ("default_midi");
1441 return id ? id->mididef () : new Midi_def ;