Compiler Lab Assignment
Compiler Lab Assignment
\\lab_1
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char c;
cout<<"ENTER A LETTER:";
cin>>c;
lab_2
#include <stdio.h>
#include<string.h>
//#include <stdlib.h>
int main()
{
char str[100];
int ts[3][2]={{1,0},{1,2},{2,2}};
int j=0, state=0,check=0;
int i=0;
printf("\n enter ny string :");
gets(str);
for(i=0;i<strlen(str);i++)
{
if(str[i]=='0')
{
state=ts[state][0];
}
else if(str[i]=='1')
{
state=ts[state][1];
}
else
check=0;
}
if(state==2&&check==1)
printf("the string accepted ");
else
printf("not accepted");
return 0
lab_3
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
//int ptr= 566879;
int x=0, n, i=0,j=0;
void *p,*address[5];
char ch,Search,b[15],d[15],c;
printf("Input the expression ending with $ sign :");
while((c=getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf(" \nSymbol Table display \n");
printf("Symbol \t address \t type");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
p=malloc(c);
address[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);
address[x]=p;
d[x]=ch;
printf("\n %c \t %d \t operator\n",ch,p);
x++;
j++;
}
}
}
}
lab_4
#include<iostream>
#include<stdlib.h>
using namespace std;
int is_number(int a)
{
cout<<"enter a number:";
cin>>a;
if(a=='0'||a=='1'||a=='2'||a=='3'a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9')
cout<<a<<"this is number"<<endl;
else{
cout <<a<<"it is not number"<<endl;
}
}
string iskeyword(string key)
{
cout>>"inter keyword :"endl;
cin<<key;
if(key=='asm'|| key=='void'|| key=='new'|| key'switch'|| key=='auto'
||key=='else'|| key=='operator'|| key=='template'|| key=='break'
|| key=='enum'|| key=='private'|| key=='this'|| key=='case'|| key=='extern'
||key=='protected'|| key=='throw'|| key=='catch'|| key=='public '
||key=='try'|| key=='for'|| key=='char'|| key=='typedef'|| key=='register'
||key=='union'|| key=='class'|| key=='friend'|| key=='return'|| key=='delete'
|| key=='unsigned'|| key=='const'|| key=='goto'|| key=='short'
|| key=='virtual'|| key=='continue'|| key=='if'|| key=='signed'||key=='default'
||key=='inline'|| key=='int'|| key=='sizedof' || key=='static'
|| key=='volatile'||key=='do' ||key=='long'|| key=='whil'|| key=='struct')
{
cout<<key<<"it is keyword"<<endl;
}
else
{
cout<<key<<"it is not keyword "<<endl;
}
}
else
{
cout<<id<<"it is not an identifier "endl;
}
}
string operator(op)
{
cout<<"enter an operator :"<<endl;
cin>>op;
if(op=='+'||op=='-'||op=='/'||op=='*'||)
{
cout<<op<<" is operator."endl;
}
else
{
cout<<op<<" is not a operator"endl;
}
int main()
{
int a;
string keyword, id;
is_number(a);
iskeyword(keyword);
operator(op);
isidentifier(id);