Bug Hunt
Time limit1sMemory limit128 MB
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 are the integers from to , 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 and an array b of length 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 and , 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 lines, and no line exceeds 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 within each program. If the program has no bug, output . The output must not contain any extra characters such as spaces.