以下是我为一些语法编写的语法Demo.java代码,该语法称为LOOL。而不是直接给它一个字符串(如下所示: CharStream input = new ANTLRStringStream("一些随机LOOL代码"); )我希望能够加载文本文件进行扫描,但不知道如何进行扫描。非常感谢任何帮助。谢谢。 我的Demo.Java代码:
import org.antlr.runtime.*;
class Demo{
public static void main(String[] args) throws Exception{
CharStream input = new ANTLRStringStream("sample.lool");
lool scanner = new lool(input);
while(true){
Token token = scanner.nextToken();
if(token.getType() == Token.EOF )
break;
else if(token.getType()==scanner.ID)
System.out.print("ID ");
else if(token.getType() == scanner.IF)
System.out.print(token.getText()+ " ");
else if(token.getType() == scanner.MODULE)
System.out.print("MODULE ");
else if(token.getType()== scanner.CHILDOF)
System.out.println("CHILDOF ");
else if(token.getType()== scanner.VIRTUAL)
System.out.println("VIRTUAL ");
else if(token.getType()== scanner.CREATE)
System.out.println("CREATE ");
else if(token.getType()== scanner.CHILDOF)
System.out.println("CHILDOF ");
else if(token.getType()== scanner.INT_LIT)
System.out.println("INT_LIT ");
else if(token.getType()== scanner.VOID)
System.out.println("VOID ");
else if(token.getType()== scanner.AND)
System.out.println("AND ");
else if(token.getType()== scanner.OR)
System.out.println("OR ");
else if(token.getType()== scanner.NOT)
System.out.println("NOT ");
else if(token.getType()== scanner.BOOL_LIT)
System.out.println(scanner.BOOL_LIT);
else if(token.getType()== scanner.RETURN)
System.out.println("RETURN ");
else if(token.getType()== scanner.IF)
System.out.println("IF ");
else if(token.getType()== scanner.THEN)
System.out.println("THEN ");
else if(token.getType()== scanner.ENDIF)
System.out.println("ENDIF ");
else if(token.getType()== scanner.WHILE)
System.out.println("WHILE ");
else if(token.getType()== scanner.LOOP)
System.out.println("LOOP ");
else if(token.getType()== scanner.ENDLOOP)
System.out.println("ENDLOOP ");
else if(token.getType()== scanner.BREAK)
System.out.println("BREAK ");
else if(token.getType()== scanner.CONTINUE)
System.out.println("CONTINUE ");
else if(token.getType()== scanner.READ)
System.out.println("READ ");
else if(token.getType()== scanner.WRITE)
System.out.println("WRITE ");
else if(token.getType()== scanner.INT_LIT)
System.out.println("INT_LIT ");
else if(token.getType()== scanner.BOOL_LIT)
System.out.println("BOOL_LIT ");
else if(token.getType()== scanner.STRING_LIT)
System.out.println("STRING_LIT ");
else if(token.getType()== scanner.SL_COMMENT)
System.out.println("SingleLineComment ");
else if(token.getType()== scanner.ML_COMMENT)
System.out.println("MultiLineComment ");
else if(token.getType()== scanner.ADD_OP)
System.out.println(" + ");
else if(token.getType()== scanner.SUB_OP)
System.out.println(" - ");
else if(token.getType()== scanner.MULT_OP)
System.out.println(" * ");
else if(token.getType()== scanner.DIV_OP)
System.out.println(" / ");
else if(token.getType()== scanner.ASSIGN_OP)
System.out.println(" =: ");
else if(token.getType()== scanner.COLON)
System.out.println(" : ");
else if(token.getType()== scanner.LT_OP)
System.out.println(" < ");
else if(token.getType()== scanner.GT_OP)
System.out.println(" > ");
else if(token.getType()== scanner.LT_EQ_OP)
System.out.println(" <= ");
else if(token.getType()== scanner.GT_EQ_OP)
System.out.println(" >= ");
else if(token.getType()== scanner.EQ_OP)
System.out.println(" = ");
else if(token.getType()== scanner.NOT_EQ_OP)
System.out.println(" != ");
else if(token.getType()== scanner.SEPARATE)
System.out.println(", ");
else if(token.getType()== scanner.ACCESS)
System.out.println(scanner.ACCESS);
else if(token.getType()== scanner.ACCESS_ARRAY)
System.out.println(scanner.ACCESS_ARRAY);
else if(token.getType()== scanner.METHOD_PRECEDENCE)
System.out.println(scanner.METHOD_PRECEDENCE);
else if(token.getType()== scanner.MOD_BLOCK)
System.out.println(scanner.MOD_BLOCK);
else if(token.getType()== scanner.SEMICOLON)
System.out.println(";");
else if(token.getType()== scanner.ACCESS)
System.out.println(".");
else if(token.getType()== scanner.ERR1)
System.out.println("ERR1(" + token.getText() + ") ");
else if(token.getType()== scanner.ERR2)
System.out.println( "Err2(" + token.getText() + ')' );
else if(token.getType()== scanner.ERR3)
System.out.println("ERR3");
}
}
}
答案 0 :(得分:1)
而不是
CharStream input = new ANTLRStringStream("sample.lool");
你想要使用子类ANTLRFileStream
子类
CharStream input = new ANTLRFileStream(new File("sample.lool"));