Compiler-Design

Lab codes for Compiler design

View on GitHub

Compiler Design codes for the course 21CS63 conducted at RV College of Engineering, Bangalore.

LABEXAM TO REPO MAPPING

LAB Question Repo Program path
Words,lines,chars,Special Characters Link
a^m b^m+n c^n Link
Integers and Fractions Link
Arithmetic Expression Link
Remove comment lines Link
Nested For loops Link
Number of identifiers and operators Link
Nested IF statements Link
Variable Declaration Link
Three address code Link
Function definition Link
Assembly code generation Link

Pre-requisites

How to run the code

Running just the Lexical Analyser

 lex <filename>.l
 gcc lex.yy.c
 ./a.out

Running the Lexical Analyser and Parser

 lex <filename>.l
 yacc -d <filename>.y
 gcc lex.yy.c y.tab.c -lfl
 ./a.out

Input format for each of the programs

  1. a. Count the number of characters, words, lines and special characters in a given input file.

     any string with numbers and special characters
    

    b Validate the strings of the type a^m b^m+n c^n where m,n>=0

      aabbcc
      abc
      ab
      bc
    
  2. a. Count the number of positive and negetive integers and positive and negetive fractions

    > 1 -2 3/4 -5/6 7/-8 -9/-10 +11/12
    > // Just give a series fo space separated numbers
    

    b. Evaluate a given arithmetic expression with + - * / operators.

    1+2*3/4-5
    
  3. a. Count the number of valid nested FOR loops

    for(a;b;c){}
    for(a;b;c){for(a;b;c){d;}}
    

    b. Validate function defination

    int main(){};
    int foo(int a){}
    int bar(int a,int b){return a;}
    
  4. Generate the intermediate 3 address code and quadruples for a given expression

    a=b+c*d
    a+b+c+d+e+f
    a=(b+d)*(c+e)
    
  5. Generate the assembly code for a given set of arithmetic expressions

    ./outputfilename.out < input.txt
    

LLVM Programs

Program 1

This program is to print the unoptimised and optimised code for a bubble sort code

# to print the unoptimised assembly code
clang filename.c -S -emit-llvm -o filename

# to print the optimised assembly code
clang filename.c -S -emit-llvm -o filename -O3

Program 2

This program is to print the unoptimised and optimised code for a binary search code

# to print the unoptimised assembly code
clang filename.c -S -emit-llvm -o filename

# to print the optimised assembly code
clang filename.c -S -emit-llvm -o filename -O3

Program 3

this is about loop unrolling

# comment out the unroll pragma to see the difference
clang filename.c -S -emit-llvm -o filename

# for unrolled optimisation
clanf filename.c -S -emit-llvm -o filename -O3

Change Log

Version 1

Version 2

Version 3

Version 4


Contribution

This repo is open for contributions. Please open an Issue or open a PR with appropriate edits.