You are on page 1of 6

5/11/23, 5:46 PM COMP4403/7402 Compiling and Running PL0 Compiler

Last updated: Sun 5 Feb 2023 18:29:18 AEST.

Compiling the PL0 compiler


Sections in blue refer to the compilers used for assignments 2 and 3, so ignore the blue parts when
doing tutorial 4 and assignment 1.

Running the PL0 compiler under IntelliJ

Importing the Project


Download the .zip file containing the compiler from the course web page
Unzip the assignment .zip file to create a directory containing the compiler as a IntelliJ project
Start IntelliJ Idea -- version 2018-3 (or later) is required to use the supplied run configurations
File > Open
Navigate to the unzipped directory containing the compiler and select the directory
Open
If there is a bar at the top of the code stating "Project SDJ is not defined" then select "Setup
SDK" (on the right of the bar). That will display your available SDKs. Select one that is at least
Java 1.8 (and Project language level 8) and select OK. [This is required if the SDK you are using
is different to the one I'm using.]

Run Configurations Provided


The compiler IntelliJ project comes with two run configurations.

"PL0_RD" ("PL0_LALR") runs the compiler on whatever file is currently displayed in IntelliJ. It is
recommended that this be a PL0 file -- if it is a Java file the PL0 compiler will still attempt to
parse it but many error messages will ensue as its not valid PL0. You may want to edit this
configuration to change the program parameters, e.g. to add "-s" to turn off static checking
(and everything beyond that) or add "-d" to turn on debugging output.
"Test_RD" ("Test_LALR") runs the PL0 compiler on every test case and compares the output with
that expected. Ticked test cases succeeded and crossed test cases failed.

Compiler options
If a compilation of a PL0 program is successful, the compiler normally runs the interpreter to execute
the generated code. However there are a number of compiler parameters that can change its
behaviour.

Usage: java pl0.PL0_RD [-dsigevth] filename.pl0


https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 1/6
5/11/23, 5:46 PM COMP4403/7402 Compiling and Running PL0 Compiler

-d = turn debug messages on (verbose trace of parsing, etc.)


-s = turn static checking off (useful for debugging the parser)
-i = turn interpretation off
-g = turn code generation off
-e = turn code execution off
-v = output of generated code
-t = trace execution of generated code (verbose)
-h = output this usage information
filename.pl0 file to be compiled.

The -d option gives a blow-by-blow (very verbose) trace of the recursive descent parsing process (for
assignment 1 compiler) or the shift-reduce parsing process (for assignment 2 and 3 compilers). It also
gives extra debugging output during static checking and interpretation or code generation.

The -t option is useful for debugging problems with the code your compiler generates; it gives a very
verbose blow-by-blow trace of the execution of the code.

To debug the parsing, run the compiler with the "-s" option and perhaps "-d" to get the debugging
output to see what it is doing.

To debug the static checker, run with "-i" and possibly "-d" as well.

To debug the code generation, run with just "-v" to see generated code, and if things go wrong at run
time, try adding "-t" to get a very detailed trace of execution, instruction by instruction; this is where
smaller programs will be easier.

Test Programs
There is a suite of test PL0 programs within the subdirectory test-pgm. When you run a program
using the PL0_RD run configuration the output of the compiler appears in the "Console" window
(usually under the program).

Compiling test-based-write1.pl0
Parsing complete
Static semantic analysis complete
Running ...
1

Terminated
No errors detected.

Process finished with exit code 0

The line containing "1" is the output produced by this particular (simple) PL0 program.

https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 2/6
5/11/23, 5:46 PM COMP4403/7402 Compiling and Running PL0 Compiler

Syntax Highlighting
To enable PL0 syntax highlighting

File > Manage IDE Settings > Import Settings


Navigate to the unzipped directory and select pl0-syntax.jar and open and OK
You'll need to restart IntelliJ

To enable Java CUP syntax highlighting:

File (IntelliJ Idea on Mac) > Settings (Preferences on Mac) > Plugins > Browse repositories >
Cup support (install and restart IntelliJ)

Using git
To enable git version control in IntelliJ:

VCS > Enable Version Control Integration > Git > Ok


Select all the files > Right Click > Git > Add
VCS > Git > Commit File > Select all files (if not already selected) > Enter commit message and
commit (don't select Commit and Push)

Running CUP and JFlex to build parser and lexical analyser (scanner)
Java source files
Run scripts Run CUP and Run JFlex are provided with the compilers using Java CUP and JFLex to
generate the parser and lexical analyser, respectively.

The run script PL0_LALR automatically runs both Java CUP and JFlex but the errors messages for each
of these only flash up in the console window for a short time. To see the messages from Java CUP or
JFLex you can select the "Run CUP" or "Run JFlex" tabs in the console window of IntelliJ (at the
bottom). Alternatively, you can run Java Cup and JFlex separately (see below) and check for errors,
before running PL0_LALR.

Run CUP
This script runs Java CUP on input file PL0.cup to produce CUPParser.java and CUPToken.java.
You need to rerun this script any time you change PL0.cup.
If there are any errors Java CUP will not (re-)generate the Java files for CUPParser and
CUPToken.
You may get warnings like

Warning: Terminal "ILLEGAL" was declared but never used

https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 3/6
5/11/23, 5:46 PM COMP4403/7402 Compiling and Running PL0 Compiler

which are OK unless you are expecting the terminal symbol to be used in your grammar (that is
never the case for "ILLEGAL").
Common typos that lead to errors are
forgetting the ":}" at the end of a block of action code (e.g. just writing "}")
forgetting the ";" at the end of a production
Using the CUP support syntax highlighting in IntelliJ can help you to find issues like this and
other syntax issues in your CUP file but not syntax issues within the action code blocks.
After running Java CUP the generated CUPParser.java may have compiler errors because Java
CUP just copies the action code blocks verbatim into CUPParser. There are comments in the
generated CUPParser file indicating which production the action code is associated with. When
you work out what is causing the problem, fix it in PL0.cup and rerun Java CUP.
The following are instructions to set up the Run CUP run script in IntelliJ. You should not need to use
these.

Run > Edit Configurations > Plus Button > Application


Give the configuration a name like "Run CUP"
Select the main class using the drop down and searching for java_cup.Main.
Put "-interface -locations -parser CUPParser -symbols CUPToken PL0.cup" into the program
arguments
Select the src/parse directory as the working directory using the file browser (or append
/src/parse to the end of the default path)

Run JFlex
This script runs JFlex on input file PL0.flex to generate the lexical analyser Lexer.java
It links to the CUPToken class generated by JavaCUP.
The terminal symbols (lexical tokens) defined in PL0.flex must match the terminal symbols
defined in PL0.cup in its "terminals" section.
Any additions to the terminal symbols require both PL0.flex and PL0.cup to be updated and
both Java CUP and JFlex rerun.

The following are instructions to set up the Run CUP run script in IntelliJ. You should not need to use
these.

Under the left column > Plus Button > Application


Give the configuration a name like "Run JFlex"
Select the main class using the drop down and searching for jflex.Main.
Put PL0.flex into the program arguments
Select the src/parse directory as the working directory using the file browser (or append
/src/parse to the end of the default path)
Under 'Before launch' > Plus Button > Run Another Configuration > Compile CUP (or the name
used for the previous configuration)

Tracing generated code


https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 4/6
5/11/23, 5:46 PM COMP4403/7402 Compiling and Running PL0 Compiler

In the tracing

PC stands for "Program Counter", i.e. the location of the instruction being executed
FP stands for "Frame Pointer", 0 for the main program but non-zero for procedures called
directly or indirectly from the main program (this isn't all that relevant for assignment 1)
SP stands for "Stack Pointer"; a push increases it and a pop decreases it; it starts at 3 and should
never get below 3
Opcode gives the instruction being executed
All pops and pushes on the stack are traced giving the values pushed and popped
Store [3] <= 100 means 100 is stored at location 3
Load [3] => 100 means 100 was loaded from location 3 (it then gets pushed onto the stack)
Branch => 1017 means branch to location 1017, i.e. set the PC to 1017

The tricky (and common) errors are when you get the branch offset out and branch to a different
instruction to what you expected. That can lead to weird behaviour including stack under/over flows,
etc. If you branch into the second word of a LOADCON, it may not even be a valid opcode for an
instruction.

Using Eclipse (not preferred)


If you are using Eclipse then it will automatically compile the Java source files. For the compiler
generated via Java-CUP (assignments 2 and 3):

to run the Java-CUP parser generator to rebuild CUPParser.java and CUPTokens.java from
PL0.cup run the ant script build-cup.xml from within Eclipse
Select build-cup.xml and right click
Select Run As > Ant Build
The output from JavaCup appears in the Console window
Be sure to check any for any JavaCUP errors, as it will not generate the Java parser
CUPParser.java if there are any
JavaCUP warnings of the form "Warning : Terminal "ILLEGAL" was declared but never
used" are OK
you also need to refresh the parser files after running the ant script, but you can set up Eclipse
to do this automatically as part of running the ant script
to run the JFlex lexical analyser generator to build Lexer.java from PL0.flex run the ant script
build-flex.xml from within Eclipse, and refresh the parser files; you should only need to generate
Lexer.java once.

Running the PL0 compiler under Eclipse


You'll need to set up a run configuration to run the PL0 compiler under Eclipse. While in the
assignment compiler Eclipse project that you have imported.

https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 5/6
5/11/23, 5:46 PM COMP4403/7402 Compiling and Running PL0 Compiler

Select Run>Run Configurations...


Select Java Application
Press the new icon (the box with the "+" in the corner)
Give your new configuration a name (like "A1run")
The Project field should correspond to the compiler project (eg, "T4", "A1")
Under the "Main" tab, press "search" under "Main class"
Select PL0_RD (for recursive descent) or PL0_LALR (for Java-CUP version) from package pl0
Select the "Arguments" tab
Insert in text box
-v "${resource_loc}"
These are the arguments to the PL0 compiler, which are described in more detail below. If the
path name to your workspace includes blanks, don't forget to put the quotes around
${resource_loc}. In Eclipse ${resource_loc} is replaced by the currently selected file. Hence to
compile a PL0 program, you should first select the PL0 program, e.g., select "test-pgm/test-
based-write1.pl0" (which is the closest PL0 gets to a "Hello World" program) under Eclipse, and
then select "Run>Run" to run the run configuration that you previously ran, or "Run>Run
Configurations..." to switch to a different configuration, or create a new configuration.

https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 6/6

You might also like