280 likes | 350 Views
SNOBOL4 . CSC 507 Hwan-Jun, Lee 11/29/2005. Content of Presentation. Introduction Significant Features of SNOBOL Pattern Matching Primitive Functions Predicates Programmer-Defined Functions Array, Table and Defined Data Types. Introduction.
E N D
SNOBOL4 CSC 507 Hwan-Jun, Lee 11/29/2005
Content of Presentation • Introduction • Significant Features of SNOBOL • Pattern Matching • Primitive Functions • Predicates • Programmer-Defined Functions • Array, Table and Defined Data Types
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 )
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.
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
Pattern Matching • Pattern Matching Statement • No explicit pattern matching operator • Form : subject pattern • Example : TRADE = ‘PROGRAMMER’ TRADE ‘GRAM’
Pattern Matching cont’ • Replacement Statement • Form : subject pattern = object • Example : WORD = ‘GRID’ WORD ‘I’ = ‘OU’ * ‘I’ is replaced by ‘OU’ WORD => ‘GROUD’
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.
Pattern Matching cont’ • Concatenation – Concatenate strings or patterns. P6 = P4 P5 No explicit operator Expressions are separated by one or more blanks.
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
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 • Example: BR = (‘B’ | ‘R’) . FIRST (‘E’ | ‘EA’) (‘D’ | ‘DS’) . LAST BED BR = FIRST ‘I’ LAST ( => replacement ) BED changed into BID
Pattern Matching cont’ • Primitive Functions • LEN(integer) – matches any string of specified length. • SPAN(string) - matches runs of characters • BREAK(string) – matches everything upto. • Ex : ‘IT RUNS.’ BREAK(‘ ‘) SPAN(‘ ‘) BREAK(‘.’) ‘.’ => Pattern matches.
Pattern Matching cont’ • Primitive Functions • ANY(string) – matches any single chracters EX : ANY(‘AEIOU’) =>match any vowel same as ‘A’ | ‘E’ | ‘I’ | ‘O’ | ‘U’ • NOTANY(string) • TAB(interger) – matches all characters from the current cursor position up to integer
Pattern Matching cont’ • Example : reformatting 1290 SEP. 27 1293 MAY 20 to SEP. 27, 1290 MAY 20, 1293 ***************************************************************************************** &ANCHOR = 1 DATE = LEN(4) . YR ‘ ‘ LEN(4) . MO ‘ ‘ LEN(2) . DAY LOOP CARD = INPUT :F(END) CARD DATA = MO ‘ ‘ DAY ‘, ‘ YR ‘ ‘ :F(ERROR) OUTPUT = CARD :(LOOP) ERROR OUTPUT = CARD ‘ IMPROPERLY FORMATTED.’ :(LOOP) END *****************************************************************************************
Primitive Functions • LEN(), SPAN(), BREAK(), ANY(), NOTANY(), TAB() • IDENT(arg1,arg2) • Argument can be any type • On success, arg1 becomes the value of function call • On fail, function call fails. • IDENT(‘APPLE’) => 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) => 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
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)
Programmer-Defined Functions • call primitive function DEFINE() to define programmer-defined function. • DEFINE(PROTOTYPE, ENTRYLABEL) • DEFINE() must be executed before the call is made. • Example : • DEFINE(‘F(X,Y)L1, L2’, ‘FENTRY’) • DEFINE(‘G(Z)’, ‘GENT’) • DEFINE(‘MARK()’)
Programmer-Defined Functionscont’ • Before execution of function begins • The name of the function • All formal arguments • All local variables are saved • 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 • Formal arguments are assigned their values • All local variables are assigned null
Programmer-Defined Functionscont’ • Example: 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 = UNION CH :(ULOOP)
Programmer-Defined Functionscont’ • Example : Decimal to Binary Conversion, Using Recursion DEFINE(‘BINARY(N)’) :F(BINEND) BINARY BINAY = GT(N,1) BINARY(N/2) REMDR(N,2) + :S(RETURN) BINARY = N :(RETURN) BINEND • GT(N,1) TESTS THE CASES N = 0 OR 1 • N / 2 RETURNS INTEGER
Array, Table and Defined Data Types • ARRAY(prototype, initValue) • Indexing starts at 1 • Each element may have any type ( integer, pattern.. • VECTOR = ARRAY(10) => one-dimensional array of length 10, null • LINE = ARRAY(‘-5:5’) => one-dimensional array, upper bound : 5, lower bound : -5 • BOARD = ARRAY(‘3,3’,’X’) => three-by-three array • A1 = ARRAY(5) A2 = ARRAY(5, A1) Each element of A2 has the same array A1. • OUPUT = BOARD<2,3> =>prints out the value of 2,3 element
Array, Table and Defined Data Types • ARRAY • 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. A = ARRAY(3) A<2> = ‘TWO’ B = COPY(A) B<2> = ‘SIX’ => A and B point two distinct physical objects
Array, Table and Defined Data Types • TABLE(N,M) • 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 ) • N – initial size of the table • M - # of additional variables if needed • TABLE(20, 15) =>20, 35, 50,… • T = TABLE() • T<‘A’> = 3
Array, Table and Defined Data Types • Programmer-Defined Data Type • DATA(P) defines a new data type. • Example: DATA(‘COMPLEX(R,I)’) => creates a new type COMPLEX which has two fields (R, I) => 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
SNOBOL4 Interpreters • The Minnesota SNOBOL4 Interpreter • Runs SNOBOL4 under the IBM Disk Operating System (IBM DOS), MS DOS, OS/2, Windows 95 and others.
Reference • http://www.snobol4.org/ • http://www.sachsdavis.clara.net • http://www.atariarchives.org • http://calico.org/journalarticles