Docsity

SNOBOL Features - Programming Languages - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download SNOBOL Features - Programming Languages - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Concatenation

P1 = ‘A’ | ‘B’

P2 = ‘C’ | ‘D’

P3 = P1 P

 ‘AC’ | ‘AD’ | ‘BC’ | ‘BD’

“ABCDEFGH” P

Conditional Assignment

  • assignment occurs ONLY if the entire pattern match is successful pattern. variable
  • Upon successful completion of pattern matching, the substring matched by the pattern is assigned to the variable as the value.

Conditional Assignment

BR = ( ‘B’ | ‘R’ ) ( ‘E’ | ‘EA’ ) ( ‘D’ | ‘DS’ )

BR = BED | BEDS | BEAD | BEADS |

+ RED | REDS | READ | READS

Conditional Assignment

BR = ( ‘B’ | ‘R’ ) ( ‘E’ | ‘EA’ ) ( ‘D’ | ‘DS’ ). BRVAL Associates variable BRVAL with pattern BR

  • On successful completion of the matching, the entire substring matched is assigned as value of the BRVAL

Control Flow

LOOP PUNCH = INPUT :S(LOOP)

END

LOOP PUNCH = INPUT :F(END)

OUTPUT = PUNCH :(LOOP)

END

Control Flow

TEXT “REDPURPLEYELLOWBLUE” COLOR = ‘RED’ | ‘BLUE’ | ‘GREEN’ BASIC TEXT COLOR = :S(BASIC)F(OTHER) OTHER

Indirect Reference

MONTH = ‘APRIL’

$MONTH = ‘FOOL’

Is equivalent to saying APRIL = ‘FOOL’

Indirect Reference

RUN = 10

WORD = ‘RUN’

$(WORD) = $(WORD) + 1

Functions

  • Primitive
  • User defined

Functions

SIZE(TEXT)

PART1 = “NUT”

OUTPUT = SIZE(PART1)

PART2 = “BOLT”

N = 64

OUTPUT = SIZE(‘PART’ N + 36)

Arrays

  • Data stored in an array is not necessarily of the same type. V = ARRAY(10, 1.0) - creates and assigns to V a one-dimensional array of 10 elements, each assigned to the real value of 1. X = ARRAY(‘2:9’) - creates a one-dimensional array X of 8 elements with the lower index being 2 and the upper index being 9 N = ARRAY(‘3,5’) - Creates a two dimensional array of 3 x 5. - Each cell is initialized to NULL string. A = ARRAY(INPUT) - Size and initial value of the array is determined by the input.

Arrays

&TRIM = 1

I = 1

ST = ARRAY(INPUT)

MORE ST = INPUT :F(GO)

I = I + 1 :(MORE)

GO

Example

&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

EXAMPLE - RPN

**&ANCHOR = 1; &TRIM = 1 PMPAT = (ARBNO(BAL ANY(‘+-’)) $ X FAIL |

  • DIFFER(X) TAB((SIZE(X) – 1))). X
  • LEN(1). OP REM. Y MDPAT = (ARBNO(BAL ANY(‘/*’)) $ X FAIL |
  • DIFFER(X) TAB((SIZE(X) – 1))). X
  • LEN(1). OP REM. Y STRIP = ‘(‘ BAL. POL ‘)’ RPOS(0) DEFINE( ‘POL(POL)X,Y,OP’) OUTPUT = ‘ INFIX’ DUPL(‘ ‘,26) ‘PREFIX’ OUTPUT = LOOP STRING = INPUT OUTPUT = STRING DUPL(‘ ‘,36 – SIZE(STRING)) POL(STRING) :(LOOP) POL POL STRIP :S(POL) POL PMPAT = OP ‘(‘ POL(X) ‘,’ POL(Y) ‘)’ :S(RETURN) POL MDPAT = OP ‘(‘ POL(X) ‘,’ POL(Y) ‘)’ :(RETURN) END**