Professional Documents
Culture Documents
B.TECH
CSE IIIrd year
Session 2023-24
Submitted to:
Mrs. Aparna Sharma
(Assistant Professor)
Department of Computer Science & Engineering
Mission
M1: To impart high quality education in the science of computing.
M2: To prepare educated and skilled computer professionals.
M3: To create excellence by providing comprehensive knowledge of the
latest tools and technologies in the domain of computer science, so that
students strive to become leaders.
M4: To inculcate ethical values in students so that they understand their
responsibility towards the nation with focus on upliftment of all sections of
society.
M5: To facilitate establishment of research centers and encourage students
to solve complex technological problems.
Program Educational Objectives
PEO1: Make valuable contributions to design, development and
production in the practice of computer science and engineering in related
engineering areas or application areas, and at the interface of computers
and physical systems.
PSO2: Ability to design and develop software for web based and mobiles
androids under real world environment.
7.
8.
9
1.WRITE A PROGRAM TO CONSTRUCT DFA ACCEPTING THE
LANGUAGE END WITH ‘01’
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int transition(int state, char symbol) {
switch(state) {
case 0:
if(symbol == '0')
return 0;
else if(symbol == '1')
return 1;
break;
case 1:
if(symbol == '0')
return 2;
else if(symbol == '1')
return 1;
break;
case 2:
if(symbol == '0')
return 0;
else if(symbol == '1')
return 1;
break;
}
return -1;
}
return state == 2;
}
int main() {
char input[100];
return 0;
}
2.DESIGN A LEXICAL ANALYZER FOR GIVEN LANGUAGE &
LEXICAL ANALYZER SHOULD IGNORE REDUNDANT SPACE,
TABS & NEW LINES.(identify tokens)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
typedef enum {
IDENTIFIER,
INTEGER,
OPERATOR,
KEYWORD,
DELIMITER,
ERROR,
END
} TokenType;
bool isDelimiter(char ch) {
char delimiters[] = " \t\n(),;{}[]";
for(int i = 0; delimiters[i] != '\0'; i++) {
if(ch == delimiters[i])
return true;
}
return false;
}
bool isOperator(char ch) {
char operators[] = "+-*/%=&|<>!";
for(int i = 0; operators[i] != '\0'; i++) {
if(ch == operators[i])
return true;
}
return false;
}
bool isKeyword(char *str) {
char keywords[][10] = {"if", "else", "while", "for", "int", "float", "char", "return"};
for(int i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
if(strcmp(str, keywords[i]) == 0)
return true;
}
return false;
}
TokenType getToken(char *lexeme) {
if(isalpha(lexeme[0]) || lexeme[0] == '_') {
if(isKeyword(lexeme))
return KEYWORD;
else
return IDENTIFIER;
} else if(isdigit(lexeme[0])) {
for(int i = 1; lexeme[i] != '\0'; i++) {
if(!isdigit(lexeme[i]))
return ERROR; // Invalid token
}
return INTEGER;
} else if(isOperator(lexeme[0])) {
return OPERATOR;
} else if(isDelimiter(lexeme[0])) {
return DELIMITER;
} else {
return ERROR; // Invalid token
}
}
int main() {
char input[1000];
printf("Enter the input code (press Ctrl+D to end):\n");
while (fgets(input, sizeof(input), stdin)) {
char *token = strtok(input, " \t\n");
while(token != NULL) {
TokenType type = getToken(token);
if(type != ERROR)
printf("Token: %s, Type: %d\n", token, type);
token = strtok(NULL, " \t\n");
}
}
printf("End of input.\n");
return 0;
}
3.WRITE A PROGRAM TO FIND SIMULATE FIRST AND
FOLLOW OF ANY GRAMMER
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_PROD 10
#define MAX_LEN 10
char grammar[MAX_PROD][MAX_LEN];
int nProd;
char first[26][MAX_LEN];
char follow[26][MAX_LEN];
bool nullable[26];
int main() {
printf("Enter the number of productions: ");
scanf("%d", &nProd);
printf("Enter the productions:\n");
for(int i = 0; i < nProd; i++) {
scanf("%s", grammar[i]);
}
printf("\nFIRST sets:\n");
for(int i = 0; i < 26; i++) {
if(first[i][0] != '\0') {
printf("%c: %s\n", i + 'A', first[i]);
}
}
printf("\nFOLLOW sets:\n");
for(int i = 0; i < 26; i++) {
if(follow[i][0] != '\0') {
printf("%c: %s\n", i + 'A', follow[i]);
}
}
return 0;
}
4.CONSTRUCT A RECURSIVE DECENT PARSER FOR AN
EXPRESSION
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
int main() {
char input[100];
printf("Enter an expression: ");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
if (expression(input)) {
printf("Valid expression!\n");
} else {
printf("Invalid expression!\n");
}
return 0;
}
5.CONSTRUCT SHIFT REDUCE PARSER FOR A GIVEN
LANGUAGE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
typedef enum {
NUM,
PLUS,
MINUS,
TIMES,
DIVIDE,
LPAREN,
RPAREN,
END
} TokenType;
typedef struct {
TokenType type;
int value;
} Token;
Token getNextToken(char **input);
bool parseExpression(char **input);
bool parseTerm(char **input);
bool parseFactor(char **input);
void consumeWhitespace(char **input) {
while (**input == ' ') {
(*input)++;
}
}
Token getNextToken(char **input) {
consumeWhitespace(input);
Token token;
switch (**input) {
case '+':
token.type = PLUS;
(*input)++;
break;
case '-':
token.type = MINUS;
(*input)++;
break;
case '*':
token.type = TIMES;
(*input)++;
break;
case '/':
token.type = DIVIDE;
(*input)++;
break;
case '(':
token.type = LPAREN;
(*input)++;
break;
case ')':
token.type = RPAREN;
(*input)++;
break;
case '\0':
token.type = END;
break;
default:
if (isdigit(**input)) {
token.type = NUM;
token.value = strtol(*input, input, 10);
} else {
printf("Invalid token\n");
exit(1);
}
}
return token;
}
bool parseFactor(char **input) {
Token token = getNextToken(input);
switch (token.type) {
case NUM:
return true;
case LPAREN:
if (!parseExpression(input)) {
return false;
}
token = getNextToken(input);
if (token.type != RPAREN) {
printf("Expected ')'\n");
return false;
}
return true;
default:
printf("Invalid factor\n");
return false;
}
}
bool parseTerm(char **input) {
if (!parseFactor(input)) {
return false;
}
while (true) {
Token token = getNextToken(input);
switch (token.type) {
case TIMES:
case DIVIDE:
if (!parseFactor(input)) {
return false;
}
break;
default:
(*input)--;
return true;
}}}
bool parseExpression(char **input) {
if (!parseTerm(input)) {
return false;
}
while (true) {
Token token = getNextToken(input);
switch (token.type) {
case PLUS:
case MINUS:
if (!parseTerm(input)) {
return false;
}
break;
default:
(*input)--;
return true;
}}}
int main() {
char input[100];
printf("Enter an expression: ");
fgets(input, sizeof(input), stdin);
if (parseExpression(&input)) {
printf("Valid expression!\n");
} else {
printf("Invalid expression!\n");
}
return 0; }
6.IMPLEMENT INTERMEDIATE CODE GENERATION OF A
SIMPLE EXPRESSION (W:a*b+c/d-e/f+g*h)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char op; // Operator
char arg1; // Argument 1
char arg2; // Argument 2
char result; // Result
} Quadruple;
void generateIntermediateCode(char *expression) {
Quadruple quadruples[100]; // Array to store quadruples
int numQuadruples = 0; // Counter for number of quadruples
char result = 't'; // Temporary variable for results
int main() {
char expression[] = "a*b+c/d-e/f+g*h";
generateIntermediateCode(expression);
return 0;
}