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

CD Lab Manual Aim - Algorithm (1)

The document outlines a series of experiments for a Compiler Design Lab, focusing on implementing a lexical analyzer, generating YACC specifications, and performing operations related to symbol tables and code optimization. Each experiment includes aims, descriptions, algorithms, and results, detailing the steps to implement various components of a compiler using tools like LEX and YACC. The total duration for the lab is 30 periods.

Uploaded by

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

CD Lab Manual Aim - Algorithm (1)

The document outlines a series of experiments for a Compiler Design Lab, focusing on implementing a lexical analyzer, generating YACC specifications, and performing operations related to symbol tables and code optimization. Each experiment includes aims, descriptions, algorithms, and results, detailing the steps to implement various components of a compiler using tools like LEX and YACC. The total duration for the lab is 30 periods.

Uploaded by

gocon41581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CS3501 COMPILER DESIGN LAB

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:

Step-1: Create a program in the vi editor and save it with .l extension.


Step-2:Compile the LEX program with LEX compiler as: lex filename.l.
Step-3:It produces the output file as lex.yy.c.
Step-4: Compile that file with C compiler as cc lex.yy.c -ll.
Step-5: Incase of errors, edit the program and recompile it.
Step-6: Else, run the program as ./a.out and see the output.

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:

To write a YACC specification to develop a calculator application.

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: Read the variables, functions and arrays.


Step-2: Create an object for this structure as head
Step-3: For assignment operator 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: If type found correct, do the operation.

Step-5: Type mismatches, semantic error will be notified.

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:

Thus the implementation of three address code has been successfully in C.

8
Ex. No: 5
IMPLEMENTATION OF TYPE CHECKING
Date:

AIM:
To implement the type checking concept using LEX & YACC.

ALGORITHM:

Step-1: Read the variables.


Step-2: Check the size of these variables and track the global scope type.
Step-3: On any assignment operation, if the size of the variable on the left hand side does
not match with the size of the result obtained in the right hand side, then type checker
must pop the error statement.
Step-4: If type found correct, do the operation.

Step-5: Type mismatches, semantic error will be notified.

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:

Step-1: Read the number of lines in the source code.


Step-2: Get the operands on the Left Hand Side of each statement.
Step-3: Read the expressions on the Right Hand Side of each statement.
Step-4: Now, identify the dead codes. i.e. assignment statements that are not needed in the
program.
Step-5: Then eliminate the duplicate variables pointing to the same set of statements.
Step-6: After all these optimizations, rewrite the given code with lesser number of
statements.

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

You might also like