CD File Manjot 4 To 9 PDF
CD File Manjot 4 To 9 PDF
Objective: Write a Cpp program to recognize strings under 'a', 'a*b+', 'abb'
Program:
#include <iostream>
#include <string>
// Match 'a*'
while (i < str.length() && str[i] == 'a') {
i++;
}
// Match 'b+'
if (i == str.length() || str[i] != 'b') {
return false; // No 'b' found or invalid character
}
while (i < str.length() && str[i] == 'b') {
i++;
}
Output:
Objective: Write a Cpp program to test whether a given identifier is valid or not.
Program:
#include <iostream>
#include <string>
#include <cctype>
#include <unordered_set>
return true;
}
if (isValidIdentifier(identifier)) {
cout << "\"" << identifier << "\" is a valid identifier." << endl;
} else {
cout << "\"" << identifier << "\" is not a valid identifier." << endl;
}
return 0;
}
Output:
Objective: Write a CPP Program to stimulate the lexical analyzer for validating operator
Program:
#include <iostream>
#include <string>
#include <cctype>
#include <unordered_set>
using namespace std;
bool isOperator(char c) {
string input;
cout << "Enter an expression to analyze: ";
getline(cin, input);
return 0;
}
Output:-
Objective: Implement the lexical analyzer using JLex, flex or other lexical analyzer generating tools.
Program:
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
int main()
{
FILE *f1;
char c,str[10];
int lineno=1,num=0,i=0;
printf("\nEnter the c program\n");
f1=fopen("input.txt","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input.txt","r");
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
num=c-48;
c=getc(f1);
while(isdigit(c))
{
num=num*10+(c-48);
c=getc(f1);
}
printf("%d is a number \n",num);
ungetc(c,f1);
}
else if(isalpha(c))
{
str[i++]=c;
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
str[i++]=c;
c=getc(f1);
}
str[i++]='\0';
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
strcmp("double",str)==0||strcmp("static",str)==0||
strcmp("switch",str)==0||strcmp("case",str)==0)
printf("%s is a keyword\n",str);
else
printf("%s is a identifier\n",str);
ungetc(c,f1);
i=0;
}
else if(c==' '||c=='\t')
printf("\n");
else if(c=='\n')
lineno++;
else
printf("%c is a special symbol\n",c);
}
printf("Total no. of lines are: %d\n",lineno);
fclose(f1);
getch();
return 0;
}
Output:-
Objective: Write a CPP Program to stimulate the lexical analyzer for validating operator
Program:
#include <iostream> #include <vector> #include <stack> #include
<unordered_map> #include <string> using namespace std; class LL1Parser
{ public: // Define the grammar rules as a map of non-terminal ->
production rules unordered_map<string, vector<string>> grammar;
unordered_map<string, unordered_map<char, string>> parseTable;
}
b o o l p a r s e ( s t r i n g i n p u t ) {
stack<string> parseStack;
parseStack.push("$"); // End of input marker
parseStack.push("S"); // Start symbol
int i = 0;
input += '$'; // Append end-of-input marker
cout << "Parsing steps:" << endl;
while (!parseStack.empty()) {
Output:-
Objective: Write a Cpp program for implementing the functionality of predictive parser
Program:
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
// Function to check if a character is a non-terminal
bool isNonTerminal(char c) {
return c >= 'A' && c <= 'Z';
}
// Function to implement predictive parser
bool predictiveParser(const string &input, const map<char, vector<string>>
&parsingTable) {
stack<char> parseStack;
parseStack.push('$'); // End symbol
parseStack.push('S'); // Start symbol (assumed to be S)
parseStack.pop();
i++;
if (currentInput == '$') {
break; // Successfully parsed the entire input
}
}
// If the top of the stack is a non-terminal
{ else if (isNonTerminal(top)) {
// If there is no production rule for the non-terminal and input symbol, error
if (parsingTable.find(top) == parsingTable.end() || parsingTable.at(top).empty())
cout << "Error: No production rule for " << top << " with input " <<
currentInput << endl;
return false;
}
s t r i n g r u l e = " " ;
f o r ( c o n s t s t r i n g & p r o d : p a r s i n g T a b l e . a t
if (prod[0] == currentInput) {
rule = prod;
break;
}
}
i f ( r u l e . e m p t y ( ) ) {
coutcurrentInput
symbol " << << "Error: No
<<matching
endl; rule for non-terminal " << top << " and input
return false;
}
/ / P o p t h e t o p s y m b o l a n d p u s h t h e p r o d u
p a r s e S t a c k . p o p ( ) ;
f o r ( i n t j = r u l e . l e n g t h ( ) - 1 ; j > = 0 ; j - -
if (rule[j] != 'ε') { // If not epsilon (empty)
parseStack.push(rule[j]);
}
}
}
Output:-