#P2509. Static Analysis of a Simple Scripting Language

    ID: 15779 Type: Default 1000ms 256MiB

Static Analysis of a Simple Scripting Language

Static Analysis of a Simple Scripting Language

This problem involves a simple scripting language with only three kinds of statements: assignment, conditional (if) and return. Variables are single uppercase letters and all variables are 32‐bit signed integers. The program is given as a sequence of lines (with line numbers starting from 1) with no extra spaces. The first line is a head statement defining the parameters (using the PARAM keyword) and the last line is always a RETURN statement. An IF statement always has a matching END IF statement and may optionally have an ELSE part. Note that the language uses a simple BNF:

       ::=  |  |  | ELSE | END IF | 
       ::= PARAM  | PARAM
 ::=  = 
         ::= IF    THEN
     ::= RETURN 
  ::=  |  
     ::=  |   
      ::=  | 
   ::= + | - | * | /
   ::= 
   ::= A | B | ... | Z
    ::= a 32-bit signed integer without leading zero

You are to perform static analysis on a given program and output two kinds of warnings:

  • Unreachable Code: report any executable statement that cannot be reached regardless of the Boolean outcomes of any IF statements.
  • Potentially Uninitialized Variable: report if an executable statement uses a variable that is not declared as a parameter (in the head) and has not been assigned a value in any execution path leading to that statement. Note that if the statement is unreachable, no warning should be issued for it.
  • The keywords ELSE and END IF are structural and do not themselves trigger warnings.

    Assume that each IF condition can evaluate either to true or false independently. Your task is to analyze the program and output the warnings in the order in which they are detected.

    inputFormat

    The input consists of several lines forming the program. There are no blank lines. The first line is a PARAM statement (which may include one or more parameters) and the last line is a RETURN statement. Other lines can be assignment or IF constructs. Tokens in each line are separated by a single space. You should read until end-of-file.

    outputFormat

    Output the warnings, one per line, in the order they are detected. Use the following formats exactly (without quotes):
    Warning: Unreachable code at line X.
    Warning: Variable Y might be uninitialized at line X.
    If there are no warnings, output nothing.

    sample

    PARAM A B
    A = 1
    IF A < 5 THEN
    B = 2
    RETURN B
    ELSE
    B = 3
    RETURN B
    END IF
    A = 10
    Warning: Unreachable code at line 9.