home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: linden.icn
- #
- # Subject: Program to generate sentences in 0L-systems
- #
- # Author: Ralph E. Griswold
- #
- # Date: October 11, 1988
- #
- ###########################################################################
- #
- # This program reads in a 0L-system (Lindenmayer system) consisting of
- # rewriting rules in which a string is rewritten with every character
- # replaced simultaneously (conpectually) by a specified string of
- # symbols.
- #
- # The last line of input consists of an initial string followed by a colon
- # (which cannot be a symbol in the initial string) and the number of times
- # the rewriting rules are to be applied. An example is
- #
- # 1->2#3
- # 2->2
- # 3->2#4
- # 4->504
- # 5->6
- # 6->7
- # 7->8(1)
- # 8->8
- # (->(
- # )->)
- # #->#
- # 0->0
- # 1:14
- #
- # Here, the initial string is "1" and the rewriting rules are to be
- # applied 14 times.
- #
- # If no rule is provided for a character, the character is not changed
- # by rewriting. Thus, the example above can be expressed more concisely
- # as
- #
- # 1->2#3
- # 3->2#4
- # 4->504
- # 5->6
- # 6->7
- # 7->8(1)
- # 1:14
- #
- # If -a is given on the command line, each rewriting is written out.
- # Otherwise, only the final result is written out.
- #
- # Reference:
- #
- # Formal Languages, Arto Salomaa, Academic Press, 1973. pp. 234-252.
- #
- ############################################################################
- #
- # Links: options
- #
- ############################################################################
-
- link options
-
- global rewrite
-
- procedure main(args)
- local line, count, axiom, detail, opts, i, result, s, c, symbol
-
- rewrite := table()
-
- # What follows is a trick. It takes advantage of the fact that Icon
- # functions are first-class data objects and that function invocation
- # and mutual evaluation have the same syntax. If -a is specified,
- # the value of "detail" becomes the function for writing and the
- # value of "write" becomes 1. See below.
-
- detail := 1
-
- opts := options(args,"a")
- if \opts["a"] then detail :=: write
-
- while line := read() do
- line ? {
- if symbol := move(1) & ="->" then
- rewrite[symbol] := tab(0)
- else if result := tab(upto(':')) then {
- move(1)
- count := tab(0)
- }
- else write(&errout, "malformed input: ", tab(0))
- }
-
- detail(result) # initial configuration
-
- every 1 to count do {
- s := ""
- every c := !result do
- s ||:= (\rewrite[c] | c)
- result := s
- detail(result)
- }
-
- write(result) # write result if not already written
-
- end
-