Top Banner
by Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou [email protected] http:// www.sci.brooklyn.cuny.edu/ ~zhou/
14
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Compiler Construction

CIS 707

Prof. Neng-Fa Zhou

[email protected]

http://www.sci.brooklyn.cuny.edu/~zhou/

Page 2: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

What is a Compiler?

Gccgcc –S myprog.c

Javapjavap –c MyClass

compilersource

program

targetprogram

errormessages

Page 3: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Why Do We Learn Compilers?

Design and implement languages Use languages more effectively Many other application areas

– Data (text, image, audio, video) processing– Information retrieval– Bio-informatics

The principles and techniques of compiler writing are so pervasive that the ideas foundin this book will be used many times in the career of a computer scientist.

Page 4: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

The Context of a Compiler

analysis

synthesis

Page 5: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Analysis of Source Programs

lexical analyzer

syntax analyzer

semantic analyzer

source program

tokens

parse trees

parse trees

Page 6: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Lexical Analysis

tokens

Page 7: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Syntax Analysis

parse tree

Page 8: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Semantic Analysis

type checkingtype conversion

Page 9: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Symbol Table

There is a record for each identifier The attributes include name, type, location, etc.

Page 10: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Synthesis of Object Code

intermediate code generator

code optimizer

code generator

parse tree & symbol table

intermediate code

optimized intermediate code

target program

Page 11: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Intermediate Code Generation

Page 12: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Code Optimization

Page 13: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Code Generation

Page 14: By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou zhou@sci.brooklyn.cuny.edu zhou/

by Neng-Fa Zhou

Interpreters and Compilers

The boundary between interpreters and compilers is becoming vague– Interpreters interpret intermediate forms of

programs• Parse trees (Basic, Tcl, and Pearl)

• Virtual machine code (Java, Lisp, and Prolog)

Native code compiler