1 / 20

Compilation in More Detail

Principles of Programming Languages. Compilation in More Detail. Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department. Phases of Compilation. The Structure of a Compiler. Lexical Analysis Parsing Semantic Analysis Symbol Table Optimization

forbes
Télécharger la présentation

Compilation in More Detail

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Principles of Programming Languages Compilation in More Detail Asst. Prof. Dr. AhmetSayar Spring-2012 Kocaeli University Computer Engineering Department

  2. Phases of Compilation

  3. The Structure of a Compiler • Lexical Analysis • Parsing • Semantic Analysis • Symbol Table • Optimization • Code Generation The first 3, at least, can be understood by analogy to how humans comprehend English.

  4. 1. Lexical Analysis • First step: recognize words. • Smallest unit above letters This is a sentence. • Note the • Capital “T” (start of sentence symbol) • Blank “ “ (word separator) • Period “.” (end of sentence symbol)

  5. More Lexical Analysis • Lexical analysis is not trivial. Consider: ist his ase nte nce • Plus, programming languages are typically more cryptic than English: *p->f += -.12345e-5

  6. And More Lexical Analysis • Lexical analyzer divides program text into “words” or “tokens” if x == y then z = 1; else z = 2; • Units: if, x, ==, y, then, z, =, 1, ;, else, z, =, 2, ;

  7. 2. Parsing • Once words are understood, the next step is to understand sentence structure • Parsing = Diagramming Sentences • The diagram is a tree

  8. This line is a longer sentence article noun verb article adjective noun subject object sentence Diagramming a Sentence

  9. x == y z 1 z 2 relation assign assign predicate then-stmt else-stmt if-then-else Parsing Programs • Parsing program expressions is the same • Consider: If x == y then z = 1; else z = 2; • Diagrammed:

  10. 3. Semantic Analysis • Once sentence structure is understood, we can try to understand “meaning” • But meaning is too hard for compilers • Compilers perform limited analysis to catch inconsistencies • Some do more analysis to improve the performance of the program Semantic Analyzer Parse tree Intermediate Program

  11. Semantic Analysis in English • Example: Jack said Jerry left his assignment at home. What does “his” refer to? Jack or Jerry? • Even worse: Jack said Jack left his assignment at home? How many Jacks are there? Which one left the assignment?

  12. { int Jack = 3; { int Jack = 4; cout << Jack; } } Semantic Analysis in Programming • Programming languages define strict rules to avoid such ambiguities • This C++ code prints “4”; the inner definition is used

  13. More Semantic Analysis • Compilers perform many semantic checks besides variable bindings • Example: Jack left her homework at home. • A “type mismatch” between her and Jack; we know they are different people • Presumably Jack is male

  14. 4. Symbol Table • After the lexical analyzing and parsing, symbol table is created. • Lexical anayzer gives symbol table as an output • Below table shows tokens for a pascal statemet • toplam:=değer+10;

  15. 5. Optimization • No strong counterpart in English, but akin to editing • Automatically modify programs so that they • Run faster • Use less memory • In general, conserve some resource

  16. Machine independent optimization • Result = function1(a+b) + function2(a+b) • How do you optimize this code?

  17. Machine independent optimization • Code motion • Invariant expressions should be executed only once • E.g. for (inti = 0; i < x.length; i++) x[i] *= Math.PI * Math.cos(y); double picosy = Math.PI * Math.cos(y); for (inti = 0; i < x.length; i++) x[i] *= picosy;

  18. Machine dependent optimization • Multiplying a number with the power of 2 • Shift to the left • Dividing a number with the power of 2 • Shift to the right

  19. Issues • Compiling is almost this simple, but there are many pitfalls. • Example: How are erroneous programs handled? • Language design has big impact on compiler • Determines what is easy and hard to compile • Course theme: many trade-offs in language design

  20. Compilers Today • The overall structure of almost every compiler adheres to this outline • The proportions have changed since FORTRAN • Early: lexing, parsing most complex, expensive • Today: optimization dominates all other phases, lexing and parsing are cheap

More Related