home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / lindsys.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  143 lines

  1. ############################################################################
  2. #
  3. #    File:     lindsys.icn
  4. #
  5. #    Subject:  Program to generate sentences in 0L-systems
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     October 23, 1998
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program reads in a 0L-system (Lindenmayer system) consisting of
  18. #  rewriting rules in which a string is rewritten with every character
  19. #  replaced simultaneously (conceptually) by a specified string of
  20. #  symbols.
  21. #
  22. #  Rules have the form
  23. #
  24. #    S->SSS...
  25. #
  26. #  where S is a character.
  27. #
  28. #  In addition to rules, there are keywords that describe attributes of the
  29. #  system.  These include the "axiom", the string on which rewriting is
  30. #  started and "gener", the number of generations.
  31. #
  32. #  The keyword "name" may be used to identify different L-systems in
  33. #  a file.  If a name is given, it must be the first line of the L-system.
  34. #
  35. #  If the keyword "end" is present, it is taken as the termination of
  36. #  the grammar.  Otherwise, the end of the file serves this purpose.
  37. #
  38. #  Other keywords may be present, but are ignored.  For example,
  39. #
  40. #    comment:This produces a great tree.
  41. #
  42. #  is ignored.
  43. #
  44. #  Keywords are followed by a colon.
  45. #
  46. #    An example 0L-system is:
  47. #
  48. #    name:dragon
  49. #    X->-FX++FY-
  50. #    Y->+FX--FY+
  51. #    F->
  52. #    -->-
  53. #    +->+
  54. #    axiom:FX
  55. #
  56. #  Here, the initial string is "FX".
  57. #
  58. #  Note that "-" is a legal character in a 0L-system -- context determines
  59. #  whether it's 0L character or part of the "->" that stands for "is
  60. #  replaced by".
  61. #
  62. #  If no rule is provided for a character, the character is not changed
  63. #  by rewriting. Thus, the example above can be expressed more concisely
  64. #  as
  65. #
  66. #    name:dragon
  67. #    X->-FX++FY-
  68. #    Y->+FX--FY+
  69. #    F->
  70. #    axiom:FX
  71. #
  72. #  The file containing the 0L-system is read from standard input.
  73. #
  74. #  The command-line options are:
  75. #
  76. #    -g i    number of generations if not given, default 3
  77. #    -a s    axiom (overrides axiom given in the grammar)
  78. #    -A    generate all intermediate results, not just the last
  79. #
  80. #  Note:  An earlier version of this program had the ability to
  81. #  extract an L-System specification by name from a file with
  82. #  multiple specifications.  This version does not -- the former
  83. #  functionality was deemed to cumbersome.
  84. #
  85. #  References:
  86. #
  87. #     Formal Languages, Arto Salomaa, Academic Press, 1973. pp. 234-252.
  88. #
  89. #     The Algorithmic Beauty of Plants, Przemyslaw Prusinkiewicz and
  90. #     Aristid Lindenmayer, Springer Verlag, 1990.
  91. #
  92. #     Lindenmayer Systems, Fractals, and Plants, Przemyslaw Prusinkiewicz
  93. #     and James Hanan, Springer Verlag, 1989.
  94. #
  95. ############################################################################
  96. #
  97. #  See linden.dat for an example of input data.
  98. #
  99. #  See also linden.icn for a graphics version.
  100. #
  101. ############################################################################
  102. #
  103. #  Links: lindgen, makelsys, options
  104. #
  105. ############################################################################
  106.  
  107. link lindgen
  108. link makelsys
  109. link options
  110.  
  111. procedure main(args)
  112.    local line, gener, axiom, opts, i, s, c, symbol, rewrite
  113.    local low, lsys, lst
  114.  
  115.    opts := options(args,"n:g+a:A")
  116.  
  117.    lst := []
  118.  
  119.    while put(lst, read())
  120.  
  121.    lsys := makelsys(lst)
  122.  
  123.    axiom := lsys.axiom
  124.    gener := lsys.gener
  125.    rewrite := lsys.productions
  126.  
  127.    axiom := \opts["a"]
  128.    gener := \opts["g"]
  129.    /gener := 3
  130.  
  131.    if /axiom then stop("*** no axiom")
  132.  
  133.    #  The following approach is inefficient if low is not gener.
  134.  
  135.    low := if /opts["A"] then gener else 1 
  136.  
  137.    every i := low to gener do {
  138.       every writes(lindgen(!axiom, rewrite, i))
  139.       write()
  140.       }
  141.  
  142. end
  143.