%begin
   %constinteger max text = 4000
   %constinteger max words = 300
   %constinteger max line = 400
   %recordformat linefm(%short line, link)
   %recordformat wordfm(%short wp, xp, link)
   %record(linefm)%array line(1:max line)
   %record(wordfm)%array word(1:max words)
   %byteintegerarray text, xtext(1:max text)
   %integer tp = 1, lp = 0, wp = 0, xp = 1
   %integer n
   %string(*)%name param
   %owninteger escape = '$', max = 0
   %routine strip(%string(*)%name from, to)
      %integer j,let,s
      to = ""
      let = 0
      %for j = 1,1,length(from) %cycle
         s = charno(from, j)
         %if s = escape %start
            let = 1
         %else
            s = s-32 %if 'a' <= s <= 'z'
            to = to.tostring(s) %if let # 0 %or (s#'_' %and s#'%')
            let = 0
         %finish
      %repeat
   %end
   %integerfn next word
      %string(*)%name sn, xn
      %integer j,p, s
      sn == string(addr(text(tp)))
      sn = ""
      readsymbol(s) %until s # ' '
      %cycle
         sn = sn.tostring(s)
         readsymbol(s)
      %repeat %until s = nl
      %for j = 1,1,wp %cycle
         %result = j %if string(addr(text(word(j)_wp))) = sn
      %repeat
      wp = wp+1
      word(wp)_xp = xp
      word(wp)_wp = tp;  tp = tp+length(sn)+1
      word(wp)_link = 0
      xn == string(addr(xtext(xp)))
      strip(sn, xn)
      max = length(xn) %if length(xn) > max
      xp = xp+length(xn)+1
      %result = wp
   %end
   %routine add line(%integer wp, line no)
      %record(linefm)%name l
      %record(wordfm)%name w
      %shortname p
      w == word(wp)
      p == w_link
      %while p # 0 %cycle
         l == line(p)
         %exit %if l_line > line no
         p == l_link
      %repeat
      lp = lp+1;  l == line(lp)
      l_line = line no
      l_link = p;  p = lp
   %end
   %routine process words
      %integer line, sect
      %cycle
         read(sect)
         read(line)
         %return %if line < 0
         add line(next word, line)
      %repeat
   %end
   %routine output index
      %record(wordfm)%name w
      %record(linefm)%name l
      %integer j, p
      printsymbol(escape);  printstring("A TAB=1,")
      write(max+5, 0);  newline
      printsymbol(escape);  printstring("A INDENT=2; JUST=1");  newline
      %for j = 1,1,wp %cycle
         w == word(j)
         printsymbol(escape);  printstring("B0")
         printsymbol(escape);  printstring("T1 ")
         printstring(string(addr(text(w_wp))))
         p = w_link
         printsymbol(escape);  printstring("T2 ")
         %while p # 0 %cycle
            l == line(p);  p = l_link
            write(l_line, 3)
            printsymbol(',') %if p # 0
         %repeat
         newline
      %repeat
      printsymbol(escape);  printsymbol('E');  newline
   %end
   %routine sort words
      %record(wordfm)%name w1, w2
      %record(wordfm) w
      %integer j,n,flag
      %for n = wp-1, -1, 1 %cycle
         flag = 0
         w2 == word(1)
         %for j = 1,1,n %cycle
            w1 == w2
            w2 == w2++1
            %if string(addr(xtext(w1_xp))) > string(addr(xtext(w2_xp))) %start
               flag = 1
               w = w1;  w1 = w2;  w2 = w
            %finish
         %repeat
      %repeat %until flag = 0
   %end

   param == string(comreg(1))
   escape = charno(param,1) %unless param = ""
   process words
   sort words
   output index
%endofprogram
