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 / parens.icn < prev    next >
Text File  |  2002-03-26  |  3KB  |  118 lines

  1. ############################################################################
  2. #
  3. #    File:     parens.icn
  4. #
  5. #    Subject:  Program to produce random balanced strings
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     March 26, 2002
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #  
  17. #     This program produces parenthesis-balanced strings in which
  18. #  the parentheses are randomly distributed.
  19. #  
  20. #  Options: The following options are available:
  21. #  
  22. #       -b n Bound the length of the strings to n left and right
  23. #            parentheses each. The default is 10.
  24. #  
  25. #       -n n Produce n strings. The default is 10.
  26. #  
  27. #       -l s Use the string s for the left parenthesis. The default
  28. #            is ( .
  29. #  
  30. #       -r s Use the string s for the right parenthesis. The default
  31. #            is ) .
  32. #  
  33. #       -v   Randomly vary the length of the strings between 0 and
  34. #            the bound.  In the absence of this option, all strings
  35. #            are the exactly as long as the specified bound.
  36. #  
  37. #     For example, the output for
  38. #  
  39. #          parens -v -b 4 -l "begin " -r "end "
  40. #  
  41. #  is
  42. #  
  43. #          begin end
  44. #          begin end begin end
  45. #          begin begin end end begin end
  46. #          begin end begin begin end end
  47. #          begin end
  48. #          begin begin end end
  49. #          begin begin begin end end end
  50. #          begin end begin begin end end
  51. #          begin end begin end
  52. #          begin begin end begin end begin end end
  53. #  
  54. #  
  55. #  Comments: This program was motivated by the need for test data
  56. #  for error repair schemes for block-structured programming lan-
  57. #  guages. A useful extension to this program would be some
  58. #  way of generating other text among the parentheses.  In addition
  59. #  to the intended use of the program, it can produce a variety of
  60. #  interesting patterns, depending on the strings specified by -l
  61. #  and -r.
  62. #  
  63. ############################################################################
  64. #
  65. #  Links: options, random
  66. #
  67. ############################################################################
  68.  
  69. link options
  70. link random
  71.  
  72. global r, k, lp, rp
  73.  
  74. procedure main(args)
  75.    local string, i, s, bound, limit, varying, opts
  76.  
  77.    randomize()
  78.    
  79.    bound := limit := 10            # default bound and limit
  80.    lp := "("                # default left paren
  81.    rp := ")"                # default right paren
  82.  
  83.    opts := options(args,"l:r:vb+n+")
  84.    bound := \opts["b"] | 10
  85.    limit := \opts["n"] | 10
  86.    lp := \opts["l"] | "("
  87.    rp := \opts["r"] | ")"
  88.    varying := opts["v"]
  89.    every 1 to limit do {
  90.       if \varying then k := 2 * ?bound else k := 2 * bound
  91.       string := ""
  92.       r := 0
  93.       while k ~= r do {
  94.          if r = 0 then string ||:= Open()
  95.          else if ?0 < probClose()
  96.             then string ||:= Close() else string ||:= Open()
  97.          }
  98.       while k > 0 do string ||:= Close()
  99.       write(string)
  100.       }
  101. end
  102.  
  103. procedure Open()
  104.    r +:= 1
  105.    k -:= 1
  106.    return lp
  107. end
  108.  
  109. procedure Close()
  110.    r -:= 1
  111.    k -:= 1
  112.    return rp
  113. end
  114.  
  115. procedure probClose()
  116.    return ((r * (r + k + 2)) / (2.0 * k * (r + 1)))
  117. end
  118.