12
Chapter 1 Introduction Dr. Frank Lee

Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

Embed Size (px)

Citation preview

Page 1: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

Chapter 1

Introduction

Dr. Frank Lee

Page 2: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.1 Why Study Compiler?• To write more efficient code in a high-level language• To provide solid foundation in parsing theory for parser

writing• To make compiler design as an excellent “capstone” project• To apply almost all of the major computer science fields,

such as – automata theory, – computer programming, – programming language design theory, – assembly language, – computer architecture, – data structures, – algorithms, and

– software engineering,

Page 3: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.2 Basic Compiler Design

• Write a huge program that takes as input another program in the source language for the compiler, and gives as output an executable that we can run.

• For modifying code easily, usually, we use modular design (decomposition) methodology to design a compiler.

• Two design strategies:1. Write a “front end” of the compiler (i.e. the lexer, parser,

semantic analyzer, and assembly tree generator), and write a separate back end for each platform that you want to support

2. Write an efficient highly optimized back end, and write a different front end for several languages, such as Fortran, C, C++, and Java.

Page 4: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.3 Phases of Compilation1.3.1 Lexical Analysis• The low-level text processing portion of the compiler• The source file, a stream of characters, is broken into larger chunks

called token, for example:void main() {

int x;

X=3;}

• The lexical analyzer scans through the inputs file and returns a stream of tokens.

• Typically, spaces, tabs, end-of-line characters and comments are ignored by the lexical analyzer

• The example above will be broken into 13 tokens as below:void main ( ) { int x ; X = 3 ; }

• To design a lexical analyzer: input a description (regular expressions) of the tokens in the language, and output a lexical analyzer (a program).

Page 5: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.3.2 Parsing

• The parser ensures that the sequence of tokens returned by the lexical analyzer forms a syntactically correct program

• It catches the syntax errors as the statement below:if if (x > 3) then x = x + 1

• The parser also builds a structured representation of the program called an abstract syntax tree that is easier for the type checker to analyze than a stream of tokens

• We also use a tool to generate a parser• Context-free grammars will be used (as the input) by the

parser generator to describe the syntax of the compiling language

Page 6: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.3.3 Semantic Analysis

• To detect the semantics (meanings) errors of the program, such as use variables before they are declared, assign an integer value to a Boolean variable.

• See page 3 for other semantic rules• In the semantic analysis phase, the abstract

syntax tree that is produced by the parser is traversed, looking for semantic errors

• The main tool used by the semantic analyzer is a symbol table

Page 7: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.3.4 Intermediate Code Generation

• If there is no compile-time errors, the semantic analyzer translates the abstract syntax tree into the abstract assembly tree

• The abstract assembly tree could simply the code optimization and assembly code generation

Page 8: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.3.5 Assembly Code Generation

• Code generator coverts the abstract assembly tree into the actual assembly code

• To do code generation– the generator covers the abstract assembly

tree with tiles (each tile represents a small portion of an abstract assembly tree) and

– output the actual assembly code associated with the tiles that we used to cover the tree

Page 9: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

1.3.6 Machine Code Generation and Linking

• The final phase of compilation coverts the assembly code into machine code and links (by a linker) in appropriate language libraries

• See next two slides

• Also see Fig 1.2 on page 2.

Page 10: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

Steps in Compilation Process(Front End)

source code

Lexical Analyzer

Parser

Semantic Analyzer

tokens

parse tree (abstract syntax tree)

abstract assembly tree

regular expressions

context free grammars

Page 11: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

Steps in Compilation Process(Back End)

abstract assembly tree

Code Generator

Assembler

Linker

assembly language

machine code

executable code

language libraries

Page 12: Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing

Chapter 1 Homework

• Due: 1/30/2013• Search the detail definition from Internet

for the following computer terms:1. Interpreter2. Compiler3. Phases of a compiler4. Regular Expressions5. Lexical Analyzer6. Context-free Grammars7. Parser8. Semantic Analyzer9. Type Checker10. Code Generator11. Assembler12. Linker13. Loader

• Site your sources from Internet.