Parsing infinite streams style

Hal Snyder hal@REDACTED
Tue Mar 9 00:04:10 CET 2004


Joe Armstrong <joe@REDACTED> writes:

> Let me make this clearer with an example.
>
> Suppose we have a infinite stream
>
> like this
>
> begin <Int> end
> begin <Int> end

Thank you. Gradually a light is going on, and some of the confusion
experienced reading Erlang coded by the Masters is abating.

Need to go back to school or otherwise intake megadose FP therapy -
Friedman, Wand and Haynes 1 ed. Ch 8-9 perhaps....

I think I understand why it works, but need a few more iterations to
actually think that way - it's like the difference between listening
to an In-Flight Swedish CD on the plane to Stockholm vs. being able
converse in the language on arrival.

> but this can be broken into arbitrary chucks - we can write a
> reentrant parser like this:
>
> get_begin("begin " ++ T) -> get_int(T, 0);
> get_begin(S)             -> {more, fun(Data) -> get_begin(S ++ D) end}.
>
> get_int(" " ++ T, N) -> get_end(T, N);
> get_int([H|T], N)    -> get_int(T, N*10-H-$0);
> get_int([], N)       -> {more, fun(S) -> get_int(S, N)}.
>
> get_end("end" ++ T, N) -> {done, N, fun(I) -> get_begin(I)};
> get_end(T, N)          -> {more, fun(S) -> get_end(T++S, N) end}.
>
> This way avoids having to restart the parsing at the beginning 
> every time.
>
>   Having said  all of this I'd  be rather careful  - the "continuation
> passing with funs" method is elegant BUT a bugger to debug if you get
> it wrong - continuation passing with explicit data structures is also a
> bit of a pain.
...



More information about the erlang-questions mailing list