An Introduction To Snobol-Modern Programing Language-Lecture Handout, Exercises of Programming Languages

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: Snobol, Introduction, Language, Manipulation, Polenksy, Grammers, String, Designing, Interpretting, Expressions

Typology: Exercises

2011/2012

Uploaded on 08/04/2012

dhanvin
dhanvin 🇮🇳

4.2

(14)

108 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
An Introduction to SNOBOL(Lecture 9-12)
Introduction
SNOBOL stands for StriNg Oriented SymBOlic Language. It is a Special purpose
language for string manipulation and handling. It was developed in 1962 at the Bell Labs
by Farber, Griswold, and Polensky.
It was created out of the frustrations of working with the languages of that time because
they required the developers to write large programs to do a single operation. It was used
to do most of the work in designing new software and interpreting different language
grammars. Toward the end of the eighties, newer languages were created which
could also be used for string manipulation.
They include the PERL and AWK. Unlike SNOBOL, Perl and Awk use regular
expressions to perform the string operations. Today, roughly forty years after its release,
the language is rarely used!
SIMPLE DATA TYPES
Initial versions of SNOBOL only supported Integers and Strings as the basic data types.
Real numbers were originally not allowed but were added to the language later on.
Integers
Both positive and negative integers were supported. Following are some examples of
integers in SNOBOL.
14 -234 0 0012 +12832 -9395 +0
It was illegal to use a decimal point or comma in a number. Similarly, numbers greater
than 32767 (that is, 215 1) could not be used as well. For the negative numbers, the
minus sign has to be used without any spaces between the sign and the number.
Following are therefore some examples of illegal numbers:
13.4 49723 - 3,076
As mentioned earlier, real numbers were allowed in the later versions of the language and
hence 13.4 is a legal number in SNOBOL4.
Strings
The second basic data type supported by the language is a string. A string is represented
by a sequence of characters surrounded by quotes (single of double). The maximum
length allowed for a string is 5,000 characters. Examples of strings are: “This is a string”,
„this is also a string‟,
“it‟s an example of a string that contains a single quote”
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download An Introduction To Snobol-Modern Programing Language-Lecture Handout and more Exercises Programming Languages in PDF only on Docsity!

An Introduction to SNOBOL(Lecture 9-12)

Introduction

SNOBOL stands for StriNg Oriented SymBOlic Language. It is a Special purpose language for string manipulation and handling. It was developed in 1962 at the Bell Labs by Farber, Griswold, and Polensky.

It was created out of the frustrations of working with the languages of that time because they required the developers to write large programs to do a single operation. It was used to do most of the work in designing new software and interpreting different language grammars. Toward the end of the eighties, newer languages were created which could also be used for string manipulation. They include the PERL and AWK. Unlike SNOBOL, Perl and Awk use regular expressions to perform the string operations. Today, roughly forty years after its release, the language is rarely used!

SIMPLE DATA TYPES

Initial versions of SNOBOL only supported Integers and Strings as the basic data types. Real numbers were originally not allowed but were added to the language later on.

Integers

Both positive and negative integers were supported. Following are some examples of integers in SNOBOL.

14 -234 0 0012 +12832 -9395 +

It was illegal to use a decimal point or comma in a number. Similarly, numbers greater than 32767 (that is, 2^15 – 1) could not be used as well. For the negative numbers, the minus sign has to be used without any spaces between the sign and the number. Following are therefore some examples of illegal numbers:

13.4 49723 - 3,

As mentioned earlier, real numbers were allowed in the later versions of the language and hence 13.4 is a legal number in SNOBOL4.

Strings

The second basic data type supported by the language is a string. A string is represented by a sequence of characters surrounded by quotes (single of double). The maximum length allowed for a string is 5,000 characters. Examples of strings are: “This is a string”, „this is also a string‟, “it‟s an example of a string that contains a single quote”

SIMPLE OPERATORS

SNOBOL supports the usual arithmetic operators. This includes +, -, *, and /. In addition it also supports the exponentiation operator which is ** or !. The unary – and + are also supported. = operator is used for assignment. The precedence of the operators is as follows: the unary operations are performed first, then exponentiation, then multiplication, followed by division, and finally addition and subtraction. All arithmetic operations are left associative except the exponentiation which is right associative. Therefore, 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) which is 512 and not (2 ** 3) ** 2 which would be 64. Like most languages, parentheses can be used to change the order of evaluation of an expression.

Just like C, if both the operands of an operator are integers then the result is also an integer. Therefore, 5 / 2 will be evaluated to be 2 and not 2.5.

VARIABLE NAMES

A variable name must begin with an upper or lower case letter. Unlike C, SNOBOL is case insensitive. That is, the literals Wager and WAGER represent the same variable. If the variable name is more than one character long, the remaining characters may be any combination of letters, numbers, or the characters period „.‟ and underscore „_‟. The name may not be longer than the maximum line length (120 characters). Following are some of the examples of variable names:

WAGER P23 VerbClause SUM.OF.SQUARES Verb_Clause

SNOBOL4 STATEMENT Structure

A SNOBOL statement has three components. The structure of a statement is as follows:

Label Statement body :GOTOField

Label The must begin in the first character position of a statement and must start with a letter or number.

The GOTO Field

The goto field is used to alter the flow of control of a SNOBOL program. A goto filed has the following forms:

:(label) :S(label) :F(label) :S(label1) F(label2)

The first one is an command for unconditional jump to the label specified within parentheses. The second is a command for jump to the specified label if the statement in the body of the statement was executed successfully or resulted in true Boolean value. The third one is opposite to the second and the control jumps to the label if the statement

Now a value of -39.4 will be assigned to x.

Strings Concatenation

SNOBOL was designed mainly for manipulating and handling strings. It has therefore rich string manipulation mechanism. The first one is string concatenation. A Space is used as an operator for concatenation as shown below:

TYPE = ‘Semi’ OBJECT = TYPE ‘Group’

The above set of statements will assign „SemiGroup‟ to the variable OBJECT. Numbers can also be concatenated with strings, producing interesting results. For example, consider the following code segment:

ROW = ‘K’ NO. = 22 SEAT = ROW NO.

In this case NO. is concatenated with ROW and a value of „ K22’ is assigned to SEAT.

The Space operator has a lower precedence than the arithmetic operators and hence we can have arithmetic expressions in string concatenation. For example,

SEAT = ROW NO. + 6 / 2

will assign the value „ K25’ to SEAT if ROW and No. have the values „K‟ and 22 respectively.

Pattern Matching Pattern matching and manipulation is another very important feature of SNOBOL. The first statement in this regards is the Pattern Matching Statement. Once again Space is used as the pattern matching operator and the statement has the following form:

subject pattern

Note that there is no assignment operator in this case. Both the subject and the pattern are strings and this statement tries to match the pattern in the subject. It will be successful if the match is found and will result in failure otherwise. This is demonstrated with the help of the following example:

TRADE = „PROGRAMMER‟ PART = „GRAM‟ TRADE PART

Now „GRAM‟ will be searched in „PROGRAMMER‟. Since it is present in it PROGRAM („MER‟), it will result in success.

We can use Space for string concatenation as well as for pattern matching in the same statement. In this case, the first space is used as pattern matching and the second one is

used as concatenation with concatenation taking precedence over pattern matching. This is shown with the help of the following example:

ROW = „K‟ NO. = 2 „LUCK22‟ ROW NO.

In this case, NO. is first concatenated with ROW resulting in „K2‟ which will then be matched in „LUCK22‟. Once again the match will be successful.

Replacement

Replacement statement is used in conjunction with the pattern matching statement in the following manner.

subject pattern = object

In this case the pattern is searched in the subject and if found it is replaced by the object as demonstrated by the following example:

SENTENCE = „THIS IS YOUR PEN‟ SENTENCE „YOUR‟ = „MY‟

Since „YOUR‟ is present in the subject, it will be replaced by „MY‟, resulting in changing the value of SENTENCE to „THIS IS MY PEN‟.

If we now have the following statement

SENTENCE „MY‟ =

then SENTENCE will be further modified to „THIS IS PEN‟ as we are now replacing „MY‟ with a NULL string, effectively deleting „MY‟ from the subject.

Pattern

There are two type of statements for pattern building. These are Alternation and Concatenation.

Alternation

Vertical bar is used to specify pattern alternation as shown in the example below.

P1 | P

This is example of a pattern that will match either P1 or P2.

Here are some more examples:

KEYWORD = „INT‟ | „CHAR‟

TEXT = “INT INT CHAR CHAR INT”

TEXT KEYWORD =

In this case, the variable K which was associated with the pattern will get the value INT.

Here is another example.

Let us define the following pattern where BRVAL has been associated to this pattern by the dot operator.

BR = ( „B‟ | „R‟ ) ( „E‟ | „EA‟ ) ( „D‟ | „DS‟ ). BRVAL

Associates variable BRVAL with the pattern BR

It may be noted that the pattern BR is created by concatenating three different alternations as shown below.

BR = ( „B‟ | „R‟ ) ( „E‟ | „EA‟ ) ( „D‟ | „DS‟ )

Note that it is equivalent to

BR = „BED‟ | „BEDS‟ | „BEAD‟ | „BEADS‟ | „RED‟ | „REDS‟ | „READ‟ |

  • „READS‟

This „+‟ in the first column of a line states that this is continuation of the last line and not a new statement. Anyway, let us come back to our example.

On successful completion of matching, the entire substring matched will be assigned as value of the BRVAL

So „BREADS‟ BR

will assign „READS‟ to BRVAL.

Let us now define the BR pattern as shown below and also associate FIRST, SECOND, and THIRD to the respective constituent sub-patterns of this larger pattern.

BR = ( ( „B‟ | „R‟ ). FIRST ( „E‟ | „EA‟ ). SECOND

  • ( „D‟ | „DS‟ ). THIRD). BRVAL

On successful completion of matching, the entire substring matched will be assigned as value of the BRVAL. In addition B or R will become the value of FIRST, E or EA will be assigned to SECOND, and THIRD will get either D or DS.

So

„BREADS‟ BR

will result in the following assignments.