190 likes | 261 Views
Departamento de Estatística e Informática Universidade Federal de Sergipe Compiladores. Prática: um pretty printer para SOL. Giovanny Lucero giovanny@sergipe.ufs.br. Program(ClassDecs, Statement) ClassDec EmptyClassDec() ClassDeclaration(Symbol name,
E N D
Departamento de Estatística e Informática Universidade Federal de Sergipe Compiladores Prática: um pretty printer para SOL Giovanny Lucero giovanny@sergipe.ufs.br
Program(ClassDecs, Statement) ClassDec EmptyClassDec() ClassDeclaration(Symbol name, Symbol superclass, ClassInterf, ClassImp) SequenceClassDec(ClassDec, ClassDec) ClassInterf(VarDec, MethodHeader) ClassImp(VarDec, MethodDec)
VarDec EmptyVarDecs() VarDeclaration(Symbol, Type) SequenceVarDec(VarDec, VarDec) Em VarDeclaration não usamos IdList, pois é açucar sintático.
MethodHeader EmptyMehtodHeader() FunctionHeader(Symbol, VarDeclararionList, Type) ProcedureHeader(Symbol, VarDeclarationList) ConstructorHeader(VarDeclarationList) SequenceMethodHeader(MehtodHeader, MethodHeader)
MethodDec EmptyMethodDec() FunctionDec(Symbol, VarDeclararionList, Type, Expression) ProcedureDec(Symbol, VarDeclarationList, Statement) ConstructorDec(VarDeclarationList, Statement) SequenceMethodDec(MethodDec, MethodDec)
Type Primitive() IdType(Symbol) ArrayP(Primitive, int) ArrayO(Symbol, int) Primitivos: integer, character ou boolean Por simplicidade podemos, alternativamente, representar tipos com Strings ou Symbols.
Statement BlockStmt(varDecList, Statement) IfStmt(Expression, Statement, Statement) WhileStmt(Expression, Statement) PrintStmt(Expression) CallStmt(Expression target, Symbol name, ExpressionList) AssignStmt(Variable, Expression) SkipStmt() SequenceStmt(Statement, Statement) else condicional é açucar sintático f(x) é um açucar sintático de this.f(x)
Expression BlockExp(VarDec, Statement, Expression) BinaryBoolExp(int operand, Expression, Expression) BinaryArithmeticExp(int operand, Expression, Expression) BinaryComparissonExp(int operand, Expression, Expression) UnaryOperationExp(int operand, Expression, Expression) ...
CallExp(Expression target, Symbol name, ExpressionList) VarExp(Variable) NewObjectExp(Symbol, ExpressionList) NewArrayExp(Type, Expression) IntLiteralExp(int) CharLiteralExp(char) BoolLiteralExp(bool) selfExp() nullExp()
Variable IdVar(Symbol) FieldVar(Expression target, Symbol field) ArrayElementVar(Expression target, Expression index)
SolVisitor interface SolVisitor { visitProgram(Program p); visitEmptyClassDec(EmptyClassDec dec); visitClassDeclaration(ClassDeclaration dec); visitSequenceClassDec(SequenceClassDec dec); .... visitAssignStmt(AssignStmt stm); }
Sintaxe Abstrata com visitors • class ClassDeclaration • extends ClassDec { .... • void accept(SolVisitor visitor) { • visitor.visitClassDeclaration(this) } • class AssingStmt • extends Statement { .... • void accept(SolVisitor visitor) { • visitor.visitAssingStmt(this); • } • }
class AssignStmt extends Statement { • Variable var; • Expression exp; • AssignStm(Variable var, Expression exp) { • this.var = var; • this.exp = exp; • void accept(SolVisitor visitor) { • visitor.visitAssingStmt(this); • } • }
abstract class ClassDec { • void abstract accept(SolVisitor visitor); • }
Um Pretty Printer public class PrettySol implementes SolVisitor { private int indent = 0; private static int indInc = 3; private void indent() { indent += indInc; } private void unindent() { unindent -= indInc; } private void write(String st) { System.out.print(st); } private void writeln(String st) { System.out.println(st); char [] ws = Arrays.fill(new char[indent], ' '); System.out.print(new String(ws)); }
void visitProgram(Program p) { p.classDec.accept(this); writeln(“main”); indent(); p.main.accept(this); }
void visitBinaryArithmeticExp (BinaryArithmeticExp exp) { write (“( ”); exp.left.accept(this); switch (exp.operator) { case '+': write(“ + ”); break; case '*': write (“ * ”); break; ... } exp.right.accept(this); write(“)”); }
void visitAssignStm(AssignStm stm) { stm.var.accept(this); write(" := "); stm.exp.accept(this); write(";") } void visitSequenceStmt(SequenceStmt stm) { stm.stm1.accept(this); writeln(""); stm.stm2.accept(this); }
void visitIfStm(IfStm stm) { write("if “); stm.exp.accept(this); writeln(“ then ”); indent(); stm.stm1.accept(this); unindent(); writeln(""); writeln("else "); indent(); stm.stm2.accept(this); unindent(); }