[m-users.] Returning error instead of stopping early during parse

Philip White philipwhite at cedarville.edu
Thu Aug 22 13:07:42 AEST 2019


I'm writing a lambda calculus parser. My trouble right now is with
parsing adjacent expressions, such as "a b", which is an application the
function "a" with the parameter "b". If the name "a" is not defined
already, I have it such that I fail_with_message. However, if "a" is
defined, but "b" is not defined, the parse just stops parsing and
returns a successful result as if the whole string was "a ".

Below is the code which parses application. parse_factor is the
predicate that could possible fail_with_message. I'd like the parser to
always fail when the input is not totally processed, but this doesn't
seem to be the root issue here. one_or_more should be running
parse_factor twice, and failing the second time; I assume that this
happens, but that it interprets the failure as an end to it's iteration
and that some other part of the parser will succeed. Maybe parse_factor
should be nonbacktrackable?

:- pred parse_term(list(string)::in, 
                   context::in,
                   src::in, 
                   term::out, 
                   ps::in, ps::out) is semidet.
parse_term(NameStack, Context, Src, Term, !PS) :-
  one_or_more(parse_factor(NameStack, Context), Src, Factors, !PS),
  (
    Factors = [Term]
  ;
    Factors = [Head, Second | Rest ],
    Tail = [Second | Rest],
    foldl((pred(Factor::in, !.Term::in, !:Term::out) is det :-
      !:Term = appl(!.Term, Factor)), Tail, Head, Term)
  ).

Hopefully this code is enough to go on, but let me know if it would be
helpful to see more.

- Philip


More information about the users mailing list