[erlang-questions] *current* value?

Thomas Lindgren thomasl_erlang@REDACTED
Thu Oct 18 14:06:54 CEST 2007


--- Joel Reymont <joelr1@REDACTED> wrote:

> The process dictionary could be used to simplify
> parsing, I think.
> 
> You could write this Haskell parser combinator
> 
> whileStatement = do
>    reserved "While"
>    e <- expr
>    reserved "Begin"
>    xs <- statements
>    reserved "End"
>    return $ While e (Compound xs)
> 
> like this
> 
> reserved("while"),
> E = expr(),
> reserved("begin"),
> Xs = statements(),
> reserved("end"),
> {while, e, {compound, xs}}.
> 
> instead of the more verbose
> 
> {S1, ok} = reserved(S, "while"),
> {S2, E} = expr(S1),
> {S3, ok} = reserved(S2, "begin"),
> {S4, Xs} = statements(S3),
> {S5, ok} = reserved(S4, "end"),
> {S5, {while, e, {compound, xs}}}.
> 
> This assumes that parsers throw exceptions upon
> failure.
> 
> What do you think?

That notation looks a lot like Prolog's DCG:s (which
used failure and backtracking instead of exceptions).
DCG:s were implemented by the equivalent of a parse
transform, however.

The implementation depends on the grammar, I guess.
For instance, if it needs backtracking, you will also
have to push tokens back onto the input stream when
you throw exceptions to try another grammar rule. But
if the grammar is LL(1) (um, if I remember the jargon
correctly ...) you can parse the input by just
repeatedly looking at the next symbol and can then
safely forget that symbol (or store it, etc). 

I'd probably prefer a parse transform, because it's
cleaner and probably faster, but in a pinch, using the
process dictionary might work. But you are basically
mixing a notational convenience with a method of
implementation.

(Then there's good old yecc, of course.)

Best,
Thomas


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the erlang-questions mailing list