home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / compilers / p5 / CheckerStarter.java < prev    next >
Text File  |  2005-11-01  |  9KB  |  251 lines

  1. // -------------------------------- Checker --------------------------------
  2. //
  3. // Methods to walk an Abstract Syntax Tree, performing type checking.
  4. // There should only be one instance of this class.  The primary method is:
  5. //      checker.checkAST (ast)
  6. // This method will walk the tree (calling other methods as necessary).
  7. // If semantic errors occur, a message will be printed and the walk will
  8. // continue, looking for additional errors.
  9. //
  10. // <Your name here> -- 00/00/00
  11. //
  12.  
  13. import java.io.*;
  14.  
  15. class Checker {
  16.  
  17.     //
  18.     //  Constructor
  19.     //
  20.     Checker () { }
  21.  
  22.  
  23.  
  24.     // semanticError (t, msg)
  25.     //
  26.     // This method is passed a pointer to a node.  It prints out the
  27.     // line number of that node and a description of that node.  It then
  28.     // prints the given message.  It increments the "errorCount".
  29.     // It does not halt the compiler.  Instead, it returns.
  30.     //
  31.     void semanticError (Ast.Node t, String msg)
  32.         throws FatalError
  33.     {
  34.         Main.errorCount ++;
  35.         System.err.print ("Error on line " + t.lineNumber);
  36.         printNear (t);
  37.         System.err.println (": " + msg);
  38.     }
  39.  
  40.  
  41.  
  42.     //
  43.     // printNear (t)
  44.     //
  45.     // This method is passed a pointer to a tree.  It prints
  46.     // " near 'xxx'" where xxx is a description of the node.
  47.     //
  48.     void printNear (Ast.Node t)
  49.         throws FatalError
  50.     {
  51.         if (t instanceof Ast.Body) {
  52.             System.err.print (" near 'begin'");
  53.         } else if (t instanceof Ast.VarDecl) {
  54.             System.err.print (" near '" + ((Ast.VarDecl) t).id + "'");
  55.         } else if (t instanceof Ast.TypeDecl) {
  56.             System.err.print (" near '" + ((Ast.TypeDecl) t).id + "'");
  57.         } else if (t instanceof Ast.ProcDecl) {
  58.             System.err.print (" near '" + ((Ast.ProcDecl) t).id + "'");
  59.         } else if (t instanceof Ast.Formal) {
  60.             System.err.print (" near '" + ((Ast.Formal) t).id + "'");
  61.         } else if (t instanceof Ast.TypeName) {
  62.             System.err.print (" near '" + ((Ast.TypeName) t).id + "'");
  63.         } else if (t instanceof Ast.ArrayType) {
  64.             System.err.print (" near 'array'");
  65.         } else if (t instanceof Ast.RecordType) {
  66.             System.err.print (" near 'record'");
  67.         } else if (t instanceof Ast.FieldDecl) {
  68.             System.err.print (" near '" + ((Ast.FieldDecl) t).id + "'");
  69.         } else if (t instanceof Ast.AssignStmt) {
  70.             System.err.print (" near ':='");
  71.         } else if (t instanceof Ast.CallStmt) {
  72.             System.err.print (" near '" + ((Ast.CallStmt) t).id + "'");
  73.         } else if (t instanceof Ast.ReadStmt) {
  74.             System.err.print (" near 'read'");
  75.         } else if (t instanceof Ast.ReadArg) {
  76.             printNear (((Ast.ReadArg) t).lValue);
  77.         } else if (t instanceof Ast.WriteStmt) {
  78.             System.err.print (" near 'write'");
  79.         } else if (t instanceof Ast.IfStmt) {
  80.             System.err.print (" near 'if'");
  81.         } else if (t instanceof Ast.WhileStmt) {
  82.             System.err.print (" near 'while'");
  83.         } else if (t instanceof Ast.LoopStmt) {
  84.             System.err.print (" near 'loop'");
  85.         } else if (t instanceof Ast.ForStmt) {
  86.             System.err.print (" near 'for " + ((Ast.Variable) ((Ast.ForStmt) t).lValue).id + "'");
  87.         } else if (t instanceof Ast.ExitStmt) {
  88.             System.err.print (" near 'exit'");
  89.         } else if (t instanceof Ast.ReturnStmt) {
  90.             System.err.print (" near 'return'");
  91.         } else if (t instanceof Ast.IntToReal) {
  92.             printNear (((Ast.IntToReal) t).expr);
  93.         } else if (t instanceof Ast.UnaryOp) {
  94.             Ast.UnaryOp unOp = (Ast.UnaryOp) t;
  95.             if (unOp.op == Token.PLUS) {
  96.                 System.err.print (" near '+'");
  97.             } else if (unOp.op == Token.MINUS) {
  98.                 System.err.print (" near '-'");
  99.             } else if (unOp.op == Token.NOT) {
  100.                 System.err.print (" near 'not'");
  101.             } else {
  102.                throw new LogicError ("Unknown UnaryOp.op in method printNear");
  103.             }
  104.         } else if (t instanceof Ast.BinaryOp) {
  105.             Ast.BinaryOp binOp = (Ast.BinaryOp) t;
  106.             if (binOp.op == Token.PLUS) {
  107.                 System.err.print (" near '+'");
  108.             } else if (binOp.op == Token.MINUS) {
  109.                 System.err.print (" near '-'");
  110.             } else if (binOp.op == Token.STAR) {
  111.                 System.err.print (" near '*'");
  112.             } else if (binOp.op == Token.SLASH) {
  113.                 System.err.print (" near '/'");
  114.             } else if (binOp.op == Token.EQUAL) {
  115.                 System.err.print (" near '='");
  116.             } else if (binOp.op == Token.LESS) {
  117.                 System.err.print (" near '<'");
  118.             } else if (binOp.op == Token.GREATER) {
  119.                 System.err.print (" near '>'");
  120.             } else if (binOp.op == Token.MOD) {
  121.                 System.err.print (" near 'mod'");
  122.             } else if (binOp.op == Token.DIV) {
  123.                 System.err.print (" near 'div'");
  124.             } else if (binOp.op == Token.OR) {
  125.                 System.err.print (" near 'or'");
  126.             } else if (binOp.op == Token.AND) {
  127.                 System.err.print (" near 'and'");
  128.             } else if (binOp.op == Token.NEQ) {
  129.                 System.err.print (" near '<>'");
  130.             } else if (binOp.op == Token.LEQ) {
  131.                 System.err.print (" near '<='");
  132.             } else if (binOp.op == Token.GEQ) {
  133.                 System.err.print (" near '>='");
  134.             } else {
  135.                 throw new LogicError ("Unknown BinaryOp.op in method printNear");
  136.             }
  137.         } else if (t instanceof Ast.FunctionCall) {
  138.             System.err.print (" near '" + ((Ast.FunctionCall) t).id + "'");
  139.         } else if (t instanceof Ast.Argument) {
  140.             printNear (((Ast.Argument) t).expr);
  141.         } else if (t instanceof Ast.ArrayConstructor) {
  142.             System.err.print (" near '" + ((Ast.ArrayConstructor) t).id + "'");
  143.         } else if (t instanceof Ast.ArrayValue) {
  144.             printNear (((Ast.ArrayValue) t).valueExpr);
  145.         } else if (t instanceof Ast.RecordConstructor) {
  146.             System.err.print (" near '" + ((Ast.RecordConstructor) t).id + "'");
  147.         } else if (t instanceof Ast.FieldInit) {
  148.             System.err.print (" near '" + ((Ast.FieldInit) t).id + "'");
  149.         } else if (t instanceof Ast.IntegerConst) {
  150.             System.err.print (" near '" + ((Ast.IntegerConst) t).iValue + "'");
  151.         } else if (t instanceof Ast.RealConst) {
  152.             System.err.print (" near '" + ((Ast.RealConst) t).rValue + "'");
  153.         } else if (t instanceof Ast.StringConst) {
  154.             System.err.print (" near '" + ((Ast.StringConst) t).sValue + "'");
  155.         } else if (t instanceof Ast.BooleanConst) {
  156.             if (((Ast.BooleanConst) t).iValue == 0) {
  157.                 System.err.print (" near 'false'");
  158.             } else {
  159.                 System.err.print (" near 'true'");
  160.             }
  161.         } else if (t instanceof Ast.NilConst) {
  162.             System.err.print (" near 'nil'");
  163.         } else if (t instanceof Ast.ValueOf) {
  164.             printNear (((Ast.ValueOf) t).lValue);
  165.         } else if (t instanceof Ast.Variable) {
  166.             System.err.print (" near '" + ((Ast.Variable) t).id + "'");
  167.         } else if (t instanceof Ast.ArrayDeref) {
  168.             printNear (((Ast.ArrayDeref) t).lValue);
  169.         } else if (t instanceof Ast.RecordDeref) {
  170.             printNear (((Ast.RecordDeref) t).lValue);
  171.         } else {
  172.             throw new LogicError ("Unknown class in method printNear");
  173.         }
  174.     }
  175.  
  176.  
  177.  
  178.     //
  179.     // checkAst (body)
  180.     //
  181.     // This method checks the Abstract Syntax Tree.
  182.     //
  183.     void checkAst (Ast.Body body)
  184.         throws FatalError
  185.     {
  186.         Main.parser.lexer.lineNumber = 0;    // For newly created nodes in proj 6
  187.  
  188.         // Initialize nilString, trueString,...
  189.  
  190.         checkBody (body);
  191.     }
  192.  
  193.  
  194.  
  195.     // checkBody (body)
  196.     //
  197.     // Check the given Body for semantic errors.
  198.     //
  199.     void checkBody (Ast.Body body)
  200.         throws FatalError
  201.     {
  202.         enterTypeDecls (body.typeDecls);
  203.         checkTypeDecls (body.typeDecls);
  204.         enterProcDecls (body.procDecls);
  205.         enterAndCheckVarDecls (body.varDecls);
  206.         checkProcDecls (body.procDecls);
  207.         // SymbolTable.printTable ();          // Debugging: print out symbol table
  208.         checkStmts (body.stmts);
  209.     }
  210.  
  211.  
  212. ...
  213.  
  214.  
  215.     //
  216.     // checkIfStmt (p)
  217.     //
  218.     // Check the IF_STMT pointed to by "p" for semantic errors.
  219.     //
  220.     void checkIfStmt (Ast.IfStmt p)
  221.         throws FatalError
  222.     {
  223.         checkExpr (p.expr);
  224.         checkStmts (p.thenStmts);
  225.         checkStmts (p.elseStmts);
  226.     }
  227.  
  228.  
  229.  
  230. ...
  231.  
  232.  
  233.  
  234.     //
  235.     // checkExpr (p)
  236.     //
  237.     // Check the expression pointed to by "p" for semantic errors.
  238.     //
  239.     void checkExpr (Ast.Node p)
  240.         throws FatalError
  241.     {
  242.         if (p instanceof Ast.BinaryOp) {
  243.             checkBinaryOp ((Ast.BinaryOp) p);
  244.         } else if (p instanceof Ast.UnaryOp) {
  245.             checkUnaryOp ((Ast.UnaryOp) p);
  246. ...
  247.         } else {
  248.             throw new LogicError ("Unknown class within checkExpr");
  249.         }
  250.     }
  251.