943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 441
  • C++ RSS
Oct 20th, 2009
1

Segmentation Fault ??

Expand Post »
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)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
klackey19 is offline Offline
19 posts
since Oct 2009
Oct 20th, 2009
-7
Re: Segmentation Fault ??
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 }
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 21st, 2009
0
Re: Segmentation Fault ??
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?
Reputation Points: 11
Solved Threads: 0
Newbie Poster
klackey19 is offline Offline
19 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with random number
Next Thread in C++ Forum Timeline: Help with creating Input exclusion/ignoring code.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC