
5d1edb0bbb540911f292e5aa7b7f109e.ppt
- Количество слайдов: 32
Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University 1
Objectives ª Introduce principles and techniques for compiler construction ª Introduce principles and techniques for compiler-compiler construction ª Use a compiler-compiler to exercise the compiler construction for a small language 2
Introduction 3
Programming Languages ª Human use nature languages to communicate with each other – Chinese, English, French ª Human use programming languages to communicate with computers – Fortran, Pascal, C 4
Computer Organization Applications Compiler Operating System Hardware Machine 5
Compiler Source Code Compiler Target Code Error Messages 6
Components of a Compiler ª Analysis – Lexical Analysis – Syntax Analysis – Semantic Analysis ª Synthesis – Intermediate Code Generation – Code Optimization – Code Generation 7
Lexical Analysis Someone the ice breaks Someone breaks the ice final : = initial + rate * 60 id 1 : = id 2 + id 3 * 60 8
Syntax Analysis Someone breaks the ice id 1 : = id 2 + id 3 * 60 sentence : = subject verb object id 1 id 2 + id 3 * 60 Someone breaks the ice 9
Semantic Anaysis Someone plays the piano (meaningful) The piano plays someone (meaningless) id 1 : = id 2 : = id 1 id 2 + * id 3 + id 3 * 60 i 2 r 60 10
Intermediate Code Generation Someone breaks the ice 有人打破冰 id 1 : = id 2 + id 3 * i 2 r 60 temp 1 : = i 2 r ( 60 ) temp 2 : = id 3 * temp 1 temp 3 : = id 2 + temp 2 id 1 : = temp 3 11
Code Optimization 有人打破冰 temp 1 : = i 2 r ( 60 ) temp 2 : = id 3 * temp 1 temp 3 : = id 2 + temp 2 id 1 : = temp 3 有人打破沉默 temp 1 : = id 3 * 60. 0 id 1 : = id 2 + temp 1 12
Code Generation 有人打破沉默 temp 1 : = id 3 * 60. 0 id 1 : = id 2 + temp 1 movf id 3, r 2 mulf #60. 0, r 2 movf id 2, r 1 addf r 2, r 1 movf r 1, id 1 13
Structure of a Compiler ª Front End – Lexical Analysis – Syntax Analysis – Semantic Analysis – Intermediate Code Generation ª Back End – Code Optimization – Code Generation 14
Reuse of Components Fortran Pascal C Intermediate Code MIPS SPARC Pentium 15
Applications ª Interpreters ª WWW Browsers ª Word Processors ª Computer-Aided Design ª Computer-Aided Manufacture 16
Compiler-Compiler Description Compiler-compiler Compiler Error Messages 17
Formal Language Theory language define grammar accept generate automaton automatically 18
Grammars ª The sentences in a language may be defined by a set of rules called a grammar L: {00, 01, 10, 11} G: the set of strings of length 2 19
Automata ª An acceptor of a language is an automaton which determines if an input string is a sentence in the language ª A transducer of a language is an automaton which determines if an input string is a sentence in the language and may produce strings as output if it is in the language 20
Transducers language L 1 language L 2 accept define grammar G 1 transduce automaton generate define grammar G 2 21
Metalanguage ª Metalanguage: a language used to define another language define metalanguage accept generate automatically automaton 22
Automatic Compiler Generation ª We will use different metalangauges to define the various components of a programming language so that these components can be generated automatically 23
Definition of Programming Languages ª Lexical syntax: regular expressions ª Syntax: context free grammars ª Semantics: attribute grammars ª Intermediate code generation: attribute grammars ª Code generation: tree grammars 24
Languages ª Alphabet - any finite set of symbols {0, 1}: binary alphabet ª String - a finite sequence of symbols from the alphabet 1011: a string of length 4 : the empty string ª Language - any set of strings on the alphabet {00, 01, 10, 11}: the set of strings of length 2 : the empty set 25
Terms for Parts of a String ª string: banana ª (proper) prefix: , b, ban, . . . , banana ª (proper) suffix: , a, na, ana, . . . , banana ª (proper) substring: , b, a, n, ba, an, na, . . . , banana ª subsequence: , b, a, n, ba, bn, aa, nn, . . . , banana ª sentence: a string in the language 26
Operations on Strings ª concatenation: x = dog y = house xy = doghouse ª exponentiation: s 0 = s 1 = s s 2 = ss 27
Operations on Languages ª Union of L and M, L M = { s | s L or s M} ª Concatenation of L and M, LM LM = {st | s L and t M} ª Kleene closure of L, L* L* = ª Positive closure of L, L+ L+ = 28
Implementation of Programming Languages ª Regular expressions: finite automata, lexical analyzer ª Context free grammars: pushdown automata, parser ª Attribute grammars: attribute evaluators, type checker and intermediate code generator ª Tree grammars: finite tree automata, code generator 29
Automatic Program Generators problem define specification language solve generate automatically program 30
Content ª Lexical analysis ª Syntax analysis ª Semantic analysis ª Intermediate code generation ª Code generation 31
共勉 子曰 ︰ 「學而時習之,不亦說乎? 有朋自遠方來,不亦樂乎? 人不知而不慍,不亦君子乎? 」 -- 論語 32