[英]ANTLR java test file can't create object of tree grammar
我正在使用針對Java的ANTLR 3.x創建解析器。 我已經寫了解析器語法(用於創建抽象語法樹,AST)和樹語法(用於對AST執行操作)。 最后,為了測試兩個語法文件,我用Java編寫了一個測試文件。
看下面的代碼,
協議語法
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;};
協議行者
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("");}
;
協議測試
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();
}
}
問題:
在protocolWalker中,我無法訪問令牌(protocol.tokens)
//Error, eclipse can't access tokenVocab named protocol tokenVocab = protocol; //import tokens from protocol.g ie, from protocol.tokens file
在ProtocolWalker中,可以在操作列表中創建名為Configuration的java類的對象嗎?
protocol :^(PROT ID) {System.out.println("create protocol " +$ID.text); Configuration conf = new Configuration(); } ;
在Protocoltest.java中
//create a tree walker attached to the nodes stream //Error, can't create TreeGrammar object called walker protocolWalker walker = new protocolWalker(nodes);
無法創建protocolWalker的對象。 在示例和教程中,我已經看到創建了這樣的對象。
在protocolWalker中,我無法訪問令牌(protocol.tokens)...
似乎正在訪問protocol.tokens
:將tokenVocab
更改為其他內容會產生一個錯誤,該錯誤現在不會發生。 protocolWalker.g的問題在於它被定義為令牌解析器( grammar protocolWalker
),但它的使用像樹解析器一樣。 將語法定義為tree grammar protocolWalker
Walker消除了我所看到的有關未定義標記的錯誤。
在protocolWalker中,可以在操作列表中創建名為Configuration的java類的對象嗎?
是的你可以。 常規的Java編程警告適用於導入類等問題,但是您可以像System.out.println
這樣的代碼使用它。
在Protocoltest.java中...無法創建protocolWalker的對象。
protocolWalker.g(現在)生成一個名為protocolWalkerParser
的令牌解析器。 當您將其更改為樹語法時,它將生成一個名為protocolWalker
的樹解析器。
非常感謝您發布整個語法。 這使得回答問題變得容易得多。
感謝您的答復,這是一個愚蠢的錯誤。 令牌問題和protocolWalker的創建對象現在已解決,但是無論何時更改語法,無論protocol.g還是protocolWalker.g,我都必須(每次)再次在protocolParser.java和protocolWalker.java中寫程序包名稱。 以前我對lexer文件有同樣的問題,但是下面的聲明解決了這個問題。
@header {
package com.javadude.antlr3.x.tutorial;
}
但是我不知道如何克服這個問題?
另外,我使用SWING在Java中開發了一個GUI,其中有一個文本區域。 在該文本區域,用戶將編寫輸入內容,就像我的語法用戶將編寫的內容一樣,
protocol test;
pin = 5;
p = 3;
transitions = 2(5,0) (5,1);
如何在Java Swing GUI中處理此輸入並在那里產生輸出?
此外,如果我將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 ");}
;
並使用以下輸入來運行測試文件:
protocol test;
pin = 5;
p = 3;
transitions = 2(5,0) (5,1);
我得到以下輸出
(PROT test) (INITIALP 5) (PROC 3) (TRANSITIONS 2 5 0 5 1)
create protocol test
為什么在輸出中未顯示protocolWalker.g中的第二和第三個println?
有什么想法/幫助嗎?
再一次感謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.