简体   繁体   English

ANTLR Java测试文件无法创建树语法对象

[英]ANTLR java test file can't create object of tree grammar

I am creating a parser using ANTLR 3.x that targets java. 我正在使用针对Java的ANTLR 3.x创建解析器。 I have written both parser grammar (for creating Abstract Syntax Tree, AST) and Tree Grammar (for performing operations on AST). 我已经写了解析器语法(用于创建抽象语法树,AST)和树语法(用于对AST执行操作)。 Finally, to test both grammar files, I have written a test file in Java. 最后,为了测试两个语法文件,我用Java编写了一个测试文件。

Have a look at the below code, 看下面的代码,

protocol grammar 协议语法

grammar protocol;
options {
      language = Java;
  output = AST;
}

tokens{ //imaginary tokens
PROT;
INITIALP;
PROC;
TRANSITIONS;
}
@header {
import twoprocess.Configuration;
package com.javadude.antlr3.x.tutorial;
}

@lexer::header {
  package com.javadude.antlr3.x.tutorial;
}
/*
parser rules, in lowercase letters
*/
program
    : declaration+
    ;
declaration
    :protocol
    |initialprocess
    |process
    |transitions
    ;

protocol
    :'protocol' ID ';' -> ^(PROT ID)
    ;
initialprocess
    :'pin' '=' INT ';' -> ^(INITIALP INT)
    ;
process
    :'p' '=' INT ';' -> ^(PROC INT)
    ;
transitions
    :'transitions' '=' INT ('(' INT ',' INT ')') + ';' -> ^(TRANSITIONS INT INT INT*)
    ;

/*
lexer rules (tokens), in upper case letters
*/
ID  
    : (('a'..'z' | 'A'..'Z'|'_')('a'..'z' | 'A'..'Z'|'0'..'9'|'_'))*;
INT 
    : ('0'..'9')+;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;};

protocolWalker 协议行者

grammar protocolWalker;

options {
  language = Java;
  //Error, eclipse can't access tokenVocab named protocol
  tokenVocab = protocol;    //import tokens from protocol.g i.e, from protocol.tokens file
  ASTLabelType = CommonTree;
  }

@header {
import twoprocess.Configuration;
package com.javadude.antlr3.x.tutorial;
}

program
    : declaration+
    ;

declaration
    :protocol
    |initialprocess
    |process
    |transitions
    ;

protocol
    :^(PROT ID)
    {System.out.println("create protocol " +$ID.text);}
    ;

initialprocess
    :^(INITIALP INT)
    {System.out.println("");}
    ;

process
    :^(PROC INT)
    {System.out.println("");}
    ;

transitions
    :^(TRANSITIONS INT INT INT*)
    {System.out.println("");}
    ;

Protocoltest.java 协议测试

package com.javadude.antlr3.x.tutorial;  
import org.antlr.runtime.*;  
import org.antlr.runtime.tree.*;  
import org.antlr.runtime.tree.CommonTree;  
import org.antlr.runtime.tree.CommonTreeNodeStream;  
public class Protocoltest { 



/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    //create input stream from standard input
    ANTLRInputStream input = new ANTLRInputStream(System.in);
    //create a lexer attached to that input stream
    protocolLexer lexer = new protocolLexer(input);
    //create a stream of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    //create a pareser attached to teh token stream
    protocolParser parser = new protocolParser(tokens);
    //invoke the program rule in get return value
    protocolParser.program_return r =parser.program();
    CommonTree t = (CommonTree)r.getTree();
    //output the extracted tree to the console
    System.out.println(t.toStringTree());

    //walk resulting tree; create treenode stream first
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
    //AST nodes have payloads that point into token stream
    nodes.setTokenStream(tokens);


    //create a tree walker attached to the nodes stream  
            //Error, can't create TreeGrammar object called walker
    protocolWalker walker = new protocolWalker(nodes);

    //invoke the start symbol, rule program
    walker.program();
    }
}

Problems: 问题:

  1. In protocolWalker, I can't access the tokens (protocol.tokens) 在protocolWalker中,我无法访问令牌(protocol.tokens)

     //Error, eclipse can't access tokenVocab named protocol tokenVocab = protocol; //import tokens from protocol.g ie, from protocol.tokens file 
  2. In In protocolWalker, can I create the object of java class, called Configuration, in the action list? 在ProtocolWalker中,可以在操作列表中创建名为Configuration的java类的对象吗?

     protocol :^(PROT ID) {System.out.println("create protocol " +$ID.text); Configuration conf = new Configuration(); } ; 
  3. In Protocoltest.java 在Protocoltest.java中

     //create a tree walker attached to the nodes stream //Error, can't create TreeGrammar object called walker protocolWalker walker = new protocolWalker(nodes); 

    Object of protocolWalker can't be created. 无法创建protocolWalker的对象。 I have seen in the examples and the tutorials that such object is created. 在示例和教程中,我已经看到创建了这样的对象。

In protocolWalker, I can't access the tokens (protocol.tokens)... 在protocolWalker中,我无法访问令牌(protocol.tokens)...

It seems to be accessing protocol.tokens fine: changing tokenVocab to something else produces an error that it doesn't produce now. 似乎正在访问protocol.tokens :将tokenVocab更改为其他内容会产生一个错误,该错误现在不会发生。 The problem with protocolWalker.g is that it's defined as a token parser ( grammar protocolWalker ) but it's being used like a tree parser. protocolWalker.g的问题在于它被定义为令牌解析器( grammar protocolWalker ),但它的使用像树解析器一样。 Defining the grammar as tree grammar protocolWalker took away the errors that I was seeing about the undefined tokens. 将语法定义为tree grammar protocolWalker Walker消除了我所看到的有关未定义标记的错误。

In protocolWalker, can I create the object of java class, called Configuration, in the action list? 在protocolWalker中,可以在操作列表中创建名为Configuration的java类的对象吗?

Yes, you can. 是的你可以。 The normal Java programming caveats apply about importing the class and so on, but it's as available to you as code like System.out.println . 常规的Java编程警告适用于导入类等问题,但是您可以像System.out.println这样的代码使用它。

In Protocoltest.java ... Object of protocolWalker can't be created. 在Protocoltest.java中...无法创建protocolWalker的对象。

protocolWalker.g (as it is now) produces a token parser named protocolWalkerParser . protocolWalker.g(现在)生成一个名为protocolWalkerParser的令牌解析器。 When you change it to a tree grammar, it'll produce a tree parser named protocolWalker instead. 当您将其更改为树语法时,它将生成一个名为protocolWalker的树解析器。

Thanks a lot for posting the whole grammars. 非常感谢您发布整个语法。 That made answering the question much easier. 这使得回答问题变得容易得多。

Thank you for your reply, that was a silly mistake. 感谢您的答复,这是一个愚蠢的错误。 Tokens problem and creating object of protocolWalker is resolved now but whenever, I change the grammar whether, protocol.g or protocolWalker.g, I had to write package name again(every time) in protocolParser.java and protocolWalker.java. 令牌问题和protocolWalker的创建对象现在已解决,但是无论何时更改语法,无论protocol.g还是protocolWalker.g,我都必须(每次)再次在protocolParser.java和protocolWalker.java中写程序包名称。 I had the same problem with lexer file before but that was overcomed by the following declaration. 以前我对lexer文件有同样的问题,但是下面的声明解决了这个问题。

@header {
package com.javadude.antlr3.x.tutorial;
}

but I don't know how to overcome this problem? 但是我不知道如何克服这个问题?

Also, I have developed a GUI in Java using SWING where I have a textarea. 另外,我使用SWING在Java中开发了一个GUI,其中有一个文本区域。 In that text area, user will write the input, like for my grammar user will write, 在该文本区域,用户将编写输入内容,就像我的语法用户将编写的内容一样,

protocol test;
pin = 5;
p = 3;
transitions = 2(5,0) (5,1);

How can I process this input in Java Swing GUI and produce output there? 如何在Java Swing GUI中处理此输入并在那里产生输出?

Moreover, if I give the following section of protocolWalker.g to 此外,如果我将protocolWalker.g的以下部分

protocol
    :^(PROT ID)
    {
     System.out.println("create protocol " +$ID.text);
     Configuration conf = new Configuration();
     conf.showConfiguration();
    }
    ;

initialprocess
    :^(INITIALP INT)
    {System.out.println("create initial process (with state) ");}
    ;

process
    :^(PROC INT)
    {System.out.println("create processes ");}
    ;

and run the test file with the following input, 并使用以下输入来运行测试文件:

protocol test;
pin = 5;
p = 3;
transitions = 2(5,0) (5,1);

I get the following output 我得到以下输出

(PROT test) (INITIALP 5) (PROC 3) (TRANSITIONS 2 5 0 5 1)
create protocol test

why the second and the third println in the protocolWalker.g are not shown in the output? 为什么在输出中未显示protocolWalker.g中的第二和第三个println?

Any thoughts/help? 有什么想法/帮助吗?

Thank you once again. 再一次感谢你。

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

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