San Jose State University : Site Name


Main Content

Working in Mars Mission Control, JPL

Ronald Mak

Department of Computer Engineering
Department of Computer Science
Department of Applied Data Science
Fall Semester 2020

Office hours: TuTh: 4:30-5:30 PM online via Zoom
Office location: ENG 250 (but working from home)

CS/SE 153 Concepts of Compiler Design

Section 1: TuTh 9:00 - 10:15 AM online via Zoom

Lab Assignments

# Date Due Assignment
1 Aug 20 Aug 27 Write Simple Pascal Programs

Assignment input file: presidents.txt
Solutions: GoSpartans.pas   ListMerge.pas   Presidents.pas

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

Simple Pascal (Java version):
Test input files: Newton.txt    ScannerTest.txt
Sample output: ScannerTest.out.txt
Sample solution:
3 Sep 3 Sep 17 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 17 Sep 28 Pcl interpreter using ANTLR

Executor skeleton:
Sample solution:
5 Oct 8 Oct 19 Pascal to Java Converter

Partial converter code:
6 Oct 22 Nov 9 Pascal Compiler

Partial compiler code:


Week Date Content
1 Aug 20 Zoom recording Password: p?AU7Dhs
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
2 Aug 25 Zoom recording Password: 1iLG==x5
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:
Aug 27 Zoom recording Password: 1&b@.YPc
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; symbol table concepts and operations; who needs a symbol table?
3 Sep 1 Zoom recording Password: S7B3N*.6
Slides: Building the parse tree with parser methods; the symbol table and its entries; a hack; a simple interpreter
Sep 3 Zoom recording Password: 98Y%X#=j
Slides: Simple interpreter; "run time"; using the parse tree and symbol table at run time; another hack; visiting parse nodes during run time; REPEAT statement; Assignment #3; WHILE statement; IF statement and the dangling ELSE; FOR statement; CASE statement
4 Sep 8 Zoom recording Password: R71LS3E^
Slides: Assignment #3; the dangling ELSE; 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
Sep 10 Zoom recording Password: yv!.AB43
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

ANTLR example: Expr.g4    input.txt
5 Sep 15 Zoom recording Password: y$4R*2Kp
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; main

ANTLR example:
Sep 17 Zoom recording Password: 3D%m#NnE
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
6 Sep 22 Zoom recording Password: 1XP$M#Pk
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:
Sep 24 Zoom recording Password: 2EiFu&%!
Slides: Multipass compilers; type declaration structures; multidimensional array; predefined scalar types; type checking
7 Sep 29 Zoom recording Password: H&?r7e=9
Slides: Pcl6 main; pass2 methods for type definitions; type checking; assignment and comparison compatible; new fields for parse tree nodes; class TypeChecker; type checking expressions; cross-reference listing

ANTLR example:
Oct 1 Zoom recording Password: #6FC4=m&
Slides: Pascal.g4; pass 3 execution; 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; midterm review
8 Oct 8 Zoom recording Password: 2k.t^rxd
Slides: Interpreter features; 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; backend converter to a different language; convert Pascal's REPEAT statement; Assignment #5
9 Oct 13 Zoom recording Password: C5d.0WDa
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
Oct 15 Zoom recording Password: 3B.LH9K5
Slides: 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
10 Oct 20 Zoom recording Password: 0g%jnx$R
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
Oct 22 Broken internet connection.
11 Oct 27 Zoom recording Password: RFp#=&3!
Slides: Calling methods on objects; invokestatic vs. invokevirtual; code for println() and printf(); translation recap; Assignment #6; final compiler project
Oct 29 Zoom recording Password: kS?=u2AG
Slides: 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
12 Nov 3 Zoom recording Password: d3ql5AR#
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:   ReferenceParm1.cpp   ReferenceParm2.cpp
Runtime library example:   LibraryTest.j
Nov 5 Slides: Zoom recording Password: 3?Y#S$Tt
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
13 Nov 10 Zoom recording Password: 6m^.SrWp
Slides: backend compiler architecture
Nov 12 Zoom recording Password: 1$DxQvzP
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
14 Nov 17 Zoom recording Password: e?QLU8*!
Slides: Interactive source-level debugger; machine-level vs. source-level debugging; simple debugger command-line language; breakpoints; watchpoints; parsing and executing the debugger commands; a GUI-based debugger; integrated development environment (IDE); interprocess communication

Pascal command-line debugger:
Nov 19 Zoom recording Password: 1ndm4$G!
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

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

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.