1 / 19

Prática: um pretty printer para SOL

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,

Download Presentation

Prática: um pretty printer para SOL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Program(ClassDecs, Statement) ClassDec EmptyClassDec() ClassDeclaration(Symbol name, Symbol superclass, ClassInterf, ClassImp) SequenceClassDec(ClassDec, ClassDec) ClassInterf(VarDec, MethodHeader) ClassImp(VarDec, MethodDec)

  3. VarDec EmptyVarDecs() VarDeclaration(Symbol, Type) SequenceVarDec(VarDec, VarDec) Em VarDeclaration não usamos IdList, pois é açucar sintático.

  4. MethodHeader EmptyMehtodHeader() FunctionHeader(Symbol, VarDeclararionList, Type) ProcedureHeader(Symbol, VarDeclarationList) ConstructorHeader(VarDeclarationList) SequenceMethodHeader(MehtodHeader, MethodHeader)

  5. MethodDec EmptyMethodDec() FunctionDec(Symbol, VarDeclararionList, Type, Expression) ProcedureDec(Symbol, VarDeclarationList, Statement) ConstructorDec(VarDeclarationList, Statement) SequenceMethodDec(MethodDec, MethodDec)

  6. 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.

  7. 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)

  8. 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) ...

  9. CallExp(Expression target, Symbol name, ExpressionList) VarExp(Variable) NewObjectExp(Symbol, ExpressionList) NewArrayExp(Type, Expression) IntLiteralExp(int) CharLiteralExp(char) BoolLiteralExp(bool) selfExp() nullExp()

  10. Variable IdVar(Symbol) FieldVar(Expression target, Symbol field) ArrayElementVar(Expression target, Expression index)

  11. SolVisitor interface SolVisitor { visitProgram(Program p); visitEmptyClassDec(EmptyClassDec dec); visitClassDeclaration(ClassDeclaration dec); visitSequenceClassDec(SequenceClassDec dec); .... visitAssignStmt(AssignStmt stm); }

  12. 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); • } • }

  13. 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); • } • }

  14. abstract class ClassDec { • void abstract accept(SolVisitor visitor); • }

  15. 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)); }

  16. void visitProgram(Program p) { p.classDec.accept(this); writeln(“main”); indent(); p.main.accept(this); }

  17. void visitBinaryArithmeticExp (BinaryArithmeticExp exp) { write (“( ”); exp.left.accept(this); switch (exp.operator) { case '+': write(“ + ”); break; case '*': write (“ * ”); break; ... } exp.right.accept(this); write(“)”); }

  18. 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); }

  19. 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(); }

More Related