SCSX1019 System programming Unit III Unit III Compiler is a translator program which converts the high level language into an equivalent program in another language (the object or target language). An important part of any compiler is the detection and reporting of errors; Commonly, the source language is a high-level programming language (i.e. a problem-oriented language), and the target language is a machine language or assembly language (i.e. a machine-oriented language). Thus compilation is a fundamental concept in the production of software: it is the link between the (abstract) world of application development and the low-level world of application execution on machines. The following section deals with the compilation procedure of any program. Preprocessor Preprocessing performs (usually simple) operations on the source file(s) prior to compilation. Typical preprocessing operations include: Expanding macros. For example, in C, #define foo(x,y) (3*x+y*(2+x)) defines a macro foo, that when used in later in the program, is expanded by the preprocessor. For example, a = foo(a,b) becomes a = (3*a+b*(2+a)) Inserting named files. For example, in C, #include "header.h" is replaced by the contents of the file header.h
SCSX1019 System programming Unit III Linker Linker is used to link different object program to produce binary equivalent code. Compilation process skeletal source program preprocessor source program compiler assembly program assembler relocatable m/c code link/load editor absolute m/c code Fig 3.1 Compilation process The above figure deals with compilation procedure of any program Loader Loader is used to load the program in main memory for execution.
SCSX1019 System programming Unit III The Phases of a Compiler The process of compilation is split up into six phases, each of which interacts with a symbol table manager and an error handler. This is called the analysis/synthesis model of compilation. There are many variants on this model, but the essential elements are the same. The various phases of compilers are Lexical phase or scanner Syntax and semantic phase (or) parser Intermediate code generation Code optimization Code generation All the phases of compiler uses symbol table. Symbol table is a data structure which contains information about symbols. Apart from symbol table all the phases uses its own error recovery management mechanism. The following Figure 3.2 explains the structure of compiler. Lexical Analyzer Parser Intermediate code Symbol table generation Code optimization Code generation Fig 3.2 structure of compiler Error recovery manger
SCSX1019 System programming Unit III Lexical Analysis A lexical analyser or scanner is a program that groups sequences of characters into lexemes, and outputs (to the syntax analyser) a sequence of tokens. Here: (a) Tokens are symbolic names for the entities that make up the text of the program; e.g. if for the keyword if, and id for any identifier. These make up the output of the lexical analyser. (b) A pattern is a rule that specifies when a sequence of characters from the input constitutes a token; e.g the sequence i, f for the token if , and any sequence of alpha numerics starting with a letter for the token id. (c) A lexeme is a sequence of characters from the input that match a pattern (and hence constitute an instance of a token); for example if matches the pattern for if , and foo123bar matches the pattern for id. Consider the following example program foo(input,output);var x:integer;begin Lexeme Token Pattern program program p, r, o, g, r, a, m newlines, spaces, tabs foo id (foo) letter followed by seq. of alphanumerics ( lef tpar a left parenthesis input input i, n, p, u, t , comma a comma output output o, u, t, p, u, t ) rightpar a right parenthesis ; semicolon a semi-colon var var v, a, r