home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / PIE.ARC / PIE.SCA < prev    next >
Encoding:
Text File  |  1988-06-21  |  5.2 KB  |  184 lines

  1. /*****************************************************************************
  2.             Prolog Inference Engine
  3.             =======================
  4.  
  5. Copyright (c) 1986, 88 by Borland International, Inc
  6.  
  7. Module PIE.SCA: The declarations and main program
  8.  
  9. This scanner breaks a string up into a list of tokens of the following
  10. kinds:
  11.  
  12. TOK    = lbrack; rbrack;    [ ]
  13.       lpar; rpar;        ( )
  14.       lcurly; rcurly;    { }
  15.       var(STRING);       X,  This, _ok
  16.       atom(STRING);      hello, :-, :::, fail
  17.  
  18.       int(INTEGER);      123, -456
  19.       str(STRING);       "This is a string"
  20.       char(CHAR);        `*
  21.       
  22.       comma;             ,
  23.       bar;               |
  24.       dot                .
  25.  
  26. A symbol is either a name starting with a lowercase letter, or a sequence
  27. of the following characters:
  28.  
  29.   +  -  *  /  =  `  :  .  \  ^  <  >  ?  @  #  $  &
  30.  
  31. /* Comments in this following format are accepted */
  32.  
  33. ************************************************************************/
  34.  
  35. PREDICATES
  36.   scan_error(STRING,CURSOR)
  37.   tokl(CURSOR,STRING,TOKL)
  38.   maketok(CURSOR,STRING,TOK,STRING,STRING,CURSOR)
  39.   scan_str(CURSOR,CHAR,STRING,STRING,STRING)
  40.   search_ch(CHAR,STRING,INTEGER,INTEGER)
  41.   skipspaces(STRING,STRING,INTEGER)
  42.   isspace(CHAR)
  43.   white_follow(STRING)
  44.   is_symbchar(CHAR)
  45.   scan_atom(STRING,CURSOR)
  46.   commentlength(INTEGER,STRING,CURSOR,CURSOR)
  47.   ok_follow_uscore(CHAR)
  48.  
  49. CLAUSES
  50.   scan_error(STRING,_):-write("\n>> SCANNER ERROR: ",STRING).
  51.  
  52.   tokl(POS,STR,[t(TOK,POS1)|TOKL]):-
  53.     skipspaces(STR,STR1,NOOFSP),
  54.     POS1=POS+NOOFSP,
  55.     fronttoken(STR1,STRTOK,STR2),!,
  56.     maketok(POS,STRTOK,TOK,STR2,STR3,LEN1),
  57.     str_len(STRTOK,LEN),
  58.     POS2=POS1+LEN+LEN1,
  59.     tokl(POS2,STR3,TOKL).
  60.   tokl(_,_,[]).
  61.  
  62.   skipspaces(STR,STR2,NOOFSP):-
  63.     frontchar(STR,CH,STR1),isspace(CH),!,
  64.     skipspaces(STR1,STR2,N1),
  65.     NOOFSP=N1+1.
  66.   skipspaces(STR,STR3,NOOFSP):-
  67.     frontchar(STR,'%',STR1),
  68.     search_ch('\n',STR1,0,N),
  69.     frontstr(N,STR1,DROP,STR2),
  70.     skipspaces(STR2,STR3,N1),
  71.     str_len(DROP,N2),
  72.     NOOFSP=N1+1+N2,!.
  73.   skipspaces(STR,STR4,NOOFSP):-
  74.     frontchar(STR,'/',STR1),
  75.     frontchar(STR1,'*',STR2),
  76.     commentlength(0,STR2,0,N),
  77.     frontstr(N,STR2,DROP,STR3),
  78.     skipspaces(STR3,STR4,N1),
  79.     str_len(DROP,N2),
  80.     NOOFSP=N1+2+N2,!.
  81.   skipspaces(STR,STR,0).
  82.  
  83.   isspace(' ').  isspace('\t').  isspace('\n').
  84.  
  85.   white_follow(S):-
  86.     frontchar(S,CH,_),
  87.     not(isspace(CH)),!,fail.
  88.   white_follow(_).
  89.  
  90.   ok_follow_uscore('_'):-!.
  91.   ok_follow_uscore(CH):-CH>='0',CH<='9',!.
  92.   ok_follow_uscore(CH):-CH>='a',CH<='z',!.
  93.   ok_follow_uscore(CH):-CH>='A',CH<='Z',!.
  94.  
  95.   maketok(_,"(",lpar,S,S,0):-!.
  96.   maketok(_,")",rpar,S,S,0):-!.
  97.   maketok(_,"[",lbrack,S,S,0):-!.
  98.   maketok(_,"]",rbrack,S,S,0):-!.
  99.   maketok(_,"{",lcurly,S,S,0):-!.
  100.   maketok(_,"}",rcurly,S,S,0):-!.
  101.   maketok(_,"|",bar,S,S,0):-!.
  102.  
  103.   maketok(_,".",dot,S,S,0):-
  104.     white_follow(S),!.
  105.  
  106.   maketok(_,",",comma,S,S,0):-!.
  107.   maketok(_,";",atom(";"),S,S,0):-!.
  108.   maketok(_,"!",atom("!"),S,S,0):-!.
  109.  
  110.   maketok(_,"`",char(T),S1,S2,1):-!,
  111.     frontchar(S1,T,S2).
  112.   maketok(_,STRING,atom(SYMB),S,S1,LEN):-
  113.     str_char(STRING,CH),
  114.     is_symbchar(CH),!,
  115.     scan_atom(S,LEN),
  116.     frontstr(LEN,S,SYMB1,S1),
  117.     frontchar(SYMB,CH,SYMB1).
  118.   maketok(CURSOR,"\"",str(STR),S1,S2,LEN):-!,
  119.     scan_str(CURSOR,'"',S1,S2,STR),str_len(STR,LEN1),LEN=LEN1+1.
  120.   maketok(CURSOR,"'",atom(ATOM),S1,S2,LEN):-!,
  121.     scan_str(CURSOR,''',S1,S2,ATOM),str_len(ATOM,LEN1),LEN=LEN1+1.
  122.   maketok(_,INTSTR,int(INTEGER),S,S,0):-str_int(INTSTR,INTEGER),!.
  123. %  maketok(_,REALSTR,real(REAL),S,S,0):-str_real(REALSTR,REAL),!.
  124.   maketok(_,STRING,var(STRING),S,S,0):-
  125.     frontchar(STRING,CH,_),CH>='A',CH<='Z',isname(STRING),!.
  126.   maketok(_,"_",var(NAME),S1,S2,LEN):-
  127.     frontchar(S1,CH,_),ok_follow_uscore(CH),!,
  128.     fronttoken(S1,TOK,S2),
  129.     str_len(TOK,LEN),
  130.     frontchar(NAME,'_',TOK).
  131.   maketok(_,"_",var("_"),S,S,0):-!.
  132.   maketok(_,STRING,atom(STRING),S,S,0):-isname(STRING),!.
  133.   maketok(CURSOR,STR,_,S,_,_):-
  134.     write(STR,S),nl,
  135.     scan_error("Illegal token",CURSOR),fail.
  136.  
  137.   scan_str(_,CH,IN,OUT,STR):-
  138.     search_ch(CH,IN,0,N),
  139.     frontstr(N,IN,STR,OUT1),!,
  140.     frontchar(OUT1,_,OUT).
  141.   scan_str(CURSOR,_,_,_,_):-
  142.     scan_error("String not terminated",CURSOR),fail.
  143.  
  144.   commentlength(0,STR,N1,N):-
  145.     frontchar(STR,'*',STR1),
  146.     frontchar(STR1,'/',_),!,
  147.     N=N1+2.
  148.   commentlength(NEST,STR,N1,N):-
  149.     frontchar(STR,'*',STR1),
  150.     frontchar(STR1,'/',STR2),!,
  151.     NEST1=NEST-1,
  152.     N11=N1+2,
  153.     commentlength(NEST1,STR2,N11,N).
  154.   commentlength(NEST,STR,N1,N):-
  155.     frontchar(STR,'/',STR1),
  156.     frontchar(STR1,'*',STR2),!,
  157.     NEST1=NEST+1,
  158.     N11=N1+2,
  159.     commentlength(NEST1,STR2,N11,N).
  160.   commentlength(NEST,STR,N1,N):-
  161.     frontchar(STR,_,STR1),
  162.     N11=N1+1,
  163.     commentlength(NEST,STR1,N11,N).
  164.  
  165.   search_ch(CH,STR,N,N):-
  166.     frontchar(STR,CH,_),!.
  167.   search_ch(CH,STR,N,N1):-
  168.     frontchar(STR,_,S1),
  169.     N2=N+1,
  170.     search_ch(CH,S1,N2,N1).
  171.  
  172.   scan_atom(S1,LEN):-
  173.     frontchar(S1,CH,S2),
  174.     is_symbchar(CH),!,
  175.     scan_atom(S2,LEN1),
  176.     LEN=LEN1+1.
  177.   scan_atom(_,0).
  178.  
  179.   is_symbchar('+').  is_symbchar('-').  is_symbchar('*').  is_symbchar('/').
  180.   is_symbchar('=').  is_symbchar(':').  is_symbchar('.').  is_symbchar('&').
  181.   is_symbchar('\\'). is_symbchar('^').  is_symbchar('<').  is_symbchar('>').
  182.   is_symbchar('?').  is_symbchar('@').  is_symbchar('#').  is_symbchar('$').
  183.  
  184.