Docsity

Compiler Construction Part 3-Compiler Construction-Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Compiler Construction Part 3-Compiler Construction-Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler ConstructionCompiler Construction

Lecture 03

LayoutLayout

  1. Grouping of Phases
  2. T Diagramsg
  3. Bootstrapping 4 4. PortingPorting
  4. Compiler Construction Tools

Some Issues in Compilerp Structure

  1. The Grouping of Phases
    • • Analysis and SynthesisAnalysis and Synthesis
    • Front End and Back End
  2. Passes
  3. Compiler Construction Toolsp
  4. Error Handling

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^33)

Grouping of PhasesGrouping of Phases

ƒ Analysis ƒ Synthesis sourceprogram analyser lexical analysersyntax semanticanalyser generatorcode objectprogram

Front End := Back End velocity (^) + … velocity := initial_velocity + acceleration * time; …

initial_velocity l i

acceleration (^) time

Compiler PassesCompiler Passes

ƒ Compilers are complex programs. As a consequence they are broken into several distinct chunks called passes, that communicate with one another via temporary files. ƒ These passes are only a part of the compilation process. ƒ Structure of a typical four pass compiler is shown on next slide.

PassesPasses

ƒ First pass is the Preprocessor that typically performs

  • MMacro substitution b tit ti
  • Strip Comments from Source etc. ƒƒ The second pass is the heart of the compilerThe second pass is the heart of the compiler. It is It is made up of a Lexical Analyzer, Parser and code generator and it translates the source code into ang intermediate language that is much like an assembly language. 15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^99)

PassesPasses

ƒ The third pass is the optimizer that improves the quality of the generated intermediate code & ƒƒ TheThe fourthfourth passpass, thethe backback endend, translatestranslates thethe optimized code to real assembly language or some form of binary executable code. ƒ Number of Passes

  • Single - Preferred efficient compilation but less efficient target codetarget code
  • Multiple - Easier, less efficient compilation but efficient target code
  • Tradeoffs
  • Tradeoffs ……..

Bootstrapping and PortingBootstrapping and Porting

Compiler ForLanguage A Running LanguageWritten in A C Existingil f C il f Language B

Compiler for Language A

Compiler for Language B

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^1313)

T DiagramsT Diagrams

A compiler written in Language H (for host language) that

translates language S (for Source Language) into language T

(for Target Language) can be drawn as following T Diagram

S T H

T Diagrams (contd )T Diagrams (contd.)

To Directly Translate from Language A to C

A B B C A C H H H

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^1515)

T Diagrams (contd )T Diagrams (contd.)

Compiler Form Machine “H” to Machine “K”

A B A B H H K K

K

T Diagrams (contd )T Diagrams (contd.)

It is common to write a compiler in the same language that it is to compile.

S T S

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^1919)

Bootstrapping (Step 1)Bootstrapping (Step 1)

A H

A H A

A H A H H

A (^) H

Compiler written in its own Language A (^) “Quick and Dirty” Compiler written in itsM hine L ng ge

Running but inefficient Compiler Machine Language

Bootstrapping (Step 2)Bootstrapping (Step 2)

A H (^) A H A H H

A H A

A H H H

its own Language ACompiler written in Running but inefficient its own Language A Final Version of theCompiler Running but inefficient Compiler (from first stage)

Compiler

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^2121)

BootstrappingBootstrapping

ƒ After bootstrapping we have a compiler in both source code and the existing code.

ƒƒ The advantage of this is that any improvement toThe advantage of this is that any improvement to the source code of the compiler can be immediately bbootstrapped to working compiler by applying the t t d t ki il b l i th same two-step process as before.

Compiler Construction ToolsCompiler Construction Tools

ƒ PP arser GeneratorsG t

  • Produce Syntax Analyzers ƒƒ Scanner GeneratorsScanner Generators
  • Produce Lexical Analyzers ƒƒ Syntax directed Translation EnginesSyntax-directed Translation Engines
  • Generate Intermediate Code ƒ AutomaticAutomatic CodeCode GeneratorsGenerators
  • Generate Actual Code ƒ Data-Flow Engines 15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^2525)

g

  • Support Optimization

CC ompiler Construction Toolsil C t ti T l

  1. YaYacc Parser Generates
    • YaYacc, or Yet Another Yacc, generates C++ parsers.
  2. Bison Parser Generator
    • Bison generates a parser when presented with context- free grammar that is yacc compatible. The generated parser is in C 3 3. ElElex Scanner Generator S G
    • Elex is a lexical scanner (lexer) generator that supports multiple programming languages multiple programming languages

CC ompiler Construction Toolsil C t ti T l

  1. JACCIE (Java-based Compiler Compiler)
    • Jaccie includes a scanner generator and a variety ofg y parser generators.
  2. Byacc (Berkeley YACC)y ( y )
    • Berkeley YACC
    • And many more....And many more....

15 15--OctOct--10 10 Sufi Tabassum GulSufi Tabassum Gul (^2727)

ConclusionConclusion

  1. Issues related to compiler structure
  2. Bootstrapping and porting
  3. Grouping of phases

4 4. Compiler construction toolsCompiler construction tools