简体   繁体   English

如何从在ANTLR中已标记化的代码中获取ID Lexer

[英]How to Get ID Lexer from code that has tokenize in ANTLR

I have a lexer class that determined the token ID Lexer. 我有一个词法分析器类,它确定令牌ID词法分析器。 The code is : 代码是:

public class Antlr3JavaLexer extends Lexer {
public static final int PACKAGE=84;
public static final int PUBLIC=87;
public static final int STATIC=90;
public static final int IDENT=164;
public static final int CLASS = 70;  
}

Now I have a java class named hello.java that will be generated by ANTLR. 现在,我有一个名为hello.java的Java类,它将由ANTLR生成。 The code is like this public class hello{ public static void main(String args[]){ System.out.print("Hello World");} } 代码就像这个public class hello{ public static void main(String args[]){ System.out.print("Hello World");} }

now, time to ANTLR lexer that i created get shot.. 现在,我创建的ANTLR词法分析工具的时机已经开始。

BufferedReader in = null;
try {
        in = new BufferedReader(new FileReader(mainFile.getAbsolutePath())); // Assumption this is to read the hello.class
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    final Antlr3JavaLexer lexer = new Antlr3JavaLexer();

    try {
        lexer.setCharStream(new ANTLRReaderStream(in));
    } catch (IOException e) {
        e.printStackTrace();
        // return false;
    }

    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);

    Antlr3JavaParser parser = new Antlr3JavaParser(tokens); 
    System.out.println(tokens + "and" + "\n"); //First Print
    System.out.println(tokens.getTokens); // Second Print

Lucky, I get the output like this : https://www.dropbox.com/s/tsogz10eouo9f9h/ID%20Token.bmp 幸运的是,我得到这样的输出: https : //www.dropbox.com/s/tsogz10eouo9f9h/ID%20Token.bmp

So, the question is : How can I get the ID from the tokens?. 因此,问题是:如何从令牌中获取ID? For example public class hello is public = 87, class = 70, hello is identifier = 164. SO, perhaps the output like this 例如, public class hellopublic = 87, class = 70, hello是标识符=164。因此,也许这样的输出

8770164 

Thanks 4 the help... 感谢4的帮助...

When you append an object to a StringBuilder (or StringBuffer , which you should probably not be using), it calls ToString() on the object and appends the resulting text. 将对象附加到StringBuilder (或您可能不应该使用的StringBuffer )时,它将在对象上调用ToString()并附加结果文本。 If you want to format the list in another way, you'll need to iterate over the elements and append the text in the desired format. 如果要以其他方式设置列表格式,则需要遍历元素并以所需格式附加文本。

In this case, the desired format appears to be Token.getType() . 在这种情况下,所需的格式似乎是Token.getType()

boolean first = true;
for (Token token : tokens.getTokens()) {
  if (first) {
    first = false;
  } else {
    sb.append(", ");
  }

  sb.append(token.getType());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM