Under construction

Still being built. Things may change

Bug Hunt

Time limit1sMemory limit128 MB

Summary
Simulate a small array based language line by line and report the first line where an out of range index or an unassigned array element is used.
Level

Medium5 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

In this problem we consider a simple programming language that has only declarations of one-dimensional integer arrays and assignment statements. Your task is to find a bug in a given program.

The syntax of the language is given in BNF as follows.

       <program> ::= <declaration> | <program><declaration> | <program><assignment>
   <declaration> ::= <array name>[<number>]<new line>
    <assignment> ::= <array name>[<expression>]=<expression><new line>
    <expression> ::= <number> | <array name>[<expression>]
        <number> ::= <digit> | <digit_positive><digit_string>
  <digit_string> ::= <digit> | <digit><digit_string>
<digit_positive> ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
         <digit> ::= 0 | <digit_positive>
    <array_name> ::= a | b | c | d | e | f | g | h | i | j | k | l | m |
                     n | o | p | q | r | s | t | u | v | w | x | y | z |
                     A | B | C | D | E | F | G | H | I | J | K | L | M |
                     N | O | P | Q | R | S | T | U | V | W | X | Y | Z

Here <new line> is a single newline character (LF). The only characters that appear in a program are alphabetic letters, decimal digits, =, [, ], and newline characters; no other characters occur.

A declaration declares an array and specifies its length. The valid indices of an array of length nn are the integers from 00 to n−1n - 1, inclusive. Array names are case sensitive, so array a and array A are different arrays. Each element of a freshly declared array is initially undefined. For example, an array a of length 1010 and an array b of length 55 are declared as follows.

a[10]
b[5]

An expression evaluates to a non-negative integer. A <number> is read as a decimal integer, and <array name>[<expression>] evaluates to the value stored in the element of that array whose index is the value of <expression>. An assignment stores the value of the right-hand side into the array element named on the left-hand side. Some example assignments are shown below.

a[0]=3
a[1]=0
a[2]=a[a[1]]
a[a[0]]=a[1]

A program is executed from the first line, one line at a time. You may assume that every array is declared exactly once before any of its elements is assigned to or read.

Given a program, find the following bugs.

  • An array is accessed with an invalid index.
  • In an assignment, an array element that has not yet been assigned is read, either as an array index or as the value to be stored.

You may assume that no other kind of bug (such as a syntax error) occurs, and that every integer written as a <number> is between 00 and 231−1=21474836472^{31} - 1 = 2147483647, inclusive.

Input

The input consists of several datasets, followed by a line that contains only a single . (period). Each dataset is a program that is likewise followed by a line containing only a single . (period). A program has at most 10001000 lines, and no line exceeds 8080 characters, excluding the newline character.

Output

For each program, output the line number of the assignment in which the first bug occurs. Line numbers start at 11 within each program. If the program has no bug, output 00. The output must not contain any extra characters such as spaces.

Examples1

  1. Example 1

    Input
    a[3]
    a[0]=a[1]
    .
    x[1]
    x[0]=x[0]
    .
    a[0]
    a[0]=1
    .
    b[2]
    b[0]=2
    b[1]=b[b[0]]
    b[0]=b[1]
    .
    g[2]
    G[10]
    g[0]=0
    g[1]=G[0]
    .
    a[2147483647]
    a[0]=1
    B[2]
    B[a[0]]=2
    a[B[a[0]]]=3
    a[2147483646]=a[2]
    .
    .
    
    Expected output
    2
    2
    2
    3
    4
    0