CD Lab Manual Aim - Algorithm (1)
CD Lab Manual Aim - Algorithm (1)
LIST OF EXPERIMENTS:
1. Using the LEX tool, Develop a lexical analyzer to recognize a few patterns in C.
(Ex.identifiers, constants, comments, operators etc.). Create a symbol table, while recognizing
Identifiers.
2. Implement a Lexical Analyzer using LEX Tool
3. Generate YACC specification for a few syntactic categories.
a. Program to recognize a valid arithmetic expression that uses operator +, -, * and /.
b. Program to recognize a valid variable which starts with a letter followed by any
number of letters or digits.
c. Program to recognize a valid control structures syntax of C language (For loop,
while loop, if-else, if-else-if, switch-case, etc.).
d. Implementation of calculator using LEX and YACC
4. Generate three address code for a simple program using LEX and YACC.
5. Implement type checking using Lex and Yacc.
6. Implement simple code optimization techniques (Constant folding, Strength reduction and
Algebraic transformation)
7. Implement back-end of the compiler for which the three address code is given as input and
the 8086 assembly language code is produced as output.
TOTAL: 30 PERIODS
1
Ex. No: 1
IMPLEMENTATION OF LEXICAL ANALYZER IN C
Date:
AIM:
To write a C program to implement the Lexical Analysis phase of Compiler.
DESCRIPTION:
Lexical Analysis is the first phase of the compiler. It analyses the source code and identify
the suitable token name from each meaningful lexemes with the help of patterns.
ALGORITHM:
Step-1:Store the list of keywords, header files, operators and special characters each in a
separate array.
Step-2:Read the input program and stop whenever a whitespace or a newline character is
encountered.
Step-3:Compare the read string with each of the input arrays stored previously.
Step-4: Display the corresponding tokens then and there itself.
RESULT:
Thus the Lexical Analyzer had been implemented in C successfully.
2
Ex. No: 2 IMPLEMENTATION OF LEXICAL ANALYZER USING LEX
Date: TOOL
AIM:
To write a Lex coding to implement Lexical Analyzer.
DESCRIPTION:
LEX tool is inbuilt with Linux Operating Systems. It is used for writing compilers. It
consists of three segments namely, Declaration, Translation Rules and Auxiliary Procedure.
The declaration section includes declaration of variables, maintest, constants and regular
definitions.
Translation rule of LEX program are statements of the form:
a. P1 { action }
b. P2 { action }
c. …
d. …
e. Pn { action }
Auxiliary procedure involves the sub routines.
ALGORITHM:
RESULT:
Thus the Lexical Analyzer had been implemented successfully using LEX Tool.
3
Ex. No: 3A
VALIDATING AN ARITHMETIC EXPRESSION
Date:
AIM:
To generate a YACC specification for validating an arithmetic expression with +, -, * and
/ operators.
ALGORITHM:
Step-1: Create anarith.y file using vi editor.
Step-2: Write the declaration part for variables that reads the expression.
Step-3: Write the rules for performing validation.
Step-4: Print the result.
Step-5: For compiling the program, type yacc –d filename.y
Step-6: It produces y.tab.c and y.tab.h as output files.
Step-7: Compile it with C compiler as cc y.tab.c –ll.
Step-8: Finally execute the program using ./a.out.
RESULT:
Thus the YACC specification to validate an arithmetic expression had been executed successfully.
4
Ex. No: 3B
RECOGNIZING A VALID VARIABLE NAME
Date:
AIM:
To write a YACC specification to recognize a valid name for a variable.
ALGORITHM:
Step-1: Create avar.y file using vi editor.
Step-2: Declare the variables.
Step-3: In the rules section, write as a variable can only start with a letter followed by any
number of letters or digits.
Step-4: Get the input from the user and check it against the rules.
Step-5: If the rule matches, then it is a valid variable name. Eg: A1, b555, Car, studentname,
etc,.
Step-6: Else display it is an invalid variable name. Eg: 2g, +a, !5, etc,.
RESULT:
Thus the YACC specification for recognizing a valid variable name had been executed
successfully.
5
Ex. No: 3C
IMPLEMENTING CONTROL STRUCTURES USING C
Date:
AIM:
To implement the operations on symbol table like insert, delete, search and display using C
language.
ALGORITHM:
Step-1: Create a structure named symbol with its members Label as character array, address
as integer and create an pointer object named next for the same structure.
Step-2: Create an object for this structure as head.
Step-3: For inserting a symbol into the symbol table,
Step – 3.a: Read the element.
Step - 3.b: Check the table for duplication.
Step – 3.c: If not present, then create a node for the structure and insert this at the end
of the table.
Step – 3.d: Else, inform the user that the label is already present in the symbol table.
Step-4: For searching the label,
Step – 4.a: Get the label.
Step – 4.b: Start from head node, check whether the label is matching.
Step – 4.c: If yes, return true, else move to next node and repeat step – 4.b.
Step - 4.d: If the loop is ended and the label is not found still, then return false.
Step-5: For displaying the contents of the symbol table, start from head node, visit each node
and display the label and address of that node.
Step-6: For Deleting a label,
Step - 6.a: Search for the label to be deleted.
Step – 6.b: If found, then make its previous node to point to its next node and delete
it.
Step-7: Press exit option to end the program.
RESULT:
Thus the control structure operations had been implemented successfully using C.
6
Ex. No: 3D IMPLEMENTING CALCULATOR OPERATIONS USING LEX
Date: AND YACC
AIM:
ALGORITHM:
Step-1: Create a calci.l file using vi editor.
Step-2: Write the code for converting the operands from character to integer.
Step-3: Create a calci.yfile using vi editor.
Step-4: Get the input from the user and perform corresponding operations.
Step-5: First compile the calci.l program as: lex calci.l
Step-6: Then compile the calci.y program as: yacc –d calci.y
Step-7: Using C compiler, compile both the outputs as: cc lex.yy.cy.tab.c.
Step-8: Now execute the program as: ./a.out.
RESULT:
Thus the calculator operations had been implemented successfully using YACC
specification.
7
Ex. No: 4
THREE ADDRESS CODE USING C
Date:
AIM:
To implement the three address code using C.
ALGORITHM:
Step-1: Create a structure named symbol with its members Label as character array, address
as integer and create an pointer object named next for the same structure.
Step-2: Create an object for this structure as head.
Step-3: For inserting a symbol into the symbol table,
Step – 3.a: Read the given expression.
Step - 3.b: Compare the expression into assignment symbol.
Step – 3.c: Perform string operations to store it in a temporary variable.
Step-4: For inserting an arithmetic operator into the symbol table,
Step – 4.a: Read the given expression.
Step – 4.b: Splitting the operators.
Step – 4.c: Compare it with an existing operator to assign it in temp variable.
Step-5: For inserting a relational operator into the symbol table
Step – 5.a: Read the given expression.
Step – 5.b: Splitting the relational operators.
Step – 5.c: If the expression not contain relational operator it encounter errors.
Step – 5.d: Finally place it into temporary allocation of address with keywords.
Step-6: Press exit option to end the program.
RESULT:
8
Ex. No: 5
IMPLEMENTATION OF TYPE CHECKING
Date:
AIM:
To implement the type checking concept using LEX & YACC.
ALGORITHM:
RESULT:
Thus the simple type checker have been successfully implemented in LEX & YACC.
9
Ex. No: 6
SIMPLE CODE OPTIMIZATION TECHNIQUES
Date:
AIM:
To implement the code optimization techniques like constant folding, dead code
elimination, etc,. using C Language.
ALGORITHM:
RESULT:
Thus the simple Code Optimization techniques had been implemented successfully.
10
Ex. No: 7
THREE ADDRESS CODE TO ASSEMBLY LANGUAGE CODE
Date:
AIM:
To implement the intermediate code generation phase of the compiler using C program.
DESCRIPTION:
Intermediate Code Generation phase takes three address code as its input and produces the
Assembly Language instructions as its output. Sample three address code format is:
a := 5;
b := a + 3;
Equivalent Assembly Language Instructions are:
MOV R1, #5
ADD R1, #3
MOV b, R1
ALGORITHM:
Step-1: Read the three address code in an array.
Step–2: Identify the operators used in the array.
Step-3: If the operator is ‘=’, then use MOV instruction.
Step-4: If it is ‘+’, ‘-’, ‘*’, or ‘/’, then use ADD, SUB, MUL or DIV instructions
respectively.
Step-5: Finally, move the end result to the variable used in the program.
RESULT:
Thus the intermediate code generation phase had been implemented successfully.
11