CSE304
CSE304
Construction
Amity University, Greater Noida
Practical File
Supervisor:
Ms Meenakshi
Department of Computer Science and Engineering
3. Write a program which accepts a regular expression from the user and
generates a regular grammar which is equivalent to the R.E. entered by user.
The grammar will be printed to a text file, with only one production rule in
eac h line. Also, make sure that all production rules are displayed in
compact forms e.g. the production rules: S--> aB, S--> cd S--> PQ Should be
written as S--> aB | cd | PQ And not as three different production rules.
Also, there should not be any repetition of product
7. Consider the following grammar: S --> ABC A--> abA | ab B--> b | BC C-->
c | cC Following any suitable parsing technique (prefer top-down), design
a parser which accepts a string and tells whether the string is accepted by
above grammar or not.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main() {
char input[100];
printf("Enter a string to test against (0 + 1)* + 01: ");
scanf("%s", input);
if (matches_a(input)) {
printf("The string matches the pattern.\n");
} else {
printf("The string does not match the pattern.\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int i = 0;
while (i < len) {
// Check for ab*c pattern
if (i < len && str[i] == 'a') {
i++;
int b_count = 0;
while (i < len && str[i] == 'b') {
b_count++;
i++;
}
if (i < len && str[i] == 'c') {
i++;
continue;
} else if (b_count == 0 && i < len && str[i] == 'c') {
i++;
continue;
}
}
return true;
}
int main() {
char input[100];
printf("Enter a string to test against (ab*c + (def)+ + a*d+e)+: ");
scanf("%s", input);
if (matches_b(input)) {
printf("The string matches the pattern.\n");
} else {
printf("The string does not match the pattern.\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Check for b*
while (i < len && str[i] == 'b') {
i++;
}
// Check for c*
while (i < len && str[i] == 'c') {
i++;
}
// Check for d
if (i != len - 1 || (i < len && str[i] != 'd')) {
case2 = false;
}
return case2;
}
int main() {
char input[100];
printf("Enter a string to test against ((a + b)(c + d))* + ab*c*d: ");
scanf("%s", input);
if (matches_c(input)) {
printf("The string matches the pattern.\n");
} else {
printf("The string does not match the pattern.\n");
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
// Token types
enum {KEYWORD, IDENTIFIER, NUMBER, OPERATOR, PUNCTUATOR};
// C keywords
const char *kws[] = {"int","float","if","else","while","return"};
while(i<len) {
// Skip whitespace
while(i<len && isspace(src[i])) i++;
if(i>=len) break;
int main() {
char code[] = "int main() { int x = 5+3; return 0; }";
analyze(code);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct {
char lhs;
char rhs[MAX_RHS][20];
int rhs_count;
} Production;
Production productions[MAX_PROD];
int prod_count = 0;
char non_terminals[26] = {0};
char start_symbol = 'S';
char new_nt() {
for (char c = 'A'; c <= 'Z'; c++)
if (!non_terminals[c - 'A'] && c != start_symbol) {
non_terminals[c - 'A'] = 1;
return c;
}
return '\0';
}
int main() {
char re[100];
printf("Enter a regular expression: ");
scanf("%s", re);
non_terminals[start_symbol - 'A'] = 1;
re_to_grammar(re, 0, strlen(re) - 1, start_symbol);
write_grammar("grammar.txt");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_PROD 20
#define MAX_RHS 10
#define MAX_SYM 20
typedef struct {
char lhs;
char rhs[MAX_RHS][MAX_SYM];
int rhs_count;
} Production;
Production productions[MAX_PROD];
int prod_count = 0;
productions[prod_count].lhs = lhs;
strcpy(productions[prod_count].rhs[0], rhs);
productions[prod_count].rhs_count = 1;
prod_count++;
}
void print_grammar() {
printf("\nGrammar:\n");
for (int i = 0; i < prod_count; i++) {
printf("%c -> ", productions[i].lhs);
for (int j = 0; j < productions[i].rhs_count; j++) {
if (j > 0) printf(" | ");
printf("%s", productions[i].rhs[j]);
}
printf("\n");
}
}
int is_non_terminal(char c) {
return isupper(c);
}
void eliminate_left_recursion() {
for (int i = 0; i < prod_count; i++) {
char A = productions[i].lhs;
if (!has_left_recursion) continue;
void input_grammar() {
printf("Enter productions (one per line, empty line to stop):\n");
printf("Format: A->aB|b\n");
char line[100];
while (1) {
fgets(line, sizeof(line), stdin);
if (line[0] == '\n') break;
int main() {
input_grammar();
print_grammar();
eliminate_left_recursion();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char *input;
int position = 0;
void error() {
fprintf(stderr, "Syntax error at position %d\n", position);
exit(1);
}
char peek() {
return input[position];
}
char consume() {
return input[position++];
}
int is_at_end() {
return input[position] == '\0';
}
void skip_whitespace() {
while (isspace(peek())) consume();
}
int number() {
int result = 0;
while (isdigit(peek())) {
result = result * 10 + (consume() - '0');
}
return result;
}
int expression();
int factor() {
skip_whitespace();
if (peek() == '(') {
consume(); // '('
int result = expression();
skip_whitespace();
if (peek() != ')') error();
consume(); // ')'
return result;
} else if (isdigit(peek())) {
return number();
} else {
error();
return 0;
}
}
int term() {
int result = factor();
skip_whitespace();
while (peek() == '*' || peek() == '/') {
char op = consume();
int next = factor();
if (op == '*') {
result *= next;
} else {
if (next == 0) {
fprintf(stderr, "Division by zero\n");
exit(1);
}
result /= next;
}
skip_whitespace();
}
return result;
}
int expression() {
int result = term();
skip_whitespace();
while (peek() == '+' || peek() == '-') {
char op = consume();
int next = term();
if (op == '+') {
result += next;
} else {
result -= next;
}
skip_whitespace();
}
return result;
}
int main() {
char buffer[256];
printf("Enter an arithmetic expression: ");
fgets(buffer, sizeof(buffer), stdin);
input = buffer;
if (!is_at_end()) {
error();
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#define MAX_WORD_LENGTH 50
return "unknown";
}
printf("Word Classification:\n");
printf("-------------------\n");
// Extract word
word_pos = 0;
while (sentence[sentence_pos] != '\0' &&
is_word_char(sentence[sentence_pos])) {
word[word_pos++] = tolower(sentence[sentence_pos++]);
}
word[word_pos] = '\0';
int main() {
char sentence[256];
classify_words_in_sentence(sentence);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Function prototypes
bool parse_S();
bool parse_A();
bool parse_B();
bool parse_C();
// Helper functions
char peek() {
return input[position];
}
void consume() {
position++;
}
bool parse_A() {
// A → abA | ab
if (match('a') && match('b')) {
if (peek() == 'a') {
return parse_A(); // abA case
}
return true; // ab case
}
return false;
}
bool parse_B() {
// B → b | BC
if (match('b')) {
if (peek() == 'c' || peek() == 'C') {
return parse_B() && parse_C(); // BC case
}
return true; // b case
}
return false;
}
bool parse_C() {
// C → c | cC
if (match('c')) {
if (peek() == 'c') {
return parse_C(); // cC case
}
return true; // c case
}
return false;
}
int main() {
char buffer[100];
printf("Enter a string to parse: ");
scanf("%s", buffer);
input = buffer;
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#define MAX_PROD 20
#define MAX_RHS 10
#define MAX_SYM 20
typedef struct {
char lhs;
char rhs[MAX_RHS][MAX_SYM];
int rhs_count;
} Production;
Production productions[MAX_PROD];
int prod_count = 0;
char start_symbol;
productions[prod_count].lhs = lhs;
strcpy(productions[prod_count].rhs[0], rhs);
productions[prod_count].rhs_count = 1;
prod_count++;
}
void input_grammar() {
printf("Enter productions (one per line, empty line to stop):\n");
printf("Format: A->aB|a\n");
char line[100];
while (1) {
fgets(line, sizeof(line), stdin);
if (line[0] == '\n') break;
return false;
}
int main() {
input_grammar();
char str[100];
printf("Enter a string to check: ");
scanf("%s", str);
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct {
int type; // 0: number, 1: operator, 2: parenthesis
int value;
char op;
} Token;
typedef struct {
Token tokens[MAX_STACK];
int top;
} Stack;
if (expr[i] == '(') {
Token t = {2, 0, '('};
push(&ops, t);
fprintf(out, "Shift '(': ");
print_stack(&values, out);
}
else if (isdigit(expr[i])) {
int val = 0;
while (expr[i] && isdigit(expr[i])) {
val = val * 10 + (expr[i] - '0');
i++;
}
i--;
while (!is_empty(&ops)) {
Token op = pop(&ops);
Token b = pop(&values);
Token a = pop(&values);
if (values.top == 0) {
fprintf(out, "\nFinal result: %d\n", values.tokens[0].value);
} else {
fprintf(out, "\nInvalid expression\n");
}
}
int main() {
char expr[100];
printf("Enter a mathematical expression (integers only): ");
fgets(expr, sizeof(expr), stdin);
expr[strcspn(expr, "\n")] = 0; // Remove newline
evaluate_expression(expr, out);
fclose(out);
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
// Helper functions
int apply_op(int a, int b, char op);
int precedence(char op);
int main() {
char expr[100];
printf("Enter expression: ");
fgets(expr, 100, stdin);
return 0;
}
while(*p) {
if(isdigit(*p)) {
val = 0;
while(isdigit(*p))
val = val*10 + (*p++ - '0');
stack[++top] = val;
}
else if(*p == '(') {
ops[++top] = *p++;
}
else if(*p == ')') {
while(top >= 0 && ops[top] != '(') {
int b = stack[top--];
int a = stack[top--];
stack[++top] = apply_op(a, b, ops[top+1]);
}
top--; // Remove '('
p++;
}
else if(*p == '+' || *p == '-' || *p == '*' || *p == '/') {
while(top >= 0 && precedence(ops[top]) >= precedence(*p)) {
int b = stack[top--];
int a = stack[top--];
stack[++top] = apply_op(a, b, ops[top+1]);
}
ops[++top] = *p++;
}
else p++;
}
while(top >= 0) {
int b = stack[top--];
int a = stack[top--];
stack[++top] = apply_op(a, b, ops[top+1]);
}
return stack[0];
}