• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Need Assistance

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not sure if this was too much of an advanced request, but no one is answering in the other forum, so Ill try my luck here.
http://www.javaranch.com/ubb/Forum38/HTML/000313.html
Anyway, I keep trugging along slowly but surely. Here is my next question.
How would you use replace the lexer in the code below using StringTokenizer so I can parse words and numbers. The way it is now, it is only capable of parsing 1 letter and 1 digit expressions with no spaces in between. I am trying to make it grow and more flexible, but i keep "breaking" the software. Right now it will do this just fine:
C:\jdk1.3\bin>java compiler
Please enter your expression --> a=p*3+b/c- The postfix form is a p 3 * b c / + o - =
The next step is to have it do this
C:\jdk1.3\bin>java compiler
The postfix form is apple pear 354 * banana 879 / + orange - =
The file read in will contain the text:
apple = pear * 354 + banana / 879 - orange
Please help, Thanks in advance.
The compiler.java file is located at: http://members.nbci.com/acidmaxx/Compiler/compiler.java.html

Thanks again, and Happy Easter.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well first off, I had a bit of a hard time reading your code, as the indentation level kept changing at random. It would be worth your while to format the code very carefully so that the indentation is consistent, as this will mean people are more likely to want to take the time to understand it. I recommend editing the code in a plain text file (use only spaces for indentation, never tabs), and then use the < pre > tag to preserve the indentation in HTML. Using MS Word seems to make it harder to see how much you've actually indented; avoid it for code.
Also, it would help to put a few more comments in the code, especially just before classes and methods. A brief explanation of what a class is for helps people to understand more quickly what you're trying to do.
Anyway, after I worked out how the code was constructed - it looks like it should actually be pretty simple to put it a StringTokenizer. The lexer() method is the one that currently is responsible for returning tokens, which it does by returning single characters. Just take out the lexer() method entirely and instead use a StringTokenizer to return tokens. Declare a Compiler instance variable:<code><pre> StringTokenizer tokenizer;
</pre></code>and initialize it in the Compiler constructor:<code><pre> tokenizer = new StringTokenizer(theExpression);</pre></code>And then whenever you need another token, get it from the tokenizer. E.g. instead of <code><pre> try {
token = lexer();
}
catch (StringIndexOutOfBoundsException sioobe) {
token = "";
}
</pre></code>use
<code><pre> if (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
}
else {
token = "";
}
</pre></code>(I'm actually not too sure about how your code knows when the expression is completed, but that's a separate issue.)
I wouldn't use a StreamTokenizer unless there are specific features you need which StringTokenizer does not provide; it is somewhat more complex to use.
Enjoy...
[This message has been edited by Jim Yingst (edited April 13, 2001).]
 
Kevin Luludis
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Jim. I'll try this and see what happens. And as for the formatting, I have had trouble with that forever. Cutting and pasting from the IDE to the web never seems to work out right for me. I'll also try your advice on that too. Thanks again, and I'll let you know how I make out with StringTokenizer. Best Regards.
Kevin
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!