| | |
Segmentation Fault ??
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
So I'm working on a project for a class in which we have to create a parser for a made up grammar. The input is read from a file. When I use input that is in the grammar, the program works. For certain input not in the grammar such as 9=b+a (which is not in the grammar) the program says it is not in the grammar. However, when I test other input that is not in the grammar such as b=g**h (basically whenever I use two operators (+ - / *) in a row, the program crashes with I believe a segmentation fault. Any ideas why this is happening? Thanks in advance for your help. Here is my code with the grammar at the top commented out.
C++ Syntax (Toggle Plain Text)
/* A -> I = E E -> T + E | T - E | T T -> P * T | P / T | P P -> I | L | (E) I -> a | b | ... | y | z L -> 0 | 1 | ... | 8 | 9 */ #include <iostream> #include <fstream> #include <string> using namespace std; bool literal(char *); bool integer(char *); bool primary(char *); bool term(char *); bool expr(char *); bool assign(char *); int main () { ifstream inFile; inFile.open("experiment.txt"); string line; inFile >> line; char *c; c = &line[0]; cout << "String read from file: " << line << endl; if (assign(c)) cout << "The string: '" << line << "' is in the language" << endl; else cout << "The string: '" << line << "' is not in the language" << endl; system("Pause"); return 0; } bool assign (char *c) { if (integer(c)) { ++c; if (*c == '=') { ++c; if (expr(c)) { ++c; return true; } } } else return false; } bool literal(char *c) { if (*c == 0 || *c == 1 || *c == 2 || *c == 3 || *c == 4 || *c == 5 || *c == 6 || *c == 7 || *c == 8 || *c == 9) { return true; } return false; } bool integer (char *c) { cout << "in integer" << endl; if (*c == 'a' || *c == 'b' || *c == 'c' || *c == 'd' || *c == 'e' || *c == 'f' || *c == 'g' || *c == 'h' || *c == 'i' || *c == 'j' || *c == 'k' || *c == 'l' || *c == 'm' || *c == 'n' || *c == 'o' || *c == 'p' || *c == 'q' || *c == 'r' || *c == 's' || *c == 't' || *c == 'u' || *c == 'v' || *c == 'w' || *c == 'x' || *c == 'y' || *c == 'z') { return true; } else return false; } bool primary (char *c) { if(integer(c)) return true; else if (literal(c)) return true; else if (*c == '(') ++c; if (expr(c)) { ++c; if (*c == ')') { ++c; return true; } else return false; } else return false; } bool term (char *c) { if (primary(c)) { ++c; if (*c == '*' || *c == '/') { ++c; if (term(c)) { return true; } else return false; } return true; } else return false; } bool expr (char *c) { if (term(c)) { ++c; if (*c == '+' || *c == '-') { ++c; if (expr(c)) { return true; } } return true; } else return false; }
-7
#2 Oct 20th, 2009
lines 33 and 34:
>>char *c;
>>c = &line[0];
That is not necessary. Just pass line.c_str() to the function on line 39
line 71: all you have to do is use isdigit() function
line 82: get rid of all that crap and use isalpha()
The program is missing a set of { and }
>>char *c;
>>c = &line[0];
That is not necessary. Just pass line.c_str() to the function on line 39
line 71: all you have to do is use isdigit() function
if( isdigit(*c)) line 82: get rid of all that crap and use isalpha()
if( isalpha(*c) ) The program is missing a set of { and }
C++ Syntax (Toggle Plain Text)
bool primary (const char *c) { if(integer(c)) return true; else if (literal(c)) return true; else if (*c == '(') { ++c; if (expr(c)) { ++c; if (*c == ')') { ++c; return true; } return false; } } return false; }
Last edited by Ancient Dragon; Oct 20th, 2009 at 10:57 pm.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
![]() |
Similar Threads
- segmentation fault (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unix/C++ segmentation fault (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with random number
- Next Thread: Help with creating Input exclusion/ignoring code.
Views: 324 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct studio temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






