cd_file
cd_file
Source Code:
%{
#include <stdio.h>
#include <string.h>
%%
[a-zA-Z]+ { suggestVerb(yytext); }
[ \t\n]+ ; // Ignore whitespace
. { printf("Unknown character: %s\n", yytext); }
%%
int main() {
printf("Enter words:\n");
yylex();
return 0;
}
int yywrap() {
return 1;
}
Output:
Program – 10
Source Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_RULES 10
#define MAX_SYMBOLS 10
typedef struct {
char nonTerminal;
char productions[MAX_RULES][MAX_SYMBOLS];
int count;
} Rule;
Rule grammar[MAX_RULES];
int ruleCount = 0;
Output:
Program – 11
Source Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_RULES 10
#define MAX_SYMBOLS 10
typedef struct {
char nonTerminal;
char productions[MAX_RULES][MAX_SYMBOLS];
int count;
} Rule;
Rule grammar[MAX_RULES];
int ruleCount = 0;
Output:
Program – 12
Source Code:
#include <stdio.h>
#include <ctype.h>
void FIRST(char);
int count, n = 0;
char prodn[10][10], first[10];
int main() {
int i, choice;
char c, ch;
printf("How many productions? : ");
scanf("%d", &count);
printf("Enter %d productions (use '$' for epsilon):\n", count);
for (i = 0; i < count; i++) {
scanf("%s%c", prodn[i], &ch);
}
do{
n = 0;
printf("\nEnter element to find FIRST: ");
scanf("%c", &c);
FIRST(c);
printf("FIRST(%c) = { ", c);
for (i = 0; i < n; i++) {
printf("%c ", first[i]);
}
printf("}\n");
printf("Press 1 to continue: ");
scanf("%d%c", &choice, &ch);
} while (choice == 1);
return 0;
}
void FIRST(char c) {
int j;
if (!isupper(c)) {
first[n++] = c;
return;
}
for (j = 0; j < count; j++) {
if (prodn[j][0] == c) {
if (prodn[j][2] == '$'){
first[n++] = '$'; }
else if (islower(prodn[j][2])) {
first[n++] = prodn[j][2]; }
else {
FIRST(prodn[j][2]); }
}
}
}
Output:
Program – 13
Source Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void follow(char c);
void first(char c);
int n, m = 0, i = 0, j = 0;
char a[10][10], f[10];
int main() {
int z;
char c, ch;
printf("Enter the no. of productions: ");
scanf("%d", &n);
printf("Enter the productions (use '$' for epsilon):\n");
for (i = 0; i < n; i++) {
scanf("%s%c", a[i], &ch);
}
do {
m = 0;
printf("\nEnter the element whose FOLLOW is to be found: ");
scanf("%c", &c);
follow(c);
printf("FOLLOW(%c) = { ", c);
for (i = 0; i < m; i++) {
printf("%c ", f[i]);
}
printf("}\n");
printf("Press 1 to continue: ");
scanf("%d%c", &z, &ch);
} while (z == 1);
return 0;
}
void follow(char c) {
if (a[0][0] == c) {
f[m++] = '$';
}
for (i = 0; i < n; i++) {
for (j = 2; j < strlen(a[i]); j++) {
if (a[i][j] == c) {
if (a[i][j + 1] != '\0') {
first(a[i][j + 1]);
}
if (a[i][j + 1] == '\0' && c != a[i][0]) {
follow(a[i][0]);
}
}
}
}
}
void first(char c) {
int k;
if (!isupper(c)) {
f[m++] = c;
return;
}
for (k = 0; k < n; k++) {
if (a[k][0] == c) {
if (a[k][2] == '$') {
follow(a[k][0]);
}
else if (islower(a[k][2])){
f[m++] = a[k][2];
}
else {
first(a[k][2]);
}
}
}
}
Output:
Program – 14
Source Code:
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct Node{
string value;
Node *left;
Node *right;
Node(string val) : value(val), left(nullptr), right(nullptr) {}
};
int main(){
string postfix;
cout << "Enter a postfix expression: ";
cin >> postfix;
Node *root = buildParseTree(postfix);
cout << "Parse Tree:\n";
printParseTree(root);
return 0;
}
Output:
Program – 15
Aim: Write a program to check whether a string belong to the grammar or not.
Source Code:
#include <iostream>
using namespace std;
string input;
int main() {
cout << "Enter a string for grammar aSb|#: ";
cin >> input;
if (isValid(0, input.length() - 1)) {
cout << "The string belongs to the grammar.\n";
} else {
cout << "The string does NOT belong to the grammar.\n";
}
return 0;
}
Output:
Program – 16
Aim: Write a program to check whether a grammar is left recursion and remove left recursion.
Source Code:
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
if (alpha.empty()) {
cout << nonTerminal << " - ";
for (size_t i = 0; i < productions.size(); i++) {
cout << productions[i] << (i == productions.size() - 1 ? "" : " | ");
}
cout << endl;
return;
}
int main() {
int n;
cout << "Enter the number of non-terminals: ";
cin >> n;
cin.ignore();
vector<string> productions;
stringstream ss(productionLine);
string prod;
while (getline(ss, prod, '|')) {
productions.push_back(prod);
}
if (hasLeftRecursion(nonTerminal, productions)) {
cout << "Left recursion detected! Removing it...\n";
removeLeftRecursion(nonTerminal, productions);
} else {
cout << "No left recursion found.\n";
}
cout << endl;
}
return 0;
}
Output: