views:

32

answers:

3

Making a Lexical Analyzer in Java and I'm using PushbackInputStream because I need to be able to push back what I read in case it's not what I wanted. But whenever the stream is empty and read() returns -1. It doesn't allow me to use unread().

EDIT: I'm currently using a regular InputStream, reading it all at once, putting it into a stack, and using the stack as the stream so I can pop() and push() items on it.

+1  A: 

I think your problem is that you are using a crufty 3rd-party pushback stream.

You probably should be using java.io.PushbackInputStream or java.io.PushbackReader which have a more clearly specified API, and don't do peculiar things like "flushing" the buffer.

Stephen C
Oops! I was actually using java.io.PushbackInputStream. But at the time of posting this I was at school and didn't have access to my code so couldn't remember exactly what I used.
this is a dead end
A: 

If you can get by with single token look-ahead and push-back, the quaintly venerable StreamTokenizer class may suffice. It's used in this simple implementation of a recursive descent parser.

trashgod
Wow thanks but I actually have to code the Tokenizer myself lol.
this is a dead end
A: 

But whenever the stream is empty and read() returns -1. It doesn't allow me to use unread().

Please elaborate 'doesn't allow me to use unread()'. From a quick look at the source code it should work OK.

Also curious to know why you are pushing back at EOF.

EJP
It was giving me an IOException. In the case where the stream is something like "FOO" and I'm trying to find "FOL". I'll read the first two characters, they will match. But when I read the third it won't. So I need to put those 3 characters back into the empty stream so I can match it with something else.
this is a dead end
What was the text of the IOException?
EJP
Nothing because I was using catch. >.>
this is a dead end
So? That doesn't stop the IOException having text. It doesn't answer the question either.
EJP