Compiler Design
Compiler Design
OUTPUT :
1)
2)
PROGRAM-2
AIM : Write a program to check whether a string belongs to the
grammar or not.
THEORY : The steps involved in checking whether a string belongs to
a Grammar or not, are as follows:
Start with the Start Symbol and choose the closest production that
matches the given string.
Replace the Variables with their most appropriate production. Repeat
the process until the string is generated or until no other productions
are left.
1.1) S -> aS
S -> Sb
S -> ab
String of the form : aab
1.2) S -> aSa
S -> bSb
S -> a
S -> b
The Language generated is : All Odd Length Palindromes
1.3) S -> aSbb
S -> abb
The Language generated is : anb2n , where n>1
1.4) S -> aSb
S -> ab
The Language generated is : anbn, where n>0
CODE :
#include<iostream>
using namespace std;
int main(){
int inp;
cout<<"Enter number 1 for grammar 1"<<endl;
cout<<"Enter number 2 for grammar 2"<<endl;
cout<<"Enter number 3 for grammar 3"<<endl;
cout<<"Enter number 4 for grammar 4"<<endl;
cin>>inp;
if(inp==1){
cout<<"Enter a string: ";
char str[10];
cin>>str;
if(str[0]!='a')
{ cout<<"String is invalid because of incorrect first character";
exit(0); }
int n=1;
while(str[n]=='a') n++;
if ( str[n] != 'b')
{ cout<<"string does not belong to grammar";
exit(0); }
n++;
while (str[n]=='b') n++;
if (str [n] != '\0')
{ cout<<"String does not belong to grammar";
exit(0);}
cout<<"string is valid";
}
else if(inp==2){
char str[10];
cout<<"Enter a string: ";
cin>>str;
int c=0;
int i=0;
while(str[i]!='\0'){
c+=1;
i+=1;
}
if(c%2==0){
cout<<"string does not belong to grammar\n";
exit(0);
}
else{
int s=0,e=c-1;
while(s<e){
if(str[s]==str[e]){
s=s+1;
e=e-1;
}
else{
cout<<"string does not belong to grammar\n";
exit(0);
}
}
cout<<"String belongs to grammar\n";
}
}
else if(inp==3){
char str[10];
cout<<"Enter a string: ";
cin>>str;
if(str[0]!='a'){
cout<<"string does not belong to grammar\n";
exit(0);
}
int ca=0,cb=0,i=0;
while(str[i]=='a'){
ca+=1;
i=i+1;
}
while(str[i]=='b'){
cb+=1;
i=i+1;
}
if(str[i]!='\0'){
cout<<"string does not belong to grammar\n";
exit(0);
}
else{
if(cb==2*ca){
cout<<"string belongs to grammar\n"; }
else{
cout<<"string does not belong to grammar\n"; }
}
}
else if(inp==4){
char str[10];
cout<<"Enter a string: ";
cin>>str;
if(str[0]!='a'){
cout<<"string does not belong to grammar\n";
exit(0);
}
int ca=0,cb=0,i=0;
while(str[i]=='a'){
ca+=1;
i=i+1;
}
while(str[i]=='b'){
cb+=1;
i=i+1;
}
if(str[i]!='\0'){
cout<<"string does not belong to grammar\n";
exit(0); }
else{
if(cb==ca){
cout<<"string belongs to grammar\n"; }
else{
cout<<"string does not belong to grammar\n";
}
}
}
return 0;
}
OUTPUT :
PROGRAM-3
AIM : Write a program to check whether a string includes a keyword
or not.
THEORY : We break the given string into tokens and check each
word if its a keyword of C++ language. Keywords are predefined
words, we cannot name variables as keywords, there are 32
keywords in C++. We print the count of keywords present in the
string.
CODE :
#include <bits/stdc++.h>
using namespace std;
void simple_tokenizer(string s)
{
stringstream ss(s);
string word;
map<string,int> m;
string
key[32]={"auto","break","case","char","const","continue","default","do","doub
le","else","enum","extern","float","for","goto","if","int","long","register","retur
n","short","signed","sizeof","static","struct","switch","typedef","union","unsig
ned","void","volatile","while"};
while (ss >> word) {
for(int i=0;i<32;i++){
if(key[i] == word){
m[word]+=1;
}
}
}
if(!m.empty()){
for (auto i = m.begin(); i != m.end(); i++)
cout << i->first << " " << i->second << endl;
}
else{
cout<<"No keyword is present"<<endl; }
}
int main()
{
string a;
cout<<"enter a string"<<endl;
getline(cin,a);
simple_tokenizer(a);
cout << endl;
cout<<"Swastik Sharma 039";
return 0; }
OUTPUT :