编译原理中间代码生成--java实现

本人博客内编译原理文章的配套资源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
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花月诗人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值