Advertisement
Guest User

Untitled

a guest
Jun 30th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 6.35 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. sentence_op --> dcg_peek(`.`) ;  dcg_peek(`|`) ; dcg_peek(`=>`).
  61.  
  62. gee_token(Txt) -->  \+ sentence_op,
  63.   ({quotes(Q,EQ)}, (Q -> until(EQ, Txt) ; until_0(eot,Txt))).
  64.  
  65.  
  66. padded(DCG) --> optionally(some(white)), DCG, optionally(some(white)).
  67.  
  68. gee_unit(DCG) --> ` `, !, gee_unit(DCG).
  69. gee_unit(DCG) --> gee_unit_1(DCG), (eot;!). % trim off end
  70.  
  71. gee_unit_1(DCG,S,E) :-
  72.   var(DCG) -> gee_token(Txt,S,E), name(DCG,Txt) ; % forward  
  73.   atomic(DCG) -> name(DCG,Txt), gee_token(Txt,S,E) ; % reverse  
  74.   phrase(DCG,S,E). % mixed
  75.  
  76.  
  77. gee_dcg( end_of_file ) --> end_of_file, !.
  78. gee_dcg( Out ) --> gee_dcg1( A ), (end_of_file -> {Out=A} ;  gee_dcg( B ) , {Out = (A,B)}).
  79.  
  80.  
  81. gee_dcg1(DCG) --> (eol ; white), !, gee_dcg1(DCG).
  82. gee_dcg1( comment(Comment) ) --> gee_comment(Codes), {name(Comment,Codes)}.
  83. gee_dcg1( Out) --> gee_unit(L), (  "=>" -> gee_body(R), { Out = (L --> R)} ; gee_body(R), {Out = (:- do_gee(L,R))}), ".".
  84. gee_dcg1( done) --> [], !.
  85.  
  86. gee_body([]) --> `null`.
  87. gee_body(A;B) --> gee_body1(A), "|",  gee_body(B).
  88. gee_body(Body) --> gee_body1(Body).
  89.  
  90. gee_body1(Body) --> conj_until( Body, gee_unit,  sentence_op).
  91.  
  92. write_clause(X):- writeq(X), nl.
  93.  
  94.  
  95. test_g:- phrase_from_file(({trace},conj_until(DCG, gee_dcg, end_of_file)), 'tml.g'),
  96.   maplist(write_clause, DCG).
  97.  
  98.  
  99. % call(+SyntaxName, +Content, +SyntaxArgs, +VariableNames, -Result)
  100.  
  101. qq_string(Data,_Args,_VarsIn, Result):- phrase_from_quasi_quotation(any(Result),Data).
  102. :- quasi_quotation_syntax(qq_string).
  103.  
  104.  
  105. gee(Data,_Args,_VarsIn,Result):- !,
  106.   phrase_from_quasi_quotation((
  107.    seq_until(Result, gee_dcg, end_of_file)),Data).
  108.  
  109.  
  110.  
  111.  
  112. :- quasi_quotation_syntax(qq_string).
  113. :- quasi_quotation_syntax(gee).
  114.  
  115.  
  116.  
  117. gee(Data,_Args,_VarsIn,Result):- !,
  118.   phrase_from_quasi_quotation(({trace},
  119.    seq_until(Result, gee_dcg, end_of_file)),Data).
  120.  
  121. quotes(`<`,`>`).
  122. quotes(S,S):- quotes1(S).
  123. quotes1(`"`). quotes1("'").    
  124.  
  125.  
  126. write_clauses((A,B)):- !, write_clauses(A),write_clauses(B).
  127. write_clauses(X):- writeq(X), nl.
  128.  
  129. % Prints out the DCG from
  130.  
  131. :- write_clauses({|qq_string||
  132.  
  133.   directive => ws "@string" space ws strdir ws '.' ws |
  134.        ws "@stdout" space ws term ws '.' ws |
  135.        ws "@trace" space ws relname ws '.' ws |
  136.        ws "@bwd" ws '.' ws.
  137.  strdir => relname ws fname | relname ws string | relname ws cmdline | relname ws term.
  138.  
  139.  |).
  140.  
  141.  
  142. % loads the DCG forms
  143. {|qq_string||
  144.  
  145. #{
  146. @string str <tml.g>.
  147.  
  148. identifier => sym | var.
  149. args => identifier ws args | null.
  150. var => '?' chars.
  151. sym => chars.
  152. relname => sym.
  153. chars => alpha chars1 | '_' chars1.
  154. chars1=> alnum chars1 | '_' chars1 | null.
  155. ws =>   space ws | ws '#' printable_chars eol ws | null.
  156. terminal => quoted_char | string.
  157. quoted_char =>  '\'' printable '\'' | "'\\r'" | "'\\n'"
  158.        | "'\\t'" | "'\\''" | "''".
  159. eol => '\r' | '\n' | ''.
  160. nonterminal => relname.
  161. fname => '<' printable_chars '>' ws.
  162. string => '"' printable_chars '"' ws.
  163. printable_chars => printable printable_chars | null.
  164. cmdline => '$' digits ws.
  165. query => '!' ws term | "!!" ws term.
  166.  
  167. term => relname args.
  168. pred => term | '~' ws term ws.
  169. args => ws '(' ws args1 ws ')' ws | null.
  170. args1 => identifier ws args1 ws | args | null.
  171.  
  172. directive =>    ws "@string" space ws strdir ws '.' ws |
  173.         ws "@stdout" space ws term ws '.' ws |
  174.         ws "@trace" space ws relname ws '.' ws |
  175.         ws "@bwd" ws '.' ws.
  176. strdir => relname ws fname | relname ws string | relname ws cmdline | relname ws term.
  177.  
  178. production => relname ws "=>" ws alt ws alts ws '.' ws.
  179. alt =>  terminal ws alt ws | nonterminal ws alt ws | null.
  180. alts => null | '|' ws alt ws alts ws.
  181.  
  182. fact => pred '.' ws.
  183. preds => ws pred preds_rest.
  184. preds_rest => ws ',' ws pred ws preds_rest | null.
  185. rule => ws preds ws ":-" ws preds ws '.' ws.
  186.  
  187. fof => term ws ':' '=' ws form ws '.' ws.
  188. form => term |
  189.     ws prefix ws var ws '(' ws form ws ')' ws |
  190.     ws '(' ws form ws ')' ws "and" ws '(' ws form ws ')' ws |
  191.     ws '(' ws form ws ')' ws "or" ws '(' ws form ws ')' ws |
  192.     ws "not" '(' ws form ws ')' ws |
  193.     ws term ws "and" ws '(' ws form ws ')' ws |
  194.     ws term ws "or" ws '(' ws form ws ')' ws |
  195.     ws "not" '(' ws form ws ')' ws |
  196.     ws term ws "and" ws term ws |
  197.     ws term ws "or" ws term ws |
  198.     ws "not" term ws |
  199.     ws '(' ws form ws ')' ws "and" ws term ws |
  200.     ws '(' ws form ws ')' ws "or" ws term ws.
  201. prefix => "forall" | "exists" | "unique".
  202.  
  203. prog => directive S | rule S | production S | fof S | query S | null.
  204. S => ws '{' ws prog ws '}' ws S ws | ws prog ws | null.
  205. #}
  206. #{
  207. #   ~S(?x?x):-S(?x?x).
  208. #   ~prog(?x?x):-prog(?x?x).
  209. #   ~alt(?x?x):-alt(?x?x).
  210. #   ~alts(?x?x):-alts(?x?x).
  211. #}
  212. |}).
  213.  
  214.  
  215. :- phrase(seq_until(X,gee_unit,`.`), `@string a.` , O), O == `.`, X ==  ['@string', a].
  216.  
  217. :-               (phrase(seq_until(O,gee_unit,`.`), `@string str <tml.g> .` , O)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement