0% found this document useful (0 votes)
18 views

Structure of Compiler

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
18 views

Structure of Compiler

Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
(RUCTURE OF A COMP! ‘The compilation process is a sequence of various phases. Each phase takes input from its previous, stage, has its own representation of the source program, and feeds its output to the next phase of the compiler. Let us understand the phases of a compiler. ‘aon Ate yn Aneto Semantic Anaiyeer ‘Cosa open Lexical Analysis: '¢ lexical analysis or scanning forms the first phase of a compiler. ‘* The lexical analyzer reads the stream of characters which makes the source program and groups them into meaningful sequences called lexemes, For each lexeme, the lexical analyzer produces tokens as output. ‘A token format is shown below. ‘token-name, attribute-value> ‘* These tokens pass on to the subsequent phase known as syntax analysis, ‘¢ The token elements are listed below ‘Token-name: This is an abstract symbol used during syntax analysis Attribute-value: This points to an entry in the symbol table for the corresponding token, ‘¢ Information from the symbol-table entry ‘is needed for semantic analysis and code ger For example, let us try to analyze a simple arithmetic expression evaluation in Lexical context Assignment Symbol I y ‘The expression is seen by Lexical analyzer as <=> <+> <*> <60> Syntax Analysis ‘© Syntax analysis forms the second phase of the compiler. ‘© The list of tokens produced by the lexical analysis phase forms the input and arranges them in the form of tree structure (called the syntax tree). This reflects the structure of the program. This phase is also called parsing. ‘* The syntax tree consists of interior node representing an operation and the child of the node representing arguments. A syntax tree for the token statement is as shown in the above example, © Operators are considered as root nodes of this syntax tree. In the above case = has left and a right node. The left node consists of and the right node is again parsed and the immediate operator is taken as right node (id,1y~ + aay a (id, 3) 60 Semantic analysis ‘© Semantic analysis forms the third phase of the compiler. This phase uses the syntax tree and the formation in the symbol table to check the source program for consistency with the language definition, ‘* This phase also collects type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation, ‘© Type checking forms an important part of semantic analysis. Here the compiler checks whether each operator has matching operands ‘* Coercions (some type of conversions) may be permitted by the language specifications. If the operator is applied to a floating-point number and an integer, the compiler may convert or coerce the integer into a floating-point number, Coercion exists in the example quoted position = initial + rate * 60 ‘© Suppose position, initial and rate variables are declared as float. Since the is a floating point, then 60 is also converted to a floating-point. ‘© The syntax tree is modified to include the conversion/semantic aspects. In the example quoted 60 is converted to float as inttofloat, (id, 1) gp (id,27~ (id,3)~ — inttofloat 1 ’ 60 Intermediate Code Generation ‘Intermediate code generation forms the fourth phase of the compiler. After syntax and semantic analysis of the source program, many compilers generate a low level or machine-like intermediate representation, which can be thought as a program for an abstract machine, ‘+ This intermediate representation must have two important properties (a) It should be easy to produce (b) It should be easy to translate into the target machine t1 = inttofloat (60) Code Optimization ‘© Code Optimization forms the fifth phase in the compiler design ‘+ This is a machine-independent phase that attempts to improve the intermediate code for generating better (Faster) target code, t1= id3 * 60.0 id1 = id2 +t1 Code Generator © Code Generator forms the sixth phase in the compiler design, This takes the intermediate representation of the source program as input and maps it to the target language. ‘© The intermediate instructions are translated into sequences of machine instructions that perform the same task. A critical aspect of code generation is the assignment of registers to hold variables. Using R1 & R2 the intermediate code will get converted into machine code. LDF R2, id3 MULF R2, R2, #60.0 ADDF R1, R1, R2 STF id1,R1 Symbol-Table Management ‘© Anessential function of a compiler isto record the variable names used in the source program and collect {information about various attributes of each name. ‘+ These attributes may provide information about the storage allocated for a name, its type, its scope (wherein the program its value may be used), and in the case of procedure names, such things as the number and types of its arguments, the method of passing each argument (for example, by value or by reference), and the type returned. © The symbol table is a data structu attributes of the name. Error Handling ‘© One of the most important functions of a compiler is the detection and reporting of errors in the source program. The error message should allow the programmer to determine exactly where the err occurred. Errors may occur in all or the phases of a compiler. containing a record for each variable name, with fields for the ‘© Whenever a phase of the compiler discovers an error, it must report the error to the error handler, which. issues an appropriate diagnostic msg. Both of the table-management and error-Handling routines interact swith all phases of the compiler.

You might also like