Question: Before we begin building the compiler, we discuss a simple, yet powerful, high - level programming language called Simple. Every Simple statement consists of a
Before we begin building the compiler, we discuss a simple, yet powerful, highlevel programming language called Simple. Every Simple statement consists of a line number and a Simple instruction. Line numbers must appear in ascending order. Each instruction begins with one of the following Simple commands: rem, input, data, let, print, goto, ifgoto, or end. Simple evaluates only integer expressions using the arithmetic operators and The operators have the same precedence as in C Parentheses can be used to change the order of evaluation of an expression. All variables in a Simple program are lower case single letters. Simple uses only integer variables and constants. Simple does not have variable declarations merely mentioning a variable name in a program causes the variable to be declared and initialized to zero automatically. Simple uses the conditional ifgoto statement and the unconditional goto statement to alter the flow of control during program execution. If the condition in the ifgoto statement is true, control is transferred to the specified line of the program. The following relational operators are valid in an ifgoto statement: or The precedence of these operators is the same as in C Consider the following Simple program that accepts two input values and prints the maximum of the two: rem rem print the maximum of two numbers rem data rem rem get values rem input x input y rem rem check x y rem if x y goto rem rem y is maximum, print y rem print y goto rem rem x is maximum, print x rem print x data end Each line in a Simple program must start with a line number. The line numbers must appear in ascending order and must always start in the leftmost column, ie no leading whitespace. Execution of a Simple program starts at the first line. In our example program above, this is line a rem command. rem commands are used to document the program and are not executed. Anything appearing after the word rem is simply ignored. Execution continues through the other documentation lines and and we reach line a data command. data commands, like rem commands are not executed. However, unlike rem commands, data commands do serve a purpose explained below data commands always end with a single integer. Execution proceeds through lines and and we approach line an input command. input commands acquire a value from a data command and store that value in its variable. The very first input command that is executed uses the very first data command in the program. Subsequently executed input commands use the remaining data commands in the order in which they appear in the program. data commands are never used more than once. The same input command will consume many data commands if it is executed many times eg in a "loop" Line is the first input command to be executed, so it acquires its value from the first data command line and stores the value in the variable x Line our second input command to be executed, acquires its value from the next data command line and stores the value in the variable y Execution continues through the documentation lines and we approach line an ifgoto command. ifgoto commands are always of the form if goto where and are always either a single variable or constant ie not expressions is one of the relational operators listed above, and is a line number appearing in the program. Line tests x y In our example program, this is false, so execution continues to line Had the test been true, then execution would have continued at line Note that line is a rem command. Simple programs may branch ifgoto or goto to rem andor data lines, despite the fact that these lines are not executed. Execution continues through lines and we approach line a print command. print commands print either a single variable or constant. In our example program, the number is printed and execution continues to line a goto command, which branches unconditionally to line our second data command. Since data commands are not executed, we proceed to line an end command where program execution is terminated. As a final example, consider the following Simple program that accepts a nonnegative integer x and computesprints the sum of integers from through x rem sum to x input x data if x goto rem add x to total let t t x let x x rem loop x goto rem output result print t end As always, execution starts with first line of the program. The first executable statement is line an input command, which stores the value from t
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
