Compiler Design codes for the course 21CS63 conducted at RV College of Engineering, Bangalore.
- The following codes are written in C and are meant to be compiled with CC or GCC compilers.
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
- Need to have the following installed :
- GCC Compiler
- Flex
- Yacc
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
-
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
-
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
-
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;}
-
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)
-
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
- Added all 5 programs
- Had some conflicts in Shift/Reduce and Reduce/ Reduce
Version 2
- @DeathStroke1991(https://github.com/DeathStroke19891) Fixed the issues in 3a and 3b
- Added comments on grammer.
Version 3
- Added LLVM programs
- Added the new commands to run the programs
Version 4
- Added Extra lab codes
- Added lab to repo mapping
Contribution
This repo is open for contributions. Please open an Issue or open a PR with appropriate edits.