Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.

Similar presentations


Presentation on theme: "What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language."— Presentation transcript:

1 What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language (target language). –Traditionally, the source language is a high level language and the target language is a low level language (machine code). compiler Source program Target program Error message

2 Why do we need to learn how a compiler works? –It is very unlikely for anyone to write another C/C++/java compiler. –But … Parsing a small language occurs quite often: scripts languages such as python, parsing a set of commands for a mail server, etc. For systems people, the compiler knowledge is the basic requirement. –A new processor is fast because it runs code generated by a compiler fast. –If you implement a new operating system, you need to know how compiler interacts with the OS. An OS is fast because it runs code generated by a compiler fast. –New architectures keep coming: new compiler optimizations are always needed.

3 Knowledge required to write a good compiler: –Formal language (lexical analysis and parsing) –Programming language (activation record, scope, type checking, etc) –Algorithm (optimization, register allocation, etc) –Graph theory (optimization, data flow analysis) –Computer architecture (target language, machine dependent optimizations) –Software engineering (large amount of code)

4 Source program with macros Preprocessor Source program Compiler Target assembly program assembler Relocatable machine code Loader/link-editor Absolute machine code Try gcc with –v, -E, -S flags On linprog. A typical compilation process

5 Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol table manager Error handler Front End Backend

6 Compiler phases: –source program is a stream of characters: pos = init+rate*60 –lexical analysis: groups characters into non-separatable units, called token, and generates token stream: Id1 = id2 + id3 * const the information about the identifiers must be stored somewhere (symbol table). –Syntax analysis: checks whether the token stream meets the grammatical specification of the language and generates the syntax tree. –Semantic analysis: checks whether the program has a meaning (e.g. if pos is a record and init and rate are integers then the assignment does not make sense). = Id1+ id2* id360

7 Compiler phases: –intermediate code generation, intermediate code is something that is both close to the final machine code and easy to manipulate (for optimization). One example is the three-address code: dst = op1 op op2 The three-address code for the assignment statement: temp1 = 60; temp2 = id3 + temp1; temp3 = id2 + temp2; id1 = temp3 –Code optimization: produces better/semantically equivalent code. temp1 = id3 * 60.0 id1 = id2 + temp1 –code generator: generates assembly MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1

8 The use of intermediate code can also reduce the complexity of compilers: –m front ends and n backends share a common intermediate code. 1 2 m IC 1 2 n Front Ends Backends

9 Compiler construction tools: –scanner generator (lex) –parser generator (yacc) –syntax-directed translation engines –automatic code generators –data-flow engines


Download ppt "What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language."

Similar presentations


Ads by Google