home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / v / vsnbl220.zip / DEMO.TXT < prev    next >
Text File  |  1991-02-14  |  11KB  |  283 lines

  1.                        THE SNOBOL4 PROGRAMMING LANGUAGE
  2.  
  3. If you ever have to work with textual data, the SNOBOL4 language lets you do
  4. it quickly and easily.  Give us 10 minutes and we'll give you a quick tour of
  5. the language and explain why reviewers call it the "standard for text
  6. processing."
  7.  
  8. We've included a working version of SNOBOL4, a tutorial and reference manual
  9. and many sample programs, so if you're intrigued by what you see, you can
  10. immediately start learning and using the language in earnest.
  11.  
  12. About This Demo
  13. ----------------
  14.  
  15. SNOBOL4 is controlling this demo, so the sample statements and results you see
  16. here are produced "live" by SNOBOL4.  After the demonstration, you'll have the
  17. opportunity to enter your own practice statements.
  18.  
  19. The demo runs in text mode, and should run on any MS-DOS computer with a 25-
  20. line monitor.
  21.  
  22. Ready to learn about SNOBOL4?
  23. <
  24.                                LANGUAGE BASICS
  25. Variables
  26. ---------
  27.  
  28. Like other languages, SNOBOL4 provides variables to hold values:
  29.  
  30. *    Game = 5
  31. *    Score = "Love"
  32.  
  33. Unlike many other languages though, you don't have to declare the type of data
  34. to be held in each variable.  SNOBOL4 adjusts the variable automatically,
  35. depending upon what you store into it.  That means that a variable such as
  36. Score can have a text string like "Love" at one moment, then hold an integer
  37. sometime later:
  38.  
  39. *    Score = 15
  40.  
  41. For the technically minded, SNOBOL4 variables are "dynamically typed."
  42. <
  43. Type Checking
  44. -------------
  45.  
  46. Because SNOBOL4 remembers the type of data currently in each variable, all
  47. operators and functions can check the values given them.  SNOBOL4 will
  48. automatically convert any that are in the wrong form.  Suppose we try to add
  49. the text string "30" to the value in Score:
  50.  
  51. >    Score = Score + "30"
  52.  
  53. The addition operator (+) automatically converts the string "30" to the
  54. integer 30 before adding it to the integer 15 in variable Score.  Here we show
  55. the result of the operation within parentheses.
  56.  
  57. Type checking eliminates many hidden program bugs, and automatic type
  58. conversion greatly simplifies programming.
  59. <
  60. String Concatenation
  61. --------------------
  62.  
  63. SNOBOL4's real strength lies in its ability to manipulate textual data, or
  64. "strings" of characters.  The simplest operation is "concatenation," which
  65. involves appending one string to another.  In SNOBOL4, we do this simply by
  66. writing the variables or literal strings to be concatenated one after the
  67. other, with a space between each:
  68.  
  69. >    Test = "Quick" "Brown" "Fox"
  70. >    State = "The score in game number " Game " is " Score
  71.  
  72. Here's another example of type conversion.  The integers 5 and 15 in the
  73. variables Game and Score were converted automatically to strings so they could
  74. be concatenated with the other strings in the second statement above.
  75.  
  76. Notice what you DON'T have to do in SNOBOL4:  explicitly manage memory stor-
  77. age.  It happens automatically -- SNOBOL4 allocates space as needed, and
  78. releases it when the data is no longer needed.  It's effortless, and it's
  79. foolproof.
  80. <
  81. Pattern Matching
  82. ----------------
  83.  
  84. There are other operations besides concatenation to manipulate strings, but
  85. for brevity, we'll skip them and move on to SNOBOL4's most unique and powerful
  86. feature, pattern matching.
  87.  
  88. Pattern matching examines a "subject" string for some combination of charac-
  89. ters, called a "pattern."  The matching process may be very simple, or
  90. extremely complex.  For example:
  91.  
  92. 1. The subject contains several color names.  The pattern is the string
  93.    "BLUE". Does the subject string contain the word "BLUE"?
  94. 2. The subject contains a record of data read from a file.  The pattern
  95.    deciphers the field structure within the record, and stores the component
  96.    pieces into variables.
  97. 3. The subject contains a paragraph of English text.  The pattern describes
  98.    the spacing rules to be applied after punctuation.  Does the subject string
  99.    conform to the punctuation rules?
  100. 4. The subject string represents the current board position in a game of Tick-
  101.    Tack-Toe.  The pattern examines this string and determines the next move.
  102. 5. The subject contains a statement from a prototype language.  The pattern
  103.    contains the grammar of that language.  Is the statement properly formed
  104.    according to the grammar?
  105. <
  106. A pattern is a unique data type in SNOBOL4, something that can be compiled and
  107. stored in a variable.  For example,
  108.  
  109. *    Colors = "red" | "green" | "blue"
  110.  
  111. assigns to Colors a pattern that will match the string "red" OR the string
  112. "green" OR the string "blue".  To use the pattern, we simply write the string
  113. or variable to be tested, followed by the pattern.
  114.  
  115. *    "turn at the red house" Colors . Result
  116.  
  117. The period between Colors and Result says "take whatever matched on the left
  118. side of the period, and assign it to the variable on the right."  Since the
  119. "red" portion of Colors matched, SNOBOL4 assigned it to the variable named
  120. Result.
  121. <
  122. If the subject is a variable, we can replace just whatever matches:
  123.  
  124. *    Instruction = "turn at the red house"
  125.  
  126.        subject  pattern = replacement
  127.        ───┬───  ───┬───   ──┬────────
  128.           │        │        │
  129.      ─────┴───── ──┴───   ──┴──
  130. >     Instruction Colors = "BIG"
  131. <
  132. The real power in pattern matching comes from two additional features:
  133.  
  134. 1. Patterns can be more than simple literal strings.  They can specify the
  135.    KIND of characters to be matched, such as a run of digits, or characters
  136.    "up to" a punctuation mark, or to a fixed position in the subject, etc.
  137.  
  138. 2. Patterns can be built using AND and OR combinations of other patterns.
  139.    Patterns may even be defined in terms of themselves (recursive patterns).
  140.  
  141. Here are a few simple examples:
  142.  
  143. *    Vowel = any("aeiou")
  144. *    DoubleVowel = Vowel Vowel
  145. *    "vacuum" DoubleVowel . Result
  146.  
  147. *    Letters = "abcdefghijklmnopqrstuvwxyz"
  148. *    Wordpat = break(Letters) span(Letters) . Result
  149. *    Instruction Wordpat = ""
  150. *    Instruction Wordpat = ""
  151. <
  152. Program Structure
  153. -----------------
  154.  
  155. Statements like the ones shown will "succeed" if the indicated operation can
  156. be carried out, or "fail" if they cannot.  SNOBOL4 also provides all the
  157. "testing" operations familiar from other languages, such as comparison of
  158. strings or numeric values.  Comparisons also will "succeed" or "fail".
  159.  
  160. Normally, SNOBOL4 executes statements in sequence.  However the success or
  161. failure of a statement can be tested, and program control passed to a differ-
  162. ent statement instead.  Statements are identified by "statement labels" that
  163. precede the statement:
  164.  
  165. *         Test = "sunspots and flares"
  166. *Again    Test Wordpat = ""                  :S(Again)
  167.  
  168. This little program uses Wordpat to look for a word in the subject, Test.  If
  169. found, it is replaced with an empty string, effectively deleting it from Test.
  170. The pattern match succeeds, and the ":S" test sends the program back to the
  171. same statement to get another word.  When the pattern match ultimately fails,
  172. control proceeds to the next statement.
  173. <
  174. Miscellaneous
  175. -------------
  176.  
  177. There are lots of other goodies in SNOBOL4.  You can have arrays of data,
  178. where each array element can hold data of a different type.  A Table is like a
  179. one-dimensional array, except that its subscripts aren't limited to integers.
  180. Any type of SNOBOL4 data can be used as subscripts.  Here's an example using
  181. string subscripts:
  182.  
  183. *    Sound = table()
  184. *    Sound["dog"] = "bark"
  185. *    Sound["cat"] = "meow"
  186.  
  187. Tables expand automatically as you store things in them.  They're handy for
  188. storing relationships and building indexes to data.
  189.  
  190. You also can define your own data types, redefine SNOBOL4's operators, and
  191. create your own functions.  You can even compile new program statements while
  192. your program is running (as this demo does).
  193.  
  194. It's a rich language, and the parts work harmoniously to let you express
  195. complex ideas concisely.
  196. <
  197. Where to Next?
  198. --------------
  199.  
  200. The file README.DOC provides a road map to the files on this disk.
  201.  
  202. If you want to learn more about the SNOBOL4 language, the batch file
  203. PRINTMAN.BAT will produce a language tutorial and reference manual.
  204.  
  205. If you want to transform a file by performing multiple line-by-line search-
  206. and-replaces, we've included a pre-written SNOBOL4 program that does just
  207. that.  It can replace literal strings, or use the full range of SNOBOL4
  208. pattern matching.  Print the file GENTRAN.DOC for instructions.  You can run
  209. GENTRAN at the DOS prompt by typing:
  210.  
  211.      >SNOBOL4 GENTRAN
  212.  
  213. If you want to contact SNOBOL4's authors, or ask about our enhanced versions
  214. for MS-DOS, 386 systems, Unix, or Macintosh, here's how to reach us:
  215.  
  216. Catspaw, Inc.            Voice: 719-539-3884,  8 am-5 pm (GMT-7)
  217. P.O. Box 1123            Fax: 719-539-4830
  218. Salida, Colorado 81201   Internet: cats!support@cs.arizona.edu
  219. USA                      uucpnet: ...{uunet,allegra,noao}!arizona!cats!support
  220. <
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. Welcome to SNOBOL4!
  233. -------------------
  234.  
  235. This concludes our brief tour of the SNOBOL4 language.
  236.  
  237. Would you like to type a few statements interactively, or return to DOS?  In
  238. keeping with the spirit of this demo, we'll use a SNOBOL4 statement to read
  239. and process your response to this question.
  240.  
  241. Enter "Y" to try out a few statements, or anything else to return to DOS.
  242.  
  243. *    Input ("Y" | "y")                       :S(next)F(end)
  244. Interactive Statement Execution
  245. -------------------------------
  246.  
  247. Here are some guidelines to get you started:
  248.  
  249. 1. Begin all statements with blank or tab (to skip the label field).
  250. 2. Surround all operators (=, +, -, etc.) with a blank on both sides.
  251. 3. This Vanilla version of SNOBOL4 does not support real numbers.
  252. 4. You can display data by assigning values to the variable named Output.
  253. 5. You can read data from the keyboard by accessing the variable named Input.
  254. 6. Don't try to use statement labels or program Gotos at this time.  Just
  255.    concentrate on writing simple statements.
  256. 7. SNOBOL4 is supposed to be fun, so don't get frustrated!  This demonstration
  257.    can't substitute for reading the tutorial in the manual on the disk.
  258.  
  259. Here are some samples to get you started.  SNOBOL4 will prompt with a "?", and
  260. will display "Success" or "Failure" after each statement.
  261.  
  262. ?    Output = 3 + 4
  263. <
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. ?    b = "blue" "bird"
  275. ?    output = b
  276. ?    b ("gold" | "blue") . Output ("fish" | "bird") . Output
  277.  
  278.  
  279. OK!  You've got the helm.  Type END at the left margin to terminate, or type
  280. Control-C.
  281.  
  282. &
  283.