1
a)
Program program() {
Vector v = new Vector();
v.addElement(
command() );
while ( token ==
'a' || token == 'b' || token == 'f' ||
token ==
'd' || token == 'r' )
v.addElement(
command() );
return new
Program(v);
}
Command command() {
if ( token == 'r' )
return
repeatCommand();
else
return
simpleCommand();
}
BackCommand backCommand() {
nextToken();
return new
BackCommand(number());
}
RepeatCommand repeatCommand() {
nextToken();
int n = number();
return new
RepeatCommand(n, simpleCommand());
}
b)
public class Program {
private Vector
v;
}
abstract public class Command { }
public BackCommand extends Command
{
private int n;
}
public RepeatCommand extends
Command {
private int n;
private Command
simpleCommand;
}
2)
-
caráter não pertencente à linguagem. Ex: @
-
número inteiro muito grande. Ex: 11111111111111111111
-
número em ponto flutuante muito grande. Ex: 1E1111111111
-
caráter vázio. Ex: ''
-
mais de um caráter entre apóstrofos. Ex: 'abc'
-
caráter não terminado. Ex: 'a
-
comentário aberto e não fechado. Ex: /*
-
número em ponto flutuante mal formado. Ex: 1.5E*1 1.0.1
1eF
-
símbolos mal formados. Ex:
<> =>
-
números seguidos de letras. Ex: 1B
-
string não terminada. Ex:
“a
-
caráter não permitido seguindo \ em uma string. Ex: “\q”. Seria correto “\n”, por exemplo.
-
número fora dos limites seguindo \ em string. Ex: “\500”
3)
a)
if ( st.getInLocal(name) != null )
error();
Variable
v = new Variable(name);
st.putInLocal(name,
v);
OU
Variable
v = new Variable(name);
if ( st.putInLocal(name, v) != null )
error();
b)
if ( left.getType() != Symbol.INTEGER ||
right.getType() != Symbol.INTEGER )
error();
4)
Vector
v = new Vector();
Variable
vn = new Variable("n"); // não precisava do
tipo
v.addElement( new ReadStatement(vn) );
v.addElement(
new WhileStatement(
new
CompositeExpr(
new
VariableExpr(vn),
Symbol.LT,
new
NumberExpr(100)
),
new
AssignmentStatement(
nv,
new
CompositeExpr(
new
VariableExpr(vn),
Symbol.PLUS,
new
NumberExpr(1)
)
)
)
);
6.
Correção na aula.