Difference between revisions of "PLC Project"
(30 intermediate revisions by the same user not shown) | |||
Line 20: | Line 20: | ||
* floating point numbers - <code>float</code> - sequence of digits containing a <code>'.'</code> character. | * floating point numbers - <code>float</code> - sequence of digits containing a <code>'.'</code> character. | ||
* booleans - <code>bool</code> - values: <code>true</code> and <code>false</code>. | * booleans - <code>bool</code> - values: <code>true</code> and <code>false</code>. | ||
− | * strings - <code>string</code> - text given in quotation marks: <code>"text"</code>. | + | * strings - <code>string</code> - text given in quotation marks: <code>"text"</code>. Escape sequences are optional in our strings. |
=== Variables === | === Variables === | ||
Line 35: | Line 35: | ||
* <code>read variable, variable, ... ;</code> - it reads values from standard input and then these values are assigned to corresponding variables. Each of these input values is on a separate line and it is verified, that have an appropriate type. | * <code>read variable, variable, ... ;</code> - it reads values from standard input and then these values are assigned to corresponding variables. Each of these input values is on a separate line and it is verified, that have an appropriate type. | ||
* <code>write expression, expression, ... ;</code> - it writes values of expressions to standard output. The <code>"\n"</code> character is written after the value of the last expression. | * <code>write expression, expression, ... ;</code> - it writes values of expressions to standard output. The <code>"\n"</code> character is written after the value of the last expression. | ||
− | * <code>{ statement | + | * <code>{ statement statement ... }</code> - block of statements. |
* <code>if (condition) statement [else statement] </code> - conditional statement - condition is an expression with a type: <code>bool</code>. The else part of the statement is optional. | * <code>if (condition) statement [else statement] </code> - conditional statement - condition is an expression with a type: <code>bool</code>. The else part of the statement is optional. | ||
* <code>while (condition) statement </code> - a cycle - condition must be a <code>bool</code> expression. This cycle repeats the given statement while the condition holds (it is <code>true</code>). | * <code>while (condition) statement </code> - a cycle - condition must be a <code>bool</code> expression. This cycle repeats the given statement while the condition holds (it is <code>true</code>). | ||
Line 67: | Line 67: | ||
|- | |- | ||
| relational operators | | relational operators | ||
− | | <code>< | + | | <code>< ></code> |
− | | <math>x \times x \rightarrow B,~where~x \in \{I, | + | | <math>x \times x \rightarrow B,~where~x \in \{I, F\}</math> |
|- | |- | ||
− | | | + | | comparison |
− | | <code> | + | | <code>== !=</code> |
− | | <math>S \times | + | | <math>x \times x \rightarrow B,~where~x \in \{I, F, S\}</math> |
+ | |- | ||
+ | | logic and, or | ||
+ | | <code>&&, <nowiki>|</nowiki><nowiki>|</nowiki> </code> | ||
+ | | <math>B \times B \rightarrow B </math> | ||
+ | |- | ||
+ | | logic not | ||
+ | | <code>!</code> | ||
+ | | <math>B \rightarrow B</math> | ||
|- | |- | ||
− | | | + | | assignment |
− | | <code> | + | | <code>=</code> |
− | | <math> | + | | <math>x \times x \rightarrow x,~where~x \in \{I, F, S, B\}</math> |
|} | |} | ||
+ | |||
+ | In the assignment, left operand is strictly a variable and the right operand is expression. The type of the variable is the type of the left operand. A side effect is storing the value on the right side into the variable. The automatic conversion cannot change the type of the variable, i.e., it is impossible to store <code>float</code> value in <code>int</code> variable. | ||
+ | |||
+ | We can '''use parentheses ''' in expressions. | ||
+ | |||
+ | All operators (except <code>=</code>) have left associativity (<code>=</code> have right associativity), and their priority is (from lowest to highest): | ||
+ | |||
+ | # <code>=</code> | ||
+ | # <code>||</code> | ||
+ | # <code>&&</code> | ||
+ | # <code> == !=</code> | ||
+ | # <code>< ></code> | ||
+ | # <code>+ - .</code> | ||
+ | # <code>* / %</code> | ||
+ | #<code>!</code> | ||
+ | #<code>unary -</code> | ||
== Sample Inputs == | == Sample Inputs == | ||
+ | |||
+ | Sample inputs: | ||
+ | * [http://linedu.vsb.cz/~beh01/wiki_data/PLC_t1.in Sample input 1] [http://linedu.vsb.cz/~beh01/wiki_data/PLC_t1.out Generated code for sample input 1] | ||
+ | * [http://linedu.vsb.cz/~beh01/wiki_data/PLC_t2.in Sample input 2] [http://linedu.vsb.cz/~beh01/wiki_data/PLC_t2.out Generated code for sample input 2] | ||
+ | * [http://linedu.vsb.cz/~beh01/wiki_data/PLC_t3.in Sample input 3] [http://linedu.vsb.cz/~beh01/wiki_data/PLC_t3.out Generated code for sample input 3] | ||
+ | |||
+ | Sample input contaning errors: | ||
+ | * [http://linedu.vsb.cz/~beh01/wiki_data/PLC_errors.in Sample errors] | ||
== Our (Stack-based) Instructions Set == | == Our (Stack-based) Instructions Set == | ||
+ | |||
+ | All instructions are stack based. The main memory is a stack and while evaluating the instructions, the input data are taken from stack and the results are put also in stack. | ||
+ | |||
+ | {| class="wikitable" | ||
+ | !Instruction | ||
+ | !Description | ||
+ | |- | ||
+ | | <code>add</code> | ||
+ | | binary <code>+</code> | ||
+ | |- | ||
+ | | <code>sub</code> | ||
+ | | binary <code>-</code> | ||
+ | |- | ||
+ | | <code>mul</code> | ||
+ | | binary <code>*</code> | ||
+ | |- | ||
+ | | <code>div</code> | ||
+ | | binary <code>/</code> | ||
+ | |- | ||
+ | | <code>mod</code> | ||
+ | | binary <code>%</code> | ||
+ | |- | ||
+ | | <code>uminus</code> | ||
+ | | unary <code>-</code> | ||
+ | |- | ||
+ | | <code>concat</code> | ||
+ | | binary <code>.</code> - concatenation od strings | ||
+ | |- | ||
+ | | <code>and</code> | ||
+ | | binary <code>&&</code> | ||
+ | |- | ||
+ | | <code>or</code> | ||
+ | | binary <code><nowiki>|</nowiki><nowiki>|</nowiki></code> | ||
+ | |- | ||
+ | | <code>gt</code> | ||
+ | | binary <code>></code> | ||
+ | |- | ||
+ | | <code>lt</code> | ||
+ | | binary <code><</code> | ||
+ | |- | ||
+ | | <code>eq</code> | ||
+ | | binary <code>==</code> - campares two values | ||
+ | |- | ||
+ | | <code>not</code> | ||
+ | | unary <code>!</code> - negating boolean value | ||
+ | |- | ||
+ | | <code>itof</code> | ||
+ | | Instruction takes int value from the stack, converts it to float and returns it to stack. | ||
+ | |- | ||
+ | | <code>push T x</code> | ||
+ | | Instruction pushs the value <code>x</code> of type <code>T</code>. Where <code>T</code> represents <code>I - int</code>, <code>F - float</code>, <code>S - string</code>, <code>B - bool</code>. Example: push I 10, push B true, push S "A B C " | ||
+ | |- | ||
+ | | <code>pop</code> | ||
+ | | Instruction takes on value from the stack and discards it. | ||
+ | |- | ||
+ | | <code>load id</code> | ||
+ | | Instruction loads value of variable <code>id</code> on stack. | ||
+ | |- | ||
+ | | <code>save id</code> | ||
+ | | Instruction takes value from the top of the stack and stores it into the variable with name <code>id</code> | ||
+ | |- | ||
+ | | <code>label n</code> | ||
+ | | Instruction marks the spot in source code with unique number <code>n</code> | ||
+ | |- | ||
+ | | <code>jmp n</code> | ||
+ | | Instruction jumps to the label defined by unique number <code>n</code> | ||
+ | |- | ||
+ | | <code>fjmp n</code> | ||
+ | | Instruction takes boolean value from the stack and if it is <code>false</code>, it will perform a jump to a label with unique number <code>n</code> | ||
+ | |- | ||
+ | | <code>print n</code> | ||
+ | | Instruction takes <code>n</code> values from stack and prints them on standard output | ||
+ | |- | ||
+ | | <code>read T</code> | ||
+ | | Instruction reads value of type <code>T</code> (<code>I - int</code>, <code>F - float</code>, <code>S - string</code>, <code>B - bool</code>) from standard input and stores in on the stack | ||
+ | |} |
Latest revision as of 08:34, 28 March 2024
Contents
Basic Description
Project will be composed from following steps:
- Using ANTLR, implement a parser for the language specified bellow. If there is at least one syntax error, report this error (or errors) and stop the computations.
- If there are no syntactic errors, perform the type checking. If there are some type errors, report all these errors and stop the computation.
- If there are no type errors, generate appropriate target code. It will be a text file composed from stack-based instructions that are defined bellow.
- Implement an interpreter, that gets a text file with these instructions and evaluates them.
Language Specification
Program's formatting
The program consists of a sequence of commands. Commands are written with free formatting. Comments, spaces, tabs, and line breaks serve only as delimiters and do not affect the meaning of the program. Comments are bounded by two slashes and the end of the line. Keywords are reserved. Identifiers and keywords are case sensitive.
Literals
There are following literals:
- integers -
int
- sequence of digits. - floating point numbers -
float
- sequence of digits containing a'.'
character. - booleans -
bool
- values:true
andfalse
. - strings -
string
- text given in quotation marks:"text"
. Escape sequences are optional in our strings.
Variables
Variable's identifiers are composed from letters and digits, and it must start with a letter. Each variable must be declared before it is used. Repeated declaration of a variable with the same name is an error. Variables must have one of the following types: int
, float
, bool
or string
. After the variables are declared, they have initial values: 0
, 0.0
, ""
respectively false
.
Statements
Following statements are defined:
;
- empty command.type variable, variable, ... ;
- declaration of variables, all these variables have the same typetype
. It can be one of:int
,float
,bool
,String
expression ;
- it evaluates given expression, the resulting value of the expression is ignored. Note, there can be some side effects like an assignment to a variable.read variable, variable, ... ;
- it reads values from standard input and then these values are assigned to corresponding variables. Each of these input values is on a separate line and it is verified, that have an appropriate type.write expression, expression, ... ;
- it writes values of expressions to standard output. The"\n"
character is written after the value of the last expression.{ statement statement ... }
- block of statements.if (condition) statement [else statement]
- conditional statement - condition is an expression with a type:bool
. The else part of the statement is optional.while (condition) statement
- a cycle - condition must be abool
expression. This cycle repeats the given statement while the condition holds (it istrue
).
Expression
Lists in expressions trees are literals or variables. Types of operands must preserve the type of the operator. If necessary, int
values are automatically cast to float
. In other word, the type of 5 + 5.5
is float
, and number 5
which type int
is automatically converted to float
. There is no conversion from float
to int
!
Following table defines operators in our expressions. Operator Signature is defined using letters: I, R, B, S which corespods to types: int
, float
, bool
, string
.
Description | Operator | Operator's Signature |
---|---|---|
unnary minus | -
|
|
binary arithmetic operators | +, -, *, /
|
|
modulo | %
|
|
concatenation of strings | .
|
|
relational operators | < >
|
|
comparison | == !=
|
|
logic and, or | &&, ||
|
|
logic not | !
|
|
assignment | =
|
In the assignment, left operand is strictly a variable and the right operand is expression. The type of the variable is the type of the left operand. A side effect is storing the value on the right side into the variable. The automatic conversion cannot change the type of the variable, i.e., it is impossible to store float
value in int
variable.
We can use parentheses in expressions.
All operators (except =
) have left associativity (=
have right associativity), and their priority is (from lowest to highest):
=
||
&&
== !=
< >
+ - .
* / %
!
unary -
Sample Inputs
Sample inputs:
- Sample input 1 Generated code for sample input 1
- Sample input 2 Generated code for sample input 2
- Sample input 3 Generated code for sample input 3
Sample input contaning errors:
Our (Stack-based) Instructions Set
All instructions are stack based. The main memory is a stack and while evaluating the instructions, the input data are taken from stack and the results are put also in stack.
Instruction | Description |
---|---|
add
|
binary +
|
sub
|
binary -
|
mul
|
binary *
|
div
|
binary /
|
mod
|
binary %
|
uminus
|
unary -
|
concat
|
binary . - concatenation od strings
|
and
|
binary &&
|
or
|
binary ||
|
gt
|
binary >
|
lt
|
binary <
|
eq
|
binary == - campares two values
|
not
|
unary ! - negating boolean value
|
itof
|
Instruction takes int value from the stack, converts it to float and returns it to stack. |
push T x
|
Instruction pushs the value x of type T . Where T represents I - int , F - float , S - string , B - bool . Example: push I 10, push B true, push S "A B C "
|
pop
|
Instruction takes on value from the stack and discards it. |
load id
|
Instruction loads value of variable id on stack.
|
save id
|
Instruction takes value from the top of the stack and stores it into the variable with name id
|
label n
|
Instruction marks the spot in source code with unique number n
|
jmp n
|
Instruction jumps to the label defined by unique number n
|
fjmp n
|
Instruction takes boolean value from the stack and if it is false , it will perform a jump to a label with unique number n
|
print n
|
Instruction takes n values from stack and prints them on standard output
|
read T
|
Instruction reads value of type T (I - int , F - float , S - string , B - bool ) from standard input and stores in on the stack
|