Download presentation
Presentation is loading. Please wait.
Published byMadlyn Campbell Modified over 9 years ago
1
SNOBOL4 CSC 507 Hwan-Jun, Lee 11/29/2005
2
Content of Presentation Introduction Significant Features of SNOBOL Pattern Matching Primitive Functions Predicates Programmer-Defined Functions Array, Table and Defined Data Types
3
Introduction SNOBOL : StriNg Oriented symBOlic Language Developed by Ralph Griswold aad associates at Bell Labs (where C/C++) in 1963 SNOBOL is a special purposed language developed to provide a powerful means for the manipulation of strings of symbols Evolved throughout 1960s ( latest version SNOBOL4 )
4
Evolution of PLs
5
Significant Features of SNOBOL String Manipulation Operations - allow a string to be tested and make replacement. Built-In string pattern-matching – examine the occurrence of specified pattern. Dynamically Typed – No type declaration, No restrictions on the data type of the value of any variable.
6
Significant Features of SNOBOL cont’ Interpretive language – compiler translates the program into a notation that interpreter execute. Garbage-collected Suitable for text-manipulation and processing Pattern matching, goto and label
7
Pattern Matching Pattern Matching Statement No explicit pattern matching operator Form : subject pattern Example : TRADE = ‘PROGRAMMER’ TRADE ‘GRAM’
8
Pattern Matching cont’ Replacement Statement Form : subject pattern = object Example : WORD = ‘GRID’ WORD ‘I’ = ‘OU’ * ‘I’ is replaced by ‘OU’ WORD => ‘GROUD’ WORD => ‘GROUD’
9
Pattern Matching cont’ Alternation - Builds a new pattern from its alternatives. P3 = P1 | P2 Builds a new pattern and assigns it as the value of P3.
10
Pattern Matching cont’ Concatenation – Concatenate strings or patterns. P6 = P4 P5 No explicit operator Expressions are separated by one or more blanks. Expressions are separated by one or more blanks.
11
Pattern Matching cont’ Example P = ‘BE’ | ‘BEA’ | ‘BEAR’ Q = ‘RO’ | ‘ROO’ | ROOS’ R = ‘DS’ | ‘D’ S = ‘TS’ | ‘T’ PAT = P R | Q S ( same as PAT = (P R) | (Q S), concatenation has higher precedence than alternation ) PAT matches the followings BEDS BED BEADS BEAD BEARDS BEARD ROTS ROOT ROT ROOSTS ROOTS ROOST ROTS ROOT ROT ROOSTS ROOTS ROOST
12
Pattern Matching cont’ Conditional value Assignment BR = (‘B’ | ‘R’). FIRST Upon successful completion of pattern matching, the substring matched becomes the values of FIRST Immediate Value Assignment BR = ‘B’ $ FIRST ‘E’ $ SECOND Whenever ‘B’ matches, the substring immediately becomes the value of FIRST Whenever ‘B’ matches, the substring immediately becomes the value of FIRSTExample: BR = (‘B’ | ‘R’). FIRST (‘E’ | ‘EA’) (‘D’ | ‘DS’). LAST BED BR = FIRST ‘I’ LAST ( => replacement ) BED changed into BID
13
Pattern Matching cont’ Primitive Functions LEN(integer) – matches any string of LEN(integer) – matches any string of specified length. specified length. SPAN(string) - matches runs of characters SPAN(string) - matches runs of characters BREAK(string) – matches everything upto. BREAK(string) – matches everything upto. Ex : Ex : ‘IT RUNS.’ BREAK(‘ ‘) SPAN(‘ ‘) BREAK(‘.’) ‘.’ => Pattern matches.
14
Pattern Matching cont’ Primitive Functions ANY(string) – matches any single chracters ANY(string) – matches any single chracters EX : ANY(‘AEIOU’) =>match any vowel same as ‘A’ | ‘E’ | ‘I’ | ‘O’ | ‘U’ same as ‘A’ | ‘E’ | ‘I’ | ‘O’ | ‘U’ NOTANY(string) NOTANY(string) TAB(interger) – matches all characters from the current cursor position up to integer TAB(interger) – matches all characters from the current cursor position up to integer
15
Pattern Matching cont’ Example : reformatting1290 SEP. 27 reformatting1290 SEP. 27 1293 MAY 20 toSEP. 27, 1290 toSEP. 27, 1290 MAY 20, 1293 ***************************************************************************************** &ANCHOR = 1 &ANCHOR = 1 DATE = LEN(4). YR ‘ ‘ LEN(4). MO ‘ ‘ LEN(2). DAY DATE = LEN(4). YR ‘ ‘ LEN(4). MO ‘ ‘ LEN(2). DAY LOOP CARD = INPUT:F(END) CARD DATA = MO ‘ ‘ DAY ‘, ‘ YR ‘ ‘ :F(ERROR) CARD DATA = MO ‘ ‘ DAY ‘, ‘ YR ‘ ‘ :F(ERROR) OUTPUT = CARD:(LOOP) OUTPUT = CARD:(LOOP) ERROR OUTPUT = CARD ‘ IMPROPERLY FORMATTED.’ :(LOOP) END*****************************************************************************************
16
Primitive Functions LEN(), SPAN(), BREAK(), ANY(), NOTANY(), TAB() IDENT(arg1,arg2) Argument can be any type Argument can be any type On success, arg1 becomes the value of function call On success, arg1 becomes the value of function call On fail, function call fails. On fail, function call fails. IDENT(‘APPLE’) => fails IDENT(‘APPLE’) => fails IDENT(3, 3.0) => fails IDENT(3, 3.0) => fails DIFFER(‘BCD’,’B’ ‘CD’) =>fails SIZE(‘abc’) => 3 NEW_S = REPLACE(S, ‘.,;:?!’, ’ ‘) => replace all punctuation marks in S with blanks and return it. NEW_S = TRIM(‘ABC ‘) => remove trailing blanks. DUPL(‘ABC’, 10) => duplicate ‘ABC’ 10 times. OUPUT = DUPL(DUPL(‘ ‘,10 – SIZE(‘ABC’)) ‘ABC’, 5) OUPUT = DUPL(DUPL(‘ ‘,10 – SIZE(‘ABC’)) ‘ABC’, 5) => 5 lines of “ ABC” OUTPUT = REMDR(15,2) => prints 1 OUTPUT = REMDR(15, -4) => prints 3 (the sign of REMDR is the sign of f.a
17
Predicates Testing relations b/w arguments On success, null for the call GE(17.0, ‘3’) => succeeds LT(17.0, ‘3’) => fails EQ(2) => fails, EQ(2, NULL) = EQ(2, 0) Numerical predicates are used “for loop” LOOP N = LT(N, M) N + 1:S(LOOP)F(OUT)
18
Programmer-Defined Functions call primitive function DEFINE() to define programmer-defined function. programmer-defined function. DEFINE(PROTOTYPE, ENTRYLABEL) DEFINE() must be executed before the call is made. Example : DEFINE(‘F(X,Y)L1, L2’, ‘FENTRY’) DEFINE(‘F(X,Y)L1, L2’, ‘FENTRY’) DEFINE(‘G(Z)’, ‘GENT’) DEFINE(‘G(Z)’, ‘GENT’) DEFINE(‘MARK()’) DEFINE(‘MARK()’)
19
Programmer-Defined Functions cont’ Before execution of function begins The name of the function The name of the function All formal arguments All formal arguments All local variables are saved All local variables are saved It is possible for a function to be recursive It is possible for a function to be recursive New values are assigned to variables as follows: The name of the function is assigned null The name of the function is assigned null Formal arguments are assigned their values Formal arguments are assigned their values All local variables are assigned null All local variables are assigned null
20
Programmer-Defined Functions cont’ Example: DEFINE(‘UNION(X,Y)CH’,’UN’) DEFINE(‘UNION(X,Y)CH’,’UN’)…… ZSET Z = SET1 UNION(SET2, SET3) SET4 …… UN UNION = X ULOOP Y LEN(1). CH =:F(RETURN) UNION BREAK(CH):S(ULOOP) UNION BREAK(CH):S(ULOOP) UNION = UNION CH:(ULOOP) UNION = UNION CH:(ULOOP)
21
Programmer-Defined Functions cont’ Example : Decimal to Binary Conversion, Using Recursion DEFINE(‘BINARY(N)’) :F(BINEND) DEFINE(‘BINARY(N)’) :F(BINEND) BINARY BINAY = GT(N,1) BINARY(N/2) REMDR(N,2) + :S(RETURN) BINARY = N :(RETURN) BINARY = N :(RETURN)BINEND GT(N,1) TESTS THE CASES N = 0 OR 1 GT(N,1) TESTS THE CASES N = 0 OR 1 N / 2 RETURNS INTEGER N / 2 RETURNS INTEGER
22
Array, Table and Defined Data Types ARRAY(prototype, initValue) Indexing starts at 1 Indexing starts at 1 Each element may have any type ( integer, pattern.. Each element may have any type ( integer, pattern.. VECTOR = ARRAY(10) VECTOR = ARRAY(10) => one-dimensional array of length 10, null LINE = ARRAY(‘-5:5’) LINE = ARRAY(‘-5:5’) => one-dimensional array, upper bound : 5, lower bound : -5 => one-dimensional array, upper bound : 5, lower bound : -5 BOARD= ARRAY(‘3,3’,’X’) BOARD= ARRAY(‘3,3’,’X’) => three-by-three array => three-by-three array A1 = ARRAY(5) A1 = ARRAY(5) A2 = ARRAY(5, A1) A2 = ARRAY(5, A1) Each element of A2 has the same array A1. OUPUT = BOARD =>prints out the value of 2,3 element OUPUT = BOARD =>prints out the value of 2,3 element
23
Array, Table and Defined Data Types ARRAY A = ARRAY(3) A = ARRAY(3) B = A => A and B have the same array ( like pointers pointing at the same object) COPY function produces a copy of an array. COPY function produces a copy of an array. A = ARRAY(3) A = ‘TWO’ B = COPY(A) B = ‘SIX’ => A and B point two distinct physical objects
24
Array, Table and Defined Data Types TABLE(N,M) Default of N,M : 10 Default of N,M : 10 Similar to a one-dimensional array but variable in a table can be referenced by an y data object. ( array – integer reference ) Similar to a one-dimensional array but variable in a table can be referenced by an y data object. ( array – integer reference ) N – initial size of the table N – initial size of the table M - # of additional variables if needed M - # of additional variables if needed TABLE(20, 15) =>20, 35, 50,… TABLE(20, 15) =>20, 35, 50,… T = TABLE() T = TABLE() T = 3 T = 3
25
Array, Table and Defined Data Types Programmer-Defined Data Type DATA(P) defines a new data type. DATA(P) defines a new data type. Example: Example:DATA(‘COMPLEX(R,I)’) => creates a new type COMPLEX which has two fields (R, I) two fields (R, I) => No limit to the number of fields. => No limit to the number of fields. C = COMPLEX(1.5, 2.0) A = R(C) => the first field is referenced R(C) = 3.2 => assign value to the first field
26
SNOBOL4 Interpreters The Minnesota SNOBOL4 Interpreter Runs SNOBOL4 under the IBM Disk Operating System (IBM DOS), MS DOS, OS/2, Windows 95 and others. Runs SNOBOL4 under the IBM Disk Operating System (IBM DOS), MS DOS, OS/2, Windows 95 and others.
27
Reference http://www.snobol4.org/http://www.sachsdavis.clara.nethttp://www.atariarchives.orghttp://calico.org/journalarticles
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.