File:  [CSRG BSD Unix] / 43BSD / contrib / icon / book / 20 / rsg1.icn
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:12:55 2018 UTC (8 years, 1 month ago) by root
Branches: MAIN, BSD
CVS tags: HEAD, BSD43
BSD 4.3

global defs

record nonterm(name)

procedure main()
   local line
   defs := table()
   while line := read() do
      (define | generate)(line)
end

procedure define(line)
   return line ?
      defs[(="<",tab(find(">::=")))] := (move(4),alts(tab(0)))
end

procedure alts(defn)
   local alist
   alist := []
   defn ? while put(alist,syms(tab(many(~'|')))) do move(1)
   return alist
end

procedure syms(alt)
   local slist
   slist := []
   alt ? while put(slist,tab(many(~'<')) |
      nonterm(2(="<",tab(upto('>')),move(1))))
   return slist
end

procedure generate(line)
   local goal, count
   line ? {
      ="<" &
      goal := tab(upto('>')) &
      move(1) &
      count := tab(0)
      }
   every write(gener(goal)) \ count
   return
end

procedure gener(goal)
   local pending, genstr, symbol
   repeat {
      pending := [nonterm(goal)]
      genstr := ""
      while symbol := get(pending) do
         if type(symbol) == "string" then genstr ||:= symbol
         else pending := ?defs[symbol.name] ||| pending
      suspend genstr
      }
end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.