Professional Documents
Culture Documents
"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.
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.
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 (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:
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
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 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.
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.
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.
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
https://learn.uq.edu.au/bbcswebdav/pid-8905703-dt-content-rid-53965072_1/courses/COMP4403S_7320_24595/publicweb/compiler.html 6/6