0

I am trying to write a parser for the Lilypond language of music notation in JavaScript. My first manual attempts work, but can only deal with a very small subset of the language. As Lilypond uses bison files to define its grammar[1] and JISON claims to be able to work from bison files, my idea was to perhaps use these definitions to generate a parser in JavaScript.

I cannot find any examples of this anywhere, and trying to feed these files directly into JISON just throws errors.

What would be the best way to approach this?

[1]: see https://github.com/lilypond/lilypond/blob/master/lily/lexer.ll and https://github.com/lilypond/lilypond/blob/master/lily/parser.yy),

mauritslamers
  • 527
  • 3
  • 10

1 Answers1

1

The parser is relatively(!) easy: strip the C code (you should keep the AST construction at first to see how they did it), keep the precedences as they are (JISON understands the Bison syntax to some wide extend) and fill in the code to fill the AST. The Lexer is quite complicated in contrast and I don't know if all of the flex-specialities are supported by JISON but otherwise: go as described above for the parser part.

But it still will be a lot of work, that's for sure ;-)

EDIT: After some twiddling with Lilypond's grammar and searching the web for more information I stumbled over the following quote:

The LilyPond grammar does a lot of weird things…

By the Lilypond developer named "dakas" in https://lwn.net/Articles/561990/

I concur.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • Could you be more specific in the kind of steps I would have to go through? Because of the lack of specifics in either Bison documentation as well as JISON, I don't even know which files or which kind of output of bison to use, and which files would work as an input for JISON. (Edit: I agree on the Lilypond grammar being weird.) – mauritslamers Nov 08 '15 at 09:53
  • I cannot be more specific without being too specific, that is doing the whole port myself. Yes, it's that bad. For a start: I just stripped the C-code from the Bison file and JISON chocked at the very first production (teh one that is topmost in the file). BTW: JISON does return any usefull diagnostics until you comment of the `try...catch` constructs around the four parser calls in lines 163 and following in `jison/lib/cli.js`. So...how shall I put it...did you give EMScripten a try? – deamentiaemundi Nov 08 '15 at 16:17
  • I sort of just figured out that the Bison grammar definition is written using the language the parser needs to be written in. I assumed that the grammar was abstract, and could be "ported" to any language, which I now see obviously is not the case. BTW I tried running JISON with the -t parameter, but it did not make any difference. – mauritslamers Nov 08 '15 at 21:58
  • I seee I forgot the little word `not` while describing how to teach JISON a proper debug output, sorry. The option `-t` does indeed nothing of value, you have to comment out the lines 163, 169-170, and 176-178 in `jison/lib/cli.js`. – deamentiaemundi Nov 08 '15 at 22:49
  • PS: I made an official pull request to add that function to `jison/lib/cli.js`. I doubt it will happen but you'll never know if you don't try ;-) – deamentiaemundi Nov 08 '15 at 23:37