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;
}



No comments:

Post a Comment

Football Player Transfer Prediction

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