Back to Home

Bison for JavaScript

javascript · bison · jison · parsing

Bison for JavaScript

    No, not Jison . A full-fledged javascript parser generator with full bison support.


    Let's start with the main question.

    What for?



    It became necessary to port a large script parser to javascript without losing the small details of the original. It is very lucky that the original parser is written in terms of yacc / bison, and not with your hands on hardcore C.

    When it came time to port this parser, the first thing that came to mind was to google “javascript bison parser”. Of course, the first result was Jison. Here is joy, I thought then. However, the very first experiment showed that Jison does not know how to do a large piece of work with intermediate states. These are such cable tokens, if you hit them, you need to execute a piece of your code in order, for example, to tell the lexer what context it is in now. A brief correspondence in the github of the project did not bring comfort. Instead of the author , a very savvy dude answered. He wrote there, in my opinion, a whole book with a bunch of details! I had to re-read and understand three times: yes, alas, I have to pick the original bison.

    Bison



    I must say that the bison is very well designed. Instead of hardcodes some constructions inside the generator, the bison uses the template language with which it beautifully decomposes the necessary variables and labels into the resulting parser. Each of the three languages ​​has its own template called the skeleton. And everything would be fine, but the GNU m4 template language there is just awful. Because of this anachronism, instead of calmly porting the skeleton for Java, I only had to deal with template garbage for the day.

    lalr1.js



    This is the name of the skeleton for javascript - lalr1.js . They are all Lalra brothers there :)

    As a result, it turned out to be very personal. We take some grammar.y in one hand, lalr1.js in the other, and bam:
    bison -S ./lalr1.js grammar.y
    

    we get grammar.js, which, after sawing a lexer to it, can very much parse something.

    Oh yes. It is important not to forget to replace all C code in this grammar.y with javascript. Well, and lexer gash. Well, and to test it well, comparing the logs of the parser in C and the parser in javascript.

    Epilogue



    As usual, everything was done because it was terrifying how interesting. But at the same time managed to bring the result to beta. If there are people among you who are interested in generating javascript parsers (well, there for coffee script, or directly javascript right away), then I will be very glad to help in running all of this in business. You can play by taking the sources from github.com/bison-lalr1.js and installing bison and v8 / d8 for yourself.

    Two weeks later (06/28/2013) : The parser does its job well with hefty gamut, intertwined with a complex lexer. Only 10 times slower than the parser. It looks like you can release soon :)

    Read Next