代写C C++ Java Python 视频讲解客服在线客服在线

代做国外C C++ Java程序 Python QQ 2365427650 Email 2365427650@qq.com 免费咨询编程问题

« INTERNET SERVICES PROTOCOLSBlueJ environment. »

L language

Consider a simple programming language named L. The L programming language has the
following features:

CSE 465/565
L variables can store a string, a Boolean, or an integer value. A single variable can switch between
string, Boolean, and integer values during program execution. Assigning a value to a variable creates
that variable for future use. A runtime error occurs if a variable is used before it is given a value.
Boolean values are denoted by the reserved words TRUE and FALSE.

2
Variables are case sensitive and consist only of upper and lower-case letters.
The following are L reserved words: PRINT, FOR, TRUE, FALSE, WHILE, DO, LESSTHAN
plus the reserved symbols { and }.
LESSTHAN, WHILE,
and DO are only relevant for graduate students.
You may assume that any L program given as an input to your interpreter is syntactically correct.
The PRINT statement should display one variable’s value. Here is an example of a PRINT statement in
language
L:
PRINT apples ;
If cookies is an integer variable with value 4, the output of your interpreter should be:
apples=4
If cookies is a Boolean variable with value TRUE, the output of your interpreter should be:
apples=TRUE
If cookies is a string variable with value "red delicious", the output should be:
apples="red delicious"
The left-hand side of an assignment statement (i.e., =) is a variable.
The right-hand side of a simple assignment statement (i.e., =) is either a variable name, a signed integer,
string literal, or the Boolean values
TRUE or FALSE. If the right-hand side of the assignment is a
variable, then this variable must already have a value; otherwise the assignment should trigger a runtime
error to be reported by your interpreter. For example, the following are valid assignments:
B = 0 ;
A = 12 ;
A = B ;
A = "hello" ;
A = "12" ;
A = TRUE ;
The following is not a valid assignment, assuming C has no value yet:
A = C ;
There are three compound assignment statements: +=, *=, and &=. The meaning of these operators
depends on the data type of the left and right-hand side of the operator.
<string var> += <string> concat right string onto end of left string
<integer var> += <integer> increment left integer with value on right
<integer var> *= <integer> multiply left integer by value on right
<Boolean var> &= <Boolean> assign to left Boolean the result of performing
a Boolean AND operation between values on left

3
and right
Examples:
A = 4 ;
A += 34 ;
A *= B ;
C = "t" ;
C += "hello world" ;
D = FALSE ;
D &= TRUE ;
All other combinations (e.g., += with string var on the left and integer on the right) are illegal and should
cause a runtime error.
Every statement is terminated by a semi-colon.
L programs must have at least one space separating all lexical elements.
There is a for loop statement – FOR – whose body contains at least one simple statement (i.e., at least
one assignment), which is presented on one line. The keyword
FOR is followed by an integer constant,
which indicates the number of times to execute the loop. Following this number is the reserved symbol
{ followed by a sequence of statements defining the loop’s body and ending with the reserved symbol },
as done here:
FOR 5 { B += A ; A *= 2 ; }
L for loops can be nested and must appear on one line:
FOR 5 { B += A ; A *= 2 ; FOR 10 { A += B ; } A += 2 ; }
Here is an example L program:
A = 1 ;
B = 0 ;
FOR 5 { B += A ; A *= 2 ; }
A += 1000 ;
PRINT A ;
PRINT B ;
This program’s output is:
A=1032
B=31
As an example, an equivalent Python program would be (Do not translate L code into Python!):
A = 1
B = 0

4
for i in range(5):
B += A
A *= 2
A += 1000
print("A=" + str(A))
print("B=" + str(B))
Here is a second L program:
A = 10 ;
A += A ;
PRINT A ;
A = "hello" ;
A += A ;
PRINT A ;
A += 123 ;
PRINT A
The output to this second program would yield an error. Your program should display the line number of
this error and then stop processing:
A=20
A="hellohello"
RUNTIME ERROR: line 7
Requirements for graduate students only: Graduate students must additionally support:
○ An assignment statement of the form shown below, where the left-hand side is a Boolean variable:
<Boolean var> = <integer> LESSTHAN <integer>
Examples:
A = 5 ;
B = 4 ;
C = A LESSTHAN B ;
C = A LESSTHAN 0 ;
○ While loops, which are statements consisting of the reserved word WHILE followed by a Boolean
variable, followed by the reserved word
DO, and ending with the loop body. The loop body starts with
the reserved symbol
{, continues with a sequence of statements, end ends with the reserved symbol }. A
whole while loop statement must appear on the same line in an
L program.
Example:
A = 3 ;
B = TRUE ;

5
WHILE B DO { A += 5 ; B = A LESSTHAN 15 ; }

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Walle Build 100427

Copyright 代写C.