Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Prática: um pretty printer para SOL Giovanny Lucero Departamento de Estatística e Informática Universidade Federal de Sergipe.

Similar presentations


Presentation on theme: "1 Prática: um pretty printer para SOL Giovanny Lucero Departamento de Estatística e Informática Universidade Federal de Sergipe."— Presentation transcript:

1 1 Prática: um pretty printer para SOL Giovanny Lucero Departamento de Estatística e Informática Universidade Federal de Sergipe Compiladores

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

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

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

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

6 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 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 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 9 CallExp(Expression target, Symbol name, ExpressionList) VarExp(Variable) NewObjectExp(Symbol, ExpressionList) NewArrayExp(Type, Expression) IntLiteralExp(int) CharLiteralExp(char) BoolLiteralExp(bool) selfExp() nullExp()

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

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

12 12 class ClassDeclaration extends ClassDec {.... void accept(SolVisitor visitor) { visitor.visitClassDeclaration(this) } class AssingStmt extends Statement {.... void accept(SolVisitor visitor) { visitor.visitAssingStmt(this); } Sintaxe Abstrata com visitors

13 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 14 abstract class ClassDec { void abstract accept(SolVisitor visitor); }

15 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 16 void visitProgram(Program p) { p.classDec.accept(this); writeln(“main”); indent(); p.main.accept(this); }

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


Download ppt "1 Prática: um pretty printer para SOL Giovanny Lucero Departamento de Estatística e Informática Universidade Federal de Sergipe."

Similar presentations


Ads by Google