Skip to content

Commit

Permalink
Added all the assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteLabyrinth committed Aug 16, 2020
0 parents commit 11c61e8
Show file tree
Hide file tree
Showing 285 changed files with 2,255,052 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# BTech Assignments
A repository of my BTech assignments for papers:
* IT351:      Data Structures (C)
* IT451:      Computer Organisation and Architecture (VHDL)
* IT453:      Computer Graphics (C)
* IT455:      Modelling and Simulation (MATLAB)
* IT552:      Operating System (C)
* IT553:      Database Management System (SQL)
* IT651:      Design & Analysis of Algorithms (C)
* IT652:      Compiler Design (C)
9 changes: 9 additions & 0 deletions compiler-design/assg-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Assignment 1

## Questions

1. Write a C program to traverse a set of input strings through an FS for integer & floating point number; output is whether the strings are integers or floating point.

2. Write a C program to filter comments: ```//``` & ```/**/``` from a given input file.

3. Implement double buffering technique for Lexical Analyzer to separate numbers (int & float), tokens, and remove white spaces and comments. Report lexical errors if there exists any. Assume N = 128, input a C program from file, output numbers, tokens, errors (if any).
11 changes: 11 additions & 0 deletions compiler-design/assg-1/input-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
123
14a
4.501
174.5.3
01.67
88.a
.51
87..
12.
077
0
20 changes: 20 additions & 0 deletions compiler-design/assg-1/input-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>

int main(int argc, char *argv[]) {
// this is an inline comment

// this is another inline comment

int a = 5; // define an integer

/* this is
a multiline
comment spread over
4 lines */

a = a + 9 * 5;
printf("a /*is equal to*/= %d\n", a);


return 0;
}
27 changes: 27 additions & 0 deletions compiler-design/assg-1/input-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
int main(int argc, char *argv[]) {
// this is an inline comment

// this is another inline comment

int a = 5; // define an integer

/* this is
a multiline
comment spread over
4 lines */

int b = 9;
b++;

a = b + 9 * a;

printf("a /*is equal to*/= %d\n", a);

int c = b * 1d; float n = 0.05;
n = (float) a / n;

c <<= 2;


return 0;
}
11 changes: 11 additions & 0 deletions compiler-design/assg-1/output-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Integer
Invalid
Float
Invalid
Invalid
Invalid
Float
Invalid
Invalid
Invalid
Integer
11 changes: 11 additions & 0 deletions compiler-design/assg-1/output-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
this is an inline comment

this is another inline comment

define an integer

this is
a multiline
comment spread over
4 lines

70 changes: 70 additions & 0 deletions compiler-design/assg-1/output-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
KEYWORD int
IDENTIFIER main
SEPARATOR (
KEYWORD int
IDENTIFIER argc
SEPARATOR ,
KEYWORD char
OPERATOR *
IDENTIFIER argv
SEPARATOR [
SEPARATOR ]
SEPARATOR )
SEPARATOR {
KEYWORD int
IDENTIFIER a
OPERATOR =
INTEGER 5
SEPARATOR ;
KEYWORD int
IDENTIFIER b
OPERATOR =
INTEGER 9
SEPARATOR ;
IDENTIFIER b
OPERATOR ++
SEPARATOR ;
IDENTIFIER a
OPERATOR =
IDENTIFIER b
OPERATOR +
INTEGER 9
OPERATOR *
IDENTIFIER a
SEPARATOR ;
IDENTIFIER printf
SEPARATOR (
STRING "a /*is equal to*/= %d\n"
SEPARATOR ,
IDENTIFIER a
SEPARATOR )
SEPARATOR ;
KEYWORD int
IDENTIFIER c
OPERATOR =
IDENTIFIER b
OPERATOR *
INVALID 1d
SEPARATOR ;
KEYWORD float
IDENTIFIER n
OPERATOR =
FLOAT 0.05
SEPARATOR ;
IDENTIFIER n
OPERATOR =
SEPARATOR (
KEYWORD float
SEPARATOR )
IDENTIFIER a
OPERATOR /
IDENTIFIER n
SEPARATOR ;
IDENTIFIER c
OPERATOR <<=
INTEGER 2
SEPARATOR ;
KEYWORD return
INVALID 0
SEPARATOR ;
SEPARATOR }
129 changes: 129 additions & 0 deletions compiler-design/assg-1/q1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Valid Integers:
begins with 1 - 9, followed by any number
of digits between 0 - 9
Valid Floating Point numbers:
begins with 0., followed by any number of
digits between 0 - 9
begins with ., followed by any number of
digits between 0 - 9
begins with 1 - 9, followed by any number
of digits between 0 - 9, followed by .,
followed any whole number of digits
between 0 - 9
*/


#include <stdio.h>
#include <string.h>


int getType(int, char *);


int main(int argc, char *argv[]) {
FILE *fi = fopen("input-1.txt", "r"); // input file
FILE *fo = fopen("output-1.txt", "w"); // output file
if (fi == NULL)
goto exit; // exit if no input file

// run until input strings exist
while (!feof(fi)) {
char string[64];
fscanf(fi, "%s", string);

int flag = getType(strlen(string), string);
if (flag == 1)
fprintf(fo, "Integer\n");
else if (flag == 2)
fprintf(fo, "Float\n");
else
fprintf(fo, "Invalid\n");
}

fclose(fi);
fclose(fo);
exit: return 0;
}


// function to determine whether passed string is
// an Integer, Float, or Invalid
int getType(int len, char *s) {
int state = 0, p = 0;

/*
state 0: initial state
state 1: first digit is between 1 - 9
(end state)
state 2: decimal has been encountered
state 3: there is atleast one digit after decimal
(end state)
state 4: first digit is 0
state 5: string isn't an integer or float
(dead state)
*/

// loop until end of string or dead state reached
while (state != 5 && p < len) {
char c = *(s + p);

switch (state) {
// initial state
case 0:
if (c == '0')
state = 4;
else if (c >= '1' && c <= '9')
state = 1;
else if (c == '.')
state = 2;
else
state = 5;
break;

// first digit is between 1 - 9
case 1:
if (c >= '0' && c <= '9')
state = 1;
else if (c == '.')
state = 2;
else
state = 5;
break;

// decimal has been encountered
case 2:
if (c >= '0' && c <= '9')
state = 3;
else
state = 5;
break;

// there is atleast one digit after decimal
case 3:
if (c >= '0' && c <= '9')
state = 3;
else
state = 5;
break;

// first digit is 0
case 4:
if (c == '.')
state = 2;
else
state = 5;
break;
}

p++;
}


if (state == 1 || state == 4)
return 1;
else if (state == 3)
return 2;
else
return 0;
}
Loading

0 comments on commit 11c61e8

Please sign in to comment.