Please read part 1 of this series.

You should be comfortable with the lexical and parser grammar files in 01…, 02…, and 03… folders in the jison tutorial github repository.

Lets kick it with “Say Hello and Goodbye”

The Codes

1 // nada.jisonlex
3 %%
5 \s+           /* ignore whitespace, VERY IMPORTANT */
7 "begin"       return 'BEGIN';
8 "end"         return "END";
9 <<EOF>>       return 'EOF';

The important parts of the lexical grammar are the following:

nada.jisonlex#L7 Lexer, if you see the word “begin” return a “BEGIN” token nada.jisonlex#L8 Lexer, if you see the word “end” return a “END” token

 1 // nada.jison
 2 %%
 4 pgm 
 5 :           /* whitespace */
 6 | BEGIN     { console.log("We have only just begun."); }
 7 | pgm END   { console.log("Leaving so soon?"); }
 8 | pgm eof
 9 ;
10 eof
11 : EOF
12 ;

As you may have guessed the colon (“:”) tells the parser that this is the first pattern to match for the “pgm” group. The vertical bar (“|”) tells the parser and here are some more. Notice that we use recursive groupings, which translats to something like this:

A “pgm” group could start with whitespace OR it could start with BEGIN – which we know from the lexical grammar is the word “begin”. Furthermore, a “pgm” group, having started with whitespace or “begin”, could also have an END token – or “end”. Lastly, including the previous statements, the “pgm” having been started, and possibly ended, could have an end of file character.

The important parts of the parser grammar are the following:

nada.jisonlex#L6 Parser, when you see the BEGIN token print “We have only just begun.”

nada.jisonlex#L7 Parser, when you see the END token print “Leaving so soon?”

Its worth noting line 8 which reads, when you see a pattern matching the group “eof”, do nothing. You can see on lines 10 & 11 that the group “eof” is defined as the single token EOF.

The nada.txt content is the following:


What do you think would happen if you removed “begin” and ran make. Would the code compile? How might the output change? What if you added more “begin” and “end” lines to the file? Give it a go!


We have moved forward with our knowledge of the inner-workings of jison and jisonlex. In part 3 – whoo! – its gonna get wild!

blog comments powered by Disqus


26 May 2014