aspose file tools*
The moose likes I/O and Streams and the fly likes Lookahead with Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Lookahead with Java" Watch "Lookahead with Java" New topic
Author

Lookahead with Java

Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
Anyone know a good way (for me good means, very simple ) to (while using some type of lookahead) Read Each New Character From Input Stream?? The ultimate objective here is to Read from a file. Take one line of text at a time. And then look at all the characters to see if they match a certain syntax. Thanks.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
The simplest and quickest approach is to read the whole file into an array, and then you can step forward and backward as you wish. Are these files likely to be very big?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
No, I am thinking that the files should be no more than 50 lines. I am working on a project in which the ultimate goal here is to make a very simple compiler. Basically, I want to be able to take something like:

And have it output in assembly language such as..

The semicolon denotes comments. I am just trying to create something very simple. Like the above example. I would use the tools such as JLex, JavaCC, ANTLR, etc. but I am unsure how to use them and they look like they are for more elaborate and complicated compilers.
saurabh panwar
Greenhorn

Joined: Feb 19, 2001
Posts: 5
try using vectors instead of arrays
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I suggested using an array because it is usually possible to examine the size of a file before loading it. This makes the flexibility and overhead of Vector or List objects redundant. It's usually best to stick with the simplest type of data structure that will do the job.
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
Frank, I agree wholeheartedly, but which is easier to do?
Reading in the whole file as an array, and then passing it to the next method(parser)? Or Reading in the whole file, keeping it in the buffer and then passing out each line as an array to the parser? Whatever is easier for me will work. I don't care about speed/performance from it. I just want it to work first. hehe
Thanks.
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
So far I have it doing this. And it reads the data and passes it along nicely to the file Code.asm.

Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If your syntax is strictly line-based, and no infomration is required to be split across lines or unwound back beyond a line break, then you can probably do it line by line. But is this true? From your example, would the following example also be valid input.

or, since you have no explicit line end characters, something like:

If you need to do "smart" processing of line ends, then you really need to be able to process more than one line at once, which breaks your line-at-a-time method.
Is this just a classroom excercise, or do you have a real use for this? It's a long time since I've seen a simplified language like this for an actual application.
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
The project I am working on is just a classroom exercise and probably after May of 2000 will never be worked on again, just used for reference. I understand what you are saying. It would be nice if it would let you write code all nice and sloppy, not worrying where the end of lines are, but like you said, eventually this would be a very bad thing. It is actually more simple than this though. I have an email simplifying things though to take care of this problem (i think), but it is on my PC at home. I will have to post the comments from there to here later. It gives a great example on what has to be done, but im not sure which methods to implement in such a case.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
probably after May of 2000 will never be worked on again I assume you mean May 2001
I look forward to seeing the problem in more detail. We may be able to come up with some very simple solutions if we can see the whole problem.
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
LOL, Wow what was I thinkin'? Yes, I definitly meant 2001. Well I asked about what the best way to pass the file information (supposed to be source code) onto the next method for parsing. And it was suggested that this is what should happen:
The lex should read in a text line from a file or a stream of characters and build an array of characters.
The lex then should look at this line of text and break it down to tokens, or if read in as characters build up to tokens.
Example:
apple=pear*34 +banana/ cherry - orange
The tokens would be apple,=,pear,*,34,banana,/,cherry,-,orange .
No spaces, no comment, no eol or eof.
If the file was read in by character I would have to combine the words to form the tokens.
The lookahead would actually be an array pointer. If the above list of tokens were actually in an array they would comsume 10 elements of the array. The lookahead would actually just index through this array.
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
Dear Frank and Friends,
Here is also what I have done so far on this project so you can see where I am actually trying to fit this in. And please, if I have made any mistakes with that as well maybe you can point them out to me. Thanks a million, appreciate all your help.
What I do have working so far is the LEXX.java which can be found linked on the site.
http://members.nbci.com/acidmaxx/Compiler/compiler_project.html
[This message has been edited by Kevin Luludis (edited February 24, 2001).]
Kevin Luludis
Ranch Hand

Joined: Feb 13, 2001
Posts: 30
I am thinking that I am going to need to use the StringTokenizer class to do what i need here. That a long with the Text class methods of int and char should allow me to do this? Am I correct in assuming this or am I barking up the wrong tree?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You might be better off with StreamTokenizer which is a bit more flexible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lookahead with Java
 
Similar Threads
regular expression help
Password Regex
And the WINNERS are
Learn Java
Regular expressions