Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 1 Introduction Dr. Frank Lee

2 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,

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

4 1.3 Phases of Compilation 1.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).

5 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

6 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

7 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

8 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

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

10 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

11 Steps in Compilation Process (Back End) abstract assembly tree Code Generator Assembler Linker assembly language machine code executable code language libraries

12 Chapter 1 Homework Due: 1/30/2013 Search the detail definition from Internet for the following computer terms: 1.Interpreter 2.Compiler 3.Phases of a compiler 4.Regular Expressions 5.Lexical Analyzer 6.Context-free Grammars 7.Parser 8.Semantic Analyzer 9.Type Checker 10. Code Generator 11. Assembler 12. Linker 13. Loader Site your sources from Internet.


Download ppt "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."

Similar presentations


Ads by Google