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, high-level 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, if...goto, 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 if...goto statement and the unconditional goto statement to alter the flow of control during program execution. If the condition in the if...goto statement is true, control is transferred to the specified line of the program. The following relational operators are valid in an if...goto 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: 10 rem 11 rem print the maximum of two numbers 12 rem 20 data 1030 rem 31 rem get values 32 rem 40 input x 50 input y 60 rem 61 rem check x > y 62 rem 70 if x > y goto 11180 rem 81 rem y is maximum, print y 82 rem 90 print y 100 goto 130110 rem 111 rem x is maximum, print x 112 rem 120 print x 130 data 20900 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, i.e., no leading whitespace. Execution of a Simple program starts at the first line. In our example program above, this is line 10, 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 11 and 12 and we reach line 20, 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 20 and 30-32 and we approach line 40, 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 (e.g., in a "loop"). Line 40 is the first input command to be executed, so it acquires its value from the first data command (line 20) and stores the value 10 in the variable x. Line 50, our second input command to be executed, acquires its value from the next data command (line 130) and stores the value 20 in the variable y. Execution continues through the documentation lines 60-62 and we approach line 70, an if...goto command. if...goto commands are always of the form if goto where and are always either a single variable or constant (i.e., not expressions), is one of the relational operators listed above, and is a line number appearing in the program. Line 70 tests x > y. In our example program, this is false, so execution continues to line 80. Had the test been true, then execution would have continued at line 111. Note that line 111 is a rem command. Simple programs may branch (if...goto or goto) to rem and/or data lines, despite the fact that these lines are not executed. Execution continues through lines 80-82 and we approach line 90, a print command. print commands print either a single variable or constant. In our example program, the number 20 is printed and execution continues to line 100, a goto command, which branches (unconditionally) to line 130, our second data command. Since data commands are not executed, we proceed to line 900, an end command where program execution is terminated. As a final example, consider the following Simple program that accepts a non-negative integer x and computes/prints the sum of integers from 1 through x.10 rem sum 1 to x 15 input x 20 data 1025 if x <=-1 goto 6030 rem add x to total 35 let t = t + x 40 let x = x -145 rem loop x 50 goto 2555 rem output result 60 print t 99 end As always, execution starts with first line of the program. The first executable statement is line 15, an input command, which stores the value 10(from t

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!