📄 lexercodegenerator.java
字号:
package edu.ustc.cs.minijool.lexer;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.CharacterLiteral;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
import edu.ustc.cs.minijool.lab2parser.SpecParser;
/**<p>
* 根据AST来生成对应的词法分析器代码,即产生NFA的代码,需要编译运行此类中的main
* 函数生成LexerCode类。我们已经在LexerCode中提供了一个已生成的代码,您可以在
* 实验中覆盖它
* </p><p>
* 在实验过程中,您需要完成此类,以适应整个minijool的需要,注意缩进。您可以在此类中添加
* 适当的私有变量来辅助您的方法,但不允许改变类的接口
* </p>
*/
public class LexerCodeGenerator extends GenericVisitor {
private PrintStream out; /** output stream for generated code */
public static void main(String args[]) {
PrintStream outfile = null;
try {
outfile = new PrintStream(new FileOutputStream("edu/ustc/cs/minijool/lexer/LexerCode.java"));
} catch(IOException e) {
System.out.println(e);
System.exit(-1);
}
LexerCodeGenerator codeGen = new LexerCodeGenerator(outfile);
ASTNode spec = SpecParser.parse("MJLex/MiniJOOL.ml");
codeGen.emitCode(spec);
}
/**
* Create a LexerCodeGenerator
* @param out Stream to output generated code to.
*/
public LexerCodeGenerator(PrintStream out) {
if(out != null) {
this.out = out;
} else {
this.out = System.out;
}
}
/**
* Generate code for the given AST
* @param lexSpec AST tree to emit Lexer code for
*/
public void emitCode(ASTNode lexSpec) {
// TODO
throw new RuntimeException("TODO");
}
/**
* 类型定义的访问函数,即类定义的访问函数。
*/
public boolean visit(TypeDeclaration t) {
t.getMethods()[0].accept(this);
return false;
}
/**
* 方法声明的访问函数
*/
public boolean visit(MethodDeclaration m) {
m.getBody().accept(this);
return false;
}
/**
* if语句的访问函数
*/
public boolean visit(IfStatement s) {
//TODO
throw new RuntimeException("TODO");
}
/**
* 赋值语句的访问函数
*/
public boolean visit(Assignment s) {
//TODO
throw new RuntimeException("TODO");
}
/**
* return语句的访问函数
*/
public boolean visit(ReturnStatement s) {
// TODO
throw new RuntimeException("TODO");
}
/**
* 中缀表达式的访问函数
*/
public boolean visit(InfixExpression s) {
//TODO
throw new RuntimeException("TODO");
}
/**
* 字符串的访问函数
*/
public boolean visit(StringLiteral s) {
//TODO
throw new RuntimeException("TODO");
}
/**
* 字符的访问函数
*/
public boolean visit(CharacterLiteral c) {
//TODO
throw new RuntimeException("TODO");
}
/**
* 标识符的访问函数
*/
public boolean visit(SimpleName n) {
//TODO
throw new RuntimeException("TODO");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -