0

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.

fent
  • 17,861
  • 15
  • 87
  • 91

3 Answers3

1

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
  • 698,415
  • 94
  • 811
  • 1,216
  • 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. – fent Feb 18 '10 at 06:44
0

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
  • 203,806
  • 29
  • 246
  • 1,045
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 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. – fent Feb 18 '10 at 08:18
  • What was the text of the IOException? – user207421 Feb 18 '10 at 10:05
  • Nothing because I was using catch. >.> – fent Feb 19 '10 at 05:27
  • So? That doesn't stop the IOException having text. It doesn't answer the question either. – user207421 Feb 19 '10 at 08:01