⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.java

📁 program assegment for compiler
💻 JAVA
字号:
package edu.berkeley.cs164.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Message;

/*
 * Created on Aug 2, 2004
 *
 * @author Ras Bodik, UC Berkeley
 */

/**
 * @author bodik
 *
 * A wrapper for invoking the Eclipse JDT Java parser.
 */
public class Parser {
	
	public static CompilationUnit parseJavaFile(String fileName) { 
		File file = new File(fileName);
		char source[] = new char[(int) file.length()];
		try {
			FileReader fileReader = new FileReader(file);
			fileReader.read(source);
		} catch (FileNotFoundException e) {
			Util.errorMsg("File "+fileName+" doesn't exist.");
		} catch (IOException e) {
			Util.errorMsg("Problems reading file "+fileName+".");
		}
		ASTParser parser = ASTParser.newParser(AST.JLS2);	
		parser.setSource(source);
		CompilationUnit ast = (CompilationUnit) parser.createAST(null);
		if ((ast.getFlags() & CompilationUnit.MALFORMED) != 0) {
			Util.errorMsg("Syntax error in file "+fileName);
			Message[] msgs = ast.getMessages();
			for (int i = 0; i < msgs.length; i++) {
				Util.errorMsg(msgs[i].getMessage());
			}
			Util.exit();
		}
		// SkimDecaf validation: check that the AST only contains valid Decaf or SkimDecaf features.
		// the following is a rough check only.  See the comments in SkimDecafValidator.
		SkimDecafValidator validator = new SkimDecafValidator();
		ast.accept(validator);
		return ast;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -