Compiler Design Lab File
Compiler Design Lab File
Compiler Design Lab File
5 LA Implementation 05/02/21
#include<bits/stdc++.h>
int prec(char c)
if(c == '^')
return 3;
return 2;
return 1;
else
return -1;
void infitopost(string s)
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
ns+=s[i];
char c = st.top();
st.pop();
ns += c;
if(st.top() == '(')
char c = st.top();
st.pop();
else{
prec(st.top()))
char c = st.top();
st.pop();
ns += c;
st.push(s[i]);
}
while(st.top() != 'N')
char c = st.top();
st.pop();
ns += c;
int main()
string exp;
cin>>exp;
infitopost(exp);
return 0;
}
Code 2:-Integer to Number
#include<iostream>
int main()
int n;
cin>>n;
int x;
int d=100;
for(int i=0;i<3;i++)
x=n/d%10;
d=d/10;
if(x==1)
{
cout<<"ONE";
else if(x==2)
cout<<"TWO";
else if(x==3)
cout<<"THREE";
else if(x==4)
cout<<"FOUR";
else if(x==5)
cout<<"FIVE";
else if(x==6)
cout<<"SIX";
else if(x==7)
{
cout<<"SEVEN";
else if(x==8)
cout<<"EIGHT";
else if(x==9)
cout<<"NINE";
else if(x==10)
cout<<"TEN";
else
cout<<"Nothing";
cout<<" ";
}
CODE 3:-TOKEN COUNTER
#include<stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cnt=0;
bool isDelimiter(char ch)
{
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
return (true);
return (false);
}
bool isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '='||ch==';')
return (true);
return (false);
}
bool validIdentifier(char* str)
{
if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
str[0] == '3' || str[0] == '4' || str[0] == '5' ||
str[0] == '6' || str[0] == '7' || str[0] == '8' ||
str[0] == '9' || isDelimiter(str[0]) == true)
return (false);
return (true);
}
bool isKeyword(char* str)
{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "while") || !strcmp(str, "do") ||
!strcmp(str, "break") ||
!strcmp(str, "continue") || !strcmp(str, "int")
|| !strcmp(str, "double") || !strcmp(str, "float")
|| !strcmp(str, "return") || !strcmp(str, "char")
|| !strcmp(str, "case") || !strcmp(str, "char")
|| !strcmp(str, "sizeof") || !strcmp(str, "long")
|| !strcmp(str, "short") || !strcmp(str, "typedef")
|| !strcmp(str, "switch") || !strcmp(str, "unsigned")
|| !strcmp(str, "void") || !strcmp(str, "static")
|| !strcmp(str, "struct") || !strcmp(str, "goto"))
return (true);
return (false);
}
bool isInteger(char* str)
{
int i, len = strlen(str);
if (len == 0)
return (false);
for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' || (str[i] == '-' && i > 0))
return (false);
}
return (true);
}
if (len == 0)
return (false);
for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' && str[i] != '.' ||
(str[i] == '-' && i > 0))
return (false);
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}
char* subString(char* str, int left, int right)
{
int i;
char* subStr = (char*)malloc(
sizeof(char) * (right - left + 2));
right++;
left = right;
} else if (isDelimiter(str[right]) == true && left != right
|| (right == len && left != right)) {
char* subStr = subString(str, left, right - 1);
if (isKeyword(subStr) == true)
printf("'%s' IS A KEYWORD\n", subStr);
cnt++;
left = right;
}
}
return;
}
int main()
{
parse(str);
printf("Total tokens %d",cnt);
return (0);
}
#include <bits/stdc++.h>
bool isVowel(char c)
c == 'u');
string pigLatin(string s)
if (isVowel(s[i])) {
index = i;
break;
if (index == -1)
return "-1";
int main()
if (str == "-1")
else
if(flag==1)
printf("!!!!Invalid expression!!!!");
else
{
printf("Variable count is : %d\n",cnt);
for(i=0;i<cnt;i++)
{
printf("%c\t%c%c%c\n",var[i][0],id[i][0],id[i][1],id[i][2]);
}
}
}
Code 6:- Implementation of Symbol Table
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void main()
{
int i=0,j=0,x=0,n;
void *p,*add[5];
char ch,srch,b[15],d[15],c;
printf("Expression terminated by $:");
while((c=getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf("\n Symbol Table\n");
printf("Symbol \t addr \t type");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("\n%c \t %d \t identifier\n",c,p);
x++;
j++;
}
else
{
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(ch);
add[x]=p;
d[x]=ch;
printf("\n %c \t %d \t operator\n",ch,p);
x++;
j++;
}}}}
Code 7:- Implementation of Left Most Derivation
#include <iostream>
using namespace std;
int main()
{
cout<<"Given CFG: E ---> (E + E) / (E - E)/ (E * E)/ (E / E)/ (E)/ (id)"<<endl;
cout<<"Given String: id + id * id"<<endl;
cout<<"Generating Left Most Derivation of given CFG"<<endl;
#include<iostream>
#include<string>
#include<deque>
int n,n1,n2;
for(int i=0;i<size;i++)
if(q == arr[i])
return i;
}
return -1;
int main()
string prods[10],first[10],follow[10],nonterms[10],terms[10];
cin>>n;
cin.ignore();
for(int i=0;i<n;i++)
getline(cin,prods[i]);
getline(cin,first[i]);
cin>>n2;
cin.ignore();
for(int i=0;i<n2;i++)
cin>>terms[i];
terms[n2] = "$";
n2++;
cin>>n1;
cin.ignore();
for(int i=0;i<n1;i++)
getline(cin,nonterms[i]);
getline(cin,follow[i]);
cout<<endl;
cout<<"Grammar"<<endl;
for(int i=0;i<n;i++)
cout<<prods[i]<<endl;
for(int j=0;j<n;j++)
if(prods[j].at(3)!='#')
{
for(int i=0;i<first[j].length();i++)
pp_table[row][col] = prods[j];
else
for(int i=0;i<follow[row].length();i++)
pp_table[row][col] = prods[j];
//Display Table
for(int j=0;j<n2;j++)
cout<<"\t"<<terms[j];
cout<<endl;
for(int i=0;i<n1;i++)
cout<<nonterms[i]<<"\t";
//Display Table
for(int j=0;j<n2;j++)
{
cout<<pp_table[i][j]<<"\t";
cout<<endl;
//Parsing String
char c;
do{
string ip;
deque<string> pp_stack;
pp_stack.push_front("$");
pp_stack.push_front(prods[0].substr(0,1));
getline(cin,ip);
ip.push_back('$');
cout<<"Stack\tInput\tAction"<<endl;
while(true)
for(int i=0;i<pp_stack.size();i++)
cout<<pp_stack[i];
cout<<"\t"<<ip<<"\t";
string p = pp_table[row1][column];
if(p.empty())
break;
pp_stack.pop_front();
if(p[3] != '#')
for(int x=p.size()-1;x>2;x--)
pp_stack.push_front(p.substr(x,1));
cout<<p;
else
if(ip.substr(0,1) == pp_stack.front())
if(pp_stack.front() == "$")
cout<<endl<<"String Parsed."<<endl;
break;
}
cout<<"Match "<<ip[0];
pp_stack.pop_front();
ip = ip.substr(1);
else
break;
cout<<endl;
cout<<"Continue?(Y/N) ";
cin>>c;
cin.ignore();
}while(c=='y' || c=='Y');
return 0;