Advertisement
Guest User

tml.g

a guest
Jun 30th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.14 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. some(P) --> P, optionally(some(P)).
  13.  
  14. any(S,S,[]).
  15.  
  16. end_of_file --> ([-1] ; [end_of_file] ; \+ [_]),!.
  17. end_of_file(S,E):- S==[],!, E=[].
  18.  
  19.  
  20. dcg_peek(Until,S,S) :- phrase(Until,S,_).
  21.  
  22. is_until(Until) --> dcg_peek(Until) ; end_of_file.
  23.  
  24. conj_until(Out, P, Until) --> call(P, A),
  25.  ( is_until(Until) -> {A = Out} ; conj_until(B, P, Until), {Out = (A,B)}).
  26.  
  27. seq_until([], _, Until) --> is_until(Until),!.
  28. seq_until([H|T], P, Until) --> call(P, H), !, seq_until(T, P, Until).
  29.  
  30.  
  31. until(Until,Stuff) --> until_0(Until,Stuff),Until.
  32. until_0(Until,[C|Before]) --> "\\",[C],!,until_0(Until,Before).
  33. until_0(DCG,[],[Found|Rest],[Found|Rest]):- phrase(DCG,[Found],[])-> true ; phrase(DCG,[Found|Rest],_).
  34. until_0(DCG,[Skipped|Before],[Skipped|More],[Found|Rest]):- until_0(DCG,Before,More,[Found|Rest]).
  35.  
  36.  
  37. % // =========                            
  38. % //  Dot Gee
  39. % // =========
  40. gee_comment(Cmt) -->
  41.  ("//" ; "#") -> (until(eol,Cmt) ; any(Cmt)) ;
  42.   "/*", any(Cmt), "*/".
  43.  
  44. white --> ` ` ; eol ; gee_comment(_).
  45.  
  46. eot --> dcg_peek(`.`) ; white.
  47. eol --> "\n\r" ; "\r" ; "\n" .
  48.  
  49.  
  50. gee_token(Txt) --> \+ dcg_peek(`.`),
  51.   {quotes(Q,EQ)}, (Q -> until(EQ, Txt) ; until_0(eot,Txt)).
  52.  
  53. quotes(`<`,`>`).
  54. quotes(S,S):- quotes1(S).
  55. quotes1(`"`). quotes1(`'`).
  56.  
  57.  
  58. padded(DCG) --> optionally(some(white)), DCG, optionally(some(white)).
  59.  
  60. gee_unit(DCG) --> ` `, !, gee_unit(DCG).
  61. gee_unit(DCG) --> gee_unit_1(DCG), (eot;!). % trim off end
  62.  
  63. gee_unit_1(DCG,S,E) :-
  64.  var(DCG) -> gee_token(Txt,S,E), name(DCG,Txt) ; % forward  
  65.  atomic(DCG) -> name(DCG,Txt), gee_token(Txt,S,E) ; % reverse  
  66.  phrase(DCG,S,E). % mixed
  67.  
  68. gee_dcg( Out ) --> gee_dcg1( A ), (end_of_file -> {Out=A} ;  gee_dcg( B ) , {Out = (A,B)}).
  69.  
  70.  
  71. gee_dcg1(DCG) --> ` `, !, gee_dcg1(DCG).
  72. gee_dcg1( comment(Comment) ) --> gee_comment(Codes), {name(Comment,Codes)}.
  73. gee_dcg1( Out) --> gee_unit(L), (  "=>" -> gee_body(R), { Out = (L --> R)} ; gee_body(R), {Out = (:- do_gee(L,R))}), ".".
  74. gee_dcg1( done) --> [], !.
  75.  
  76.  
  77. gee_body(Body) --> conj_until( Body, [E] >> padded(gee_unit(E)), ".").
  78. gee_body(A;B) --> gee_body(A), "|",  gee_body(B).
  79.  
  80. write_clause(X):- writeq(X), nl.
  81.  
  82.  
  83. test_g:- phrase_from_file(({trace},conj_until(DCG, gee_dcg, end_of_file)), 'tml.g'),
  84.  maplist(write_clause, DCG).
  85.  
  86.  
  87. % call(+SyntaxName, +Content, +SyntaxArgs, +VariableNames, -Result)
  88.  
  89. qq_string(Data,_Args,_VarsIn,Result):- !,
  90.  phrase_from_quasi_quotation(any(Resuult),Data).
  91.  
  92. gee(Data,_Args,_VarsIn,Result):- !,
  93.  phrase_from_quasi_quotation(({trace},
  94.   seq_until(Result, gee_dcg, end_of_file)),Data).
  95.  
  96.  
  97. :- quasi_quotation_syntax(qq_string).
  98. %:- quasi_quotation_syntax(gee).
  99.  
  100.  
  101. gee_string(
  102. {|qq_string||
  103.  
  104. #{
  105. @string str <tml.g>.
  106.  
  107. identifier => sym | var.
  108. args => identifier ws args | null.
  109. var => '?' chars.
  110. sym => chars.
  111. relname => sym.
  112. chars => alpha chars1 | '_' chars1.
  113. chars1=> alnum chars1 | '_' chars1 | null.
  114. ws =>   space ws | ws '#' printable_chars eol ws | null.
  115. terminal => quoted_char | string.
  116. quoted_char =>  '\'' printable '\'' | "'\\r'" | "'\\n'"
  117.         | "'\\t'" | "'\\''" | "''".
  118. eol => '\r' | '\n' | ''.
  119. nonterminal => relname.
  120. fname => '<' printable_chars '>' ws.
  121. string => '"' printable_chars '"' ws.
  122. printable_chars => printable printable_chars | null.
  123. cmdline => '$' digits ws.
  124. query => '!' ws term | "!!" ws term.
  125.  
  126. term => relname args.
  127. pred => term | '~' ws term ws.
  128. args => ws '(' ws args1 ws ')' ws | null.
  129. args1 => identifier ws args1 ws | args | null.
  130.  
  131. directive =>    ws "@string" space ws strdir ws '.' ws |
  132.         ws "@stdout" space ws term ws '.' ws |
  133.         ws "@trace" space ws relname ws '.' ws |
  134.         ws "@bwd" ws '.' ws.
  135. strdir => relname ws fname | relname ws string | relname ws cmdline | relname ws term.
  136.  
  137. production => relname ws "=>" ws alt ws alts ws '.' ws.
  138. alt =>  terminal ws alt ws | nonterminal ws alt ws | null.
  139. alts => null | '|' ws alt ws alts ws.
  140.  
  141. fact => pred '.' ws.
  142. preds => ws pred preds_rest.
  143. preds_rest => ws ',' ws pred ws preds_rest | null.
  144. rule => ws preds ws ":-" ws preds ws '.' ws.
  145.  
  146. fof => term ws ':' '=' ws form ws '.' ws.
  147. form => term |
  148.     ws prefix ws var ws '(' ws form ws ')' ws |
  149.     ws '(' ws form ws ')' ws "and" ws '(' ws form ws ')' ws |
  150.     ws '(' ws form ws ')' ws "or" ws '(' ws form ws ')' ws |
  151.     ws "not" '(' ws form ws ')' ws |
  152.     ws term ws "and" ws '(' ws form ws ')' ws |
  153.     ws term ws "or" ws '(' ws form ws ')' ws |
  154.     ws "not" '(' ws form ws ')' ws |
  155.     ws term ws "and" ws term ws |
  156.     ws term ws "or" ws term ws |
  157.     ws "not" term ws |
  158.     ws '(' ws form ws ')' ws "and" ws term ws |
  159.     ws '(' ws form ws ')' ws "or" ws term ws.
  160. prefix => "forall" | "exists" | "unique".
  161.  
  162. prog => directive S | rule S | production S | fof S | query S | null.
  163. S => ws '{' ws prog ws '}' ws S ws | ws prog ws | null.
  164. #}
  165. #{
  166. #   ~S(?x?x):-S(?x?x).
  167. #   ~prog(?x?x):-prog(?x?x).
  168. #   ~alt(?x?x):-alt(?x?x).
  169. #   ~alts(?x?x):-alts(?x?x).
  170. #}
  171.  
  172. |}).
  173.  
  174.  
  175. :- phrase(seq_until(X,gee_unit,`.`), `@string a.` , O), O == `.`, X ==  ['@string', a].
  176.  
  177. :-               (phrase(seq_until(O,gee_unit,`.`), `@string str <tml.g> .` , O)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement