Download presentation
Presentation is loading. Please wait.
Published byNigel Lester Modified over 9 years ago
1
RIT 08/11/47Chapter 11 Chapter 1: Introduction to Compiling Dr. Winai Wichaipanitch Rajamangala Institute of Technology Klong 6 Thanyaburi Pathumthani 12110 Tel: 06-999-2974 wina@rit.ac.th http://www.en.rit.ac.th/winai
2
RIT 08/11/47Chapter 12 Purpose of Compiler Compilers translate a program written into one language (source) into another (target) Compiler Source program Target Program Error messages Diverse & Varied
3
RIT 08/11/47Chapter 13 Introduction to Compilers As a Discipline, Involves Multiple CS&E Areas Programming Languages and Algorithms Theory of Computing & Software Engineering Computer Architecture & Operating Systems
4
RIT 08/11/47Chapter 14 Translation Mechanisms Compilation To translate a source program in one language into an executable program in another language and produce results while executing the new program Examples: C, C++, FORTRAN Interpretation To read a source program and produce the results while understanding that program Examples: BASIC, LISP Case Study: JAVA First, translate to java bytecode Second, execute by interpretation (JVM)
5
RIT 08/11/47Chapter 15 Comparison of Compiler/Interpreter CompilerInterpreter Overview Advantages Fast program execution; Exploit architecture features; Easy to debug; Flexible to modify; Machine independent; Disadvanta ges Pre-processing of program; Complexity; Execution overhead; Space overhead; interpreter Source Code Data Results compiler Source Code Data Results Object code
6
RIT 08/11/47Chapter 16 Classifications of Compilers Compilers Viewed from Many Perspectives However, All utilize same basic tasks to accomplish their actions Single Pass Multiple Pass Load & Go Construction Debugging Optimizing Functional
7
RIT 08/11/47Chapter 17 เรายังไม่ทราบค่าแอดเดรส ดังนั้นต้องอ่าน Source code 2 ครั้ง
8
RIT 08/11/47Chapter 18 The Model The TWO Fundamental Parts: We Will Discuss Both in This Class, and FOCUS on analysis. Analysis: Synthesis: Decompose Source into an intermediate representation Target program generation from representation
9
RIT 08/11/47Chapter 19 Important Notes Today: There are many Software Tools for helping with the Analysis Part. This Wasn’t the Case in Early Days. (some) analysis is also important in: Structure / Syntax directed editors: Force “syntactically” correct code to be entered Pretty Printers: Standardized version for program structure (i.e., blank space, indenting, etc.) Static Checkers: A “quick” compilation to detect rudimentary errors Interpreters: “real” time execution of code a “line-at-a- time”
10
RIT 08/11/47Chapter 110 Important Notes Compilation Is Not Limited to Programming Language Applications Text Formatters LATEX & TROFF Are Languages Whose Commands Format Text Silicon Compilers Textual / Graphical: Take Input and Generate Circuit Design Database Query Processors Database Query Languages Are Also a Programming Language Input is compiled Into a Set of Operations for Accessing the Database
11
RIT 08/11/47Chapter 111 The Many Phases of a Compiler Source Program Lexical Analyzer 1 Syntax Analyzer 2 Semantic Analyzer 3 Intermediate Code Generator 4 Code Optimizer 5 Code Generator 6 Target Program Symbol-table Manager Error Handler 1, 2, 3 : Analysis - Our Focus 4, 5, 6 : Synthesis
12
RIT 08/11/47Chapter 112 Phases of A Modern Compiler Lexical Analyzer Syntax Analyzer Semantic Analyzer Code Optimizer Code Generation Source Program IF (a<b) THEN c=1*d; Token Sequence Syntax Tree 3-Address Code Optimized 3-Addr. Code Assembly Code IF( ID “a” < ID “b” THEN ID “c” = CONST “1” * ID “d” IF_stmt < a b cond_expr list assign_stmt c * lhs rhs 1 d GE a, b, L1 MUlT 1, d, c L1: GE a, b, L1 MOV d, c L1: loadi R1,a cmpi R1,b jge L1 loadi R1,d storei R1,c L1:
13
RIT 08/11/47Chapter 113 Language-Processing System Source Program Pre-Processor 1 Compiler 2 Assembler 3 Relocatable Machine Code 4 Loader Link/Editor 5 Executable Library, relocatable object files
14
RIT 08/11/47Chapter 114 Three Phases: Linear / Lexical Analysis: L-to-r Scan to Identify Tokens token: sequence of chars having a collective meaning Hierarchical Analysis: Grouping of Tokens Into Meaningful Collection Semantic Analysis: Checking to ensure Correctness of Components The Analysis Task For Compilation
15
RIT 08/11/47Chapter 115 Phase 1. Lexical Analysis Easiest Analysis - Identify tokens which are the basic building blocks For Example: All are tokens Blanks, Line breaks, etc. are scanned out Position := initial + rate * 60 ; _______ __ _____ _ ___ _ __ _
16
RIT 08/11/47Chapter 116 Phase 2. Hierarchical Analysis aka Parsing or Syntax Analysis For previous example, we would have Parse Tree: identifier expression identifier expression number expression assignment statement position := + * 60 initial rate Nodes of tree are constructed using a grammar for the language
17
RIT 08/11/47Chapter 117 What is a Grammar? Grammar is a Set of Rules Which Govern the Interdependencies & Structure Among the Tokens statementis anassignment statement, or while statement, or if statement, or... assignment statement expression is an identifier := expression ; (expression), or expression + expression, or expression * expression, or number, or identifier, or...
18
RIT 08/11/47Chapter 118 if statement if expression then statement else statement ; id relop num=0 assign statement id:=expression id 0 assign statement id:=expression id mulop avg num/ Syntax Tree
19
RIT 08/11/47Chapter 119 Why Have We Divided Analysis in This Manner? Lexical Analysis - Scans Input, Its Linear Actions Are Not Recursive Identify Only Individual “words” that are the the Tokens of the Language Recursion Is Required to Identify Structure of an Expression, As Indicated in Parse Tree Verify that the “words” are Correctly Assembled into “sentences”
20
RIT 08/11/47Chapter 120 Phase 3. Semantic Analysis Find More Complicated Semantic Errors and Support Code Generation Parse Tree Is Augmented With Semantic Actions position initial rate := + * 60 Compressed Tree position initial rate := + * inttoreal 60 Conversion Action
21
RIT 08/11/47Chapter 121 Phase 3. Semantic Analysis Most Important Activity in This Phase: Type Checking - Legality of Operands
22
RIT 08/11/47Chapter 122 Supporting Phases/ Activities for Analysis Symbol Table Creation / Maintenance Contains Info (storage, type, scope, args) on Each “Meaningful” Token, Typically Identifiers Data Structure Created / Initialized During Lexical Analysis Utilized / Updated During Later Analysis & Synthesis
23
RIT 08/11/47Chapter 123 Symbol Table for Example
24
RIT 08/11/47Chapter 124 Detection of Different Errors Which Correspond to All Phases What Kinds of Errors Are Found During the Analysis Phase? What Happens When an Error Is Found? Error Handling
25
RIT 08/11/47Chapter 125 The Many Phases of a Compiler Source Program Lexical Analyzer 1 Syntax Analyzer 2 Semantic Analyzer 3 Intermediate Code Generator 4 Code Optimizer 5 Code Generator 6 Target Program Symbol-table Manager Error Handler 1, 2, 3 : Analysis - Our Focus 4, 5, 6 : Synthesis
26
RIT 08/11/47Chapter 126 The Synthesis Task For Compilation Intermediate Code Generation Abstract Machine Version of Code - Independent of Architecture Easy to Produce and Do Final, Machine Dependent Code Generation Code Optimization Find More Efficient Ways to Execute Code Replace Code With More Optimal Statements 2-approaches: High-level Language & “Peephole” Optimization Final Code Generation Generate Relocatable Machine Dependent Code
27
RIT 08/11/47Chapter 127 Reviewing the Entire Process ErrorsErrors position := initial + rate * 60 lexical analyzer syntax analyzer semantic analyzer intermediate code generator id1 := id2 + id3 * 60 := id1 id2l id3 + * 60 := id1 id2l id3 + * inttoreal 60 Symbol Table position.... initial …. rate….
28
RIT 08/11/47Chapter 128 Reviewing the Entire Process ErrorsErrors intermediate code generator code optimizer final code generator temp1 := inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 temp1 := id3 * 60.0 id1 := id2 + temp1 MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R1, R2 MOVF R1, id1 position.... initial …. rate…. Symbol Table 3 address code
29
RIT 08/11/47Chapter 129Assemblers Assembly code: names are used for instructions, and names are used for memory addresses. Two-pass Assembly: First Pass: all identifiers are assigned to memory addresses (0-offset) e.g. substitute 0 for a, and 4 for b Second Pass: produce relocatable machine code: MOV a, R1 ADD #2, R1 MOV R1, b 0001 01 00 00000000 * 0011 01 10 00000010 0010 01 00 00000100 * relocation bit
30
RIT 08/11/47Chapter 130 Loaders and Link-Editors Loader: taking relocatable machine code, altering the addresses and placing the altered instructions into memory. Link-editor: taking many (relocatable) machine code programs (with cross-references) and produce a single file. Need to keep track of correspondence between variable names and corresponding addresses in each piece of code.
31
RIT 08/11/47Chapter 131 Compiler Cousins: Preprocessors Compiler Cousins: Preprocessors Provide Input to Compilers 1. Macro Processing #define in C: does text substitution before compiling #define X 3 #define Y A*B+C #define Z getchar()
32
RIT 08/11/47Chapter 132 2. File Inclusion #include in C - bring in another file before compiling defs.h ////// main.c #include “defs.h” …---…---…--- ////// …---…---…---
33
RIT 08/11/47Chapter 133 3. Rational Preprocessors Augment “Old” Languages With Modern Constructs Add Macros for If - Then, While, Etc. #Define Can Make C Code More Pascal-like #define begin { #define end } #define then
34
RIT 08/11/47Chapter 134 4. Language Extensions for a Database System EQUEL - Database query language embedded in C ## Retrieve (DN=Department.Dnum) where ## Department.Dname = ‘Research’ is Preprocessed into: ingres_system(“Retr…..Research’”,____,____); a procedure call in a programming language.
35
RIT 08/11/47Chapter 135 The Grouping of Phases Front End : Analysis + Intermediate Code Generation Back End : Code Generation + Optimization vs. Number of Passes: A pass: requires r/w intermediate files Fewer passes: more efficiency. However: fewer passes require more sophisticated memory management and compiler phase interaction. Tradeoffs ……..
36
RIT 08/11/47Chapter 136 Compiler Construction Tools Parser Generators : Produce Syntax Analyzers Scanner Generators : Produce Lexical Analyzers Syntax-directed Translation Engines : Generate Intermediate Code Automatic Code Generators : Generate Actual Code Data-Flow Engines : Support Optimization
37
RIT 08/11/47Chapter 137 Tools Tools exist to help in the development of some stages of the compiler Lex (Flex) - lexical analysis generator Yacc (Bison) - parser generator
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.