Creating a Command Line Parser

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Creating a Command Line Parser

 
0
  #1
Feb 3rd, 2007
I need help developing a program that acts as a command line parser. Essentially, my program needs to ask the user to input a command line: eg - A+B=C. It will then output the following:
A
+
B
=
C.

Although this seems basic, my program needs to recognize different groups of characters and output them correctly. The following are examples my program needs to take into consideration:

1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343

Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.

2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C

3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]

4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6

4a)
D_1
=
Need ']' to close matrix

5) A regular sentence: Hello How are you
5a)
Hello
How
are
you

6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;

I have worked on a pseudocode for the main part of my program thus far, and I think I have all the functions named within it that I would need. I was wondering if someone could help me develop the functions correctly for my program, thanks!

Below is my pseudocode:

  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7.  
  8. int main()
  9. {
  10. cout << " Welcome to My Command Line Parser!" << endl;
  11. cout << " XXXXXXXXXX, XXXXXXXXXXXXXXXXXXXXXX" << endl;
  12. cout << " XXXXXXXX, 2007\n" << endl;
  13. cout << "\n" << endl;
  14. cout << "Input a Command Line\n";
  15. cin.getline(buffer, 500);
  16.  
  17. char piece[500];
  18. int st = 0, ed = 0, err_code = 1;
  19.  
  20. while( buffer[st]!=0 )
  21. {
  22. st = find_first_element( );
  23. if (buffer[st] == 0) err_code = 0;
  24.  
  25. ed = find_last_element( st ); // err_code can be set inside
  26.  
  27. if (err_code == 0) break;
  28. else
  29. {
  30. _copy_segment(piece, st, ed);
  31. _display_segment(piece);
  32. st = ed + 1;
  33. }
  34. }
  35. return err_code;
  36.  
  37. }
  38.  
  39. //Functions
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Creating a Command Line Parser

 
2
  #2
Feb 3rd, 2007
Originally Posted by raydogg57 View Post
1) ABKLJFDLKJLKKDFJ++++++ajkadsl;fkjld;akjk=====daskflj342343
1a)
ABKLJFDLKJLKKDFJ
++++++
ajkadsl
;
fkjld
;
akjk
=====
daskflj342343

Explanation: program groups letters and characters together, but moves to next line when a symbol is reached.
Look at isalpha(), isalnum(), and isdigit() functions

Originally Posted by raydogg57 View Post
2) It also must recognize whitespace: A + B = C
2a)
A
+
B
=
C
isspace() for this.

Originally Posted by raydogg57 View Post
3) It must recognize a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6]
3a)
D_1
=
[1 3; 2 3 4; ; 4 5 6]
Work on this last....

Originally Posted by raydogg57 View Post
4) If the user forgets the second bracket to a matrix: D_1 = [1 3; 2 3 4; ; 4 5 6

4a)
D_1
=
Need ']' to close matrix
Keep a counter for each pair of [, {, ( seen. Inc at open, dec at close. If by the end of the input any are not 0 you have a mismatch.

Originally Posted by raydogg57 View Post
5) A regular sentence: Hello How are you
5a)
Hello
How
are
you
What about them? Would aple banena charry be acceptable as a sentence? I think this would be beyond the scope of your program.

Originally Posted by raydogg57 View Post
6) Anything including invalid segments. The only valid operator segments are +-/* Invalid operator segments could be: +%-, +&!
Valid number segments can start with '.' or a digit. an invalid number would be: 3.abc, which should be parsed as 3. and then abc. Only numbers, whitespace, and ';' should be allowed in the whitespace. a sequence of ';' and ',' is valid: ;;;,;;;,,,;
Make your own isXXX() function, like isoperator() and you will have to define a set of rules and program for them.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Creating a Command Line Parser

 
0
  #3
Feb 4th, 2007
Originally Posted by WaltP View Post
What about them? Would aple banena charry be acceptable as a sentence? I think this would be beyond the scope of your program.
Yes, aple benena charry would be acceptable as a sentence. my program doesn't have to recognize real words.

I am having trouble implementing all this with the current arrangement I have. I looked up the functions you suggested to me and understand what they do. But I am unclear as to how to implement them into my main function. am I going to have to do some sort of 'if' statement?

  1. if isalpha() = 0
  2. { cout << endl;
  3. }

I'm just really confused as to where I need to begin with this current program that I have and am not sure where to start. thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Creating a Command Line Parser

 
0
  #4
Feb 5th, 2007
Basically, yes. But use the proper syntax for your if
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Creating a Command Line Parser

 
0
  #5
Feb 5th, 2007
help! this is due in a couple of hours and I just have a problem with the matrix. even when the matrix is valid my code says it is invalid, can anyone help? thanks!

  1. #include <iostream>
  2.  
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8.  
  9. bool Word_Tracker(char a);
  10. bool Symbol_Digit_Word_Tracker(char a);
  11. bool Operator_Checker(char a);
  12. bool Number_Checker(char a);
  13. bool Is_Matrix(char a);
  14. bool Characters_Allowed_Matrix(char a);
  15. bool Comma_Semicolon(char a);
  16. void Clears(char a[], int size);
  17. void First_Whitespace(char a[], int& pos);
  18. void Display(char a[]);
  19.  
  20. int main()
  21. {
  22. char string[500];
  23. cout << " Welcome to My Command Line Parser!" << endl;
  24. cout << " Raymond Guido, Northwestern University" << endl;
  25. cout << " Copyright, 2007\n" << endl;
  26. cout << "\n" << endl;
  27. cout << "Input a Command Line\n";
  28. cin.getline(string, 500);
  29.  
  30. int x = 0;
  31. char word[500] = {0};
  32. char oper_ator[500] = {0};
  33. char number[500] = {0};
  34. char matrix[500] = {0};
  35. char comma_semicolon[500] = {0};
  36.  
  37.  
  38. while( string[x]!=0 )
  39. {
  40. First_Whitespace(string,x);
  41.  
  42. if (Operator_Checker(string[x]) == true)
  43. {
  44. while (Operator_Checker(string[x]) == true)
  45. {
  46. oper_ator[x]= string[x];
  47. x ++;
  48. }
  49. Display(oper_ator);
  50. Clears(oper_ator,500);
  51. }
  52.  
  53. else if (Word_Tracker(string[x]) == true)
  54. {
  55. while ((Word_Tracker(string[x]) == true || Symbol_Digit_Word_Tracker(string[x]) == true))
  56. {
  57. word[x]= string[x];
  58. x ++;
  59. }
  60. Display(word);
  61. Clears(word,500);
  62. }
  63.  
  64. else if (Number_Checker(string[x]) == true)
  65. {
  66. while (Number_Checker(string[x]) == true)
  67. {
  68. number[x]= string[x];
  69. x ++;
  70. }
  71. Display(number);
  72. Clears(number,500);
  73. }
  74.  
  75. else if (Is_Matrix(string[x]) == true)
  76. {
  77. while (Is_Matrix(string[x]) == true || Characters_Allowed_Matrix(string[x]) == true)
  78. {
  79. matrix[x]= string[x];
  80. x ++;
  81. }
  82. if ( string[x] == ']')
  83. {
  84. matrix[x]= string[x];
  85. x ++;
  86. Display(matrix);
  87. Clears(matrix,500);
  88. }
  89. else
  90. {
  91. cout << "Invalid Matrix" << endl;
  92. break;
  93. }
  94. }
  95.  
  96. else if (Comma_Semicolon(string[x]) == true)
  97. {
  98. while (Comma_Semicolon(string[x]) == true)
  99. {
  100. comma_semicolon[x]= string[x];
  101. x ++;
  102. }
  103. Display(comma_semicolon);
  104. Clears(comma_semicolon,500);
  105. }
  106.  
  107. else
  108. {
  109. cout << "\n" <<"You have entered invalid operators";
  110. break;
  111. }
  112. }
  113.  
  114.  
  115. cout << "\n" << "Command Line Separated" << endl;
  116. }
  117. //Functions
  118.  
  119. void First_Whitespace(char a[], int& pos)
  120. {
  121. while(a[pos]==' ')
  122. {
  123. pos ++;
  124. }
  125. }
  126. bool Comma_Semicolon(char a)
  127. {
  128. bool b;
  129. if ((a == ';') || (a == ','))
  130. {
  131. b = true;
  132. return b;
  133. }
  134. else
  135. {
  136. b = false;
  137. return b;
  138. }
  139. }
  140. bool Number_Checker(char a)
  141. {
  142. bool b;
  143. if ((a == '.') || (a >= '0' && a <= '9'))
  144. {
  145. b = true;
  146. return b;
  147. }
  148. else
  149. {
  150. b = false;
  151. return b;
  152. }
  153. }
  154. bool Operator_Checker(char a)
  155. {
  156. bool b;
  157. if ((a == '+') || (a == '-') || (a == '*')
  158. || (a == '/') || (a == '='))
  159. {
  160. b = true;
  161. return b;
  162. }
  163. else
  164. {
  165. b = false;
  166. return b;
  167. }
  168. }
  169. bool Word_Tracker(char a)
  170. {
  171. bool b;
  172. if ((a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z'))
  173. {
  174. b = true;
  175. return b;
  176. }
  177. else
  178. {
  179. b = false;
  180. return b;
  181. }
  182. }
  183. bool Symbol_Digit_Word_Tracker(char a)
  184. {
  185. bool b;
  186. if ((a == '.') || (a == ',') || (a== '_') || (a >= '0' && a <= '9'))
  187. {
  188. b = true;
  189. return b;
  190. }
  191. else
  192. {
  193. b = false;
  194. return b;
  195. }
  196. }
  197. bool Is_Matrix(char a)
  198. {
  199. bool b;
  200. if (a == '[')
  201. {
  202. b = true;
  203. return b;
  204. }
  205. else
  206. {
  207. b = false;
  208. return b;
  209. }
  210. }
  211. bool Characters_Allowed_Matrix(char a)
  212. {
  213. bool b;
  214. if ((a == ' ') || (a >= 0 && a <= 9) || (a == ';'))
  215. {
  216. b = true;
  217. return b;
  218. }
  219. else
  220. {
  221. b = false;
  222. return b;
  223. }
  224. }
  225.  
  226. void Clears(char a[], int c)
  227. {
  228. for(int b=0; b<c; b++)
  229. a[b] = 0;
  230. }
  231.  
  232. void Display(char a[])
  233. {
  234. char *b = a;
  235. while( *b == 0)
  236. {
  237. b ++;
  238. }
  239. cout << b << endl;
  240. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,621
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Creating a Command Line Parser

 
0
  #6
Feb 6th, 2007
You need to put single quotes around 0 and 9 for them to be treated as characters...

  1. bool Characters_Allowed_Matrix(char a)
  2. {
  3. bool b;
  4. if ((a == ' ') || (a >= '0' && a <= '9') || (a == ';'))
  5. {
  6. b = true;
  7. return b;
  8. }
  9. else
  10. {
  11. b = false;
  12. return b;
  13. }
  14. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC