Need help with lexical analyzer

I have assignment to write a simple lex anayzer. I can't get the output to print the tokens correctly.

All that should print is the characters from a file, and their assigned token. Mine just ends in DIGIT DIGIT DIGIT every time (because i have "int token = 0". If i change the value it just changes the constant token name. Due tonight, and feeling desperate. Please show my what I am doing wrong. Thank you so much.

CO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{					
string characters;							
ifstream foo("file name here");

while (foo >> characters)
{
int t = 0;
string convertToken(int t);
cout << characters << convertToken(t);
}

foo.close();							
return 0;
}


This prints
A DIGIT
+ DIGIT
1 DIGIT
= DIGIT
word DIGIT

etc etc
it should print
A LETTER/WORD
+ PLUS
1 DIGIT
= EQUAL
word LETTER/WORD

Please help me.
CO
Last edited on
What does convertToken() do?
1
2
3
4
5
6
7
8
9
10
string convertToken(int token) 
{
  switch (token) 
  {
	case LETTER: return "<LETTER/WORD>";
  	case DIGIT:  return "<DIGIT>";
	case PLUS:  return "<PLUS>";
	case ASSIGN:  return "<ASSIGN>";
  }
}
Last edited on
I think that after reading into characters, you should look at it and determine its type. Then set token to whatever that type is.
how?
I think that's part of the point of your assignment; based on the list you've given, however, if the string contains characters, it is a "LETTER/WORD", if it is the plus sign, it is "PLUS", and so on.
It is part of the point of the assignment. Setting up all the code you cannot see is another part. And, after many many days I cannot figure out how to do it, so in desperation I am coming for help.

If i code it so that it takes information from user input, it works correctly. When I switch it to take the information from a file, something breaks. I cannot figure out what is wrong, and I am asking for straight up help at this point. Please.
"something breaks" Can you be a little descriptive? What exactly breaks?
before it printed:
A <LETTER/WORD>
+ <PLUS>
1 <DIGIT>
= <ASSIGN>
word< LETTER/WORD>


now that i am asking it to extract the information from a file, it prints
A <DIGIT>
+ <DIGIT>
1 <DIGIT>
= <DIGIT>
word <DIGIT>

That is what I mean by it "breaking"


Last edited on
I solved it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//class Scan

int main()
{
	string characters;								
	string convertToken(int t);
	int t = true;

	ifstream foo("file name here");	

	while (foo >> characters)
	{
		Scan items = characters;
		token = items.lex();
		cout << characters << convertToken(t);
	}

	foo.close();
	return 0;
}


This opens the file, prints the contents by character leaving strings in tact, and assigns each string a token.

1
2
3
4
5
6
7
For example:
A  <LETTER/WORD>
+ <PLUS>
B <LETTER/WORD>
= <ASSIGN>
7 <DIGIT>
success <LETTER/WORD>


The supporting code that operates this outcome is not shown.
Last edited on
Topic archived. No new replies allowed.