we have a C source program scanned and filtered as it was done in Session 1. We now take that modified file as input, and separate the lexemes first. We further recognize and mark the lexemes as different types of tokens like keywords, identifiers, operators, separators, parenthesis, numbers, etc.
Session 2 by Nafis Islam on Scribd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float x1 = 3.125; double f1(int x) { double z; z = 0.01+x*5.5; return z; } int main(void) { int n1; double z; n1=25; z=f1(n1); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
bool comp(char a){ | |
if(a=='=' || a=='>' || a=='<' ) return false; | |
return true; | |
} | |
bool col(char a){ | |
if(a==',' || a==';' || a=='+' || a=='-' || a=='*' || a=='/' || a=='(' || a==')' || a=='\'') return true; | |
return false; | |
} | |
bool isnum(string sp){ | |
for(int i=0;i<sp.size();i++){ | |
if(sp[i]>='0' && sp[i]<='9') continue; | |
if(sp[i]=='.') continue; | |
return false; | |
} | |
return true; | |
} | |
map<string,string>mp; | |
void t(){ | |
mp["return"] = "[kw return]"; | |
mp["int"] = "[kw int]"; | |
mp["float"] = "[kw float]"; | |
mp["double"] = "[kw double]"; | |
mp["long"] = "[kw long]"; | |
mp["char"] = "[kw char]"; | |
mp[";"] = "[sep ;]"; | |
mp[","] ="[sep ,]"; | |
mp["'"] = "[sep ']"; | |
mp["if"] = "[kw if]"; | |
mp["else"] = "[kw else]"; | |
mp["("] = "[par (]"; | |
mp[")"] = "[par )]"; | |
mp["{"] = "[par {]"; | |
mp["}"] = "[par }]"; | |
mp["+"] = "[op +]"; | |
mp["-"] = "[op -]"; | |
mp["*"] = "[op *]"; | |
mp["/"] = "[op /]"; | |
mp["="] = "[op =]"; | |
mp["=="] = "[op ==]"; | |
mp["<="] = "[op <=]"; | |
mp[">="] = "[op >=]"; | |
} | |
int main(){ | |
freopen("input.txt","r",stdin); | |
freopen("output.txt","w",stdout); | |
string s; | |
getline(cin,s); | |
cerr<<"input:\n"; | |
cerr<<s<<"\n\n"; | |
string p = ""; | |
for(int i=0;i<s.size();i++){ | |
if(col(s[i]) && col(s[i+1])==false) p = p+" "+s[i]+" "; | |
else if(col(s[i]) && col(s[i+1])) p = p+" "+s[i]; | |
else p += s[i]; | |
} | |
s = p[0]; | |
for(int i=1;i<p.size()-1;i++){ | |
if(p[i]=='=' && comp(p[i-1]) && comp(p[i+1])) s = s+" "+p[i]+" "; | |
else s +=p[i]; | |
} | |
p = ""; | |
for(int i=0;i<s.size();i++){ | |
if(i!=s.size()-1 && s[i]!=' ' && s[i+1]==' ') p = p + s[i] + ' '; | |
else if(s[i]!=' ') p += s[i]; | |
} | |
s = p[0]; | |
for(int i=1;i<p.size()-1;i++){ | |
if(comp(p[i])==false && comp(p[i+1])==false){ | |
s = s + " "+ p[i]+p[i+1] + " "; | |
i++; | |
} | |
else s += p[i]; | |
} | |
s+= p[p.size()-1]; | |
cerr<<"step 1:\n"; | |
cerr<<s<<"\n\n"; | |
istringstream ss(s); | |
t(); | |
cerr<<"step 2:\n"; | |
while(ss>>s){ | |
if(mp.find(s)==mp.end()){ | |
if(isnum(s)==false){ | |
if(s[0]>='0' && s[0]<='9'){ | |
cout<<"[unkn "+s+"] "; | |
cerr<<"[unkn "+s+"] "; | |
} | |
else{ | |
cout<<"[id "+s+"] "; | |
cerr<<"[id "+s+"] "; | |
} | |
} | |
else{ | |
cout<<"[num "+s+"] "; | |
cerr<<"[num "+s+"] "; | |
} | |
} | |
else{ | |
cout<<mp[s]<<" "; | |
cerr<<mp[s]<<" "; | |
} | |
} | |
cout<<"\n"; | |
cerr<<"\n"; | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[kw float] [id x1] [op =] [num 3.125] [sep ;] [kw double] [id f1] [par (] [kw int] [id x] [par )] [par {] [kw double] [id z] [sep ;] [id z] [op =] [num 0.01] [op +] [id x] [op *] [num 5.5] [sep ;] [kw return] [id z] [sep ;] [par }] [kw int] [id main] [par (] [id void] [par )] [par {] [kw int] [id n1] [sep ;] [kw double] [id z] [sep ;] [id n1] [op =] [num 25] [sep ;] [id z] [op =] [id f1] [par (] [id n1] [par )] [sep ;] [par }] |
No comments:
Post a Comment