San Jose State University : Site Name


Main Content

Goals of the course, JPL

Ronald Mak

Department of Computer Science
Department of Applied Data Science
Fall Semester 2022

Office hours: M  4:30 – 5:30 PM in person, Clark Hall CL 325
Tu 3:00 – 4:00 PM online via Zoom
Th 6:00 – 7:00 PM in person, Clark Hall CL 325
Office location: Clark Hall CL 325 (mostly working from home)

CS/SE 153 Concepts of Compiler Design


# Date Due Assignment
1 Aug 23 Aug 30 Compile and execute a Pascal program

Assignment files: Presidents.pas    presidents.txt

Sample Pascal program: EmployeeListing.pas
Sample program input file: employees.txt
Sample program output: employees.out.txt
2 Aug 30 Sep 6 Pascal Scanner

Simple Pascal:
Test input files: Newton.txt    ScannerTest.txt
Sample output: ScannerTest.out.txt
Sample solution:
3 Sep 6 Sep 20 Simple Pascal interpreter

Test input files and expected output: TestWhile.txt    TestWhile.out.txt    TestIf.txt    TestIf.out.txt    TestFor.txt    TestFor.out.txt    TestCase.txt    TestCase.out.txt
Sample solution:
4 Sep 20 Oct 4 Pcl interpreter using ANTLR

Executor skeleton:
Test input files: TestWhile.txt    TestIf.txt    TestFor.txt    TestCase.txt    HelloWorld.txt    Temperature.txt    SquareRootTable.txt
Sample solution:
5 Oct 4 Oct 18 Pascal to Java Converter

Partial converter code:
6 Oct 20 Nov 9 Pascal Compiler

Partial compiler code:


Week Date Content
1 Aug 23 Zoom recording Password: M$3M90SH
Slides: Goals; course learning outcomes; project teams; grading; postmortem assessment report; overview of the compilation process; compiler as translator; other forms of translation; Pascal tutorials
Aug 25 Slides: It's all about translation; conceptual design; major parts of compilers, converters, and interpreters; compilers vs. interpreters; three Java packages; syntax diagrams; how to scan for tokens; basic scanning algorithm; test the scanner; current character vs. next character; consume characters and tokens

Sample code:
2 Aug 30 Zoom recording Password: 2tJtUS$r
Slides: Assignment #2; Pascal statement syntax diagrams; expressions; operator precedence; control statements; REPEAT statement syntax; parse tree design and operations; building a parse tree; test the parser; parse tree nodes; printing parse trees
Sep 1 Slides: Building the parse tree with parser methods; handling syntax errors; the symbol table and its entries; a hack; a simple interpreter; what is "run time"; using the parse tree and symbol table at run time; another hack; parse tree visit methods

Sample erroneous code: HelloErrors-1.txt    HelloErrors-2.txt
3 Sep 6 Zoom recording Password: xi+%44!N
Slides: Simple interpreter; visiting parse nodes during run time; REPEAT statement; LOOP subtree; evaluating expressions; Assignment #3; WHILE statement; IF statement and the dangling ELSE; FOR statement; CASE statement
Sep 8 Slides: Assignment #3; maximum munch; an executor improvement; syntax and semantics; options for error recovery; top-down recursive descent parsing; accomplishments so far; temporary hacks; a DFA scanner

DFA scanner:    SimpleDFAInput.txt
4 Sep 13 Zoom recording Password: F$Zer7J@
Slides: BNF; grammars and languages; derivations and productions; EBNF; compiler-compilers; ANTLR 4; ANTLR lexer; ANTLR grammar file; Java main programs; on the command line; Eclipse ANTLR plug-in

ANTLR example: Expr.g4    input.txt    error.txt
Sep 15 Slides: What does ANTLR do for us?; ANTLR workflow; ANTLR parse trees; syntax error handling; resolving ambiguities; visitor interface; interface ExprVisitor; base visitor class ExprBaseVisitor; labeled production rules; interface ExprLabeledVisitor; base class ExprLabeledBaseVisitor; class Executor

ANTLR example:
5 Sep 20 Zoom recording Password: *iZ1=sj5
Slides: Pcl4 grammar; package structure; Pcl4 visitor interface; Pcl4 base visitor class; class Executor; Assignment #4; declarations; constant definitions; simple type definitions; array type definitions; record type definitions; variable declarations; declarations and the symbol table; scope and the symbol table stack
Sep 22 Slides: Scope and the symbol table stack; nested scopes; scope of record fields; class Symtab; class SymtabStack; class SymtabEntry; Pascal declarations; grammar file Pcl5.g4; declarations and the symbol table; type specification attributes; class Typespec

ANTLR example:
6 Sep 27 Zoom recording Password: Wu%V69$%
Slides: Multipass compilers; type declaration structures; multidimensional array; cross-reference listing; predefined scalar types; type checking; assignment and comparison compatible; type checking expressions; class TypeChecker; visit methods for type definitions; new fields for parse tree nodes; Pcl6

ANTLR example:
Sep 29 Slides: Pascal.g4; pass 3 uses node info from pass 2; runtime memory management; symbol table stack vs. runtime stack; stack frame; access to nonlocal variables; runtime display; recursive calls; allocate a stack frame; runtime error checking; Pascal interpreter

ANTLR example:
7 Oct 4 Zoom recording Password: 5^d09*a&
Slides: Grammar for a variable; type check a variable; grammar for a procedure or function definition; symbol table entry for a procedure or function; grammar for procedure and function calls; type check a call; source-level debugger; debugger command language; backend converter to a different language; convert Pascal's REPEAT statement; Assignment #5
Oct 6 Slides: On writing compilers; language conversion challenges: subscripts, nested routines, local data, parameters; review for the midterm

Midterm review questions
8 Oct 13 slides Interpreter, converter, compiler; target machines; Java Virtual Machine (JVM) architecture; JVM runtime stack and stack frame; Jasmin assembler; Jasmin assembly instructions: shortcuts, local variables, load and store, arithmetic, other instructions; code templates; compilation strategy; Jasmin datatype descriptors

Midterm solutions
9 Oct 18 Zoom recording Password: 3+^MWC%L
Slides: Example conversion of Pascal to Java; program fields; code template for the main method; loading and storing a program variable's value; code for procedures and functions; compiling local variables; generating code for expressions; comparing integer values and other datatypes; What Would James Gosling Do?; Jasper; relational expressions; load and store tips
Oct 20 Slides: Code templates: assignment statements, if statements, looping statements; Newton's square root function; FOR statement; select template; CASE statement; procedures and functions; code: main program, function, call a function; Assignment #6
10 Oct 25 Zoom recording Password: sDH9.u8t
Slides: Calling methods on objects; invokestatic vs. invokevirtual; code for println() and printf()
Oct 27 Slides: Resource acquisition is initialization (RAII); Pascal arrays; code generation for arrays and subscripts; allocate memory for scalar arrays; access an element of a 2-D array; subscripted variables; allocate memory for non-scalar arrays; 1-D array of strings; 2-D array of strings; code for records and fields; passing parameters; runtime libraries
11 Nov 1 Zoom recording Password: QS0m@v^Q
Slides: backend compiler architecture
Nov 3 Slides: Code generation; instruction selection; register allocation; data flow analysis; instruction scheduling; code optimization: safety, profitability, constant folding, constant propagation, strength reduction, dead code elimination, loop unrolling, common subexpression elimination; debugging and optimizing compilers; compiling object-oriented languages

Code optimization example: Unoptimized.pas   Unoptimized.j   Optimized.j
12 Nov 8 Zoom recording Password: =Vvu+W0D
Slides: Static and dynamic scoping; runtime memory management; Java command-line arguments; heap management; garbage collection algorithms: reference counting, mark and sweep, stop and copy, generational; aggressive heap management; garbage collection research
Nov 10 Slides: Passing parameters: Java and C++; pass by reference: C++, Java hack; runtime libraries; library example; project presentations schedule; example project

Pass-by-reference examples:   ObjectRef.cpp   ScalarRef.cpp
Runtime library example:   LibraryTest.j
13 Nov 15 Zoom recording Password: =@b5W$vH
Slides: Interactive source-level debugger; a GUI-based debugger; integrated development environment (IDE); interprocess communication
Nov 17 Slides: Context-free and context-sensitive grammars; top-down and bottom-up parsers; shift-reduce parsing; yacc and lex

Example grammar: calc.l   calc.y
14 Nov 22 Zoom recording Password: BD7d*%%&
Slides: Project survey questions; syntax-directed translation; attribute grammars; LL(1) and LR(0) parsers; FIRST and FOLLOW sets; three-address code; data-flow analysis; JavaCC

Goals of the course

This course will concentrate on practical aspects of compiler construction, programming language design, and engineering a large, complex software application.

This is a challenging course that will demand much of your time and effort throughout the semester.

Course Learning Outcomes (CLO)


Department policy is to enforce
all course prerequisites strictly

CS 47 Introduction to Computer Systems
or CMPE 102 Assembly Language Programming
grade C- or better
CS 146 Data Structures and Algorithms grade C- or better
CS 154 Formal Languages and Computability grade C- or better

Required book

The Definitive ANTLR 4 Reference, 2nd edition
Terence Parr
Pragmatic Bookshelf, 2012
ISBN: 978-1934356999

Recommended book

Writing Compilers and Interpreters, 3rd edition
Ronald Mak
Wiley Publishing, Inc., 2009
ISBN: 978-0-470-17707-5
Source files

Online Pascal tutorials

We will use Pascal as the example source language.
Pascal Tutorial  looks very good. It has an online Pascal compiler.
Learn Pascal  also looks good, although it doesn't appear to cover set types.

Online Pascal development sites

You can edit, compile, and run Pascal programs online.

Other useful tutorials.

Install the ANTLR 4 plug-in for Eclipse, etc.