Dcg interpreter


A definite clause grammar (DCG) interpreter

Source: The Art of Prolog


Program source code: dcg_interpreter.pl


        parse(Start,Tokens) :-
            The sequence of tokens Tokens represented as a difference-list
            can be reached by applying the grammar rules defined by -->/2,
            starting from Start.
:- op(40,xfx,\).
:-  op( 800, xfx, -->).
        parse(A,Tokens) :-
            nonterminal(A), A --> B, parse(B,Tokens).
        parse((A,B),Tokens\Xs) :-
            parse(A,Tokens\Tokens1), parse(B,Tokens1\Xs).
        parse(A,Tokens) :-  terminals(A), connect(A,Tokens).
        parse({A},Xs\Xs) :-  A.
	terminals(Xs) :- list(Xs).
	list([X|Xs]) :- list(Xs).
        connect([ ],Xs\Xs).
        connect([W|Ws],[W|Xs]\Ys) :-  connect(Ws,Xs\Ys).
%     Program 19.4:  A definite clause grammar (DCG) interpreter


pl/prolog/pllib/dcg_interpreter.txt · ostatnio zmienione: 2019/06/27 15:50 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0