[erlang-questions] Question in Parser Implementation

黃耀賢 (Yau-Hsien Huang) <>
Tue Jul 27 06:00:53 CEST 2010


2010/7/25 Vlad Dumitrescu <>

> 2010/7/24 黃耀賢 (Yau-Hsien Huang) <>:
> > 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.
> Hi,
>  Try
> parser:alt(fun parser:fail/1, parser:literal($3), "345").
> http://www.erlang.org/doc/reference_manual/expressions.html#id2275688
>  regards,
> Vlad
>

Thank you. And again, when implementing the parser tutored by Hutton's
paper, I was stuck  in mutual recursion of the grammar of a simple
expression.

Then I have a question. Do you know any debugging tools that
can reduce any function call or statement by expanding any
expression? And how to debug mutual recursions?

Best Regards.

And code is listed blow.

expn() ->
    parser:alt(parser:alt(
 parser:using(
   parser:then(simple:term()
       , parser:xthen(parser:literal($+)
      , simple:term()
     )
      )
   , simple:plus())
 , parser:using(parser:then(simple:term()
    , parser:xthen(
parser:literal($-)
, simple:term()
       )
   )
, simple:minus()
       )
)
       , simple:term()
      ).

term() ->
    parser:alt(parser:alt(
 parser:using(parser:then(simple:factor()
  , parser:xthen(
      parser:literal($*)
      , simple:factor()
     )
 )
      , simple:times())
 , parser:using(parser:then(simple:factor()
    , parser:xthen(
parser:literal($/)
, simple:factor()
       )
   ), simple:divide()
       )
)
       , simple:factor()
      ).

factor() ->
    parser:alt(parser:using(parser:number(), simple:value())
       , parser:xthen(parser:literal($()
      , parser:thenx(simple:expn()
     , parser:literal($))
    )
     )).

value() ->
    fun(Str)->
    case Str of
[_|_] -> {V,_} = string:to_integer(Str);
_ -> {V,_} = string:to_integer([Str])
    end,
    if
error == V -> parser:fail();
true -> V
    end
    end.

plus() -> fun({X,Y}) -> X + Y end.
minus() -> fun({X,Y}) -> X - Y end.
times() -> fun({X,Y}) -> X * Y end.
divide() -> fun({X,Y}) -> X / Y end.


More information about the erlang-questions mailing list