File: easylyparser.y

package info (click to toggle)
denemo 0.5.9-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,500 kB
  • ctags: 2,415
  • sloc: ansic: 23,057; sh: 3,321; yacc: 1,737; makefile: 449; lex: 376
file content (597 lines) | stat: -rw-r--r-- 17,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597



/* easylyparser.y
 * implements a parser and an ad-hoc lexer for the constrained
 * subset of mudela that Denemo produces */

/* (c) 2000 Matthew Hiller */
/* Tuplet code added by Yu Cheung "toby" Ho, adapted by Hiller */

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "calculatepositions.h"
#include "chordops.h"
#include "contexts.h"
#include "datastructures.h"
#include "lyparserfuncs.h"
#include "measureops.h"
#include "objops.h"
#include "scoreops.h"
#include "staffops.h"
#include "tupletops.h"

int lylex();
gint lyerror(gchar *);

  struct scoreinfo *lysi;
  FILE *lyin;   /* Global filehandle */
  mudelaobject *newmudelaobj = NULL;
  struct twoints prevduration;
  gint currentclef;
  staff *curstaffstruct;
  int lyinput (gchar *filename, struct scoreinfo *si);
  gint lyerror (char *s);
%}

%union {
  gint a;
  gchar c;
  gchar *strval;
  struct twoints t;
};

%token <strval> STAFFBEGIN
%token STAFFEND
%token <t> TIMESIGTOK
%token <strval> KEYSIGTOK
%token <strval> CLEFTOK
%token <strval> INSTRUMENT
%token BEGINCHORD
%token ENDCHORD
%token <c> TONETYPE
%token <a> ENSHIFT
%token <a> OCTIND
%token <a> BASEDURATION
%token <a> NUMDOTS
%token MINOR
%token MAJOR
%token TIE
%token BLINE
%token SLUR_START
%token SLUR_END
%token <t> BEGINTUPLET
%token ENDTUPLET
%token <a> STEMDIRECTIVETOK
%token RELOADDIRECTIVE
%token <strval> DYNAMICTOK

%type <t> keysig 
%type <t> duration
%type <strval> instrument
%type <a> reloaddirective

/* Grammar follows */
%%

staffs: /* empty */
| staff staffs
;

staff: STAFFBEGIN reloaddirective instrument TIMESIGTOK keysig CLEFTOK {
  lysi->currentstaffnum++;
  newstaff (lysi, ADDFROMLOAD, NULL);
  lysi->currentstaff = g_list_last (lysi->thescore);
  curstaffstruct = lysi->currentstaff->data;
  setstaffname (lysi, $1);
  if ($2)
    curstaffstruct->voicenumber = 2;
  setclef (lysi, $6);
  currentclef = curstaffstruct->sclef;
  setkey (lysi, $5);
  settime (lysi, $4);
  find_leftmost_staffcontext (lysi->currentstaff->data, lysi);
  g_string_assign (curstaffstruct->midi_instrument, $3);
  g_free ($3);
} measures
;

instrument: /* empty */ { $$ = g_strdup("acoustic grand") }
| INSTRUMENT { $$ = $1 }
;

reloaddirective: /* empty */ { $$ = FALSE }
| RELOADDIRECTIVE { $$ = TRUE }
;

measures: mudelaobjects BLINE {
  if (!lysi->currentmeasure->next)
    lysi->currentmeasure = addmeasures (lysi, lysi->currentmeasurenum, 1);
  else
    lysi->currentmeasure = lysi->currentmeasure->next;
  lysi->currentmeasurenum++;
} measures
| mudelaobjects STAFFEND   /* empty */
;

mudelaobjects:  /* empty */
| mudelaobject mudelaobjects
;

mudelaobject: chordandassoc
| CLEFTOK {
  currentclef = cleftypefromname ($1);
  newmudelaobj = newclefobj (currentclef);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
}
| keysig {
  if ($1.b)
    newmudelaobj = newkeyobj ($1.a - 3, $1.b);
  else
    newmudelaobj = newkeyobj ($1.a, $1.b);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
}
| TIMESIGTOK {
  newmudelaobj = newtimesigobj ($1.a, $1.b);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
}
| tuplet
| STEMDIRECTIVETOK {
  newmudelaobj = stem_directive_new ($1);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
}
| DYNAMICTOK {
  newmudelaobj = dynamic_new ($1);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
  g_free ($1);
}
;

keysig: KEYSIGTOK MINOR { $$ = twointer (keynametonumber ($1), TRUE) }
| KEYSIGTOK  { $$ = twointer (keynametonumber ($1), FALSE) }  /* legacy */
| KEYSIGTOK MAJOR { $$ = twointer (keynametonumber ($1), FALSE) }
;

chordandassoc: chord
| chord TIE {
  newmudelaobj->u.chordval.is_tied = TRUE;
}
| SLUR_END chord SLUR_START {
  newmudelaobj->u.chordval.slur_end_p = TRUE;
  newmudelaobj->u.chordval.slur_begin_p = TRUE;
}
| chord SLUR_START {
    newmudelaobj->u.chordval.slur_begin_p = TRUE;
}
| SLUR_END chord {
  newmudelaobj->u.chordval.slur_end_p = TRUE;
}
;

chord: BEGINCHORD {
  newmudelaobj = newchord (0, 0);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
} tones ENDCHORD
| {
  newmudelaobj = newchord(0, 0);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
} tone
;

tones:  /* empty */
  | tone tones
;

tone: TONETYPE ENSHIFT OCTIND duration {
  addtonewrapper (newmudelaobj, $1, $2, $3, $4, currentclef);
  prevduration = $4;
}
| TONETYPE OCTIND duration {
  addtonewrapper (newmudelaobj, $1, 0, $2, $3, currentclef);
  prevduration = $3;
}
| TONETYPE ENSHIFT duration {
  addtonewrapper (newmudelaobj, $1, $2, 0, $3, currentclef);
  prevduration = $3;
}
| TONETYPE ENSHIFT OCTIND {
  addtonewrapper (newmudelaobj, $1, $2, $3, prevduration, currentclef);
}
| TONETYPE ENSHIFT {
  addtonewrapper (newmudelaobj, $1, $2, 0, prevduration, currentclef);
}
| TONETYPE OCTIND {
  addtonewrapper (newmudelaobj, $1, 0, $2, prevduration, currentclef);
}
| TONETYPE duration {
  addtonewrapper (newmudelaobj, $1, 0, 0, $2, currentclef);
  prevduration = $2;
}
| TONETYPE {
  addtonewrapper (newmudelaobj, $1, 0, 0, prevduration, currentclef);
}
;

duration: BASEDURATION NUMDOTS {
  $$ = twointer (mutointernalduration ($1), $2);
}
| BASEDURATION {
  $$ = twointer (mutointernalduration ($1), 0);
}
;

tuplet: BEGINTUPLET {
  newmudelaobj = newtupopen ($1.a, $1.b);
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
} mudelaobjects ENDTUPLET {
  newmudelaobj = newtupclose ();
  lysi->currentmeasure->data = g_list_append (lysi->currentmeasure->data,
                                              newmudelaobj);
}
;

%%

/* Both of the otherwise infinite loops in these functions are exited when the
 * lylex returns (i.e., my ad-hoc lexer finds a token) */

int
lyinput (gchar *filename, struct scoreinfo *si)
{
  if ((lyin = fopen (filename, "r")) == NULL) {
    fprintf(stderr, "Cannot open the file: %s\n", filename);
    return -1;
  }
  else {
    lysi = si;
    free_score (lysi);
    lysi->currentstaffnum = 0;
    while (!feof (lyin)) {
      lyparse ();
    }
    fclose (lyin);
    return 0;
  }
}

/* This is an ad-hoc lexer. It really needs to be rewritten in
 * some other way - probably with the help of flex, though I 
 * was hoping to avoid this. The problem is it's very much a house
 * of cards right now, and doesn't handle standard mudela block-comments
 * correctly (though it does one-liners properly) */

int
lylex ()
{
  gchar scannedchars[2];
  gchar lotsachars[100];
  gchar thrownaway[5];  /* Nothing is done with the values assigned to
                       * this variable - it's just used to get the
                       * desired return value out of scanf */
  int getced;
  static gboolean instaff = FALSE;

  if ((getced = getc (lyin)) == EOF)
    return 0;
  else
    ungetc (getced, lyin);
  if (!instaff)
    {
      while ((getced = getc (lyin)) != EOF)
        {
          if (getced == '%')  /* a comment */
            while ((getced = getc (lyin)) != '\n' && getced != EOF)
              ;
          else
            {
              ungetc (getced, lyin);
              if (fscanf (lyin, "%s = \\notes%*[ 0-9A-Za-z=$\\]%1[{]",
                          lotsachars, thrownaway) > 1)
                /* That fscanf is really convoluted so that it'll
                 * match both pre- and post-0.5.3 staffbegins */
                {
                  instaff = TRUE;
                  lylval.strval = g_strdup(lotsachars);
#ifdef DEBUG
                  g_print ("found staffbegin\n");
#endif
                  return STAFFBEGIN;
                }
              if (fscanf (lyin, "\\tempo %d = %d;", &lylval.t.a, &lylval.t.b)
                  == 2)
                {
#ifdef DEBUG
                  g_print ("found tempo\n");
#endif
                  lysi->tempo = lylval.t.b;
                }
            }
        }
      return 0;
    }   /* End if */
  else
    {
      /* The following fscanfs have more hacks in them than you can
       * shake a stick at */

      /* The main hack to notice is that no two scanfs executed one after
       * the other will have a common prefix. This includes leading
       * whitespace */
      while ((getced = getc (lyin)) != EOF)
        {
          ungetc (getced, lyin);
          if (fscanf (lyin, "i%1[s]", thrownaway))
            {   /* sharp */
#ifdef DEBUG
              g_print ("found enshift\n");
#endif
              if (fscanf(lyin, "i%1[s]", thrownaway))  /* Double sharp */
                lylval.a = 2;
              else
                lylval.a = 1;
              return ENSHIFT;
            }
          if (fscanf (lyin, "%1[e]", scannedchars))
            {  /* either an e or a flat */
              if (fscanf (lyin, "%1[s]", thrownaway))
                {   /* flat */
#ifdef DEBUG
                  g_print ("found enshift\n");
#endif
                  if (fscanf (lyin, "e%1[s]", thrownaway))  /* Double flat */
                    lylval.a = -2;
                  else
                    lylval.a = -1;
                  return ENSHIFT;
                }
              else
                {
#ifdef DEBUG
                  g_print ("found tonetype\n"); 
#endif
                  lylval.c = scannedchars[0];
                  return TONETYPE;
                }
            }
          else if (fscanf (lyin, "%[\']%n", thrownaway, &lylval.a))
            {
#ifdef DEBUG
              g_print ("found octind\n");       
#endif
              return OCTIND;
            }
          else if (fscanf (lyin, "%[,]%n", thrownaway, &lylval.a))
            {
#ifdef DEBUG
              g_print ("found octind\n");       
#endif
              lylval.a = -lylval.a;
              return OCTIND;
            }
          else if (fscanf (lyin, "%[0-9]", lotsachars))
            {
#ifdef DEBUG
              g_print("found baseduration\n");
#endif
              lylval.a = atoi (lotsachars);
              return BASEDURATION;
            }
          else if (fscanf (lyin, "%[.]%n", thrownaway, &lylval.a))
            {
#ifdef DEBUG
              g_print ("found numdots\n");
#endif
              return NUMDOTS;
            }
          /* Note that the next fscanf will eat up any leading whitespace that
           * there might have been; all the fscanf's preceding this one
           * were specifically looking for an absence of leading whitespace */
          else if (fscanf (lyin, " %1[\\]", thrownaway))
            {
              if (fscanf (lyin, "tim%1[e]", thrownaway))
                {
                  if (fscanf (lyin, " %d/%d;", &lylval.t.a, &lylval.t.b)
                      == 2) {
#ifdef DEBUG
                    g_print ("found timesig\n");
#endif
                    return TIMESIGTOK;
                  }
                  else if (fscanf (lyin, "s %d/%d", &lylval.t.a, &lylval.t.b)
                           == 2)
                    {
#ifdef DEBUG
                      g_print ("found tupopen\n");
#endif
                      return BEGINTUPLET;
                    }
                }
              else if (fscanf (lyin, "key %[^; ]", lotsachars))
                {
#ifdef DEBUG
                  g_print ("found keysig\n");
#endif
                  lylval.strval = g_strdup(lotsachars);
                  return KEYSIGTOK;
                }
              else if (fscanf (lyin, "%1[m]", thrownaway))
                {
                  if (fscanf (lyin, "ino%1[r]", thrownaway))
                    {
#ifdef DEBUG
                      g_print ("found minor\n");
#endif
                      return MINOR;
                    }
                  else if (fscanf (lyin, "ajo%1[r]", thrownaway))
                    {
#ifdef DEBUG
                      g_print ("found major\n");
#endif
                      return MAJOR;
                    }
                }
              else if (fscanf (lyin, "clef %[^;]", lotsachars))
                {
#ifdef DEBUG
                  g_print ("found clef\n");     
#endif
                  lylval.strval = g_strdup (lotsachars);
                  return CLEFTOK;
                }
              else if (fscanf (lyin, "propert%1[y] ", thrownaway))
                {
                  if (fscanf (lyin, "Staff.midiInstrument = \"%[^\"]\"",
                              lotsachars))
                    {
#ifdef DEBUG
                      g_print ("found instrument\n");
#endif
                      lylval.strval = g_strdup (lotsachars);
                      return INSTRUMENT;
                    }
                  else if (fscanf (lyin, "Voice.noteHeadStyle = #\'%s",
                                   lotsachars))
                    {
                      /* Recognize this, but don't return anything;
                       * this is just a placeholder. */
#ifdef DEBUG
                      g_print ("found noteheadstyle directive\n");
#endif
                    }
                }
              else if (fscanf (lyin, "stem%s", lotsachars))
                {
#ifdef DEBUG
                  g_print ("found stemming directive");
#endif
                  if (strcmp (lotsachars, "down") == 0)
                    lylval.a = STEMDOWN;
                  else if (strcmp (lotsachars, "up") == 0)
                    lylval.a = STEMUP;
                  else
                    lylval.a = STEMBOTH;
                  return STEMDIRECTIVETOK;
                }
              else if (fscanf(lyin, "bar \"|.\"%1[;]", thrownaway))
                {
#ifdef DEBUG
                  g_print ("found staffend\n"); 
#endif
                  instaff = FALSE;
                  return STAFFEND;
                }
            }
          else if (fscanf (lyin, "s1*%d/%d",  &lylval.t.a, &lylval.t.b) == 2)
            {
              /* ignore full measure rests */
            }
          else if (fscanf (lyin, "%1[<]", thrownaway))
            {
#ifdef DEBUG
              g_print("found beginchord\n");    
#endif
              return BEGINCHORD;
            }
          else if (fscanf (lyin, "%1[>]", thrownaway))
            {
#ifdef DEBUG
              g_print ("found endchord \n");    
#endif
              return ENDCHORD;
            }
          else if (fscanf (lyin, "%1[a-gr]", scannedchars))
            {
#ifdef DEBUG
              g_print ("found tonetype\n");     
#endif
              lylval.c = scannedchars[0];
              return TONETYPE;
            }
          else if (fscanf (lyin, "%1[~]", thrownaway))
            {  /* Tie indicator */
#ifdef DEBUG
              g_print ("found tie\n");
#endif
              return TIE;
            }
          else if (fscanf(lyin, "%1[(]", thrownaway))
            {  /* Slur indicator */
#ifdef DEBUG
              g_print ("found slur\n");
#endif
              return SLUR_START;
            }
          else if (fscanf(lyin, "%1[)]", thrownaway))
            {  /* Slur indicator */
#ifdef DEBUG
              g_print ("found end slur\n");
#endif
              return SLUR_END;
            }
          else if (fscanf(lyin, "%1[|]", thrownaway))
            {  /* Barline marker */
#ifdef DEBUG
              g_print ("found barline\n");
#endif
              return BLINE;
            }
          else if (fscanf (lyin, "%1[}]", thrownaway))
            {  /* Close bracket - end of tuplet group */
#ifdef DEBUG
              g_print ("found tupclose\n");
#endif
              return ENDTUPLET;
            }
          else if (fscanf (lyin, "-%s", lotsachars))
            {
#ifdef DEBUG
              g_print ("found dynamic");
#endif
              lylval.strval = g_strdup (lotsachars);
              return DYNAMICTOK;
            }
          else
            {
              getced = fgetc (lyin);
              /* Shift to the next character and hope it's more interesting */
              if (getced == '%')
                {  /* a comment */
                  getced = getc (lyin);
                  if (getced == '!')
                    {
#ifdef DEBUG
                      g_print ("found reloaddirective\n");
#endif
                      while ((getced = getc (lyin)) != '\n' && getced != EOF)
                        ;
                      return RELOADDIRECTIVE;
                    }
                  else
                    {
                      ungetc (getced, lyin);
                      while ((getced = getc (lyin)) != '\n' && getced != EOF)
                        ;
                    }
                }
            }
        }
      return 0;
    }    /* End else */
}

gint lyerror (char *s)
{
  fprintf (stderr, "%s\n", s);
  return 1;
}