home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / TESTS / LF / Z_GCTOKE.LF < prev    next >
Text File  |  1996-06-04  |  10KB  |  496 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %                                                                             %
  3. % Translating grammars into LIFE                                              %
  4. %                                                                             %
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6.  
  7. op( 1000, xfy, comma) ?
  8. op( 1200, xfy, -->) ?
  9. non_strict(-->) ?
  10.  
  11. % non_strict(setq)?
  12. % setq(X,V) :- Value = eval(V), retract((X->@)), !, assert((X->Value)).
  13. % setq(X,V) :- dynamic(X), Value = eval(V), assert((X->Value)).
  14.  
  15. (Lhs --> Rhs) :- R = compileRule( Lhs, Rhs), assert(R).
  16.  
  17. %
  18. % compileRule: translates the grammar rules into clauses
  19. %
  20.  
  21. non_strict(compileRule) ?
  22. compileRule( Lhs, Rhs) -> 
  23.     (compileSymbols( Lhs, false, false, Xs, Ys, _) :- 
  24.          compileSeq( Rhs, true,  false, Xs, Ys, _)).
  25.  
  26. %
  27. % compileSeq is used to translate a sequence of symbols of the grammar into a
  28. % sequence of literals.
  29. %
  30.  
  31. compileSeq( Symbols, FoldOk, InDisj, Xs, Ys, NewFoldOk) -> 
  32.     cond( Symbols :== @,
  33.           `varSymbol( Symbols, words => Xs, rest => Ys) 
  34.       | NewFoldOk = FoldOk,
  35.           compileSymbols( Symbols, FoldOk, InDisj, Xs, Ys, NewFoldOk)).
  36.  
  37. %
  38. % compileSymbols is used to translate non-variable symbols.
  39. %
  40.  
  41. % conjunction
  42.   compileSymbols( ( Symb, Autres), FoldOk, InDisj, Xs, Ys, NewFoldOk) ->
  43.     compileSeq( Symb,   FoldOk,      InDisj, Xs, Ys1, InterFoldOk) comma 
  44.         compileSeq( Autres, InterFoldOk, InDisj, Ys1, Ys, NewFoldOk).
  45.  
  46. % disjunction
  47.   compileSymbols( ( List1 ; List2), FoldOk, _, Xs, Ys, NewFoldOk) -> 
  48.     X | Z = compileSeq( List1, FoldOk, true, Xs, Ys, InterFoldOk1),
  49.             T = compileSeq( List2, FoldOk, true, Xs, Ys, InterFoldOk2),
  50.             NewFoldOk = InterFoldOk1 and InterFoldOk2,
  51.         X = `( Z ; T ), 
  52.         ! .
  53.                                    
  54. % terminals
  55.   compileSymbols( Terms: list, true, false, Xs, Ys, NewFoldOk) -> 
  56.     succeed | Xs = termSequence(Terms, Ys), NewFoldOk = true.
  57.   compileSymbols( Terms: list, FoldOk, _, Xs, Ys, NewFoldOk) -> 
  58.     Xs = termSequence(Terms, Ys) | NewFoldOk = FoldOk.
  59.  
  60. % cut
  61.   compileSymbols( !, FoldOk, false, Xs, Ys, NewFoldOk) -> 
  62.     ! | NewFoldOk = false, Xs = Ys.  
  63.   compileSymbols( !, FoldOk, true, Xs, Ys, NewFoldOk) -> 
  64.     Xs = Ys, ! | NewFoldOk = false.
  65.  
  66. % insertion of code
  67.  
  68.   compileSymbols( Term: #, FoldOk, false, Xs, Ys, NewFoldOk)  -> 
  69.     transLifeCode( Term) | Xs = Ys, NewFoldOk = false.
  70.   compileSymbols( Term: #, FoldOk, true, Xs, Ys, NewFoldOk)  -> 
  71.     Xs = Ys, transLifeCode( Term) | NewFoldOk = false.
  72.  
  73.  
  74. % non-terminals
  75.   compileSymbols(NonTerm, FoldOk, _, Xs, Ys, NewFoldOk)    -> 
  76.     NonTerm  | NonTerm = @( words => Xs, rest => Ys), NewFoldOk = FoldOk.
  77.  
  78.  
  79. %
  80. % Inserting Life code
  81. %
  82.  
  83. transLifeCode( L) -> transList( feats(L)).
  84.  
  85. % For map:
  86. my_project(A,B) -> B.A.
  87.  
  88. feats(L) -> map( my_project( 2 => L), features(L)).
  89.  
  90. transList( []) ->  succeed.
  91. transList( [A|B]) -> A comma transList( B).
  92.  
  93. %
  94. % handling terminals
  95. %
  96.  
  97. termSequence( [], Ys)     -> Ys.
  98. termSequence( [T|Ts], Ys) -> [T|termSequence( Ts, Ys)].
  99.  
  100. %
  101. % This definition is used at run-time to evaluate variable symbols
  102. %
  103.  
  104. varSymbol( X:list, words => Xs, rest => Ys) -> 
  105.     Xs = termSequence( X, Ys). 
  106. varSymbol( X, words => Xs, rest => Ys) -> 
  107.     X | X = @( words => Xs, rest => Ys).
  108.  
  109.  
  110. %
  111. % getting rid of unnecessary succeed statements
  112. %
  113.  
  114. succeed comma A -> A .
  115. A comma succeed -> A .
  116. A comma B -> A , B.
  117. readf( File ) -> 
  118.     L 
  119.     |   open_in(File,S),
  120.     read_all(L),
  121.     close(S).
  122.  
  123. read_all(L) :-
  124.         get(X),
  125.         cond( X :=< end_of_file , 
  126.               L=[] ,
  127.               ( read_all(Y), L=[X|Y])).
  128.  
  129. %
  130. % load selectif
  131. %
  132.  
  133. loadl(X)  :- 
  134.     ( 
  135.     loadpath(X,".lf") ;
  136.     loadpath(X,".life")  
  137.     ), !.
  138. loadl(F) :-
  139.     write("*** File ",F,".l.. not found."), nl.
  140. loadin(X) :- loadpath(X,".in"), !.
  141. loadin(F) :-
  142.     write("*** File ",F,".in not found."), nl.
  143. loadgr(X) :- loadpath(X,".gr"), !.
  144. loadgr(F) :-
  145.     write("*** File ",F,".gr not found."), nl.
  146.  
  147.  
  148. loadpath(X:string,Suffix) :- 
  149.     !,
  150.     Y = load_path,
  151.     Z=strcon(strcon(Y,X), Suffix),
  152.     exists(Z),
  153.         !,
  154.     load_if_needed(Z).
  155.  
  156. loadpath(X,Suffix) :- loadpath(psi2str(X),Suffix).
  157.  
  158.  
  159.  
  160. %
  161. %  map function, from left to right.
  162. %
  163. lrmap(F,[]) -> [].
  164. lrmap(F,[E|L]) -> [F(E)|L1] | L1=lrmap(F,L).
  165.  
  166. %
  167. % For compatibility with old versions
  168. %
  169.  
  170. op(670,xfx,isa)?
  171. X isa Y -> (X :=< Y).
  172.  
  173. %
  174. % setq for predicates
  175. %
  176. setPred(A,B) :-
  177.         C = eval(B),
  178.         retract(A),
  179.         !,
  180.         U=root_sort(A),
  181.         U=@(C),
  182.         assert(U).
  183. setPred(A,B) :-
  184.         dynamic(A),
  185.         C = eval(B),
  186.         U=A,
  187.         U=@(C),
  188.         assert(U).
  189.  
  190. non_strict(setPred)?
  191.  
  192. %
  193. % pi !
  194. pi -> 3.14159265359 .
  195.  
  196. %
  197. % Opens  default_display and default_window.
  198. %
  199.  
  200. initWindow(DX:{700;real},DY:{700;real},
  201.            x=> X, y=>Y,
  202.            color => C,
  203.            windowtitle => T ,
  204.            permanent => B:{false ; bool }) :-  !,
  205.     x_window( DX, DY, x=>X, y=>Y, 
  206.                      title => T ,color=>C, permanent => B).
  207.  
  208.      
  209.  
  210. %
  211. % Produit d'une matrice 2x2 avec une matrice 2X1
  212. %
  213.  
  214. xmat2((X1,Y1),(X2,Y2),A,B) -> (A*X1+B*X2,A*Y1+B*Y2).
  215.  
  216.  
  217. %
  218. % setq Economique
  219. %
  220.  
  221. setEco(A,B) :- retract(A->@),
  222.                assert(A->B).
  223.  
  224. non_strict(setEco)?
  225.  
  226.  
  227. %
  228. %  make a new root from an old one and a suffix
  229. %
  230. suffixRoot(P,S:string) -> str2psi(strcon(psi2str(P),S)).
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. %
  240. % Tokenizer for Life
  241. %
  242.  
  243. main :- tokenize("LF/z_gctoken1.lf").
  244.  
  245. tokenize(File) :- 
  246.         W = readf(File), 
  247.     setq(rest,[]), setq(listTokens,[]),
  248.     tokens(words => W, rest => []).
  249.  
  250.  
  251. dynamic(tokens) ?
  252.  
  253. tokens([],words=>[]) :- !.
  254. tokens(T) --> 
  255.     tokenN(L), 
  256.     tokens(Ls), 
  257.     #( T = append(L,Ls)) ?
  258.  
  259. tokenN( TokenNs, rest => RestN, words => Words) :-
  260.         token_N( Tokens, 50, rest => Rest, words => Words),
  261.         setEco(rest,Rest),
  262.         setEco(listTokens,Tokens),
  263.         fail ; 
  264.         TokenNs = listTokens,
  265.         RestN = rest.
  266.  
  267. token_N( [], 0) --> ! ?
  268. token_N( Tokens, Counter) -->
  269.     void_chars,
  270.     (
  271.         token( Token),
  272.         !, 
  273.         token_N( NewToks, Counter - 1),
  274.         #( Tokens = [ Token| NewToks]) ;
  275.         #( Tokens = [])
  276.     ) ?
  277.  
  278. void_chars --> [{32;9;10}],!, void_chars ?         % blanc, tab, return
  279. void_chars --> [37],!, comment_chars, void_chars ? % commentaires
  280. void_chars --> [] ?
  281.  
  282. comment_chars --> [10], ! ?                     % un commentaire s'arrete avec
  283.                         % return.
  284. comment_chars --> [X], comment_chars ?
  285.  
  286.  
  287.  
  288. dynamic(token) ?
  289.  
  290. token( words => []) :- !, fail.
  291. token(T) --> 
  292.     ( 
  293.         variable(Y), 
  294.         #( !, T = variable(Y)) ;
  295.         syntact_object(Y), 
  296.         #( !, T = syntact_object(Y)) ;
  297.         construct( Y, type => Z), 
  298.         #( T = construct( Y, type => Z))
  299.     ) ?
  300.  
  301.  
  302. %
  303. %
  304. % variables
  305. %
  306.  
  307. variable(_) -->                               % @
  308.     [64], ! ?          
  309. variable(X) -->                               % _...
  310.     [95], !,
  311.     simple_atom_cars(Y), 
  312.     #((Y = "", ! ); X = strcon("_",Y)) ?
  313. variable(X) -->                               % M...
  314.     majuscule(Y), 
  315.     simple_atom_cars(Z), 
  316.     #( X = strcon(Y,Z)) ?
  317.  
  318.  
  319. %
  320. %
  321. % syntactic objects
  322. %
  323. %
  324.  
  325. syntact_object(X) --> 
  326.     [Y:{40;41;44;46;59;63;91;93;123;124;125}],
  327.     #( !, X = charac(Y)) ?
  328.  
  329.  
  330.  
  331. %
  332. %
  333. % constructors
  334. %
  335. %
  336.  
  337.  
  338. construct( Y, type=>T) --> 
  339.     quote(Y), #( !, T = quote);
  340.     simple_atom(Y), #( !, T = simple_atom); 
  341.     quoted_atom(Y), #( !, T = quoted_atom); 
  342.     number(Y), #( !, T = number);
  343.     car_chaine(Y), #( !, T = car_chaine); 
  344.     op_atom(Y), #( !, T = op_atom) ?
  345.  
  346. %
  347. % quote
  348. %
  349.  
  350. quote("`") --> [96] ?
  351.  
  352. %
  353. % simple atoms
  354. %             
  355.  
  356. simple_atom(X) --> 
  357.     minuscule(Y), 
  358.     simple_atom_cars(Z), 
  359.     #( X = strcon(Y,Z)) ?
  360.  
  361. simple_atom_cars(Z) --> 
  362.     simple_atom_car(X), !,
  363.     simple_atom_cars(Y), 
  364.     #( Z = strcon(X,Y)) ?
  365. simple_atom_cars("") --> [] ?
  366.  
  367. simple_atom_car(X) --> 
  368.     [Y], 
  369.     #( Y >= 48,
  370.        cond( Y =< 57,                                  % chiffre
  371.                  succeed,
  372.                  ( 
  373.              Y >= 65 , 
  374.              cond( Y =< 90 ,                       % majuscule
  375.                    succeed,
  376.                cond( Y =:= 95,                 % underscore
  377.                  succeed,
  378.                  ( Y >= 97 , Y =< 122)))   % minuscule
  379.          )
  380.           ),
  381.       X = charac(Y)) ?
  382.  
  383. %
  384. % quoted atoms
  385. %
  386.  
  387. quoted_atom(X) --> 
  388.     [39],
  389.         quoted_atom_end(X) ?
  390.  
  391. quoted_atom_end(X) --> 
  392.     [39], !,
  393.     ( [39], quoted_atom_end(Y), X = strcon("'",Y) ; 
  394.           # ( X = "" )) ?
  395. quoted_atom_end(X) --> 
  396.     quoted_atom_car(Y),
  397.     quoted_atom_end(Z),
  398.     #(  X = strcon(Y,Z)) ?
  399.  
  400. quoted_atom_car(Y) -->
  401.     [X], #( Y = charac(X) ) ?
  402.  
  403.  
  404. %
  405. % Numbers
  406. %
  407.  
  408. number(X) --> 
  409.     digits(V1),
  410.     ( [46], digits(V2,length => L2), !;
  411.           #( V2 = 0, L2= 0) ),
  412.     ( [101], !,exponent(E) ;
  413.       #(E = 0) ),
  414.     #( X = (V1 + V2 * 10^(-L2)) * 10^(E)) ?
  415.  
  416. digits(V, length=>L) --> 
  417.     digit( V1), 
  418.     ( digits(V2, length=>L2),!,
  419.       #( L = L2+1, V = V1*10^L2 + V2) ;
  420.           #( V = V1, L = 1)) ?
  421.  
  422. sign(-1) --> [45],! ?
  423. sign(1)  --> [43],! ?
  424. sign(1)  --> [] ?
  425.  
  426. exponent(V) --> sign(S), digits(V1), #(V = S*V1) ?
  427.  
  428. digit(N:int) --> [48+N], #(N =< 9, N >= 0)  ?
  429.  
  430.  
  431. %
  432. % Strings
  433. %
  434.  
  435. car_chaine(X) --> 
  436.     [34],
  437.         car_chaine_end(X) ?
  438.  
  439. car_chaine_end(X) --> 
  440.     [34], !,
  441.     ( [34], !,car_chaine_end(Y), #(X = strcon("""",Y)) ; 
  442.           # ( X = "" )) ?
  443. car_chaine_end(X) --> 
  444.     car_chaine_car(Y),
  445.     car_chaine_end(Z),
  446.     #(  X = strcon(Y,Z)) ?
  447.  
  448. car_chaine_car(Y) -->
  449.     [X], #( Y = charac(X) ) ?
  450.  
  451.  
  452. %
  453. % op_atoms
  454. %
  455.  
  456. op_atom(X) --> 
  457.     op_atom_car(Y),!, 
  458.     op_atom_cars(Z),
  459.     #(  X = strcon(Y,Z)) ?
  460.  
  461. op_atom_car(X) -->
  462.     [Y:{33;35;36;37;38;42;43;45;47;58;60;61;62;92;94;126}],!,
  463.     #( X = charac(Y)) ?
  464.  
  465. op_atom_cars(X) --> 
  466.     op_atom_car(Y),!, 
  467.     op_atom_cars(Z),
  468.     #(  X = strcon(Y,Z)) ?
  469. op_atom_cars("") -->
  470.     [] ?
  471.                       
  472.  
  473. minuscule(Y) --> [X], #( X >= 97, X =< 122, Y = charac(X) ) ?
  474. majuscule(Y) --> [X], #( X >= 65, X =< 90,  Y = charac(X) ) ?
  475.  
  476. charac(Z) -> chr(Z).
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.