且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GRUN for Antlr4:如何使用?

更新时间:2023-02-09 13:31:24

假设您已经设置了 grun 别名(如果没有,请参阅本页顶部的 QuickStart https://www.antlr.org):

您想要的是查看由 Lexer 处理您的输入生成的令牌流(而不是您的 qwe.tokens 文件)

qwe.txt:

total_sales>qwe

ANTLR on  master [✘+?]➜ antlr4 qwe.g4ANTLR on  master [✘+?]➜ javac *.javaANTLR on  master [✘+?]➜ grun qwe 令牌 -tokens ,1:0][@1,11:11='>',,1:11][@2,12:14='qwe',,1:12][@3,15:14='<EOF>',<EOF>,1:15]

正如你所看到的...... total_salesqwe 都被识别为 COLUMN 令牌,

For grammar:

grammar qwe;

query
    : COLUMN OPERATOR value EOF
    ;

COLUMN
    : [a-z_]+
    ;

OPERATOR
    : ('='|'>'|'<')
    ;

SCALAR
    : [a-z_]+
    ;

value
    : SCALAR
    ;

WS : [ \t\r\n]+ -> skip ;

there are identical rules COLUMN and SCALAR. Here I was advised to use grun aliases.

I installed it for my Ubuntu. And for folders structure:

ran this from project learning_antlr4 level:

grun qwe tokens -tokens < qwe/qwe.tokens

The output was empty.

What do I wrong? Where that aliases are saved?

Assuming you have the grun alias set up (if not, see the QuickStart at the tops of this page https://www.antlr.org):

What you want is to view the token stream produced by the Lexer processing your input (not your qwe.tokens file)

qwe.txt:

total_sales>qwe

ANTLR on  master [✘+?] 
➜ antlr4 qwe.g4      

ANTLR on  master [✘+?] 
➜ javac *.java

ANTLR on  master [✘+?] 
➜ grun qwe tokens -tokens < qwe.txt
[@0,0:10='total_sales',<COLUMN>,1:0]
[@1,11:11='>',<OPERATOR>,1:11]
[@2,12:14='qwe',<COLUMN>,1:12]
[@3,15:14='<EOF>',<EOF>,1:15]

AS you can see... both total_sales and qwe are recognized as COLUMN tokens,