You are on page 1of 6

COMPILATION PROCESS

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program. This process is called compilation process.

PSEUDO CODE FOR COMPILER


Compiler construction deals with the phases of compiler at front end as well as at the back end. Pseudo code involves handling of identifiers, numbers, brackets, operators, commas, data types and keywords and error handling checks for brackets, return types and semicolon. HEADER FILES # include<iostream.h> output// # include<conio.h> # include <fstream.h> # include<string.h> Introduce a class: { Void Main ( ) function called Type void { Declare character array for identifiers like char[] identifiers = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; //header file for input and

//header file for file// //header file for string//

Declare character array for numbers like char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; Declare character array for colons like Char[] colon={;,:} Declare character array for operators like char[] operators = { '=', '/', '*', '+', '-','<','>' }; Declare character array for brackets like char[] brackets = { '(', ')', '[', ']', '{', '}' }; Declare array for datatypes like string[] datatype = { "int", "char", void }; Declare array for keywords like string[] keywords = { "main", "if", "for" }; Take string input from user Store user entered input length in a variable like int b=input.Length Declare a variable a for(a=0; a is less than length of user entered input;a++) { If User entered int Declare character array integer Store (int) in character array integer If(user entered input==integer[0]) // if user entered input is equal to 0th index of integer array Then display datatype int on screen // present at 0th index of datatype array If User entered char Declare character array integer Store (char) in character array integer If(user entered input==integer[0]) // if user entered input is equal to 0th index of integer array Then display datatype char on screen // present at 1st index of datatype array If User entered for Declare character array integer Store (for) in character array integer If(user entered input==integer[0]) // if user entered input is equal to 0th index of integer array Then display keyword for on screen // present at 2nd index of keywords array If User entered if Declare character array integer Store (if) in character array integer If(user entered input==integer[0] && user entered input==integer[1] ) // both conditions true Then display keyword if on screen // present at 1st index of keywords array }

Declare a variable a for(a=0; a is less than length of user entered input;a++) { Brackets: Declare int c for (c=0;c <length of brackets;c++) { if(user entered input==brackets[c]) Then display bracket on screen } Operators: Declare int d for (d=0;d <length of operators;d++) { if(user entered input==operators[d]) Then display operator on screen } Numbers: Declare int e for (e=0;e <length of numbers;e++) { if(user entered input==number[e]) Then display number on screen } Identifiers: Declare int f for (f=0;f <length of identifiers;f++) { if(user entered input==identifiers[f]) Then display identifier on screen } } Semicolon,comma: Declare int h for (h=0;h <length of identifiers;h++) { if(user entered input==colon[h]) Then display semicolon or comma on screen } } GENERATION OF THE SYMBOL TABLE

Symbol table retains information about various symbols including those symbols which appear in the source program, such as variables, subprograms, types and constants, and those generated by the compiler, such as temporary variables and labels. I. II. III. Initialize its location Counter (LC) to zero. Read one line of the program. Break the line into its parts. a. Labels b. Operation c. Operands IV. V. VI. VII. Insert the new label into symbol table. Examine the operation. Return to step II. Example :
int num=2; int i;

Name main Main()


Name num i Kind variable variable

Kind function

Type int

Type int int

Scope local local

Token # 1 2

Value 2

TREE STRUCTURE OF SYMBOL TABLE The tree structure of symbol tables in C language is depicted as follows

symTableConst by a compiler. symTableRoot defined.

Constants in the source program and those generated

Built-in symbols basic types and symbols globally

symTable A Symbols defined locally in subprogram A, and symbols generated by the compiler in subprogram A excluding constants ERROR HANDLING //error handling for brackets for loop for brackets check Declare int c for (c=0;c <length of brackets;c++) { { if (user entered input == brackets[0] && user entered input == brackets[1])// opening and closing brackets Then display no error of brackets } if (user entered input == brackets[0] || user entered input == brackets[1]) { if (user entered input == brackets[0] && user entered input ! = brackets[1])// opening bracket is present but closing bracket is missing Then display closing bracket is missing if (user entered input == brackets[1] && user entered input ! = brackets[0])// closing bracket is present but opening bracket is missing Then display error opening bracket is missing } } //error handling for semi colon if (user entered input == brackets[1] && user entered input ! = colon[0])//after closing bracket, there is no semicolon Then display error semicolon is missing //error handling for return type

if (user entered input == datatype[0])//if main() function return type is an integer Then display error function must return something

You might also like