In this lab you will use yacc (or its newer version) Bison as well as lex (flex) to create a parser for a small subset of Javalet
The yacc (bison) program is in the left box, the related lex (flex) code is in the bottom box and the input program for the generated parser is in the right box.
When you press the button Edit the text and click me, the following
will happen:
The results will be displayed on the bottom of the page.
You might have to scroll down in your browser to see them.
Step 1 Looking at the bison/yacc code in the left box, write the BNF in standard form.
Hint: The first production is
lines --> epsilon | lines line
Step 2 It is really hard to see the bottom-up parse from the output, so change the input to contain just the first statement 1 + 1; and press the button again. Now you should be able to create a parse from the output (which is the reverse of a leftmost derivation). Show it as a parse tree.
Step 3 Now, change the yacc/bison grammar in the left box so that it recognizes division. Note that the lex code
already recognizes "/", calling the token DIVIDE
. (Ask us if you don't see this)
Run an input to show that the division works (if it does! If not, fix your production) and
show us your output. Draw the parse tree from the output.
Step 4 In your written BNF, add productions to perform exponentiation, "^".
Show it to us before you go on.
Now add the bison code for ^ to the left box, enter a statement using ^ in the right box and click on the button.
Note that the lex code for "^" is already there with the token called POWER
.
Show us when it works.
Step 5 Moving to unix/linux:
yacc -d ly.y
)
This creates a file called yacc.tab.c
and the "-d" creates a file of definitions
called y.tab.h
. When these are compiled they produce our parser. Stop and look at these files
and show them to us.
cc y.tab.c lex.yy.c
which, as usual, creates the file a.out. This is the Parser!
Type:
../a.out
2 + 3 * 4
Show us your output.