import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;

class TokenTest {

    // table of variables named in input stream
    private static Hashtable vars = new Hashtable();

    public static void main(String[] args) {
	try {
	    InputStream in;
	    if (args.length < 1)
		in = System.in;
	    else
		in = new FileInputStream(args[0]);

	    StreamTokenizer tokens = new StreamTokenizer(in);
	    tokens.ordinaryChar('-'); // prevent interpretation as unary minus

	    tokens.eolIsSignificant(true);

	    while (tokens.nextToken() != StreamTokenizer.TT_EOF) {

		// read variable name
		if (tokens.ttype != StreamTokenizer.TT_WORD)
		    error(String.valueOf(tokens.lineno()) +
			": variable name expected");
		String name = tokens.sval;
		double value;
		Double valueObj = (Double)vars.get(name);
		if (valueObj == null)
		    value = 0.0;	// variables are inially zero
		else
		    value = valueObj.doubleValue();

		// read operator
		int op = tokens.nextToken();
		if ("+-=".indexOf(op) == -1)
		    error(String.valueOf(tokens.lineno()) +
			": missing or invalid operator");

		// read numeric operand
		if (tokens.nextToken() != StreamTokenizer.TT_NUMBER)
		    error(String.valueOf(tokens.lineno()) +
			": syntax error");

		switch (op) {		// perform indicated operation
		  case '+':
		    value += tokens.nval;
		    break;

		  case '-':
		    value -= tokens.nval;
		    break;

		  case '=':
		    value = tokens.nval;
		    break;

		  default:
		    error(String.valueOf(tokens.lineno()) +
			": invalid operator");
		}

		vars.put(name, new Double(value));  // store result in table

		if (tokens.nextToken() != StreamTokenizer.TT_EOL)
		    error(String.valueOf(tokens.lineno()) +
			": end of line expected");
	    }

	    // print final values of all variables
	    System.out.println("final values:");
	    Enumeration enum = vars.keys();
	    while (enum.hasMoreElements()) {
		String name = (String)enum.nextElement();
		System.out.println(name + " = " + vars.get(name));
	    }

	} catch (IOException e) {
	    error("I/O Exception: " + e);
	}
    }

    public static void error(String err) {
	System.err.println("TokenTest: " + err);
	System.exit(1); // non-zero argument means "not good"
    }
}
