Lexical Analysis Question:

For my lexical analysis program (which reads in one char at a time from file with getChar) I have a getToken function that determines (then returns) a token for any character being read it also sets the lexeme (string). Once it reads this in, it outputs a string version of the token and its corresponding lexeme.

However, for the comment token case (// or */ which is shown below) the output from the command prompt is giving me problems. It always freezes right as I reach the // or */ in the file, then if i press enter it will skip the comment signifier as expected, but will still print out the corresponding letters and words as id's. Since they're in a comment, they should've been skipped

Is there anything within the code below that can be changed to fix this?

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
case '/': //could be comment // or /*
	lexeme.clear();
	lexeme += ch;   //ch is a char, lexeme is a string

	ch = getChar(); //moves to next char within the file
	switch (ch) {
		case'/':
			while (ch != EOF && ch != '\n')
			{
				ch = getchar();
			}
			return getToken(); // recursion
		case'*':
			while (ch != EOF || ch != '*')
			{
				ch = getChar();
			}
			if (ch == '*')
			{
				ch = getChar();
				if (ch == '/')
				{
					return getToken();
				}
				else
				{
					while (ch != EOF || ch != '*')
					{
						ch = getChar();
					}
					if (ch == '*') {
						ch = getChar();
						if (ch == '/')
						{
							return getToken();
						}
					}
				}
			}
		default:
			return singleCharTokenMap[lexeme];
		}
Last edited on
You do.'t seem to have a comment state.
Topic archived. No new replies allowed.