Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.05 KB | None | 0 0
  1.     :- use_module(library(http/thread_httpd)).
  2.     :- use_module(library(http/http_dispatch)).
  3.  
  4. :- use_module(library(http/http_parameters)).
  5.  
  6.     % The predicate server(+Port) starts the server. It simply creates a
  7.     % number of Prolog threads and then returns to the toplevel, so you can
  8.     % (re-)load code, debug, etc.
  9.     server(Port) :-
  10.             http_server(http_dispatch, [port(Port)]).
  11.  
  12.     % Declare a handler, binding an HTTP path to a predicate.
  13.     % Here our path is / (the root) and the goal we'll query will be
  14.     % say_hi. The third argument is for options
  15.     :- http_handler(/, say_hi, []).
  16.  
  17.     /* The implementation of /. The single argument provides the request
  18.     details, which we ignore for now. Our task is to write a CGI-Document:
  19.     a number of name: value -pair lines, followed by two newlines, followed
  20.     by the document content, The only obligatory header line is the
  21.     Content-type: <mime-type> header.
  22.     Printing can be done using any Prolog printing predicate, but the
  23.     format-family is the most useful. See format/2. */
  24.  
  25.     say_hi(Request) :-
  26.         http_parameters(Request, [d(DValue, [default('')])]),
  27.         my_parser(DValue, Msg),
  28.             format('Content-type: text/html~n~n'),
  29.         format('<p>That was ~w</p>', [Msg]),
  30.             format('<form name="input" action="/" method="get">
  31.    Username: <input type="text" name="d"><input type="submit" value="Submit"></form> ').
  32.  
  33.  
  34. my_parser(AtomIn, Tree) :-
  35.     atom_codes(AtomIn, Codes),
  36.     phrase(kumquattish(Tree), Codes).
  37. my_parser(_, 'Thats not proper kumquattish.').
  38.  
  39.  
  40. kumquattish(sentence(S,V,O)) -->  subject(S), w, verb(V), w, object(O).
  41.  
  42. subject(noun(S)) --> noun(S).
  43. subject(adjphrase(A, S)) --> adjective(A), w, subject(S).
  44. adjective(adjective(A)) --> raw_adj(A).
  45. raw_adj(big) --> "big".
  46. raw_adj(bad) --> "bad".
  47. raw_adj(green) --> "green".
  48.  
  49. w --> " ".
  50. w --> " ", w.
  51.  
  52. noun(apple) --> "apple".
  53. noun(kumquat) --> "kumquat".
  54.  
  55. verb(eats) --> "eats".
  56. verb(knows) --> "knows".
  57.  
  58. object(object(N)) --> subject(N).
  59.  
  60. any --> [].
  61. any --> [_], any.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement