本人博客内编译原理文章的配套资源jar包,包括词法分析,语法分析,中间代码生成,静态语义检查,代码解释执行以及抽象语法树的手动生成:https://download.csdn.net/download/as1072966956/10448935
转载请注明出处。
程序要求能自动生成AST抽象语法树。
Lab3Main.java
package sch.cauc.edu.token;
import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
/**
*
*
* Lab3Main
* 创建人:xrzhang
* 时间:2018年5月25日-上午8:13:05
* @version 1.0.0
*
*/
public class Lab3Main {
public static void main(String[] args) {
//String srcFileName="test/expr3.txt";
//String srcFileName="test/expr221.txt";
String srcFileName="test/expr222.txt";
//String srcFileName="test/testown.txt";
SyntaxDirectedTranslation parser=new SyntaxDirectedTranslation();
InterRepresent ir=parser.doParse(srcFileName);
ir.showIR();
}
}
SyntaxDirectedTranslation.java
package sch.cauc.edu.token;
import java.util.LinkedList;
import org.eclipse.jdt.core.dom.*;
import edu.ustc.cs.compile.platform.interfaces.InterRepresent;
import edu.ustc.cs.compile.util.ir.HIRPack;
public class SyntaxDirectedTranslation {
/*抽象语法树的根节点*/
private AST ast=null;
private BlockLexer lexer=null;
private Token lookAhead=null;
public SyntaxDirectedTranslation() {
ast = AST.newAST(AST.JLS3);
}
public InterRepresent doParse(String filePath){
lexer=new BlockLexer(filePath);
Block mainBody = this.parse();
HIRPack ir = new HIRPack();
ir.setIR(mainBody);
return ir;
}
public Token matchToken(TokenType type,String functionName){
if(lookAhead.getType()!=type){
parsingError(type.toString(),functionName);
}
Token matchedSymbol = lookAhead;
lookAhead = lexer.nextToken();
return matchedSymbol;
}
public void parsingError(String types,String functionName){
System.out.println("Parsing Error! in"+functionName);
System.out.println("encounter "+lookAhead.getLexeme());
System.out.println("at line "+lookAhead.getLine()+",column "+lookAhead.getColumn());
System.out.println("while expecting "+types);
System.exit(1);
}
/**
*
* 调用开始符号对应的方法,进行语法分析
* 方法名:parse
* 创建人:xrzhang
* 时间:2018年5月16日-上午10:27:14
* 邮件:jmzhang_15_cauc@163.com void
* @exception
* @since 1.0.0
*/
public Block parse() {
System.out.println("In parse();-----jmzhang-----");
lookAhead=lexer.nextToken();
Block mainBody=simpleblock();
System.out.println("Parsing Success!");
return mainBody;
}
/**
*
* simpleblock = LBRACE sequence RBRACE
* B->{S}
* 方法名:simpleblock
* 创建人:xrzhang
* 时间:2018年5月19日-下午6:59:57
* 邮件:jmzhang_15_cauc@163.com void
* @exception
* @since 1.0.0
*/
public Block simpleblock() {
System.out.println("In simpleblock();-----jmzhang-----");
if(lookAhead.getType()==TokenType.LBRACKET){
matchToken(TokenType.LBRACKET, "simpleblock");
System.out.println("***********{*********");
LinkedList seq =sequence();
matchToken(TokenType.RBRACKET, "simpleblock");
System.out.println("***********}*********");
Block mainBody=ast.newBlock();
if (seq!=null) {
for(int i=0;i<seq.size();i++){
mainBody.statements().add(seq.get(i));
}
}
return mainBody;
}else{
parsingError(TokenType.LBRACKET.toString(), "simpleblock");
return null;
}
}
/**
*
* sequence=assognmentStatement sequence |
* ifStatement sequence |
* whileStatement sequence |
* epsilon
* S->AS | IS |WS | ε
* 方法名:Sequence
* 创建人:xrzhang
* 时间:2018年5月16日-下午8:54:23
* 邮件:jmzhang_15_cauc@163.com void
* @exception
* @since 1.0.0
*/
private LinkedList sequence() {
System.out.println("In Sequence();-----jmzhang-----");
if(lookAhead.getType()==TokenType.IDENTIFIER){
ExpressionStatement es=assignmentStatement();
LinkedList seq=sequence();
if (seq==null) {
seq=new LinkedList();
}
seq.addFirst(es);
return seq;
}else if (lookAhead.getType()==TokenType.KEY_IF) {
IfStatement es=ifStatement();
LinkedList seq=sequence();
if (seq==null) {
seq=new LinkedList();
}
seq.addFirst(es);
return seq;
}else if (lookAhead.getType()==TokenType.KEY_WHILE) {
System.out.println("In Sequence();-----jmzhang-----WHILE");
WhileStatement es=whileStatement();
LinkedList seq=sequence();
if (seq==null) {
seq=new LinkedList();
}
seq.addFirst(es);
return seq;
}else if (lookAhead.getType()==TokenType.RBRACKET) {
//match epslon
return null;
}else {
String errorTypes=TokenType.IDENTIFIER.toString()+","+TokenType.RBRACKET.toString();
parsingError(errorTypes, "sequence");
return null;
}
}
/************************************>
* @return ************************************/
private WhileStatement whileStatement() {
System.out.println("In whileStatement();-----jmzhang-----");
if (lookAhead.getType()==TokenType.KEY_WHILE) {
matchToken(TokenType.KEY_WHILE, "whileStatement");
WhileStatement wStatement=ast.newWhileStatement();
System.out.println("***********while*********");
matchToken(TokenType.LPAREN, "whileStatement");
System.out.println("***********(*********");
Expression be=Boolexpression();
wStatement.setExpression(be);
matchToken(TokenTy