Saturday, January 27, 2018

Use of CFGs for Parsing OPEN

We can think of using CFGs to detect various language constructs in the token streams freed from simple
syntactic and semantic errors, as it is easier to describe the constructs with CFGs. But CFGs are hard to
apply practically. In this session we first exercise on simpler implementations of CFGs, and then
implement the FIRST and FOLLOW functions that facilitate efficient parsing algorithms for practical
problems.

Session 5 by Nafis Islam on Scribd


#include<bits/stdc++.h>
using namespace std;
int i=0,f=0,l;
string st;
void A() {
if (st[i] == 'a') {
i++;
f=1;
}
else {
f=0;
return;
}
if (i<l-1) A();
}
void B() {
if (st[i] == 'b') {
i++;
f=1;
return;
}
else {
f=0;
return;
}
}
void S() {
if (st[i] == 'b'){
i++;
f = 1;
return;
}
else {
A();
if (f) { B(); return;}
}
}
int main(){
freopen("i1.txt","r",stdin);
freopen("o1.txt","w",stdout);
while(getline(cin,st)){
f = 0;
i = 0;
l = st.size();
S();
if(l==i && f){
cout<<"valid\n";
}
else{
cout<<"invalid\n";
}
}
}
view raw grammar1.cpp hosted with ❤ by GitHub
b
ab
aab
aaab
view raw i1.txt hosted with ❤ by GitHub
valid
valid
valid
valid
view raw oi.txt hosted with ❤ by GitHub

#include<bits/stdc++.h>
using namespace std;
int i=0,f=0,l;
string s;
void X(){
if(s[i]=='b'){
i++;
f = 1;
}
else{
f = 0;
return;
}
if(s[i]=='b'){
i++;
f = 1;
if(i!=l-1) X();
}
else if(s[i]=='c'){
i++;
f = 1;
if(i!=l-1) X();
}
else{
f = 0;
return;
}
}
void A(){
if(s[i]=='a'){
i++;
f = 1;
}
else return;
if(i!=l-1){
X();
}
if(i==l-1 && f){
if(s[i]=='d'){
f = 1;
i++;
return;
}
else{
f = 0;
return;
}
}
}
int main(){
freopen("i2.txt","r",stdin);
freopen("o2.txt","w",stdout);
while(getline(cin,s)){
f = 0;
i = 0;
l = s.size();
A();
if(l==i && f){
cout<<"valid\n";
}
else{
cout<<"invalid\n";
}
}
}
view raw grammar2.cpp hosted with ❤ by GitHub
asasfas
bba
ba
abbd
view raw i2.txt hosted with ❤ by GitHub
invalid
invalid
invalid
valid
view raw out.txt hosted with ❤ by GitHub

#include<bits/stdc++.h>
using namespace std;
vector<string>sp,ke,ri;
map<string,string>mp,mpp;
string ans;
bool isTERMINAL(char a){
if(a>='A' && a<='Z') return true;
return false;
}
void FIRST(string key){
string val = mp[key];
if(isTERMINAL(val[0])){
string p = "";
p += val[0];
FIRST(p);
}
else{
ans += val[0];
ans += ",";
int flag = 0;
for(int i=0;i<val.size();i++){
if(val[i]=='|'){
flag = 1;
continue;
}
if(flag){
ans += val[i];
}
}
}
}
void FOLLOW(string key,int z){
int flag = 0;
for(int i=0;i<ri.size();i++){
if (ri[i].find(key) != string::npos) {
if(key.size()==1){
for(int j=0;j<ri[i].size();j++){
if(ri[i][j]==key[0]){
if(j+1<ri.size() && ri[i][j+1]!='\''){
flag = 1;
if(isTERMINAL(ri[i][j+1])==false){
if(z==0)ans += "$,";
ans += ri[i][j+1];
}
else{
string g = ri[i];
g.erase(0,1);
FIRST(g);
if(z==0)ans += "$,";
FOLLOW(mpp[ri[i]],1);
}
break;
}
}
}
}
else{
flag = 1;
for(int j=0;j+1<ri[i].size();j++){
if(ri[i][j]==key[0] && ri[i][j+1]==key[1]){
if(j+2>=ri[i].size()){
FOLLOW(mpp[ri[i]],1);
if(z==0)ans += ",$";
}
else{
}
}
}
break;
}
}
if(flag) break;
}
}
string remove_space(string s){
string p="";
for(int i=0;i<s.size();i++){
if(s[i]!=' ') p = p + s[i];
}
return p;
}
int main(){
freopen("input.txt","r",stdin);
freopen("out.txt","w",stdout);
string s;
while(getline(cin,s)){
sp.push_back(remove_space(s));
}
for(int i=0;i<sp.size();i++){
int flag = 0;
string key="",val="";
for(int j=0;j<sp[i].size();j++){
if(sp[i][j]=='='){
flag = 1;
continue;
}
if(flag==0) key += sp[i][j];
else val += sp[i][j];
}
mp[key] = val;
ke.push_back(key);
}
cerr<<"FIRST: \n\n";
cout<<"FIRST: \n\n";
for(int i=0;i<ke.size();i++){
ans = "";
FIRST(ke[i]);
cerr<<"FIRST("<<ke[i]<<")"<<" = {"<<ans<<"}\n";
cout<<"FIRST("<<ke[i]<<")"<<" = {"<<ans<<"}\n";
}
for(int i=0;i<ke.size();i++){
string val = mp[ke[i]];
string v = "";
for(int j=0;j<val.size();j++){
if(val[j]=='|') break;
v += val[j];
}
mp[ke[i]] = v;
mpp[v] = ke[i];
ri.push_back(v);
}
cerr<<"\nFOLLOW: \n\n";
cout<<"\nFOLLOW: \n\n";
for(int i=0;i<ke.size();i++){
ans = "";
FOLLOW(ke[i],0);
cerr<<"FOLLOW("<<ke[i]<<")"<<" = {"<<ans<<"}\n";
cout<<"FOLLOW("<<ke[i]<<")"<<" = {"<<ans<<"}\n";
}
}
E = TE'
E' = +TE' | #
T = FT'
T' = *FT' | #
F = (E) | id
view raw input.txt hosted with ❤ by GitHub
FIRST:
FIRST(E) = {(,id}
FIRST(E') = {+,#}
FIRST(T) = {(,id}
FIRST(T') = {*,#}
FIRST(F) = {(,id}
FOLLOW:
FOLLOW(E) = {$,)}
FOLLOW(E') = {),$}
FOLLOW(T) = {+,$,)}
FOLLOW(T') = {+,),$}
FOLLOW(F) = {*,$,+,)}
view raw output.txt hosted with ❤ by GitHub

Friday, January 5, 2018

Detecting Simple Syntax Errors

Syntax errors are very common in source programs. Suppose, a given C source program has been scanned, filtered, lexically analyzed and tokenized as that were done in earlier sessions. In addition, line numbers have been assigned to the source code lines for generating proper error messages. As the first step to Syntax Analysis, we now perform detection of simple syntax errors like duplication of tokens except parentheses or braces, Unbalanced braces or parentheses problem, unmatched ‘else’ problem, etc


Session 4 by Nafis Islam on Scribd



#include<bits/stdc++.h>
using namespace std;
string int_to_string(int a){
stringstream ss;
ss << a;
string str = ss.str();
return str;
}
vector<string> number_lines(vector<string>sp){
int flag = 0;
string s;
int flag3 = -1;
for(int i=0;i<sp.size();i++){
s = "";
int sz = sp[i].size();
flag3 = -1;
for(int j=0;j<sz;j++) if(sp[i][j]=='\t') sp[i][j] = ' ';
for(int j=0;j<sz;j++){
if(j!=sz-1 && sp[i][j]!=' ' && sp[i][j+1]==' ') s = s + sp[i][j] + ' ';
else if(sp[i][j]!=' ') s += sp[i][j];
}
for(int j=0;j<sz;j++){
if(sp[i][j]=='"'){
flag3 = j;
break;
}
}
if(flag3!=-1){
string p = "";
for(int j=0;s[j]!='"';j++) p += s[j];
p += "\"";
for(int j=flag3+1,r=0;sp[i][j]!='"';j++) p += sp[i][j];
for(int j=0,r=0;j<s.size();j++){
if(s[j]=='"') r++;
if(r==2) p +=s[j];
}
swap(s,p);
}
swap(sp[i],s);
}
vector<string>sp1;
int flag1 = 0,flag2=0;
for(int i=0;i<sp.size();i++){
string str = int_to_string(i+1);
int sz = sp[i].size();
if(sz==0){
sp1.push_back(str);
continue;
}
for(int j=0;j<sz;j++){
if(j!=sz-1 && sp[i][j]=='/' && sp[i][j+1]=='/'){
flag1 = 1;
for(int k=0;k<j;k++){
cout<<sp[i][k];
cerr<<sp[i][k];
}
break;
}
if(j!=sz-1 && sp[i][j]=='/' && sp[i][j+1]=='*'){
flag2 = 1;
for(int k=0;k<j;k++){
cout<<sp[i][k];
cerr<<sp[i][k];
}
}
if(j!=sz-1 && sp[i][j]=='*' && sp[i][j+1]=='/'){
flag2 = 0;
flag1 = 1;
break;
}
}
if(flag1){
flag1 = 0;
sp1.push_back(str);
continue;
}
if(flag2){
sp1.push_back(str);
continue;
}
str = str + " " + sp[i];
sp1.push_back(str);
}
return sp1;
}
vector<string> paranthesis_error(vector<string> sp){
stack<int>st;
vector<string>err;
for(int i=0;i<sp.size();i++){
for(int j=0;j<sp[i].size();j++){
if(sp[i][j]=='{') st.push(i+1);
else if(sp[i][j]=='}'){
if( !st.empty() ) st.pop();
else err.push_back("Error: Misplaced '}' at line "+int_to_string(i+1));
}
}
}
if( !st.empty() ) err.push_back("Error: Not Balanced Parentheses at line "+int_to_string(sp.size()));
return err;
}
vector<string> if_else_error(vector<string> sp){
bool ok = false;
vector<string>err;
int sz = sp.size();
for(int i=0;i<sz;i++){
if(sz<4)continue;
int x = sp[i].size();
for(int j=0;j<x;j++){
if(j+1<x && sp[i][j]=='i' && sp[i][j+1]=='f') ok = true;
if(j+3<x && sp[i][j]=='e' && sp[i][j+1]=='l' && sp[i][j+2]=='s' && sp[i][j+3]=='e'){
if( ok ){
ok = false;
continue;
}
else err.push_back("Error: Not Matched else at line "+int_to_string(i+1));
}
}
}
return err;
}
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;
}
vector<string> dup_token_error(vector<string> sp){
vector<string>err;
int sz = sp.size();
for(int j=0;j<sz;j++){
string p = "",s=sp[j];
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];
istringstream ss(s);
string last = "";
while(ss>>s){
if(s==last) err.push_back("Error: Duplicate token at line "+int_to_string(j+1));
last = s;
}
}
return err;
}
int main(){
freopen("input.txt","r",stdin);
freopen("out.txt","w",stdout);
string s;
vector<string>sp,paran_error,if_else_err,dup_token_err,error;
cerr<<"input\n";
while(getline(cin,s)){
sp.push_back(s);
cerr<<s<<"\n";
}
cerr<<"\n";
sp = number_lines(sp);
cerr<<"\noutput:\n";
cerr<<"Recognized tokens in the lines of code:\n";
for(int i=0;i<sp.size();i++){
cout<<sp[i]<<"\n";
cerr<<sp[i]<<"\n";
}
paran_error = paranthesis_error(sp);
if_else_err = if_else_error(sp);
dup_token_err = dup_token_error(sp);
paran_error.erase( unique( paran_error.begin(), paran_error.end() ), paran_error.end() );
if_else_err.erase( unique( if_else_err.begin(), if_else_err.end() ), if_else_err.end() );
dup_token_err.erase( unique( dup_token_err.begin(), dup_token_err.end() ), dup_token_err.end() );
cout<<"\n\nERROR: \n";
cerr<<"\n\nERROR: \n";
for(int i=0;i<paran_error.size();i++){
cout<<paran_error[i]<<"\n";
cerr<<paran_error[i]<<"\n";
}
for(int i=0;i<if_else_err.size();i++){
cout<<if_else_err[i]<<"\n";
cerr<<if_else_err[i]<<"\n";
}
for(int i=0;i<dup_token_err.size();i++){
cout<<dup_token_err[i]<<"\n";
cerr<<dup_token_err[i]<<"\n";
}
return 0;
}
/* A program fragment*/
float x1 = 3.125;;
/* Definition of function f1 */
double f1(int int x)
{if(x<x1)
double z;
else z = 0.01+x*5.5;}}
else return z;
}
/* Beginning of 'main' */
int main(void)
{
int n1; double z;
{{ n1=25; z=f1(n1);}
view raw input.txt hosted with ❤ by GitHub
1
2
3 float x1 = 3.125;;
4
5 double f1(int int x)
6 {if(x<x1)
7 double z;
8 else z = 0.01+x*5.5;}}
9 else return z;
10 }
11
12 int main(void)
13 {
14 int n1; double z;
15 {{ n1=25; z=f1(n1);}
ERROR:
Error: Misplaced '}' at line 8
Error: Misplaced '}' at line 10
Error: Not Balanced Parentheses at line 15
Error: Not Matched else at line 9
Error: Duplicate token at line 3
Error: Duplicate token at line 5
view raw output.txt hosted with ❤ by GitHub

Symbol Table Generation

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

[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 }]
view raw input.txt hosted with ❤ by GitHub
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][)][;][}]
view raw output.txt hosted with ❤ by GitHub
#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;
}

Lexical Analysis

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

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); }
view raw input.txt hosted with ❤ by GitHub
#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;
}
[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 }]
view raw output.txt hosted with ❤ by GitHub

Thursday, January 4, 2018

Scanning and Filtering a Source Program

You are given a C source program with single and multiple line comments. As the first step toward compilation you need to remove the comments and white space (extra spaces, tabs and newline characters). Develop a program that takes as input file the given source program and produces a filtered file as stated above. The program must also display both the files.

Session 1 by Nafis Islam on Scribd

/* A program fragment */
float x1 = 3.125;
/* Definition of function f1 */
double f1(int x)
{
double z;
z = 0.01+x*5.5;
return z;
}
/* Beginning of 'main'*/
int main(void)
{
int n1;
double z;
n1=25;
z=f1(n1);
}
view raw input.txt hosted with ❤ by GitHub
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); }
view raw output.txt hosted with ❤ by GitHub
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
string s;
vector<string>sp;
int flag = 0;
cerr<<"input:\n";
while(getline(cin,s)){
sp.push_back(s);
}
for(int i=0;i<sp.size();i++) cerr<<sp[i]<<"\n";
cerr<<"\noutput:\n";
int flag3 = -1;
for(int i=0;i<sp.size();i++){
s = "";
int sz = sp[i].size();
flag3 = -1;
for(int j=0;j<sz;j++) if(sp[i][j]=='\t') sp[i][j] = ' ';
for(int j=0;j<sz;j++){
if(j!=sz-1 && sp[i][j]!=' ' && sp[i][j+1]==' ') s = s + sp[i][j] + ' ';
else if(sp[i][j]!=' ') s += sp[i][j];
}
for(int j=0;j<sz;j++){
if(sp[i][j]=='"'){
flag3 = j;
break;
}
}
if(flag3!=-1){
string p = "";
for(int j=0;s[j]!='"';j++) p += s[j];
p += "\"";
for(int j=flag3+1,r=0;sp[i][j]!='"';j++) p += sp[i][j];
for(int j=0,r=0;j<s.size();j++){
if(s[j]=='"') r++;
if(r==2) p +=s[j];
}
swap(s,p);
}
swap(sp[i],s);
}
//for(int i=0;i<sp.size();i++) cerr<<sp[i]<<"\n";
int flag1 = 0,flag2=0;
for(int i=0;i<sp.size();i++){
int sz = sp[i].size();
if(sz==0) continue;
for(int j=0;j<sz;j++){
if(j!=sz-1 && sp[i][j]=='/' && sp[i][j+1]=='/'){
flag1 = 1;
for(int k=0;k<j;k++){
cout<<sp[i][k];
cerr<<sp[i][k];
}
break;
}
if(j!=sz-1 && sp[i][j]=='/' && sp[i][j+1]=='*'){
flag2 = 1;
for(int k=0;k<j;k++){
cout<<sp[i][k];
cerr<<sp[i][k];
}
}
if(j!=sz-1 && sp[i][j]=='*' && sp[i][j+1]=='/'){
flag2 = 0;
flag1 = 1;
break;
}
}
if(flag1){
flag1 = 0;
continue;
}
if(flag2){
continue;
}
cout<<sp[i]<<" ";
cerr<<sp[i]<<" ";
}
return 0;
}



Football Player Transfer Prediction

Football Player Transfer Prediction Using Different Classifiers Project Report :  Football Player Transfer Prediction Report ...