Download presentation
Presentation is loading. Please wait.
Published byAbel Gaines Modified over 7 years ago
1
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
2
About the Course Text book: Compilers – Principles, Techniques, & Tools The 8th – 16th Week (9 weeks) (Twice / week, on Monday morning and Wednesday afternoon) Final Examination: 17th week Final Grade: 20% attendance + 20% homework + 60% final exam Text book download: piler/Compilers- Principes%20Techniques%20and %20Tools.pdf
3
Introduction
4
1.1 Language Processors-compiler
What is a compiler? All the software running on all the computers was written in some program language. Before a program can be run, it first must be translated into a form in which it can be executed by a computer. The software system that do this translation are called compiler. Simply stated, a compiler is a program that can read a program in one language – the source language – and translate it into an equivalent program in another language – the target language. Source Program Compiler Target Program input Target Program output
5
1.1 Language Processors-interpreter
An interpreter is another common kind of language processor. It directly execute the operations specified in the source program on inputs supplied by the user A hybrid compiler Source Program Compiler output input Source Program Translator Intermediate program Target Program output input
6
1.1 Language Processors-system
Several other programs may be required to create an executable target program. Preprocessor Assembler Linker/Loader Questions What is a linker? What is a Loader?
7
1.2 The Structure of a Compiler
8
1.2 The Structure of a Compiler-Lexical Analysis
Lexical Analysis (Scanning) Reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexemes. For each lexeme, the lexical analyzer produces as output a token of the form E.g. Position = initial + rate * 60 <id, 1> <=> <id, 2> <+> <id,3> <*> <60> <number, 0xFFFF1230>
9
1.2 The Structure of a Compiler-Syntax Analysis
Syntax Analysis (parsing) uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream.
10
1.2 The Structure of a Compiler-Semantic Analysis
The semantic analysis uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition. It also gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation. Type checking
11
1.2 The Structure of a Compiler-Intermediate Code Generation
After syntax and semantic analysis of the source program, many compilers generate an explicit low-level or machine- like intermediate representation, which we can think of as a program for an abstract machine. Two important properties: it should be easy to produce it should be easy to translate into the target machine Three-address code t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3
12
1.2 The Structure of a Compiler-Code Optimization
The machine-independent code-optimization phase attempts to improve the intermediate code so that better target code will result. t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 t1 = id3 * 60.0 id1 = id2 + t1
13
1.2 The Structure of a Compiler- Code Generation
The code generator takes as input an intermediate representation of the source program and maps it into the target language. If the target language is machine code, registers or memory locations are selected for each of the variables used by the program. Then, the intermediate instructions are translated into sequences of machine instructions that perform the same task. judicious assignment of registers to hold variables LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1, R1, R2 STF id, R1 t1 = id3 * 60.0 id1 = id2 + t1
14
1.2 The Structure of a Compiler
Symbol-Table Management Compiler-Construction Tools Lex Yacc …..
15
1.3 Evolution of Programming Languages
First-generation language Machine language Early 1950’s, Second-generation language - mnemonic assembly language Mnemonic representations of machine instructions Latter half of the 1950’s, Third-generation language – higher- level language Fortran (scientific computation) Cobol (business data processing) Lisp (symbolic computation) Object-oriented language (C++, Java) Fourth-generation language (for specific application) SQL for database, Postscript for text formatting Fifth-generation Logic- and constraint-based languages, Prolog
16
Some Classifications of Programming Languages
Imperative language A program specifies how a computation is to be done C, C++, C#, Java Declarative language A program specifies what computation is to be done Functional language such as ML and Haskell Object-oriented language is one that support object- oriented programming Scripting languages are interpreted languages with high- level operators designed for “gluing together” computations. Awk, JavaScript, Perl, PHP, Python, Ruby and Tcl
17
1.6 Programming Language Basic
Data Type and Operation Numeric data type: integer, real number (float and double), complex (Operations, + - * \ %) Logic data type: Boolean (Operations: and, or not) Character Pointer Data structure Array Record (struct) Abstract data structure Class Statement and control structure Expression Assignment statement Control statement If then else For Do while switch
18
1.6 Programming Language Basic
The static/Dynamic Distinction Static policy: the issue can be decided at compile time; Dynamic policy: the issue only can be decided at run time. Understand keyword static in some programming language Environments and States The environment is a mapping form names to locations in the store. Variables refer to locations (“l-values” in the terminology of C) The state is a mapping from locations in store to their values. r-values
19
1.6 Programming Language Basic
The static/Dynamic Distinction Static policy: the issue can be decided at compile time; Dynamic policy: the issue only can be decided at run time. Understand keyword static in some programming language Environments and States The environment is a mapping form names to locations in the store. Variables refer to locations (“l-values” in the terminology of C) The state is a mapping from locations in store to their values. r-values
20
1.6 Programming Language Basic
Main() { int a = 1; int b = 1; { int b = 2; int a = 3; cout<<a<<b; } int b = 4; cout << a << b; cout << a<<b;
21
1.6 Programming Language Basic
Names, Identifiers and Variables Name and variable refer to the same thing We use name in compile-time and use variable in run-time Identifier is a string of characters, typically letters or digits, that refers to (identifies) an entity, such as a data object, a procedure, a class, or a type. All identifiers are names, but not all names are identifiers. Names can also be expressions. For example, the name x.y might denote the field y of a structure denoted by x. Here, x and y are identifiers, while x.y is a name, but not an identifier. Static Scope and Block Structure The scope rules for C are based on program structure; the scope of a declaration is determined implicitly by where the declaration appears in the program. Later languages, such as C++, Java, and C#, also provide explicit control over scopes through the use of keywords like public, private, and protected
22
1.6 Programming Language Basic
Static-scope rules for a language with blocks, where block is a grouping of declaration and statement Declarations and Definitions Declaration tells us the type of things Definition tell us about their values.
23
1.6 Programming Language Basic - Parameter Passing Mechanisms
How the actual parameters (the parameters used in the call of a procedure) are associated with the formal parameters (those used in the procedure definition). Call by Value: In call-by-value, the actual parameter is evaluated (if it is an expression) or copied (if it is a variable) . Call by Reference: In call-by-reference, the address of the actual parameter is passed to the callee as the value of the corresponding formal parameter. Call by Name the actual parameter were substituted literally for the formal parameter in the code of the callee, as if the formal parameter were a macro standing for the actual parameter
24
Others The generation of a compiler Directly implementation
Write a compiler for L2 language running on Machine A using L1 language and its compiler running on Machine A Transplant of a compiler Machine A -> Machine B
25
Homework Illustrate the phases of a compile. What is the main function for each phase? What are the differences among name, identifier and variable? What are the differences between declaration and definition? Briefly explain the parameter passing mechanisms “call-by- value” and “call-by-reference”.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.