[erlang-questions] Question in Parser Implementation
黃耀賢 (Yau-Hsien Huang)
g9414002.pccu.edu.tw@REDACTED
Sat Jul 24 20:40:48 CEST 2010
Hey, guys. I tried implementing parsers guided by
Graham Hutton's paper: Higher-order Functions For Parsing.
( http://www.cs.nott.ac.uk/~gmh/bib.html#parsing )
Part code is listed below.
-module(parser).
-compile(export_all).
succeed(V, Inp) ->
[{V, Inp}].
fail(_) ->
[].
satisfy(_, []) ->
fail([]);
satisfy(P, [X|Xs]) ->
case erlang:apply(P, [X]) of
true ->
succeed(X, Xs);
false ->
fail(Xs)
end.
literal(X, Inp) ->
satisfy(fun(Y) -> X == Y end, Inp).
literal(X) ->
fun(Inp) ->
erlang:apply(?MODULE, literal, [X, Inp])
end.
alt(P1, P2, Inp) ->
erlang:apply(P1, [Inp]) ++ erlang:apply(P2, [Inp]).
The alt/3 is an implementation of alternation syntax (`` | '') in BNF,
that alt/3 takes two parsers and applies each to the input string.
Then I tested it using a query
parser:alt(parser:fail, parser:literal($3), "345").
and found that it does not accept the syntax 'parser:fail' for
representing a function. The erl reports "Illegal expression" for
the query.
How to take a function name and apply anywhere?
Need the function `fail' be defined as a lambda function like
fail() -> fun(Inp) -> [] end.
?
Best Regards.
More information about the erlang-questions
mailing list