• Post Reply Bookmark Topic Watch Topic
  • New Topic

Counting lexemes in Java  RSS feed

 
Jeremiah Parrack
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the code with a comment on how many lexemes are on each line  I think i am doing this correct however a few of my friends are getting a total of 93 I just want to know if I am doing this wrong or not Thank you:


1. public class CountDigits {   // 4
2. public static void main(String[] args) {  // 11
3. SimpleIO.prompt("Enter an integer: "); // 7
4. String userInput = SimpleIO.readLine(); // 9
5. int number = Integer.parseInt(userInput); // 10
6. int numDigits = 0;  // 5
7. while (number > 0) { // 7
8. number /= 10;   // 5
9. numDigits++; // 4
10. } / 1
11. System.out.println("The number " + userInput + " has " +
12. numDigits + " digits"); // 17
13. } // 1
14. } // 1
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On which lines are your friends getting a different number of lexemes than you are?  I can't imagine they'd get more than 1 for the lines where you got 1, so the difference in totals must come from other lines.  If my math is right, your total is 82 lexemes. What are the extra 11 that they counted that you didn't?
 
Jeremiah Parrack
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:On which lines are your friends getting a different number of lexemes than you are?  I can't imagine they'd get more than 1 for the lines where you got 1, so the difference in totals must come from other lines.  If my math is right, your total is 82 lexemes. What are the extra 11 that they counted that you didn't?

The numbers on the side are the amount I think there is. They counted stuff inside quotes unsure what else they counted. ex. "Enter in a value:" I would count this as one he would count this as 7.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremiah Parrack wrote:They counted stuff inside quotes unsure what else they counted. ex. "Enter in a value:" I would count this as one he would count this as 7.

I don't think they're right. In your state machine, you'd go into a state of "in literal" when you get to an open quote, then you'd just keep reading the input until you go to the closing quote. That entire literal is one lexeme, not the individual words in a literal. The lexer shouldn't care about separate words in a String literal because these won't cause it to transition to another state.  You wouldn't consider the individual digits of a numeric literal to be separate lexemes, right? Same deal with String literals.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!