AST 과정 중 실행 코드 설명 - 자바 구현
6184 단어 컴파일링 원리
전재는 출처를 밝혀 주십시오.
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
귀속 하강 식별기-컴파일링 원리작업용 단순화 반복 감소 인식기: 예: 문법 G[S]: S-> A|B M->A|# B->bN N->B|# ①문법은 좌귀속을 포함하지 않는다 follow 아니요 {a,b} 아니요 아니요 {b} 예. 예. {b,#} ①...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.