Top Banner

Click here to load reader

CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak

Jan 04, 2016

ReportDownload

Documents

CS 153: Concepts of Compiler Design

CS 153: Concepts of Compiler DesignAugust 26 Class MeetingDepartment of Computer ScienceSan Jose State University

Fall 2015Instructor: Ron Makwww.cs.sjsu.edu/~mak

1Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak2What Else Can Compilers Do?Compilers allow you to program in a high-level language and think about your algorithms, not about machine architecture.

Compilers provide language portability.

You can run your C++ and Java programs on different machines because their compilers enforce language standards.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak3What Else Can Compilers Do? contdCompilers can optimize and improve the execution of your programs.

Optimize the object code for speed.Optimize the object code for size.Optimize the object code for power consumption.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak4Course Overview First half: Modify a Pascal interpreter.

The interpreter is written in Java (the implementation language).

The source programs are written in Pascal (the source language).

The implementation code for the interpreter will be presented to you incrementally.

MidtermComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak5Course Overview, contd Second half: Your compiler project.

JavaCC compiler-compilerJava Virtual Machine (JVM) architectureJasmin assembly languageBack end code generator

FinalComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak6AssignmentsAll but the first assignment done by the teams.

For each team assignment, turn in one per team.

The assignments will help you complete your compiler project.

At least one week to do each one.

Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak7Compiler ProjectEach team develops a working compiler for a procedure-oriented source language.

A more complete subset of Pascal.Any other procedure-oriented language.Invent your own language.No Lisp-like languages.

Start thinking about and planning for your project early in the semester!

Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak8Take roll!Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak9Conceptual Design (Version 2)We can architect a compiler with three major parts:

Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak10Major Parts of a CompilerFront endParser, Scanner, Source, Token

Intermediate tier

Intermediate code (icode)Predigested form of the source code that the back end can process efficiently.Example: parse treesAKA intermediate representation (IR)

Symbol table (symtab)Stores information about the symbols (such as the identifiers) contained in the source program.

Back end

Code generatorProcesses the icode and the symtab in order to generate the object code.Only the front end needs to be source language-specific.

The intermediate tier and the back end can be language-independent!

Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak11What about Interpreters?An interpreter executes a source program instead of generating object code.

It executes a source program using the intermediate code and the symbol table.DemoComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak12Conceptual Design (Version 3)A compiler and an interpreter can both use the same front end and intermediate tier.

Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak13Comparing Compilers and InterpretersA compiler generates object code, but an interpreter does not.

Executing the source program from object code can be several orders of magnitude faster than executing the program by interpreting the intermediate code and the symbol table.

But an interpreter requires less effort to get a source program to execute faster turnaround time

DemoComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak14Comparing Compilers and Interpreters, contdAn interpreter maintains control of the source programs execution.

Interpreters often come with interactive source-level debuggers that allow you to refer to source program elements, such as variable names.

AKA symbolic debuggerComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak15Therefore ...

Interpreters are useful during program development. Compilers are useful to run released programs in a production environment.

In this course, you will ...

Modify an interpreter for the Pascal language.Develop a compiler for a language of your choice.You can invent your own programming language!Comparing Compilers and Interpreters, contdComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak16Key Steps for SuccessWhenever you develop a complex program such as a compiler or an interpreter, key first steps for success are:

Design and implement a proper framework. Develop initial components that are well-integrated with the framework and with each other. Test the framework and the component integration by running simple end-to-end tests.

Early component integration is critical, even if the initial components are greatly simplified and dont do very much. Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak17Key Steps for Success, contdTest your framework and components and get them working together as early as possible.

The framework and the initial components then form the basis upon which you can do further development.

You should always be building on code that already works.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak18Three Java Packages

TO:UML package andclass diagrams.PackageClassFROM:

Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak19Front End Class Relationships

+public-private#protected~packagehas atransientrelationshipabstractclassThese four framework classesshould be source language-independent.classfieldComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak20

Front End Fields and Methodssubclass ofor is a abstractmethodComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak21

The Abstract Parser ClassFields iCode and symTab refer to the intermediate code and the symbol table.

Field scanner refers to the scanner.

Abstract parse() and getErrorCount() methods.To be implemented by language-specific parser subclasses.

Convenience methods currentToken() and nextToken() simply call the currentToken() and nextToken() methods of Scanner.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak22

The Abstract Scanner ClassPrivate field currentToken refers to the current token, which protected method currentToken() returns.

Method nextToken() calls abstract method extractToken().To be implemented by language-specific scanner subclasses.

Convenience methods currentChar() and nextChar() call the corresponding methods of Source.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak23

The Token ClassField text is the string that comprises the token.

Field value is for tokens that have a value, such as a number.

Field type is the token type.

Fields lineNum and position tell where the token is in the source file.

Default method extract() will be overridden by language-specific token subclasses.

Convenience methods currentChar(), nextChar(), and peekChar() call the corresponding methods of the Source class.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak24

The Source ClassField reader is the reader of the source. Field line stores a single line from the source file.

Fields lineNum and currentPos keep track of the position of the current character.

Method currentChar() returns the current source character. Method nextChar() returns the next character.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak25Current Character vs. Next CharacterSuppose the source line contains ABCDE and weve already read the first character.currentChar() AnextChar() BnextChar() CnextChar() DcurrentChar() DcurrentChar() DnextChar() EnextChar() eolComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak26Messages from the Front EndThe Parser generates messages.Syntax error messagesParser summarynumber of source lines parsednumber of syntax errorstotal parsing time

The Source generates messages.For each source line:line numbercontents of the lineComputer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak27Front End Messages, contdWe want the message producers (Parser and Source) to be loosely-coupled from the message listeners.

The producers shouldnt care who listens to their messages.The producers shouldnt care what the listeners do with the messages.

The listeners should have the flexibility to do whatever they want with the messages.Computer Science Dept.Fall 2015: August 26CS 153: Concepts of Compiler Design R. Mak28Front End Messages, contdProducers implement the MessageProducer interface.Listeners implement the MessageListener interface.

A listener registers its interest in the messages from a producer.

Whenever a producer generates a message, it