Wednesday, November 21, 2018
Monday, October 22, 2018
Image convert to Base64 (from file to string)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
function readFile() { | |
if (this.files && this.files[0]) { | |
var FR= new FileReader(); | |
FR.addEventListener("load", function(e) { | |
document.getElementById("img").src = e.target.result; | |
document.getElementById("b64").innerHTML = e.target.result; | |
}); | |
FR.readAsDataURL( this.files[0] ); | |
} | |
} | |
document.getElementById("imgFile").addEventListener("change", readFile); | |
}); | |
</script> | |
</head> | |
<body> | |
<input id="imgFile" onload="readFile();" type="file" /><br> | |
<div id="b64"></div> | |
<img id="img" width="100" height="100"/> | |
</body> | |
</html> |
Demo:
Thursday, March 1, 2018
Random Restart Hill Climbing For N Queen
Hill climbing is an optimization technique which is a local
search algorithm. It is an algorithm that starts with a random solution to a
problem. By changing a single element of the solution it attempts to find a
better solution. Hill climbing is sometimes called greedy local search because
it grabs a good neighbor state without thinking ahead about where to go next.
To find a global maxima random restart hill climbing is
used. Random restart hill climbing is a series of hill climbing searches. From
randomly generated initial state until a goal is found.
board setup: n queens, one queen per column
successors : The successors of a state are all possible states generated by moving a single queen to another square in the same column. Meaning, there are n queens on the board and each queen can move to n-1 defined positions. So, each state has n*(n-1) successors.
heuristic cost function h : h is the number of pairs of queens that are attacking each other
Basic hill climbing creates a random state of the board. Then it finds the highest value successor of the current state(for n queen the lowest valued successor), which is called the neighbor. If the heuristic cost value of the neighbor is less than the heuristic cost value of the current state, then the neighbor becomes the current state. If not then the current solution is the minima of the current hill.
What random restart does is, it runs the hill climbing until the global minima is found. In each hill climbing, it creates a new initial state of the board then runs the hill climbing algorithm to find the minima of the hill. It stops when the global minima is reached.
h(board)
1 returns the number of attacking pair on board
highest_valued_successor_of_current(current)
1 best_h ← ∞
2 best_successor ← NULL
3 while(new successor can be created)
4 successor ← new_successor(current)
5 if h(successor) < best_h
6 best_h ← h(successor)
7 best_successor ← successor
8 return successor
hill_climbing()
1 current ← A random state of the board
2 while(1)
3 neighbor ← highest_valued_successor_of_current(current)
4 if h(neighbor) >= h(current)
5 return current
6 neighbor ← current
random_restart_hill_climbing()
1 final_h ← ∞
2 best_board ← NULL
3 while(final_h != 0)
4 result ← hill_climbing()
5 now_h ← h(result)
6 if final_h > now_h
7 final_h ← now_h
8 best_board ← result
9 return best_board
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
FINAL: | |
Total Iteration needed : 13 | |
number of conflict on final board : 0 | |
board: | |
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 | |
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 | |
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 | |
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 | |
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 | |
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 | |
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 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
#include<bits/stdc++.h> | |
using namespace std; | |
#define si 16 /// 16 queen | |
///for generating random numbers | |
random_device rseed; | |
mt19937 rgen(rseed()); // mersenne_twister | |
uniform_int_distribution<int> idist(0,si-1); // [0,15] | |
int iteration = 0; | |
void show_board(vector<int> a){ | |
int b[si][si]; | |
memset(b,0,sizeof(b)); | |
for(int i=0;i<a.size();i++){ | |
b[a[i]][i] = 1; | |
} | |
for(int i=0;i<si;i++){ | |
for(int j=0;j<si;j++){ | |
cout<<b[i][j]<<" "; | |
} | |
cout<<"\n"; | |
} | |
} | |
vector<int> make_node(){ | |
vector<int> board; | |
bool vis[20]; | |
memset(vis,false,sizeof(vis)); | |
srand(time(0)); | |
for(int i=1;i<=si;i++){ | |
int num = idist(rgen); | |
while(vis[num]==true) | |
num = idist(rgen); | |
board.push_back(num); | |
vis[num] = true; | |
} | |
return board; | |
} | |
int h(vector<int> current){ | |
int val,row1,col1,row2,col2; | |
val = 0; | |
for(int i=0;i<current.size();i++){ | |
row1 = current[i]; | |
col1 = i; | |
for(int j=i+1;j<current.size();j++){ | |
row2 = current[j]; | |
col2 = j; | |
if((row1==row2) || (col1==col2) || (abs(row1-row2)==abs(col1-col2)) ){ | |
val++; | |
} | |
} | |
} | |
return val; | |
} | |
vector<int> get_highest_valued_successor_of_current(vector<int> current){ | |
vector<int> successor,best_successor; | |
int best_h = INT_MAX; | |
int row,col; | |
for(int i=0;i<current.size();i++){ | |
row = current[i]; | |
col = i; | |
for(int j=0;j<si;j++){ | |
if(row == j) continue; | |
current[i] = j; | |
successor = current; | |
current[i] = row; | |
int now = h(successor); | |
if(now<best_h){ | |
best_successor = successor; | |
best_h = now; | |
} | |
} | |
} | |
return best_successor; | |
} | |
vector<int> hill_climbing(){ | |
/// index is the column number of the queen and the value is the row number | |
vector<int> current = make_node(); | |
vector<int> neighbor; | |
while(1){ | |
neighbor = get_highest_valued_successor_of_current(current); | |
if(h(neighbor)>=h(current)) return current; | |
current = neighbor; | |
} | |
} | |
vector<int> random_restart_hill_climbing(){ | |
int val = INT_MAX; | |
vector<int> best; | |
iteration = 0; | |
while(val!=0){ | |
iteration++; | |
vector<int> result = hill_climbing(); | |
int now = h(result); | |
if(val>=now){ | |
best = result; | |
val = now; | |
} | |
} | |
return best; | |
} | |
int main(){ | |
vector<int> result = random_restart_hill_climbing(); | |
cout<<"FINAL:\n"; | |
cout<<"Total Iteration needed : "<<iteration<<"\n"; | |
cout<<"number of conflict on final board : "<<h(result)<<"\n"; | |
cout<<"board: \n"; | |
show_board(result); | |
return 0; | |
} | |
Friday, February 16, 2018
Football Player Transfer Prediction
Football Player Transfer Prediction Using Different Classifiers
Project Report : Football Player Transfer Prediction Report
DataSet : LINK
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.
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
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; | |
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"; | |
} | |
} | |
} | |
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
b | |
ab | |
aab | |
aaab |
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
valid | |
valid | |
valid | |
valid |
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; | |
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"; | |
} | |
} | |
} |
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
asasfas | |
bba | |
ba | |
abbd |
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
invalid | |
invalid | |
invalid | |
valid |
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; | |
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"; | |
} | |
} |
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
E = TE' | |
E' = +TE' | # | |
T = FT' | |
T' = *FT' | # | |
F = (E) | id |
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
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) = {*,$,+,)} |
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
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; | |
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; | |
} |
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
/* 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);} |
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
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 |
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
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; | |
} |
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
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 }] |
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
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
/* 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); | |
} |
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; | |
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; | |
} |
Subscribe to:
Posts (Atom)
Football Player Transfer Prediction
Football Player Transfer Prediction Using Different Classifiers Project Report : Football Player Transfer Prediction Report ...
-
This Project has been designed and developed for fulfillment of my 2nd year,1st semester Project Work. Video:
-
A 2D game on basic c and c++ for fulfillment of my 1st year,2nd semester Project Work. Screenshot: