9. Warnings
Ограничения: время – 2s/4s, память – 32MiB Ввод: input.txt или стандартный ввод Вывод: output.txt или стандартный вывод
Послать решение Blockly Посылки Темы Где Обсудить (0)
You are to write the part of a compiler that generates warning messages. The programming language is a simplifed
Basic where the only statements are assignments, SUB, WHILE, IF-THEN-ELSE
constructions and RETURN.
Variable names are limited to single upper case letters, and they're always 32-bit signed integers.
Given a syntactically correct subroutine written in this language, generate the appropriate warning messages as
described below.
The syntax of the language only allows one statement per line. Lines will not have leading or trailing spaces,
but tokens may be separated by several spaces. Formally, the syntax for each line is as follows:
<line> ::= <head> | END SUB | <assignment> | <if> | ELSE | END IF | <while> | WEND | RETURN
<head> ::= SUB <name> (<paramlist>) | SUB <name>
<assignment> ::= <variable> = <expr>
<if> ::= IF <expr> THEN
<while> ::= WHILE <expr> DO
<paramlist> ::= <variable> | <variable>,<paramlist>
<expr> ::= <value> | <expr> <operator> <value>
<value> ::= <variable> | <integer> | (<expr>)
<operator> ::= + | - | * | / | < | > | <= | >= | = | <>
<variable> ::= A | B | … | Z
<integer> ::= A 32-bit signed integer
<name> ::= An identificator from upper case letters
The first line in the subroutine will always be the subroutine head and the last line will always contain
a END SUB statement. No other line may contain a subroutine head and END SUB, although RETURN
statements can occur elsewhere as well.
Furthermore, each IF-statement has a corresponding END IF-statement and an optional ELSE-statement
in between. Each WHILE-statement has a corresponding WEND-statement. The IF and WHILE
statements may be nested, but then they will always be properly nested.
You should generate three different warnings, where appropriate. These are: (quotes for clarity only)
"Line <line>: unreachable code"
"Line <line>: variable <variable> might not have been initialized"
"Line <line>: parameter <variable> is never used"
The first warning should be given on lines which are never executed, no matter how the IF-statements are evaluated.
Each such line will only receive this warning, and no other warnings. Lines containing ELSE, END IF, WEND
and END SUB statements will never receive this warning (these lines don't render into executable code by the
compiler).
The second warning should be given on lines where the value of a variable is used, but that variable might
not have been assigned a value yet. A variable will have a value assigned to it if it's in the parameter
list in the subroutine head, or once it's been assigned a value by an assignment statement.
The third warning should be given on lines containing END SUB for a variable which is in the parameter list
in the subroutine head, but which is never used in the subroutine body.
When determining which warnings to give, you should assume that all IF and WHILE statements
can evaluate to either true or false, no matter which instructions have previously been executed.
The warnings should be sorted primarily by line number. If a line contains several different variables
that all might be uninitialized, those warning messages should be sorted alphabetically by the variable name.
The input file contains a number of subroutines. Lengths of all lines are not more 100 characters.
Write into the output file all founded warnings. If there are no warnings in the input file,
write the message "No warnings".
Sample Input
SUB FIRST(A,B)
IF A>5 THEN
C = B*A
END IF
D= B - C
Z=Y+X
E=T
F=E+E*A
V=G/(G+1)
END SUB
SUB SECOND(G)
RETURN
B=G
RETURN
END SUB
Sample Output
Line 5: variable C might not have been initialized
Line 6: variable X might not have been initialized
Line 6: variable Y might not have been initialized
Line 7: variable T might not have been initialized
Line 9: variable G might not have been initialized
Line 13: unreachable code
Line 14: unreachable code
Line 15: parameter G is never used