If/Else with Parser/Tokens?

Any help to lead me in the right direction will be greatly appreciated!
The error is in the 'main' section.
The following are some tests... I can only get a couple of them to work. I know I have it set up wrong. Can someone tell me what I'm doing wrong? Thank you!
int A;
no error found

int a;
illegal variable name

short B;
unrecognizable type

float C = 0.5;
no error found

int A = 10,
unexpected token

short D;
unrecognizable type

float F;
illegal variable name


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>
using namespace std;
string GetToken();
void error(int);

int main() {
	string token;
	cout << "Please enter a declaration in format "
		<< "<type> <variable> [= number];" << endl;

	token = GetToken();

	if (token == "int" + 'A' + ';' || token == "int" + 'B' + ';' || token == "int" + 'C' + ';' ||
			token == "int" + 'D' + ';' || token == "int" + 'E' + ';')
		{
			error(0);
		}
       else if (token == "int" + 'a' + ';' || token == "int" + 'a' + ';' || token == "int" + 'c' + ';' ||
			token == "int" + 'd' + ';' || token == "int" + 'e' + ';')
		{
			error(2);
		}
       else if (token == "int" + 'A' + ',' || token == "int" + 'B' + ',' || token == "int" + 'C' + ',' ||
			token == "int" + 'D' + ',' || token == "int" + 'E' + ',')
		{
			error(4);
		}
	else if (token == "float")
	{
		if (token == token + 'A' || token == token + 'B' || token == token + 'C' ||
			token == token + 'D' || token == token + 'E')
		{
			error(0);
		}
		else if (token == "float" + 'F')
		{
			error(2);
		}
	}
	else if (token != "float" && token != "int")
	{
		error(1);
	}

	system("pause");
	return 0;
}

string GetToken() {
	string token;
	string errors;
	bool theToken = true;
	char ch;

	while (theToken == true)
	{
		cin.get(ch);

		while (ch == ' ')
		{
			cin.get(ch);
		}

		if (isdigit(ch))
		{

			while (isdigit(ch))
			{
				token = token + ch;
				cin.get(ch);
				if (ch == '.')
				{
					token = token + ch;
					cin.get(ch);
				}

				if (ch == ';')
				{
					token = token + '\n' + ch;
					cin.get(ch);
				}
				if (ch == '#')
				{
					token = token + '\n' + ch + ':' + " Error: Unrecognizable token";
				}
			}
			theToken = false;
		}
		else
		{
			token = token + ch;

			if (token == "i")
			{
				theToken = true;
			}
			else if (token == "in")
			{
				theToken = true;
			}
			else if (token == "int")
			{
				theToken = false;
			}
			else if (token == "f")
			{
				theToken = true;
			}
			else if (token == "fl")
			{
				theToken = true;
			}
			else if (token == "flo")
			{
				theToken = true;
			}
			else if (token == "floa")
			{
				theToken = true;
			}
			else if (token == "float")
			{
				theToken = false;
			}
			else if (token == "A" || token == "B" || token == "C" || token == "D" || token == "E")
			{
				theToken = false;
			}
			else if (token == "=")
			{
				theToken = false;
			}
			else if (token == ";")
			{
				theToken = false;
			}
			else if (token == "\n")
			{
				token = "";
				theToken = false;
			}
			else
			{
				token = token + ':' + " Error: Unrecognizable token";
				theToken = false;
			}
		}

	}

	return token;
}

void error(int code)
{
	switch (code) {
	case 0: cout << "no errors found" << endl; break;
	case 1: cout << "unrecognizable type" << endl; break;
	case 2: cout << "illegal variable name" << endl; break;
	case 3: cout << "unexpected number" << endl; break;
	case 4: cout << "; expected" << endl;
	}
	return;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string token;
	cout << "Please enter a declaration in format "
		<< "<type> <variable> [= number];" << endl;

	//token = GetToken();
	getline(cin, token);

	if (token == "int A;" || token == "float C = 0.5;" || token == "float B;" || token == "float C = 0.2;")
	{
		error(0);
	}
	else if (token == "int a;" || token == "float F;")
	{
		error(1);
	}
	else if (token == "short B;" || token == "short D;")
	{
		error(2);
	}
	else if (token == "int A = 10,")
	{
		error(4);
	}


This is a lot easier lol, but I know this isn't the point of problem... I just don't understand how to evaluate errors in the string based on the tokens... Grrrr.
int A;
no error found

int a;
illegal variable name

float F;
illegal variable name


So do you have an grammar which you're expected to validate?
See https://en.wikipedia.org/wiki/Syntax_(programming_languages)

For example, on what basis is 'A' a valid variable name and 'F' is not a valid variable name.

> token == "float C = 0.5;"
So by your reckoning, "float C = 0.6;" is wrong?

Is "int A = 42;" valid or invalid?
Is "int A = 1.23;" valid or invalid?

Yes, the grammar is as follows:
1
2
3
4
5
6
7
// <declaration> 	::= 	<type>  <var> ’;’ | <type> <var> ’=’ <number> ’;’
// <type> 		::= 	int | float
// <var> 		::= 	A | B | C | D | E
// <number> 	::= 	<integer> | <float>
// <integer> 	::= 	<integer> <digit> | <digit>
// <digit> 		::= 	0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
// <float> 		::= 	<integer> ‘.’ <integer> 

The first part of the problem was getting the grammar set up and it taking the token, now I'm struggling with the parser.
two things:
first, it would be a lot easier if you used a map of the tokens instead of all that if-then logic
and second, a boolean is either true or false.
default one of them; either its initialized false and you check the things that can set it to true, or its initialize to true and check the things that can make it false.
you don't need both sides of the equation, it just bloats the code even more.

but if you use the map, its either in the map (set to true) or not in the map (default false) which would boil your code down to a couple of lines... 90-150 roughly would just be
bool tok = false;
if token in map
tok = true
Last edited on
Topic archived. No new replies allowed.