Reputation: 1455
main.cpp
#include <iostream>
//#include "cfgtocnf.h"
#include "cyk.h"
using namespace std;
int main(){
int option;
cout << "Select an option below: \n";
cout << "1. CFG to CNF \n";
cout << "2. CYK \n";
cin >> option;
switch(option){
case 1:
//output();
break;
case 2:
cykoutput();
break;
}
}
cyk.h
#ifndef CYK_H
#define CYK_H
// This is the content of the .h file, which is where the declarations go
void cykoutput();
// This is the end of the header guard
#endif
cyk.cpp
//CNF grammer to CYK Chart
//statement to infom user whether the string can/cannot be generated by the grammar
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "cyk.h"
using namespace std;
string grammer[50][50];
bool state_checking1( string a){
if ( a.length() == 1 && (a[0] >= 'A' && a[0] <='Z') ) {
return true;
}
else{
return false;
}
}
bool input_checking(string a){
if (a.length() == 1 && (a[0] >= 'a' && a[0] <= 'z')) {
return true;
}
else if (a.length() == 2 && (a[0] >= 'A' && a[0] <= 'Z' && a[1] >= 'A' && a[1] <= 'Z')){
return true;
}
else{
return false;
}
}
bool remove_line1(int line , string a){
bool end = false;
bool check_grammer = true;
size_t line_position = -1;
int y = 1 , start = 0;
while (!end && check_grammer){
line_position = a.find("|");
if (line_position != -1){
a.erase(line_position,1);
line_position = line_position - start;
grammer[line][y] = a.substr(start, line_position);
check_grammer = input_checking(grammer[line][y]);
if (check_grammer == false){
break;
}
y++;
start = start + line_position;
}
else{
end = true;
grammer[line][y] = a.substr(start, line_position);
check_grammer = input_checking(grammer[line][y]);
}
}
return check_grammer;
}
string search(string a, int grammer_line_count){
string temp = "";
int k;
for (int j = 0; j < grammer_line_count; j++){
k = 1;
while (grammer[j][k] != "")
{
if (grammer[j][k] == a)
{
temp += grammer[j][0];
}
k++;
}
}
return temp;
}
string compare(string a, string b, int grammer_line_count){
string temp ,temp2 = "";
string aa = a;
string bb = b;
if (aa.find("_") ==0){
aa = aa.substr(1, aa.length());
}
if (bb.find("_") == 0){
bb = bb.substr(1, bb.length());
}
for (int i = 0; i < aa.length(); i++){
for (int j = 0; j < bb.length(); j++){
temp = "";
temp = temp + aa[i] + bb[j];
temp2 = temp2 + search(temp, grammer_line_count);
}
}
return temp2;
}
void cykoutput(){
ifstream file;
string file_name , file_line , input_string;
size_t search_pointer;
bool done = false;
bool isGrammer;
string check_generated;
//to output to file
ofstream myfile;
myfile.open ("cyk.txt");
while (!done) {
int grammer_line_count = 0;
isGrammer = true;
cout<<"=============================================================================="<<endl;
cout<<"\t\tCNF Grammar to CYK Chart Generator\n";
cout<<"==============================================================================\n"<<endl;
cout<<"**Enter -1 to exit**\nPlease enter CNF Grammar file name (e.g. xxx.txt) : ";
cin>>file_name;
if (file_name != "-1"){
file.open(file_name.c_str());
if (file) {
cout << "\nCNF Grammer : \n" << endl;
for (int i = 0; getline(file, file_line); i++) {
cout << file_line << endl;
search_pointer = file_line.find("->");
if (i == 0){
check_generated = file_line.substr(0, search_pointer);
}
grammer[i][0] = file_line.substr(0, search_pointer);
grammer_line_count++;
if (isGrammer) {
isGrammer = state_checking1(grammer[i][0]);
file_line = file_line.substr(search_pointer + 2, file_line.length());
isGrammer = remove_line1(i, file_line);
}
}
file.close();
if (!isGrammer) {
cout << "\nInvalid grammar!!!" << endl;
}
else{
cout << "\nEnter the input string : ";
cin >> input_string;
//Assign value-----------------
string temp;
string temp2;
string cykTable[50][50];
int input_length = input_string.length();
for (int i = 0; i < input_length; i++){
int y = 0;
temp = "";
temp2 = "";
temp+= input_string[i];
temp2 = search(temp, grammer_line_count);
cykTable[i][y] = temp2;
}
//compare the value------------
int x,y;
for (int i = 1; i < input_length; i++){ //already write 1st 1ine need to +1
y = i - 1;
x = 0;
for (int j = 0; j < (input_length - i); j++){
if (cykTable[x][y] != "DONE"){
if (cykTable[x][y + 1] == ""){
if (cykTable[x + 1][y] != "DONE"){
temp = "";
temp = compare(cykTable[x][y], cykTable[x + 1][y], grammer_line_count);
if (temp != ""){
cykTable[x + 1][y + 1] = "DONE";
}
else{
if (cykTable[x][y].find("_") == 0){
temp = cykTable[x][y];
}
else{
temp = "_" + cykTable[x][y];
}
}
cykTable[x][y + 1] = temp;
}
else{
int xx = x;
while (cykTable[xx + 1][y] == "DONE"){
xx++;
}
temp = "";
temp = compare(cykTable[x][y], cykTable[xx + 1][y], grammer_line_count);
if (temp != ""){
cykTable[xx + 1][y + 1] = "DONE";
}
else{
if (cykTable[x][y].find("_") == 0){
temp = cykTable[x][y];
}
else{
temp = "_" + cykTable[x][y];
}
}
cykTable[x][y + 1] = temp;
}
}
}
x++;
}
for (int k = 0; k < input_length; k++){
if (cykTable[k][i] == ""){
if (cykTable[k][i - 1] != "DONE"){
if (cykTable[k][i-1].find("_") == 0){
cykTable[k][i] = cykTable[k][i - 1];
}
else{
cykTable[k][i] = "_" + cykTable[k][i-1];
}
}
else{
cykTable[k][i] = "DONE";
}
}
}
}
//print out the cyk table------
myfile << "\n=========================================================" << endl;
myfile << "\t\tCYK Chart\n";
myfile << "=========================================================\n" << endl;
string check123;
y = 0;
for (int i = 0; i < input_length; i++){
if (i == input_length - 1){
if (cykTable[0][y].find("_") != -1){
search_pointer = cykTable[0][y].find("_");
cykTable[0][y] = cykTable[0][y].substr(search_pointer + 1, cykTable[0][y].length());
}
myfile<< cykTable[0][y];
}
else
{
for (int j = 0; j < input_length - i; j++){
check123 = "";
if (cykTable[j][y] == "DONE" || (cykTable[j][y].find("_") == 0)){
check123 = "";
}
else{
check123 = cykTable[j][y];
}
myfile << check123 << "\t";
}
}
y++;
myfile << endl;
}
//check either can be generated
int test = cykTable[0][input_length - 1].find(check_generated);
if ( test >= 0){
myfile << "\nString can be generated!!!\n"<<endl;
}
else{
myfile << "\nString can not be generated!!!\n"<<endl;
}
} //end else
}
else {
cout << "\nFile Not Found!!!" << endl;
}
system("PAUSE");
system("CLS");
for (int i = 0; i < 50; i++){
for (int j = 0; j < 50; j++){
grammer[i][j] = "";
}
}
}
else{
done = true;
}
}
}
When I tried to compile, it shows
fatal error: cyk.h: no such file or directory:
What's wrong here ? I am using codeblocks and all the files are under one project. I have already created the cyk.h under Header section but it says no such file or directory.
Upvotes: 3
Views: 225
Reputation: 2695
If you want a shortcut solution:
Open Code::Blocks, open "Settings" menu, click "Compiler...". Open the "Build Options" tab, and check the option "Explicitly add the current compiling file's directory...".
If not, you should configure your Compiler settings. If you don't know, then try crazy and learn. It's in "Search Directories" tab under "Global Compiler Settings".
Upvotes: 0
Reputation: 91
The problem is not related to your source code per se, please check your compiler settings: note that source code and headers directories are set separately (although they might point to the same directory)
Upvotes: 2
Reputation: 3616
please check the header file search path configuration in Build Options of codeblocks.
Upvotes: 0