150 likes | 281 Views
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.
E N D
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 Error Messages
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.
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)
Node.H Stack.CPP Node.cpp Stack.h StackTest.cpp Preprocessor Compiler
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
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)
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:
Compilation Analysis • Example (broad) of a parse tree: position = initial + rate * 60; Operator = Operator + Identifier position Identifier Operator * initial Number Identifier 60 rate
Compilation Analysis • Semantic Analysis • Checks the source program for semantic errors • Gathers type information • Does type checking Operator * Number Identifier IntoDouble 60 rate
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.
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)
Linking • Once all the object (.o) files are created, they are passed to the linker • All references are resolved • Finished product: an executable porgram
Compiler StackTest.o Stack.o Node.o Linker Library functions a.exe
Node.H Stack.CPP Node.cpp Stack.h StackTest.cpp StackTest.o Stack.o Node.o Preprocessor Linker Libraryfunctions Compiler a.exe