JavaCC
1) Preliminaries. Obtain JavaCC and JJtree. JavaCC is a Java tool for building parsers
AssignmentTutorOnline
and lexers, JJtree is an extension to it, provided as part of the JavaCC distribution. They
can be run from a command line, or in eclipse, or in netbeans.
Read at least the following pieces of documentation:
https://javacc.java.net/doc/javaccgrm.html
https://javacc.java.net/doc/JJTree.html
http://www.cs.may.ie/~jpower/Oberon/oberon.jj
The last one (Oberon-2 compiler) is used as a case study in lectures early next term.
2) Write the rules to allow JavaCC to build a lexical analyser for the various tokens of
this language. Do not have rules for strings, comments, octal hexadecimal or floating
point numbers: they are not part of this language.
3) Write the grammar rules to allow JavaCC to parse a complete error-free program in the
language. This involves removing extensive left recursion, performing left factoring on
rules relating to Test, and writing a valid JavaCC input specification.
4) Use JJtree to get a parse tree built.
5) Arrange for a ‘visitor’ to walk the tree and generate a list of 4-element vectors
representing instructions in 3-address code. The elements of the four-element vectors
should always be strings. The legal opcodes of these instructions are listed overleaf.
• The fields named Dest, Source1, Source2, Offset should be filled by names of
program variables or temporary variables created by your code generator. Such
temporary variables may be given names like t1, t2, t3 – since program variables
have no digits in their names there will be no danger of clashes.
• Note that the grammar specified gives a left-associative meaning to the “-”
operator.
• The “tag” argument to jump, jumpmore, jumpless, jumpequal, jumpnotequal and
label opcodes is another generated name like lbl1, lbl2 etc.
• Unused slots should always be filled with the empty string.
You are not asked to implement these instructions on any real or virtual machine. They
just provide the vocabulary of the language that your compiler is translating into.