stack for Matching <<.. char array??

hello i am writing a program for matching << and >> pointers using a stack but somehow need this char < to be distinguished from < (the same character) so they can match on the stack....thinking I should put it in some sort of array but not sure how to do that any help appreciated...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

//parenthases
	char L_paren = '(';
	char R_paren = ')';
	//braces
	char L_brace = '{';
	char R_brace = '}';
	//Square brackets
	char L_square = '[';
	char R_square = ']';
	//pointers
	  char c_out_left = '<';
	  char c_out_right = '<';
	//pointers
	char c_in_right = '>';
	char c_in_left = '<';

	bool error = false; 
	char sym;

	in.get(sym);

	while (( sym != '\n')  && (!error) )
	{
		if (sym == L_paren || sym==L_brace || sym==L_square|| sym==c_out_left ||sym==c_in_left)

			push(s,sym);//intialize function push

		if (sym == R_paren || sym==R_brace || sym==R_square || sym==c_out_right|| sym==c_in_right)

			if (isEmpty(s))
			{

				cout<<"\nUnmatched closing bracket '" << sym <<"' detected." <<endl;
				error = true;
			}
			else

				pop(s,sym);
		        in.get(sym);
	}

Last edited on
I was waiting for that question.

If you are going to process the string at a character level, you need a state machine.

In State 0, if you see a '<', go to state 1.
In State 0, if you see a '>', go to state 2.
In State 1, if you see another '<', you've matched '<<', push and goto state 0.
In State 2, if you see another '>', you've matched '>>', pop and goto state 0.

I'll let you think about the error cases.
THink i got it now thanks for helping
Topic archived. No new replies allowed.