Prev Up Next
Go backward to Generating a Specialized Parser
Go up to Top
Go forward to Error Recovery

Implementing Input Streams

Essence parsers rely on streams to implement sequences of input terminals. The parsers only use three accessor procedures which may be implemented in any number of ways. (One possible implementation is to just use lists.) Essence comes with an implementation of lazy streams as well which includes stream creation procedures as well.

Streams interface

The part of the streams interface relevant to Essence parsers contains only accessors:

(stream-car  stream) procedure Stream-car returns the first element of a non-empty stream.

(stream-cdr  stream) procedure Stream-cdr, when applied to a non-empty stream stream, returns a new stream with the same elements as stream sans the first one.

(Stream-empty?  stream) procedure Stream-empty? returns #t when applied to an empty stream, and #t when applied to a non-emtpy stream.

The stream structure

A portable example implementation of (lazy) streams is in the Essence distribution as common/stream.scm. It is also the implementation of the Scheme 48 structure stream defined in packages.scm. Aside from the accessors described above, it also provides a creation procedure and procedure for converting to and from lists:

(make-stream  state-transformer state) procedure Make-stream creates a stream with initial stream state state and state transformer state-transformer. A stream state represents a state in unrolling the stream--as such it encodes the ability to produce the elements of the stream. State-transformer must accept a state as an argument and return a pair consisting of the first element of the stream as car, and a new state as cdr.

The accessor procedures only ever call state-transformer in a single-threaded manner--they will apply it to state upon access, and they will apply it only once. After that, they will apply it once to the state returned by state-transformer, and so on.

(list->stream  list) procedure List->stream produces a stream with exactly the elements of list.

(stream->list  stream) procedure Stream->list produces a list with exactly the elements of stream. Stream must be finite.


Mike Sperber, Peter Thiemann

Prev Up Next