遍历AST过程中解释执行代码–java实现

语法分析器——java实现
2017年5月20日
阿里云wordpress不能外网访问问题解决方法
2017年6月2日

遍历AST过程中解释执行代码–java实现

本人博客内编译原理文章的配套资源jar包,包括词法分析,语法分析,中间代码生成,静态语义检查,代码解释执行以及抽象语法树的手动生成:https://download.csdn.net/download/as1072966956/10448935

转载请注明出处。

Lab5Main.java

  1. package sch.cauc.edu.token;
  2. import java.io.File;
  3. import java.util.LinkedList;
  4. import java.util.Stack;
  5. import org.eclipse.jdt.core.dom.AST;
  6. import org.eclipse.jdt.core.dom.Assignment;
  7. import org.eclipse.jdt.core.dom.Block;
  8. import org.eclipse.jdt.core.dom.ExpressionStatement;
  9. import org.eclipse.jdt.core.dom.InfixExpression;
  10. import org.eclipse.jdt.core.dom.WhileStatement;
  11. import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
  12. import edu.ustc.cs.compile.platform.interfaces.InterpreterException;
  13. /**
  14. *
  15. *
  16. * Lab5Main
  17. * 创建人:xrzhang
  18. * 时间:2018年5月31日-上午8:42:30
  19. * @version 1.0.0
  20. *
  21. */
  22. public
    class
    Lab5Main {
  23.     public
    static
    void
    main(String[] args) throws InterpreterException {        
  24. String srcFileName = “test/expr5.txt”;
  25. StaticCheck checker=new StaticCheck();
  26. InterRepresent ir =checker.doParse(srcFileName);
  27. ir.showIR();
  28. Interpreter it = new Interpreter();
  29. it.interpret(ir);     
  30. }
  31. }

 

Interpreter.java

  1. package sch.cauc.edu.token;
  2. import java.util.Hashtable;
  3. import java.util.Stack;
  4. import org.eclipse.jdt.core.dom.*;
  5. import edu.ustc.cs.compile.platform.interfaces.InterpreterInterface;
  6. import edu.ustc.cs.compile.platform.interfaces.InterpreterException;
  7. import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
  8. /**
  9. *
  10. * SimpleMiniJOOL程序的解释器类。
  11. * Interpreter
  12. * 创建人:xrzhang
  13. * 时间:2018年5月31日-上午8:44:21
  14. * @version 1.0.0
  15. *
  16. */
  17. public
    class
    Interpreter
    implements
    InterpreterInterface {

  18. public
    void
    interpret(InterRepresent ir) throws InterpreterException {
  19. InterpVisitor visitor = new InterpVisitor();

  20. try {

  21. // 调用实际解释执行SimpleMiniJOOL程序的代码
  22.     ((Block)ir.getIR()).accept(visitor);
  23. } catch (IllegalArgumentException e) {
  24. e.printStackTrace();

  25. throw
    new InterpreterException();
  26. }
  27. }
  28. }
  29. /**
  30. *
  31. * 封装实际解释执行SimpleMiniJOOL程序的代码。
  32. * InterpVisitor
  33. * 创建人:xrzhang
  34. * 时间:2018年5月31日-上午8:46:06
  35. * @version 1.0.0
  36. *
  37. */
  38. class
    InterpVisitor
    extends
    ASTVisitor {
  39.     Hashtable<String, Integer> symTable = new Hashtable<String, Integer>();
  40.     Stack<Integer> stack = new Stack<Integer>();

  41. public
    boolean
    visit(IfStatement n) {
  42.     n.getExpression().accept(this);
  43.     Statement thenstatement = n.getThenStatement();
  44.     Statement elsestatement = n.getElseStatement();
  45.     int result = stack.pop();

  46. if(result == 1){
  47.      thenstatement.accept(this);
  48. }

  49. else{
  50.     if(null != elsestatement) elsestatement.accept(this);
  51. }

  52. return
    false;
  53. }

  54. public
    boolean
    visit(WhileStatement n) {
  55.     n.getExpression().accept(this);
  56.     int result = stack.pop();
  57.     while(result == 1){
  58.         n.getBody().accept(this);
  59.         n.getExpression().accept(this);
  60.         result = stack.pop();
  61.     }

  62. return
    false;
  63. }

  64. public
    void
    endVisit(Assignment n) {
  65. Assignment.Operator operator = n.getOperator();
  66. String varName = n.getLeftHandSide().toString();

  67. int value = stack.pop();
  68. stack.pop();

  69. if (operator == Assignment.Operator.ASSIGN) {
  70. symTable.put(varName, value);
  71. } /*else if (operator == Assignment.Operator.PLUS_ASSIGN) {
  72. } else if (operator == Assignment.Operator.MINUS_ASSIGN) {
  73. } else if (operator == Assignment.Operator.TIMES_ASSIGN) {
  74. } else if (operator == Assignment.Operator.DIVIDE_ASSIGN) {
  75. } else if (operator == Assignment.Operator.REMAINDER_ASSIGN) {
  76. } */
  77. System.out.println(varName + “=” + value);
  78. }

  79. public
    void
    endVisit(InfixExpression n) {
  80. InfixExpression.Operator operator = n.getOperator();

  81. int leftValue,rightValue;
  82. rightValue = stack.pop();
  83. leftValue = stack.pop();

  84. if (operator == InfixExpression.Operator.PLUS) {     
  85.     stack.push(leftValue + rightValue);
  86. } else
    if (operator == InfixExpression.Operator.MINUS) {
  87.     stack.push(leftValue – rightValue);
  88. } else
    if (operator == InfixExpression.Operator.TIMES) {
  89.     stack.push(leftValue * rightValue);
  90. } else
    if (operator == InfixExpression.Operator.DIVIDE) {
  91.     if(rightValue == 0){
  92.         System.out.println(“divided by zero”);
  93.         System.exit(1);
  94.     }else{
  95.         stack.push(leftValue / rightValue);
  96.     }
  97. } else
    if (operator == InfixExpression.Operator.REMAINDER) {
  98.     if(rightValue == 0){
  99.         System.out.println(“divided by zero”);
  100.         System.exit(1);
  101.     }else{
  102.         stack.push(leftValue % rightValue);
  103.     }
  104. } else
    if (operator == InfixExpression.Operator.GREATER){
  105.     stack.push(leftValue > rightValue?1:0) ;
  106.     
  107. } else
    if (operator == InfixExpression.Operator.GREATER_EQUALS){
  108.     stack.push(leftValue >= rightValue?1:0) ;
  109. } else
    if (operator == InfixExpression.Operator.LESS){
  110.     stack.push(leftValue < rightValue?1:0) ;
  111.     
  112. } else
    if (operator == InfixExpression.Operator.LESS_EQUALS){
  113.     stack.push(leftValue <= rightValue?1:0) ;
  114. } else
    if (operator == InfixExpression.Operator.EQUALS){
  115.     stack.push(leftValue == rightValue?1:0) ;
  116. } else
    if (operator == InfixExpression.Operator.NOT_EQUALS){
  117.     stack.push(leftValue != rightValue?1:0) ;     
  118. }
  119. }

  120. public
    void
    endVisit(SimpleName n) {
  121.     String name = n.getIdentifier();
  122.     if (!symTable.containsKey(name)){
  123.         symTable.put(name, 0);         
  124.     }
  125.     stack.push(symTable.get(name));
  126. }

  127. public
    void
    endVisit(NumberLiteral n){
  128.     stack.push(Integer.parseInt(n.getToken()));
  129. }
  130. }

 

expr.java

  1. {
  2.     int m,n,r;
  3.     m=12;n=21;
  4.     if(m<n){
  5.         int t;
  6.         t=m;m=n;n=t;
  7.     }
  8.     r=m%n;
  9.     while(r!=0){
  10.     m=n;n=r;r=m%n;}
  11. }

 


 

发表评论

邮箱地址不会被公开。