Correção da Primeira Prova de Compiladores Primeiro Semestre de 2006 Só as questões cujas respostas não estão na apostila estão resolvidas. 1. a) abstract class Command { abstract public void genC(); } class ForEachCommand extends Command { private Variable x, v; private Command command; public void genC() { sop("{"); ArrayType t = (ArrayType ) v.getType(); sop( " for( int _i = 0; _i < " + (t.getLimSuperior() - t.getLimInferior()) + "; _i++) {"); sop(" " + x.getName() + " = " + v.getName() + "[_i];"); command.genC(); sop(" }"); sop("}"); } } b) public void forEachCommand() { nextToken(); if ( lexer.token != Symbol.IDENT ) error(); Variable x = (Variable ) st.getInLocal(lexer.getStringValue()); if ( x == null ) error(); Type type_x = x.getType(); if ( type_x != Type.integerType || type_x != Type.charType || type_x != Type.booleanType ) error(); lexer.nextToken(); if ( lexer.token != Symbol.IN ) error(); lexer.nextToken(); if ( lexer.token != Symbol.IDENT ) error(); Variable v = (Variable ) st.getInLocal(lexer.getStringValue()); if ( v == null ) error(); if ( !( v.getType() instanceof ArrayType) ) error(); ArrayType t = (ArrayType ) v.getType(); if ( x.getType() != t.getElemType() ) error(); lexer.nextToken(); if ( lexer.token != Symbol.DO ) error(); lexer.nextToken(); return new ForEachCommand(x, v, command()); } 2. a) public class SymbolTable { private Hashtable globalTable, classTable, localTable; public Object getInGlobal(String name); public Object getInClass(String name); public Object getInLocal(String name); public Object get(String name); public Object putInGlobal(String name, Object obj); public Object putInClass(String name, Object obj); public Object putInLocal(String name, Object obj); } b) Veja a apostila 3. void nextToken() { while ( in[p] == ' ' || in[p] == '\n' || in[p] == '\r' ) p++; if ( in[p] == '/' && in[p+1] == '/' ) { while ( in[p] != '\n' ) p++; nextToken(); } if ( Character.isDigit(in[p]) reconheceDigito(); else switch( in[p] ) { case '+' : token = Symbol.PLUS; p++; break; case '<' : p++; if ( in[p] == '=' ) { token = Symbol.LE; p++; } else token = Symbol.LT; break; ... } }