Advertisement
Guest User

Untitled

a guest
Jun 30th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 6.43 KB | None | 0 0
  1.  
  2. :- use_module(library(must_sanity)).
  3. :- set_prolog_flag(double_quotes,codes).
  4.  
  5. % Mini DCG interpretor
  6. %
  7. % example
  8. %
  9.  
  10. optionally(G) --> G ; !.
  11.  
  12. zero_or_more(P)--> P, !, zero_or_more(P).
  13. zero_or_more(P)--> !.
  14.  
  15. one_or_more(P)--> P -> zero_or_more(P); !.
  16.  
  17. some(P) --> P, optionally(some(P)).
  18.  
  19. any([H|T],[H|TT],O):- !, any(T,TT,O).
  20. any([],[],[]). % closes off infinites
  21. any(S,S,[]).
  22.  
  23.  
  24. end_of_file --> ([-1] ; [end_of_file] ; \+ [_]),!.
  25. end_of_file(S,E):- S==[],!, E=[].
  26.  
  27.  
  28. dcg_peek(Until,S,S) :- phrase(Until,S,_).
  29.  
  30. is_until(Until) --> dcg_peek(Until) ; end_of_file.
  31.  
  32. conj_until(Out, P, Until) --> call(P, A),
  33.  ( is_until(Until) -> {A = Out} ; conj_until(B, P, Until), {Out = (A,B)}).
  34.  
  35. seq_until([], _, Until) --> is_until(Until),!.
  36. seq_until([H|T], P, Until) --> call(P, H), !, seq_until(T, P, Until).
  37.  
  38.  
  39. until(Until,Stuff) --> until_0(Until,Stuff),Until.
  40. until_0(Until,[C|Before]) --> "\\",[C],!,until_0(Until,Before).
  41. until_0(DCG,[],[Found|Rest],[Found|Rest]):- phrase(DCG,[Found],[])-> true ; phrase(DCG,[Found|Rest],_).
  42. until_0(DCG,[Skipped|Before],[Skipped|More],[Found|Rest]):- until_0(DCG,Before,More,[Found|Rest]).
  43.  
  44.  
  45. % // =========                            
  46. % //  Dot Gee
  47. % // =========
  48. gee_comment(Cmt) -->
  49.  ("//" ; "#") -> (until(eol,Cmt) ; any(Cmt)) ;
  50.   "/*", any(Cmt), "*/".
  51.  
  52.  
  53. white --> one_or_more(white_1).
  54. white_1 --> eol ; ([W], {char_type(W,white)}) ; gee_comment(_).
  55.  
  56. eot --> sentence_op ; white.
  57.  
  58. eol --> `\r\n` ; `\r` ; `\n` .
  59.  
  60.  
  61. sentence_op --> dcg_peek(`.`) ;  dcg_peek(`|`) ; dcg_peek(`=>`).
  62.  
  63. gee_token(Txt) -->  \+ sentence_op,
  64.   ({quotes(Q,EQ)}, (Q -> until(EQ, Txt) ; until_0(eot,Txt))).
  65.  
  66. quotes(`<`,`>`).
  67. quotes(S,S):- quotes1(S).
  68. quotes1(`"`). quotes1(`'`).
  69.  
  70.  
  71. padded(DCG) --> optionally(some(white)), DCG, optionally(some(white)).
  72.  
  73. gee_unit(DCG) --> ` `, !, gee_unit(DCG).
  74. gee_unit(DCG) --> gee_unit_1(DCG), (eot;!). % trim off end
  75.  
  76. gee_unit_1(DCG,S,E) :-
  77.  var(DCG) -> gee_token(Txt,S,E), name(DCG,Txt) ; % forward  
  78.  atomic(DCG) -> name(DCG,Txt), gee_token(Txt,S,E) ; % reverse  
  79.  phrase(DCG,S,E). % mixed
  80.  
  81.  
  82. gee_dcg( end_of_file ) --> end_of_file, !.
  83. gee_dcg( Out ) --> gee_dcg1( A ), (end_of_file -> {Out=A} ;  gee_dcg( B ) , {Out = (A,B)}).
  84.  
  85.  
  86. gee_dcg1(DCG) --> (eol ; white), !, gee_dcg1(DCG).
  87. gee_dcg1( comment(Comment) ) --> gee_comment(Codes), {name(Comment,Codes)}.
  88. gee_dcg1( Out) --> gee_unit(L), (  "=>" -> gee_body(R), { Out = (L --> R)} ; gee_body(R), {Out = (:- do_gee(L,R))}), ".".
  89. gee_dcg1( done) --> [], !.
  90.  
  91. gee_body([]) --> `null`.
  92. gee_body(A;B) --> gee_body1(A), "|",  gee_body(B).
  93. gee_body(Body) --> gee_body1(Body).
  94.  
  95. gee_body1(Body) --> conj_until( Body, gee_unit,  sentence_op).
  96.  
  97. write_clause(X):- writeq(X), nl.
  98.  
  99.  
  100. test_g:- phrase_from_file(({trace},conj_until(DCG, gee_dcg, end_of_file)), 'tml.g'),
  101.  maplist(write_clause, DCG).
  102.  
  103.  
  104. % call(+SyntaxName, +Content, +SyntaxArgs, +VariableNames, -Result)
  105.  
  106. qq_string(Data,_Args,_VarsIn, Result):- phrase_from_quasi_quotation(any(Result),Data).
  107. :- quasi_quotation_syntax(qq_string).
  108.  
  109.  
  110. gee(Data,_Args,_VarsIn,Result):- !,
  111.  phrase_from_quasi_quotation((
  112.   seq_until(Result, gee_dcg, end_of_file)),Data).
  113.  
  114.  
  115.  
  116.  
  117. :- quasi_quotation_syntax(qq_string).
  118. :- quasi_quotation_syntax(gee).
  119.  
  120.  
  121.  
  122. gee(Data,_Args,_VarsIn,Result):- !,
  123.  phrase_from_quasi_quotation(({trace},
  124.   seq_until(Result, gee_dcg, end_of_file)),Data).
  125.  
  126. quotes(`<`,`>`).
  127. quotes(S,S):- quotes1(S).
  128. quotes1(`"`). quotes1("'").    
  129.  
  130.  
  131. write_clauses((A,B)):- !, write_clauses(A),write_clauses(B).
  132. write_clauses(X):- writeq(X), nl.
  133.  
  134. % Prints out the DCG from
  135.  
  136. :- write_clauses({|qq_string||
  137.  
  138.    directive => ws "@string" space ws strdir ws '.' ws |
  139.         ws "@stdout" space ws term ws '.' ws |
  140.         ws "@trace" space ws relname ws '.' ws |
  141.         ws "@bwd" ws '.' ws.
  142.   strdir => relname ws fname | relname ws string | relname ws cmdline | relname ws term.
  143.  
  144.   |).
  145.  
  146.  
  147. % loads the DCG forms
  148. {|qq_string||
  149.  
  150. #{
  151. @string str <tml.g>.
  152.  
  153. identifier => sym | var.
  154. args => identifier ws args | null.
  155. var => '?' chars.
  156. sym => chars.
  157. relname => sym.
  158. chars => alpha chars1 | '_' chars1.
  159. chars1=> alnum chars1 | '_' chars1 | null.
  160. ws =>   space ws | ws '#' printable_chars eol ws | null.
  161. terminal => quoted_char | string.
  162. quoted_char =>  '\'' printable '\'' | "'\\r'" | "'\\n'"
  163.         | "'\\t'" | "'\\''" | "''".
  164. eol => '\r' | '\n' | ''.
  165. nonterminal => relname.
  166. fname => '<' printable_chars '>' ws.
  167. string => '"' printable_chars '"' ws.
  168. printable_chars => printable printable_chars | null.
  169. cmdline => '$' digits ws.
  170. query => '!' ws term | "!!" ws term.
  171.  
  172. term => relname args.
  173. pred => term | '~' ws term ws.
  174. args => ws '(' ws args1 ws ')' ws | null.
  175. args1 => identifier ws args1 ws | args | null.
  176.  
  177. directive =>    ws "@string" space ws strdir ws '.' ws |
  178.        ws "@stdout" space ws term ws '.' ws |
  179.        ws "@trace" space ws relname ws '.' ws |
  180.        ws "@bwd" ws '.' ws.
  181. strdir => relname ws fname | relname ws string | relname ws cmdline | relname ws term.
  182.  
  183. production => relname ws "=>" ws alt ws alts ws '.' ws.
  184. alt =>  terminal ws alt ws | nonterminal ws alt ws | null.
  185. alts => null | '|' ws alt ws alts ws.
  186.  
  187. fact => pred '.' ws.
  188. preds => ws pred preds_rest.
  189. preds_rest => ws ',' ws pred ws preds_rest | null.
  190. rule => ws preds ws ":-" ws preds ws '.' ws.
  191.  
  192. fof => term ws ':' '=' ws form ws '.' ws.
  193. form => term |
  194.    ws prefix ws var ws '(' ws form ws ')' ws |
  195.    ws '(' ws form ws ')' ws "and" ws '(' ws form ws ')' ws |
  196.    ws '(' ws form ws ')' ws "or" ws '(' ws form ws ')' ws |
  197.    ws "not" '(' ws form ws ')' ws |
  198.    ws term ws "and" ws '(' ws form ws ')' ws |
  199.    ws term ws "or" ws '(' ws form ws ')' ws |
  200.    ws "not" '(' ws form ws ')' ws |
  201.    ws term ws "and" ws term ws |
  202.    ws term ws "or" ws term ws |
  203.    ws "not" term ws |
  204.    ws '(' ws form ws ')' ws "and" ws term ws |
  205.    ws '(' ws form ws ')' ws "or" ws term ws.
  206. prefix => "forall" | "exists" | "unique".
  207.  
  208. prog => directive S | rule S | production S | fof S | query S | null.
  209. S => ws '{' ws prog ws '}' ws S ws | ws prog ws | null.
  210. #}
  211. #{
  212. #   ~S(?x?x):-S(?x?x).
  213. #   ~prog(?x?x):-prog(?x?x).
  214. #   ~alt(?x?x):-alt(?x?x).
  215. #   ~alts(?x?x):-alts(?x?x).
  216. #}
  217. |}).
  218.  
  219.  
  220. :- phrase(seq_until(X,gee_unit,`.`), `@string a.` , O), O == `.`, X ==  ['@string', a].
  221.  
  222. :-               (phrase(seq_until(O,gee_unit,`.`), `@string str <tml.g> .` , O)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement