Segmentation Fault ??

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 19
Reputation: klackey19 is an unknown quantity at this point 
Solved Threads: 0
klackey19 klackey19 is offline Offline
Newbie Poster

Segmentation Fault ??

 
1
  #1
Oct 20th, 2009
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.

  1. /*
  2. A -> I = E
  3. E -> T + E | T - E | T
  4. T -> P * T | P / T | P
  5. P -> I | L | (E)
  6. I -> a | b | ... | y | z
  7. L -> 0 | 1 | ... | 8 | 9
  8. */
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. bool literal(char *);
  18. bool integer(char *);
  19. bool primary(char *);
  20. bool term(char *);
  21. bool expr(char *);
  22. bool assign(char *);
  23.  
  24. int main ()
  25. {
  26. ifstream inFile;
  27. inFile.open("experiment.txt");
  28.  
  29. string line;
  30. inFile >> line;
  31.  
  32.  
  33. char *c;
  34. c = &line[0];
  35.  
  36. cout << "String read from file: " << line << endl;
  37.  
  38.  
  39. if (assign(c))
  40. cout << "The string: '" << line << "' is in the language" << endl;
  41. else
  42. cout << "The string: '" << line << "' is not in the language" << endl;
  43.  
  44.  
  45.  
  46. system("Pause");
  47. return 0;
  48. }
  49. bool assign (char *c)
  50. {
  51. if (integer(c))
  52. {
  53.  
  54. ++c;
  55. if (*c == '=')
  56. {
  57. ++c;
  58. if (expr(c))
  59. {
  60. ++c;
  61. return true;
  62. }
  63. }
  64. }
  65. else
  66. return false;
  67. }
  68.  
  69. bool literal(char *c)
  70. {
  71. if (*c == 0 || *c == 1 || *c == 2 || *c == 3 || *c == 4 || *c == 5 || *c == 6 || *c == 7 || *c == 8 || *c == 9)
  72. {
  73.  
  74. return true;
  75. }
  76. return false;
  77. }
  78.  
  79. bool integer (char *c)
  80. {
  81. cout << "in integer" << endl;
  82. 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')
  83. {
  84. return true;
  85. }
  86. else
  87. return false;
  88. }
  89.  
  90. bool primary (char *c)
  91. {
  92. if(integer(c))
  93. return true;
  94. else if (literal(c))
  95. return true;
  96. else if (*c == '(')
  97. ++c;
  98. if (expr(c))
  99. {
  100. ++c;
  101. if (*c == ')')
  102. {
  103. ++c;
  104. return true;
  105. }
  106. else
  107. return false;
  108. }
  109. else
  110. return false;
  111. }
  112.  
  113. bool term (char *c)
  114. {
  115. if (primary(c))
  116. {
  117. ++c;
  118. if (*c == '*' || *c == '/')
  119. {
  120. ++c;
  121. if (term(c))
  122. {
  123. return true;
  124. }
  125. else
  126. return false;
  127. }
  128. return true;
  129. }
  130. else
  131. return false;
  132. }
  133.  
  134. bool expr (char *c)
  135. {
  136. if (term(c))
  137. {
  138. ++c;
  139. if (*c == '+' || *c == '-')
  140. {
  141. ++c;
  142. if (expr(c))
  143. {
  144. return true;
  145. }
  146. }
  147. return true;
  148. }
  149. else
  150. return false;
  151. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-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
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 }
  1. bool primary (const char *c)
  2. {
  3.  
  4. if(integer(c))
  5. return true;
  6. else if (literal(c))
  7. return true;
  8. else if (*c == '(')
  9. {
  10. ++c;
  11. if (expr(c))
  12. {
  13. ++c;
  14. if (*c == ')')
  15. {
  16. ++c;
  17. return true;
  18. }
  19. return false;
  20. }
  21. }
  22. return false;
  23. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: klackey19 is an unknown quantity at this point 
Solved Threads: 0
klackey19 klackey19 is offline Offline
Newbie Poster
 
0
  #3
Oct 21st, 2009
Thank you so much for your help!!! My program works now !! I found the missing brackets and also added else return false to every if in the assign function. Just to make sure I understand, the error was simply in the missing brackets in the primary method?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 324 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC