Source Files for
Writing Compilers and Interpreters, 3rd ed.

Ronald Mak
Department of Computer Engineering
Department of Computer Science
Department of Applied Data Science
San Jose State University
© 2019

Java

C++

Zip file Date last modified
Chapter2cpp.zip August 17, 2019
Chapter3cpp.zip August 17, 2019
Chapter4cpp.zip August 17, 2019
Chapter5cpp.zip August 17, 2019
Chapter6cpp.zip August 17, 2019
Chapter7cpp.zip August 17, 2019
Chapter8cpp.zip August 17, 2019
Chapter9cpp.zip August 17, 2019
Chapter10cpp.zip August 17, 2019
Chapter11cpp.zip August 17, 2019
Chapter12cpp.zip August 17, 2019
Chapter13cpp.zip August 17, 2019
Chapter16cpp.zip August 17, 2019
Chapter17cpp.zip August 17, 2019
Chapter18cpp.zip August 17, 2019

Pascal Runtime Library


README for C++ chapters 2 through 13

This is a Java-to-C++ port of the source code from the book Writing Compilers and Interpreters , 3rd edition, by Ronald Mak. I tried to write the C++ code to resemble as much as possible the Java code in the book, so the code has a strong Java accent.

Compiling the .cpp and .h source files of the Pascal compiler requires the Boost header files. Download them from https://www.boost.org/ . You only need the header files; you don't need to compile any Boost libraries. Install the header files in a directory. Then in each chapter, modify the makefile's BOOST_INCLUDE_DIR variable to indicate the directory path.

If you are on Windows, you must first install the GNU g++ compiler and the rm and make utilities from Cygwin: https://www.cygwin.com/ .

The makefile and header_dependencies.txt file together enable you to use the make utility to compile and run on the command line. See https://www.gnu.org/software/make/ .

The Pascal compiler also includes a Pascal interpreter, which is completely working by Chapter 12. The interactive debugger is working by Chapter 13. The compiler's code generator is completely working by Chapter 18. There is no C++ port of the Java-based IDE from Chapter 14.

README addition for C++ chapters 16 through 18

Download the Jasmin assembler from http://jasmin.sourceforge.net/ and install jasmin.jar in a directory. Modify the makefile's JASMIN_JAR variable to indicate the filepath of the jar file.

Also modify the makefile's PASCAL_RTL_JAR variable to indicate the filepath of the Pascal runtime library PascalRTL.jar.


Generate header file dependencies

Before running any of the following make commands, run

     make dependencies

once in each chapter to generate the file header_dependencies.txt which tells the make utility how the .cpp files depend on the .h files. You should not need to run this command again in a chapter unless you add or remove .h or .cpp files, or upgrade the Boost header files.


Example uses of make on the command line

make clean Remove all generated .o files and the executable file to start fresh.
make Compile all the .cpp and .h files and generate the executable file (i.e., the compiler). There may be warning messages.
make execute src=hello.pas flags=ix Run the Pascal interpreter to execute Pascal source file hello.pas. The src parameter is required and the flags parameter is optional, and they can appear in any order. (See the flags below.)
make compile src=hello.pas Run the Pascal compiler on the Pascal source file hello.pas to generate Jasmin assembly object code hello.j. You can include the optional flags parameter.
make assemble src=hello.j Run the Jasmin assembler on assembly file hello.j to generate hello.class for the Java Virtual Machine.
make run src=hello Execute program hello.class on the Java Virtual Machine.

The optional flags

i Print the intermediate code (i.e., the parse trees).
x Print the cross-reference tables (symbol table contents).

These are for debugging a Pascal program while running it in the interpreter:

l (letter L) Print the current source line number.
a Print the value currently being assigned to each variable.
f Print the value currently being fetched from each variable.
c Print each call to a procedure or a function.
r Print each return from a procedure or a function.

You can combine the flags in any order.