0% found this document useful (0 votes)
30 views7 pages

Code Exp-4

The code defines functions to identify keywords, variables, numbers, and operators in a given string. It tokenizes the string by identifying these elements and adding them as key-value pairs to a tokens vector. It also adds the identified variables to a symbols vector with an associated integer value. It then prints the tokens and symbols vectors.

Uploaded by

Aakashdeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Code Exp-4

The code defines functions to identify keywords, variables, numbers, and operators in a given string. It tokenizes the string by identifying these elements and adding them as key-value pairs to a tokens vector. It also adds the identified variables to a symbols vector with an associated integer value. It then prints the tokens and symbols vectors.

Uploaded by

Aakashdeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Code Exp-4:

#include<bits/stdc++.h>

using namespace std;

bool keyword(char c,int &state)

if(state==0)

if(c=='i')

state++;

return 1;

else{

state=0;

return 0;

if(state==1)

if(c=='f')

state++;

return 1;

else{

state=0;

return 0;

if(state==2)
{

if(c==' ')

return 1;

else{

state=0;

return 0;

bool variable(char c,int &state)

c=tolower(c);

if(state==0)

if((c>='a' and c<='z')or c=='_')

state++;

return 1;

else {

state=0;

return 0;

if(state==1)

if((c>='a' and c<='z')or c=='_' or(c>='0' and c<='9'))

{
return 1;

else {

state=0;

return 0;

bool number(char c,int &state)

if(state==0)

if(c>='0' and c<='9')

//state++;

return 1;

else {

return 0;

bool op(char c,int &state)

if(state==0)

if(c=='=' or c=='<' or c=='>')

state++;

return 1;
}

else

state=0;

return 0;

if(state==1)

if(c=='=')

return 1;

else

state=0;

return 0;

int main()

freopen("input.txt","r",stdin);

string s;

getline(cin,s);

int vstate=0,ostate=0,nstate=0,kstate=0;

string temp;

int vflag=1,oflag=1,nflag=1,kflag=1;

vector<pair<string,string>> tokens;
vector<pair<string,string>> symbols;

int add=1;

for(int i=0;i<s.size();i++)

char c=s[i];

temp+=s[i];

if(s[i]==' ')

if(temp.size()==0)

continue;

if(kflag and kstate==2)

tokens.push_back({"keyword",temp});

else if(vflag)

string name="id"+to_string(add);

symbols.push_back({name,temp});

add++;

tokens.push_back({name,to_string(add-1)});

else if(oflag)

if(temp=="> ")

temp="GT";

else if(temp=="< ")

temp="LT";

tokens.push_back({"relop",temp});

else if(nflag)
{

tokens.push_back({"number",temp});

temp="";

vstate=0,ostate=0,nstate=0,kstate=0;

vflag=1,oflag=1,nflag=1,kflag=1;

else

if(vflag)

vflag=variable(c,vstate);

if(oflag)

oflag=op(c,ostate);

if(nflag)

nflag=number(c,nstate);

if(kflag)

kflag=keyword(c,kstate);

cout<<"Compilation First phase completed\n\n";

cout<<"Tokens:-\n\n";

for(auto v:tokens)

cout<<v.first<<" "<<v.second<<"\n";

cout<<"\n";

cout<<"Symbol Table:-\n\n";

for(auto v:symbols)

cout<<v.first<<" "<<v.second<<"\n";
}

return 0;

}I/O:

You might also like