P1 - Quarta 1. public void nextToken() { char ch; while ( (ch = input[tokenPos]) == ' ' || ch == '\r' || ch == '\t' || ch == '\n') tokenPos++; if ( ch == '\0') token = Symbol.EOF; else if ( ch >= '0' && ch <= '9' ) { // get a number valorNumero = 0; int i = 0; while ( Character.isDigit( input[tokenPos] ) ) { valorNumero = valorNumero*10 + (input[tokenPos] - ´0´); tokenPos++; i++; } token = Symbol.NUMERO; if ( i > 5 ) error.signal("Number out of limits"); } else { tokenPos++; switch ( ch ) { case '+' : token = Symbol.PLUS; break; case '*' : token = Symbol.MULT; break; default : if ( ch >= 'a' && ch <= 'z' ) { token = Symbol.LETRA; valorIdent = ch; } else error.signal("Invalid Character: '" + ch + "'"); } } } 2. a) class Program { private Vector arrayVariable; private CompositeComand compositeCommand; } class PrintCommand extends Command { private Vector exprList; } class Variable { private String name; private Type type; } b) private Program program() { Vector arrayVariable = new Vector(); while ( token == Symbol.INTEGER || token == Symbol.BOOLEAN ) arrayVariable.addElement( varDec() ); return new Program( arrayVariable, compositeCommand() ); } private PrintCommand printCommand() { Vector exprList = new Vector(); Expr e; while ( token != Symbol.SEMICOLON ) { exprList.addElement( e = orExpr() ); if ( e.getType() != Type.integerType ) error("Somente expressões inteiras podem ser impressas"); } return new PrintCommand( exprList ); } private Variable varDec() { Variable v; Type t = type(); if ( token != Symbol.IDENT ) error(); String name = stringValue; if ( symbolTable.get(name) != null ) error("Variável " + name " já foi declarada"); nextToken(); symbolTable.put(name, v = new Variable(name, t)); return v; } 3. void genC() { Enumeration e = exprList.elements(); while ( e.hasMoreElements() ) { Expr expr = (Expr ) e.nextElement(); sop( "printf\"\%d\\n\", "); expr.genC(); sop( ");"); } }