Parsing in Prolog without cut ?
I found this nice snippet for parsing lisp in Prolog (from [here][1]):

    ws --> [W], { code_type(W, space) }, ws.
    ws --> [].
    
    parse(String, Expr) :- phrase(expressions(Expr), String).
    
    expressions([E|Es]) -->
        ws, expression(E), ws,
        !, % single solution: longest input match
        expressions(Es).
    expressions([]) --> [].
    
    % A number N is represented as n(N), a symbol S as s(S).
    
    expression(s(A))         --> symbol(Cs), { atom_codes(A, Cs) }.
    expression(n(N))         --> number(Cs), { number_codes(N, Cs) }.
    expression(List)         --> "(", expressions(List), ")".
    expression([s(quote),Q]) --> "'", expression(Q).
    
    number([D|Ds]) --> digit(D), number(Ds).
    number([D])    --> digit(D).
    
    digit(D) --> [D], { code_type(D, digit) }.
    
    symbol([A|As]) -->
        [A],
        { memberchk(A, "+/-*><=") ; code_type(A, alpha) },
        symbolr(As).
    
    symbolr([A|As]) -->
        [A],
        { memberchk(A, "+/-*><=") ; code_type(A, alnum) },
        symbolr(As).
    symbolr([]) --> [].

However expressions uses a cut. I'm assuming this is for efficiency. Is it possible to write this code so that it works efficiently without cut?


  [1]: http://web.student.tuwien.ac.at/~e0225855/lisprolog/lisprolog.pl
Parsing in Prolog without cut ?
===

I found this nice snippet for parsing lisp in Prolog (from [here][1]):

    ws --> [W], { code_type(W, space) }, ws.
    ws --> [].
    
    parse(String, Expr) :- phrase(expressions(Expr), String).
    
    expressions([E|Es]) -->
        ws, expression(E), ws,
        !, % single solution: longest input match
        expressions(Es).
    expressions([]) --> [].
    
    % A number N is represented as n(N), a symbol S as s(S).
    
    expression(s(A))         --> symbol(Cs), { atom_codes(A, Cs) }.
    expression(n(N))         --> number(Cs), { number_codes(N, Cs) }.
    expression(List)         --> "(", expressions(List), ")".
    expression([s(quote),Q]) --> "'", expression(Q).
    
    number([D|Ds]) --> digit(D), number(Ds).
    number([D])    --> digit(D).
    
    digit(D) --> [D], { code_type(D, digit) }.
    
    symbol([A|As]) -->
        [A],
        { memberchk(A, "+/-*><=") ; code_type(A, alpha) },
        symbolr(As).
    
    symbolr([A|As]) -->
        [A],
        { memberchk(A, "+/-*><=") ; code_type(A, alnum) },
        symbolr(As).
    symbolr([]) --> [].

However expressions uses a cut. I'm assuming this is for efficiency. Is it possible to write this code so that it works efficiently without cut?


  [1]: http://web.student.tuwien.ac.at/~e0225855/lisprolog/lisprolog.pl