A given C source program has been scanned, filtered and then lexically analyzed as it was done in Session 2. We have all the lexemes marked as different types of tokens like keywords, identifiers, operators, separators, parentheses, numbers, etc. We now get corrected the unknown lexemes first, and then generate a Symbol Table describing the features of the identifiers. Finally, we generate a modified token stream in accordance with the Symbol Table for processing by the next phase, that is, Syntactic Analysis
Session 3 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
[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 }] |
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
step 1: | |
[float][id x1][=][3.125][;][double][id f1][(][int][id x][)][{][double][id z][;][id z][=][0.01][+][id x][*][5.5][;][return][id z][;][}][int][id main][(][id void][)][{][int][id n1][;][double][id z][;][id n1][=][25][;][id z][=][id f1][(][id n1][)][;][}] | |
step 2: | |
1 x1 var float global | |
2 f1 func double global | |
3 x var int f1 | |
4 z var double f1 | |
5 main func int global | |
6 n1 var int main | |
7 z var double main | |
step 3: | |
[float][id 1][=][3.125][;][double][id 2][(][int][id 3][)][{][double][id 4][;][id 4][=][0.01][+][id 3][*][5.5][;][return][id 4][;][}][int][id 5][(][id 0][)][{][int][id 6][;][double][id 7][;][id 6][=][25][;][id 7][=][id 2][(][id 6][)][;][}] |
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; | |
map<string,string>mp; | |
void tab(){ | |
mp["[kwint]"] = "[int]"; | |
mp["[kwvoid]"] = "[void]"; | |
mp["[kwfloat]"] = "[float]"; | |
mp["[kwdouble]"] = "[double]"; | |
mp["[kwreturn]"] = "[return]"; | |
mp["[kwchar]"] = "[char]"; | |
mp["[sep;]"] = "[;]"; | |
mp["[sep,]"] ="[,]"; | |
mp["[sep']"] = "[']"; | |
mp["[kwif]"] = "[if]"; | |
mp["[kwelse]"] = "[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==]"] = "[==]"; | |
mp["[brc}]"] = "[}]"; | |
mp["[brc{]"] = "[{]"; | |
} | |
int main(){ | |
freopen("out2.txt","r",stdin); | |
freopen("out3.txt","w",stdout); | |
string s,p=""; | |
tab(); | |
getline(cin,s); | |
cerr<<"input:\n"<<s<<"\n"; | |
for(int i=0;i<s.size();i++){ | |
if(s[i]==' ') continue; | |
p += s[i]; | |
} | |
s = ""; | |
for(int i=0;i<p.size()-1;i++){ | |
if(p[i]==']' && p[i+1]=='[') s = s + p[i]+" "; | |
else s += p[i]; | |
} | |
s += p[p.size()-1]; | |
istringstream ss(s); | |
p = ""; | |
while(ss>>s){ | |
if(mp.find(s)==mp.end()){ | |
p += s; | |
} | |
else p += mp[s]; | |
} | |
s = p[0]; | |
for(int i=1;i<p.size()-3;i++){ | |
if(p[i]=='i' && p[i+1]=='d' && p[i-1]!='o'){ | |
s += "id "; | |
i++; | |
} | |
else if(p[i]=='n' && p[i+1]=='u' && p[i+2]=='m'){ | |
i = i+2; | |
} | |
else s += p[i]; | |
} | |
for(int i=p.size()-3;i<p.size();i++){ | |
s += p[i]; | |
} | |
cerr<<"\n\noutput:\n"; | |
cout<<"step 1:\n"; | |
cerr<<"step 1:\n"; | |
cout<<s<<"\n\n"; | |
cerr<<s<<"\n\n"; | |
string type = "global"; | |
vector<string>nam,v_type,d_type,f_type; | |
string id,var_type,data_type,go_or_loc_type,last_func_name; | |
id = var_type = data_type = go_or_loc_type,last_func_name = ""; | |
int flag = 0; | |
int found_func = 0; | |
for(int i=1;i<s.size()-1;i++){ | |
int se = 2; | |
if(s[i]=='i' && s[i+1]=='d' && s[i-1]!='o'){ | |
///get id | |
string id_name=""; | |
int k = i+3; | |
while(1){ | |
if(s[k]==']') break; | |
id_name += s[k]; | |
k++; | |
} | |
if(id_name=="main") flag =1; | |
id = id_name; | |
///get func or var | |
string vt=""; | |
int kk = i+2; | |
while(1){ | |
if(s[kk]=='[' && s[kk+1]=='('){ | |
var_type = "func"; | |
vt = "func"; | |
last_func_name = id; | |
found_func = 1; | |
break; | |
} | |
if(s[kk]=='[' && s[kk+1]!='('){ | |
var_type = "var"; | |
vt = "var"; | |
break; | |
} | |
kk++; | |
} | |
/// gol or loc | |
if(vt=="func") type = "global"; | |
else if(flag) type = "main"; | |
else if(found_func==0) type = "global"; | |
else type = last_func_name; | |
///nam | |
int j = i-1; | |
string n=""; | |
while(se){ | |
if(j<0) break; | |
if(s[j]=='[') { | |
se--; | |
j--; | |
continue; | |
} | |
if(s[j]==']'){ | |
j--; | |
continue; | |
} | |
else{ | |
n = s[j]+n; | |
j--; | |
} | |
} | |
data_type = n; | |
if(data_type=="int" || data_type == "double" || data_type=="float"){ | |
d_type.push_back(data_type); | |
f_type.push_back(type); | |
nam.push_back(id); | |
v_type.push_back(var_type); | |
} | |
else continue; | |
} | |
} | |
cerr<<"step 2:\n"; | |
cout<<"step 2:\n"; | |
map<pair<string,string>,int>mmp; | |
int xx = 8; | |
for(int i=0;i<nam.size();i++){ | |
cout<<i+1<<" "<<left<<setw(xx)<<nam[i]<<setw(xx)<<v_type[i]<<setw(xx)<<d_type[i]<<setw(xx)<<f_type[i]<<"\n"; | |
cerr<<i+1<<" "<<left<<setw(xx)<<nam[i]<<setw(xx)<<v_type[i]<<setw(xx)<<d_type[i]<<setw(xx)<<f_type[i]<<"\n"; | |
mmp[make_pair(nam[i],f_type[i])] = i+1; | |
} | |
flag = 0; | |
type = "global"; | |
last_func_name = "global"; | |
queue<int>q; | |
for(int i=1;i<s.size()-1;i++){ | |
int se = 2; | |
if(s[i]=='i' && s[i+1]=='d' && s[i-1]!='o'){ | |
///get id | |
string id_name=""; | |
int k = i+3; | |
while(1){ | |
if(s[k]==']') break; | |
id_name += s[k]; | |
k++; | |
} | |
if(id_name=="main") flag =1; | |
id = id_name; | |
///get func or var | |
string vt=""; | |
int kk = i+2; | |
while(1){ | |
if(s[kk]=='[' && s[kk+1]=='('){ | |
vt = "func"; | |
last_func_name = id; | |
found_func = 1; | |
break; | |
} | |
if(s[kk]=='[' && s[kk+1]!='('){ | |
vt = "var"; | |
break; | |
} | |
kk++; | |
} | |
/// gol or loc | |
if(vt=="func") type = "global"; | |
else if(flag) type = "main"; | |
else if(found_func==0) type = "global"; | |
else type = last_func_name; | |
q.push(mmp[make_pair(id,type)]); | |
} | |
} | |
cerr<<"\nstep 3:\n"; | |
cout<<"\nstep 3:\n"; | |
cerr<<s[0]; | |
cout<<s[0]; | |
for(int i=1;i<s.size()-1;i++){ | |
if(s[i]=='i' && s[i+1]=='d' && s[i-1]!='o'){ | |
cout<<"id "<<q.front(); | |
cerr<<"id "<<q.front(); | |
q.pop(); | |
while(1){ | |
i++; | |
if(s[i]==']') break; | |
} | |
} | |
cout<<s[i]; | |
cerr<<s[i]; | |
} | |
cerr<<s[s.size()-1]<<"\n"; | |
cout<<s[s.size()-1]<<"\n"; | |
return 0; | |
} |
Great bro!
ReplyDelete