Home | Notes | Languages | Programs | Homework |
SIMPLE LANGUAGE DEFINITION |
General Description |
SIMPLE is a low-level, imperative language for manipulating a single data type, strings. A SIMPLE statement always occupies exactly one line of a SIMPLE program. There are no control structures, including subprograms. SIMPLE is designed so that a SIMPLE program can be interpreted line by line, with no line repeated after it has been interpreted once.
Syntax |
A string that is written out does not have double-quotes surrounding it. For example,
I am a SIMPLE programmer.
comment | Program documentation that has no SIMPLE semantics. |
concatenate | Concatenate two strings. |
exit | Exit the interpreter. |
reverse | Reverse a string. |
set | A SIMPLE assignment statement. |
split | Split one string by a second string. |
var | Declare an identifier for use in the SIMPLE program. |
write | Write a string to standard output. |
Semantics |
Every SIMPLE statement must have a keyword as the first field (there may be white space before the keyword), identifying the kind of statement. All subsequent fields must be separated by white space and the last field msy be followed by white space that extends to the end of the line. Extraneous fields are always an error. A line that is all white space is syntactically correct and semantically ignored.
A field denoted {string} (or {string1}, etc.) may be either a previously declared identifier or a string literal (i.e., enclosed in double-quotes).
comment {white space} {arbitrary text}The comment keyword is followed by white space and arbitrary text. The statement is otherwise ignored.
concatenate {identifier} {string1} {string2}The concatenate statement concatenates the two strings (i.e., {string1} followed by {string2}) and assigns the resulting string to {identifier}.
For example, the SIMPLE statement
concatenate xyz "September " "18, 2000"assigns the string "September 18, 2000" to the identifier xyz.
{white spaces} exit {white spaces}The exit statement marks the end of command input to the SIMPLE interpreter. The interpreter should terminate on parsing exit.
reverse {identifier} {string}The reverse statement reverses {string} and assigns the resulting string to {identifier}. For example, the SIMPLE statement
reverse abc "September "assigns the string " rebmetpeS" to the identifier abc.
set {identifier} {string}The set statement assigns {string} to {identifier}. For example, the SIMPLE statement
set mop "Logan"assigns the string "Logan" to the identifier mop.
split {identifier1} {identifier2} {string1} {string2}The split statement splits {string1} using {string2}) and assigns the resulting strings to {identifier1} and {identifier2}. In more detail, the first occurrence of {string2} in {string1}, if any, is located. The prefix of {string1} before that first occurrence is assigned to {identifier1}, while the suffix of {string1} after that first occurrence is assigned to {identifier2}. If there is no occurrence of {string2} in {string1}, then {string1} is assigned to {identifier1} and the empty string is assigned to {identifier2}.
For example, the SIMPLE statement
split xyz abc "ABAXAXAC" "AX"assigns the string "AB" to the identifier xyz and string "AXAC" to the identifier abc.
For another example, the SIMPLE statement
split ZOO BLUE "ABAXAXAC" "Ax"assigns the string "ABAXAXAC" to the identifier ZOO and string "" to the identifier BLUE (this shows that SIMPLE is case sensitive).
var {identifier}The var statement declares an identifier to be a variable of type string. The identifier cannot be a keyword. The statement also assigns an initial value to the identifier, the empty string (i.e., "").
For example, the SIMPLE statement
var xyzenters the identifier xyz in the symbol table for the interpreter if it is not already there; if it is already in the symbol table, the statement raises an error. The string initially assigned to xyz is the empty string.
write {string}The write statement writes {string} to standard output, followed by a newline. For example, the SIMPLE statements
set mop "Logan" write mopresults in the string "Logan" being written to standard output on a single line.
SIMPLE error: {descriptive error message}being written to standard output. The interpreter proceeds to the next line and continues interpretation; the line in error must not affect the interpretation of subsequent lines. (Notice that this sentence is a change from the original specification.) The descriptive error messages are not prescribed by the language definition; the designer of the interpreter chooses them.
Error categories include at least
An Example |
Interpretation of the following SIMPLE program
var firstname var lastname var name var street var town var state var zip var space set space " " set zip "24060" set state "virginia" set firstname "Tommie" set lastname "Loggerhead" set street "1212 Hopping Lane" set town "Blacksburg" concatenate firstname firstname space concatenate name firstname lastname write name write street concatenate town town "," concatenate town town space concatenate town town state concatenate town town space comment How long can this go on? concatenate town town zip write townresults in the following output to standard output:
Tommie Loggerhead 1212 Hopping Lane Blacksburg, virginia 24060
Home | Notes | Languages | Programs | Homework |