%query: parse(i,o). % File : READ.PL % Author : D.H.D.Warren + Richard O'Keefe % Updated: 5 July 1984 % Purpose: Read Prolog terms in Dec-10 syntax. % % Modified by Alan Mycroft to regularise the functor modes. % This is both easier to understand (there are no more '?'s), % and also fixes bugs concerning the curious interaction of cut with % the state of parameter instantiation. % Since this file doesn't provide "metaread", it is considerably % simplified. The token list format has been changed somewhat, see % the comments in the RDTOK file. % I have added the rule X(...) -> apply(X,[...]) for Alan Mycroft. % % -1 %:- entry(parse(X,Y),[share([[Y]]),free([Y]),ground([X])]). goal :- parse([some, tokens, here], ParseTree). %check_operator_declaration :- % ( current_op(1200, xfx, <-) -> true % ; % op(1200, xfx, [ <- ]) % alternate implication symbol % ), % ( current_op(1000, xfy, &) -> true % ; % op(1000, xfy, [ & ]) % alternate conjunction symbol % ). % %:- check_operator_declaration. % 1 parse(Tokens, Answer) :- ( read(Tokens, 1200, Term, LeftOver), all_read(LeftOver) ; syntax_error(Tokens) ), !, Answer = Term. % all_read(+Tokens) % checks that there are no unparsed tokens left over. % 2 & 3 all_read([]) :- !. all_read(S) :- syntax_error([operator,expected,after,expression], S). % expect(Token, TokensIn, TokensOut) % reads the next token, checking that it is the one expected, and % giving an error message if it is not. It is used to look for % right brackets of various sorts, as they're all we can be sure of. % 4 & 5 expect(Token, [Token|Rest], Rest) :- !. expect(Token, S0, _M) :- syntax_error([Token,or,operator,expected], S0). % I want to experiment with having the operator information held as % ordinary Prolog facts. For the moment the following predicates % remain as interfaces to current_op. % prefixop(O -> Self, Rarg) % postfixop(O -> Larg, Self) % infixop(O -> Larg, Self, Rarg) % 6 & 7 prefixop(Op, Prec, Prec) :- current_op(Prec, fy, Op), !. prefixop(Op, Prec, Less) :- current_op(Prec, fx, Op), !, Less is Prec-1. % 8 & 9 postfixop(Op, Prec, Prec) :- current_op(Prec, yf, Op), !. postfixop(Op, Less, Prec) :- current_op(Prec, xf, Op), !, Less is Prec-1. % 10, 11, 12 infixop(Op, Less, Prec, Less) :- current_op(Prec, xfx, Op), !, Less is Prec-1. infixop(Op, Less, Prec, Prec) :- current_op(Prec, xfy, Op), !, Less is Prec-1. infixop(Op, Prec, Prec, Less) :- current_op(Prec, yfx, Op), !, Less is Prec-1. % 13 ambigop(F, L1, O1, R1, L2, O2) :- postfixop(F, L2, O2), infixop(F, L1, O1, R1), !. % read(+TokenList, +Precedence, -Term, -LeftOver) % parses a Token List in a context of given Precedence, % returning a Term and the unread Left Over tokens.
content may be truncated. 'popout' for larger text window.