![Page 1: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/1.jpg)
SNOBOL 4
By: Bahman Ravaei 790649 Farhad Rad 116703
نیمسال 81-82-2
![Page 2: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/2.jpg)
Introduction to the snobol 4 P.L
• Developed at BELL laboratories in 1962• Is a string of characters (character oriented)• Define other data type• Consist of a sequence of statement• Array and Table have more flexibility • Function calls can be made recursively• Link list node and complex number are
possible programmer define data type
![Page 3: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/3.jpg)
four basic type of statements:
i) the Assignment statementii) the Pattern matching
statementsiii) the Replacement statementiv) the End statement
![Page 4: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/4.jpg)
Assignment statements:
• the simple type of assignment:variable = value
example:v = 5
• the value may be given an experssion example:
W = 14 + (16 - 10)which assigns the value 20 to the variable w
![Page 5: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/5.jpg)
• “BLANK are required around arithmetic operators”
• The value may be string of characters example:
v = ‘dog’
![Page 6: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/6.jpg)
Arithmetic: i) Integers
ii) Real
arithmetic operation is “addition +” ,”subtraction -” ,”multiplication * ” ,”division / ” and “exponentiation ** or !”
blanks are required between the binary operators and their operands ,Unary operator such as the minus sign must be adjacent to their operands.
example: Q2 = -P / -N
![Page 7: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/7.jpg)
Predicates of arithmetic operations:
-Unary operators -Exponentiation -Multiplication and Division -Addition and Subtraction
All arithmetic operations associate to the left except exponentiation .
example : 2 ** 3 ** 5is equivalent to :
2 ** (3 ** 5)
![Page 8: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/8.jpg)
Example :m = 4n = 5p = n * m / (n - 1)
assign the value 5 to pExample :
pi = 3.14159mul = 2. * pi * 5.
assign Real value to pi and mul
![Page 9: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/9.jpg)
Pattern matching statements:
Define : The operation of examining for occurrence of specified substring
Pattern matching have two type: i ) the pattern matching statement ii ) the replacement statement
![Page 10: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/10.jpg)
the pattern matching statement:
• the pattern matching statement has the form :
subject patterntwo fields separated by at least one blank
example: trade = ‘programmer ’ trade ‘gram’
• Pattern can an expression
row = ‘k’ no. = 20
‘kk2429’ row no. + 4
pattern is equivalent by ‘k24’
![Page 11: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/11.jpg)
Replacement statements
• The replacement statement has the form
subject pattern = object
• If the pattern matching operation succeeds the subject string is modified by replacing the matched substring by the object .example:
WORD = “GRID”WORD ‘I’ = ‘OU’
thus WORD is equivalent GROUD
![Page 12: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/12.jpg)
If pattern not found the statement failed example:
WORD ‘AB’ = ‘OU’example:
HAND = ‘AC4DAHDKS’RANK = 4SUIT = ‘D’HAND RANK SUIT = ‘AS’
first RANK and SUIT concatenate that it’s 4D and later HAND ‘4D’ = ‘AS’
example :HAND RANK SUIT =
![Page 13: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/13.jpg)
Patterns • Two operation available for
constructing complex patterns:i) alternation ii) concatenation
alternation is indicated by expression of the form:
P1 | P2example:
KEYWORD = ‘COMPUTER’ | ‘PROGRAM’KEYWORD = KEYWORD | ‘ALGORITM’
![Page 14: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/14.jpg)
can use KEYWORD to pattern field:
TEXT = ‘PROGRAMMING ALGORITM FOR COMPUTERS ‘
TEXT KEYWORD =after matching:TEXT = ‘MING ALGORITM FOR COMPUTERS’
![Page 15: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/15.jpg)
Concatenation• Concatenation of two patterns ,P1
and P2 ,is specified in the same way as the concatenation of two strings:
P1 P2example:
BASE = ‘BINARY’ | ‘DECIMAL’ | ‘HEX’SCALE = ‘FIXED’ | ‘FLOAT’ATTRIBUTE = SCALE BASE
![Page 16: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/16.jpg)
Array: • Arrays are created by function ARRAY
.ARRAY (p , e)
example :board = ARRAY (‘3,3’ , ‘x’)x x X
X x x
x x x
![Page 17: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/17.jpg)
Example :A1 = ARRAY ( 5 )A2 = ARRAY (5 , A1)
A2 A1
![Page 18: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/18.jpg)
Flow of control
A SNOBOL program is a sequence of statements terminated by an end statement . labels and goto provide to control the flow of the program.
Example of label:START TEXT = INPUT
the end statement is distinguished by the label END
![Page 19: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/19.jpg)
Transfer to a labelled statement is specified in the goto field which may appear at the end of a statement and is separated from the rest of the statement by a colon ‘ : ’.
Two types of transfers can be specified in the goto field :
i ) conditionalii ) unconditional
![Page 20: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/20.jpg)
conditional A conditional transfer consists of a
label enclosed within parentheses preceded by an F or S corresponding to Failure
or Success.example :
TEXT = INPUT :F(DONE)example :
LOOP PUNCH = INPUT S:LOOPEND
![Page 21: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/21.jpg)
we can use F and S in one statementexample:
COLOR = ‘RED’ | ‘GREEN’ | ‘BLUE’BRIGHT TEXT COLOR = :S(BRIGHT)F(BLAND)...BLAND …….
![Page 22: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/22.jpg)
unconditional
An unconditional transfer is indicated by the absence of an F or S before the enclosing parentheses .
example:LOOP PUNCH = INPUT :F(END)
OUTPUT = PUNCH :(LOOP)END
![Page 23: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/23.jpg)
Indirect reference Indirect referencing is indicated by
the unary operator $.examples:i) MONTH = ‘APRIL’
$MONTH = ‘CRUEL’is equivalent:
APRIL = ‘CRUEL’ii) WORD = ‘RUN’
$(WORD ‘:’) = $(WORD ‘:’) + 1iii) $(‘A’ | ‘B’)
iv) N = N + 1 :($(‘PHASE’ N))
![Page 24: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/24.jpg)
Functions:
i) primitive functionsii) predicatesiii) defined functions
![Page 25: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/25.jpg)
Primitive function
Many snobol procedure are invoked by function built into the system ,called primitive functions. Few example of primitive function:i) SIZE (string)
APE = ‘SIMIAN’OUTPUT = SIZE(APE)
ii) DUPL (string ,integer)DUPL(‘/*’,5)
iii) REPLACE( TEXT,CH1,CH2)STATEMENT = ‘A(I,J) = A(I,J) + 3’OUTPUT = REPLACE(STATEMENT,’()’,’<>’)
print the line :A<I,J> = A<I,J> + 3
![Page 26: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/26.jpg)
Predicate: A function or operation that returns the
NULL string if a given condition is satisfied.
Example: i) LE(n1 , n2 ) PUNCH = LE( SIZE(TEXT),80) TEXT ii) LT( n1 , n2 ) ADD N = LT( N , 50 ) N+1 iii) DIFFER ( st1 , st2 ) OUTPUT = DIFFER ( FIRST , SECOND ) FIRST
SECOND iv) LGT ( st1 , st2 )
![Page 27: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/27.jpg)
Defined function:
A function defined by Programmer. The primitive function DEFINE to specify
the function name , formal arguments , local variables, and the entry point of the function.
Example:
DEFINE ( ‘ DELETE ( STRING , CHAR )’ , ‘D1’ ) D1 STRING CHAR = :S ( D1 )
DELETE = STRING : ( RETURN )
![Page 28: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/28.jpg)
Example:
DEFINE ( ‘ DELETE ( STRING , CHAR )’ ) DELETE STRING CHAR = :F ( FRETURN)
D2 STRING CHAR = :S ( D1 )
DELETE = STRING : ( RETURN )
![Page 29: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/29.jpg)
Programmer define data type:
The primitive function DATA can be used to difine the new data type.
Example: DATA( ‘ NODE ( VALUE , LINK)’)
P = NODE (‘ S ‘ , ) P = NODE (‘ T ‘ , P ) P = LINK ( P )
P ‘S ’ NULL ‘T‘
![Page 30: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/30.jpg)
Program format: Example: OUTPUT = ‘ THE TOTAL NUMBER OF OCCURRENCE IS’
+ SUM< N >
Example: X = 2 ; Y = 3 ; Z = 10
* : THIS IS A COMMENT
![Page 31: SNOBOL 4 By: Bahman Ravaei 790649 Farhad Rad 116703 81-82-2 نیمسال](https://reader036.vdocument.in/reader036/viewer/2022062417/5519c04e55034660578b4b4c/html5/thumbnails/31.jpg)
End