AST 과정 중 실행 코드 설명 - 자바 구현

6184 단어 컴파일링 원리
본인 블로그에서 원리 문장을 컴파일하는 부대 자원jar 패키지는 문법 분석, 문법 분석, 중간 코드 생성, 정적 의미 검사, 코드 해석 집행과 추상적 문법 트리의 수동 생성을 포함한다.https://download.csdn.net/download/as1072966956/10448935
전재는 출처를 밝혀 주십시오.
Lab5Main.java
package sch.cauc.edu.token;

import java.io.File;
import java.util.LinkedList;
import java.util.Stack;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.WhileStatement;

import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
import edu.ustc.cs.compile.platform.interfaces.InterpreterException;

/**
 * 
 * 
 * Lab5Main
 *    :xrzhang 
 *   :2018 5 31 -  8:42:30 
 * @version 1.0.0
 *
 */
public class Lab5Main {
	public static void main(String[] args) throws InterpreterException {		
        String srcFileName = "test/expr5.txt"; 
        StaticCheck checker=new StaticCheck();
        InterRepresent ir =checker.doParse(srcFileName);
         ir.showIR();
        Interpreter it = new Interpreter();
        it.interpret(ir);            	   
    }
}

Interpreter.java
package sch.cauc.edu.token;

import java.util.Hashtable;
import java.util.Stack;

import org.eclipse.jdt.core.dom.*;
import edu.ustc.cs.compile.platform.interfaces.InterpreterInterface;
import edu.ustc.cs.compile.platform.interfaces.InterpreterException;
import edu.ustc.cs.compile.platform.interfaces.InterRepresent;


/**
 * 
 * SimpleMiniJOOL       。
 * Interpreter
 *    :xrzhang 
 *   :2018 5 31 -  8:44:21 
 * @version 1.0.0
 *
 */
public class Interpreter implements InterpreterInterface {
    public void interpret(InterRepresent ir) throws InterpreterException {
        InterpVisitor visitor = new InterpVisitor();
        try {
            //         SimpleMiniJOOL     

        	((Block)ir.getIR()).accept(visitor);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new InterpreterException();
        } 
    }
}
/**
 * 
 *         SimpleMiniJOOL     。
 * InterpVisitor
 *    :xrzhang 
 *   :2018 5 31 -  8:46:06 
 * @version 1.0.0
 *
 */
class InterpVisitor extends ASTVisitor {
	Hashtable symTable = new Hashtable();
	Stack stack = new Stack(); 
    public boolean visit(IfStatement n) { 
    	n.getExpression().accept(this);
    	Statement thenstatement = n.getThenStatement();
    	Statement elsestatement = n.getElseStatement();
    	int result = stack.pop();
        if(result == 1){
        	 thenstatement.accept(this);
        }
        else{
        	if(null != elsestatement) elsestatement.accept(this);
        }
        return false;
    }
 
    public boolean visit(WhileStatement n) {
    	n.getExpression().accept(this);
    	int result = stack.pop();
    	while(result == 1){
    		n.getBody().accept(this);
    		n.getExpression().accept(this);
    		result = stack.pop();
    	}
        return false;
    }
   
    public void endVisit(Assignment n) {
         
        Assignment.Operator operator = n.getOperator();
        String varName = n.getLeftHandSide().toString();
        int value = stack.pop();
        stack.pop();
        if (operator == Assignment.Operator.ASSIGN) {
            symTable.put(varName, value);
        } /*else if (operator == Assignment.Operator.PLUS_ASSIGN) {
        
        } else if (operator == Assignment.Operator.MINUS_ASSIGN) {
        
        } else if (operator == Assignment.Operator.TIMES_ASSIGN) {
        
        } else if (operator == Assignment.Operator.DIVIDE_ASSIGN) {
         
        } else if (operator == Assignment.Operator.REMAINDER_ASSIGN) {
          
        } */
        System.out.println(varName + "=" + value);

    }
  
    public  void endVisit(InfixExpression n) {

        InfixExpression.Operator operator = n.getOperator();
        int leftValue,rightValue;
        rightValue = stack.pop();
        leftValue = stack.pop();        
        
        if (operator == InfixExpression.Operator.PLUS) {        	
        	stack.push(leftValue + rightValue);
        } else if (operator == InfixExpression.Operator.MINUS) {
        	stack.push(leftValue - rightValue);
        } else if (operator == InfixExpression.Operator.TIMES) {
        	stack.push(leftValue * rightValue);
        } else if (operator == InfixExpression.Operator.DIVIDE) {
        	if(rightValue == 0){
        		System.out.println("divided by zero");
        		System.exit(1);
        	}else{
        		stack.push(leftValue / rightValue);
        	}
        } else if (operator == InfixExpression.Operator.REMAINDER) {
        	if(rightValue == 0){
        		System.out.println("divided by zero");
        		System.exit(1);
        	}else{
        		stack.push(leftValue % rightValue);
        	}
        } else if (operator == InfixExpression.Operator.GREATER){
        	stack.push(leftValue > rightValue?1:0) ;
        	
        } else if (operator == InfixExpression.Operator.GREATER_EQUALS){
        	stack.push(leftValue >= rightValue?1:0) ;
        } else if (operator == InfixExpression.Operator.LESS){
        	stack.push(leftValue < rightValue?1:0) ;
        	
        } else if (operator == InfixExpression.Operator.LESS_EQUALS){
        	stack.push(leftValue <= rightValue?1:0) ;
        } else if (operator == InfixExpression.Operator.EQUALS){
        	stack.push(leftValue == rightValue?1:0) ;
        } else if (operator == InfixExpression.Operator.NOT_EQUALS){
        	stack.push(leftValue != rightValue?1:0) ;       	
        }
    }

    public  void endVisit(SimpleName n) {
    	String name = n.getIdentifier();
    	if (!symTable.containsKey(name)){
    		symTable.put(name, 0);    		
    	}
    	stack.push(symTable.get(name));
    }
    public  void endVisit(NumberLiteral n){
    	stack.push(Integer.parseInt(n.getToken()));
    }
   
}


expr.java
{
	int m,n,r;
	m=12;n=21;
	if(m

좋은 웹페이지 즐겨찾기