CMPE 152: Compiler Design January 29 Class Meeting

Slides:



Advertisements
Similar presentations
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Advertisements

CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 1 Introduction Major Data Structures in Compiler
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 160 and CMPE/SE 131 Software Engineering February 25 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
CS 432: Compiler Construction Lecture 1
Advanced Computer Systems
CS 153: Concepts of Compiler Design September 14 Class Meeting
Compiler Design (40-414) Main Text Book:
CS 153: Concepts of Compiler Design August 24 Class Meeting
Introduction to Compiler Construction
CS 3304 Comparative Languages
CS 153: Concepts of Compiler Design August 29 Class Meeting
Compiler Construction (CS-636)
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 432: Compiler Construction Lecture 2
CS 153: Concepts of Compiler Design August 31 Class Meeting
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
CMPE 152: Compiler Design January 25 Class Meeting
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design September 6 Class Meeting
CMPE 152: Compiler Design August 30 Class Meeting
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 21 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
CMPE 152: Compiler Design August 28 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CMPE 152: Compiler Design January 31 Class Meeting
CMPE 152: Compiler Design January 24 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 28 / March 5 Lab
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 7 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE/SE 131 Software Engineering March 7 Class Meeting
CMPE 152: Compiler Design April 30 Class Meeting
CMPE 152: Compiler Design August 22 Class Meeting
CMPE 152: Compiler Design September 3 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
CMPE/SE 131 Software Engineering February 22 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
Presentation transcript:

CMPE 152: Compiler Design January 29 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Reminder: By Wednesday, January 30 Form teams. Email me your team information. team name team members and email addresses

Conceptual Design (Version 1) Parser Controls the translation process. Repeatedly asks the scanner for the next token. Scanner Repeatedly reads characters from the source to construct tokens for the parser. Token A source language element identifier (name) number special symbol (+ - * / = etc.) reserved word Also reads from the source Source The source program

Conceptual Design (Version 2) We can architect a compiler with three major parts:

Major Parts of a Compiler Front end Parser, Scanner, Source, Token Intermediate tier Intermediate code (icode) “Predigested” form of the source code that the back end can process efficiently. Example: parse trees AKA intermediate representation (IR) Symbol table (symtab) Stores information about the symbols (such as the identifiers) contained in the source program. Back end Code generator Processes 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!

What 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.

What Else Can Compilers Do? cont’d Compilers 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.

What 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.

Conceptual Design (Version 3) A compiler and an interpreter can both use the same front end and intermediate tier.

Comparing Compilers and Interpreters A 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 Demo

Comparing Compilers and Interpreters, cont’d An interpreter maintains control of the source program’s execution. Interpreters often come with interactive source-level debuggers that allow you to refer to source program elements, such as variable names. AKA symbolic debugger

Comparing Compilers and Interpreters, cont’d Therefore ... 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!

Take roll!

Key Steps for Success Whenever 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 don’t do very much.

Key Steps for Success, cont’d Test your framework and components. 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.

Three Java Packages / C++ Namespaces FROM: TO: Package Class UML package and class diagrams.

Front End Class Relationships field “has a” abstract class transient relationship class + public - private # protected ~ package These four framework classes should be source language-independent.

Current Character vs. Next Character Suppose the source line contains ABCDE and we’ve already read the first character. currentChar() A nextChar() B C D E eol

Messages from the Front End The Parser generates messages. Syntax error messages Parser summary number of source lines parsed number of syntax errors total parsing time The Source generates messages. For each source line: line number contents of the line

Front End Messages, cont’d We want the message producers (Parser and Source) to be loosely-coupled from the message listeners. The producers shouldn’t care who listens to their messages. The producers shouldn’t care what the listeners do with the messages. The listeners should have the flexibility to do whatever they want with the messages.

Front End Messages, cont’d Producers 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 “sends” the message to all of its registered listeners.

Front End Messages, cont’d A message producer can delegate message handling to a MessageHandler. This is the Observer Design Pattern.

Two Message Types SOURCE_LINE message PARSER_SUMMARY message the source line number text of the source line PARSER_SUMMARY message number of source lines read number of syntax errors total parsing time By convention, the message producers and the message listeners agree on the format and content of the messages.

Good Framework Symmetry

Detailed UML Diagrams From the textbook: Front end fields and methods The abstract parser class The abstract scanner class The token class The source class Message implementation Why should the parser and scanner classes be abstract?

Front End Fields and Methods abstract method “subclass of” or “is a”

The Abstract Parser Class Fields 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.

The Abstract Scanner Class Private 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.

The Token Class Field 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.

The Source Class Field 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.

Message Implementation Message producers implement the MessageProducer interface. Message listeners implement the MessageListener interface. A message producer can delegate message handling to a MessageHandler. Each Message has a message type and a body. “implements” This appears to be a lot of extra work, but it will be easy to use and it will pay back large dividends. multiplicity “zero or more”