



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Immediate, Value, Assignment, Accomplished, Input, Output, Indirect, Reference, Primitive, Function
Typology: Exercises
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Immediate Value Assignment
The „.‟ (dot) operator is used for conditional assignment only when the entire pattern is matched.
The $ is used for immediate value assignment even if the entire pattern does not match. It is used as follows:
Pattern $ Variable
In this case whenever pattern matches a substring, the substring immediately becomes the new value of the Variable. This is demonstrated with the help of the following example.
BR = ( ( „B‟ | „R‟ ) $ FIRST ( „E‟ | „EA‟ ) $ SECOND
Value assignment is done for those parts of the pattern that match, even if the overall match failed.
So the following statement
„BREAD‟ BR
will result in the following matches.
FIRST – B, FIRST – R, SECOND – E, SECOND – EA, THIRD – D, BRVAL - READ
Note that FIRST and SECOND get a value as soon as a match is made and hence are assigned values more than once. Of course only the last value will be the value of these variables at the end of entire match.
Let us now try make the following match.
„BEATS‟ BR
In this case the statement fails as the whole pattern is not matched but parts of it are matched and hence get the values as shown below.
FIRST – „B‟ ,SECOND – „E‟, SECOND – „EA‟
Input and Output
The I/O is accomplished through INPUT and OUTPUT statements. We will look at examples of I/O in the next sections.
Control Flow
Control flow is achieved through the „go to field‟ in the statement body as shown in the following examples.
The first example simply keeps on reading the entire line from the INPUT and stops when there is end of input.
LOOP PUNCH = INPUT :S(LOOP) END
The next example reads a line and prints it on the output and then goes back to read the next lines again.
LOOP PUNCH = INPUT :F(END) OUTPUT = PUNCH :(LOOP) END
Here is a more interesting example that uses control flow. In this example it continues to delete a matched pattern in the subject and stops when no match is found.
TEXT = “REDPURPLEYELLOWBLUE”
COLOR = „RED‟ | „BLUE‟ | „GREEN‟ BASIC TEXT COLOR = :S(BASIC) F(OTHER) OTHER
In the following example all the numbers for 1 to 50 are added and the result is stored in SUM and is printed on the output.
SUM = 0 N = 0 ADD N = LT(N, 50) N + 1 :F(DONE) SUM = SUM + N :(ADD) DONE OUTPUT = SUM
Indirect Reference
Indirect reference is also an interesting feature of SNOBOL. It is similar to a pointer in concept but there are certain differences as well. These differences will be highlighted in the following code segments. Let us first look at the syntax:
The unary operator „$‟ is used for indirect reference. Note that „$‟ has been overloaded in this case. This says that now instead of using the operand as the variable, use its value as the variable. The rest will remain the same. Here is an example that elaborates this concept:
MONTH = „APRIL‟ $MONTH = „FOOL‟
In this case all occurrences of ST1 in STRING will be replaced by ST2. ST1 and ST must have the same number of characters. The corresponding characters of ST1 will be replaced by characters corresponding to them in ST2. This is shown in the following example.
TEXT = “A(I,J) = A(I,J) + 3” OUTPUT = REPLACE(TEXT , ‟()‟ , „<>‟)
The output will thus display:
A<I,J> = A<I,J> + 3
Arrays SNOBOL4 has array data type just like other languages. However, unlike most languages, data stored in an array is not necessarily of the same type. That is, each element can be of different data type.
The following statement creates and assigns to V a one-dimensional array of 10 elements, each assigned to the real value of 1.0.
V = ARRAY(10, 1.0)
The next statement creates a one-dimensional array X of 8 elements with the lower index being 2 and the upper index being 9. As there is no initial value given, each cell is initialized to NULL string.
X = ARRAY(„2:9‟)
The following statement creates a two dimensional array of 3 x 5 and each cell is initialized to NULL string.
N = ARRAY(„3,5‟)
The size of the array can be determined at run time by using input from the system of using a variable. Following example shows this concept:
A = ARRAY(INPUT)
In this case size and initial value of the array is determined by the input.
Following example creates an array whose size is determined at run time. It then stores values in it from the INPUT. It is important to note here that when the array index „I‟ gets a value, as the result of increment in the second last statement, greater than the value of the last index of the array, the statement with Label MORE fails and the control goes to the Label GO. Arrays &TRIM = 1 I = 1 ST = ARRAY(INPUT) MORE ST = INPUT :F(GO) I = I + 1:(MORE)
The following example uses arrays in an interesting manner:
&TRIM = 1 WORDPAT = BREAK(&LCASE &UCASE) SPAN(&LCASE &UCASE "'-"). WORD COUNT = ARRAY('3:9',0) READ LINE = INPUT :F(DONE) NEXTW LINE WORDPAT = :F(READ) COUNT<SIZE(WORD)> = COUNT<SIZE(WORD)>+ 1 :(NEXTW) DONE OUTPUT = "WORD LENGTH NUMBER OF OCCURRENCES" I = 2 PRINT I = I + 1 OUTPUT = LPAD(I,5) LPAD(COUNT,20) :S(PRINT) END
The first statement simply deletes leading spaces from the input.
In the second statement, a pattern WORDPAT is defined. It uses tow primitive functions BREAK and SPAN. BREAK takes the cursor to the first lowercase or uppercase character in the subject. The SPAN provides a mechanism to keep moving as long as we get uppercase, lowercase, or a hyphen in the subject string. So this pattern scans the subject string for complete words. Since we have associated variable WORD with this pattern, the matched value will be stored in it.
In the third statement an array COUNT is created whose first index is 3 and last index is
Fourth statement reads a line from the input. At the end of the input, the control the statement fails and the control goes to DONE.
In the fifth statement WORDPAT is matched in the LINE and match is replaced by the NULL string. The matched value is also stored in WORD. When there is no match found, the statement fails and the control goes to READ to read the next line from the input.
In the sixth statement, the size of the word is used as the array index and the value is incremented at that index. If it is less than 3 or greater than 9 then nothing happen. In fact the statement fails in this case but since there is unconditional jump, it goes back to read the next word from the input.
The rest of the statements are used to display the values at each array index. That is, this program simply counts the occurrences of words of lengths between 3 and 9 in the input and displays that number.
Tables A table data structure maps pairs of associated data objects. Tables have varying lengths and are dynamically extended if necessary.
T = TABLE()
creates a table of unspecified length.
GOTO control structure Overloaded operators. For example, the use of a blank as a concatenation operator caused confusions in coding and maintenance. Users had to be bewared of the proper use of the blank space operator, and not to confuse it with other operators.
To top it all the compiler was not sophisticated. Any string of characters was a legal SNOBOL program, and the compiler would accept everything as long as it was a string. The legend says that Griswold accidentally gave a non-SNOBOL program to the SNOBOL compiler, and the program was successfully compiled!