1 / 15

Introduction to Compiling

Introduction to Compiling. M.A Doman. What is a compiler?. A compiler is a program that reads a program written in one language and translates it into another language Compiler reports the presence of errors in the source program. compiler. Target Program. Source Program.

jirair
Download Presentation

Introduction to Compiling

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. Introduction to Compiling M.A Doman

  2. What is a compiler? • A compiler is a program that reads a program written in one language and translates it into another language • Compiler reports the presence of errors in the source program compiler Target Program Source Program Error Messages

  3. Preprocessing • The first pass of the compiler • Reads each source file • Every time it hits an directive #include it goes and gets the header file named. It then reads and processes it. • Expands macros • Passes the results to the next phase of the compiler.

  4. Preprocessing • Macro preprocessing • Allows the user to define macros that are shorthand for larger constructs • Define variables/identifiers • #define _card.h • #define BUFFER_SIZE 256 Conditional expressions #if .. #else Contain parameters #define max(a,b)(a>b ? a: b)

  5. Node.H Stack.CPP Node.cpp Stack.h StackTest.cpp Preprocessor Compiler

  6. Compilation Model • Analysis • Breaks up the source into pieces and constructs and creates an intermediate representation of the source program • Synthesis • Constructs the desired target from the intermediate representation

  7. Compilation Analysis • Linear or lexical analysis • The source is read as a stream of characters and grouped into tokens position = initial + rate * 60; Tokens: position : Identifier of type double = : assignment operator initial : identifier of type double + : plus operator rate : Identifier of type double * : muliplication operator 60 : constant of 60 (int)

  8. Compilation Analysis • Hierarchal / Syntax analysis • Also called parsing • Groups the tokens into grammatical phrases that the compiler will later use to synthesize the logic. • Usually represented by a parse tree:

  9. Compilation Analysis • Example (broad) of a parse tree: position = initial + rate * 60; Operator = Operator + Identifier position Identifier Operator * initial Number Identifier 60 rate

  10. Compilation Analysis • Semantic Analysis • Checks the source program for semantic errors • Gathers type information • Does type checking Operator * Number Identifier IntoDouble 60 rate

  11. Preprocessing • The first pass of the compiler • Reads each source file • Every time it hits an directive #include it goes and gets the header file named. It then reads and processes it. • Expands macros • Passes the results to the next phase of the compiler.

  12. Preprocessing • Macro preprocessing • Allows the user to define macros that are shorthand for larger constructs • Define variables/identifiers • #define _card.h • #define BUFFER_SIZE 256 Conditional expressions #if .. #else Contain parameters #define max(a,b)(a>b ? a: b)

  13. Linking • Once all the object (.o) files are created, they are passed to the linker • All references are resolved • Finished product: an executable porgram

  14. Compiler StackTest.o Stack.o Node.o Linker Library functions a.exe

  15. Node.H Stack.CPP Node.cpp Stack.h StackTest.cpp StackTest.o Stack.o Node.o Preprocessor Linker Libraryfunctions Compiler a.exe

More Related