如何检查行的第一个字符是否为"*"?在ANTLR4中? [英] How can I check if first character of a line is "*" in ANTLR4?

查看:48
本文介绍了如何检查行的第一个字符是否为"*"?在ANTLR4中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一种相对简单但特有的语言编写解析器.

I am trying to write a parser for a relatively simple but idiosyncratic language.

简单地说,其中一个规则是:如果注释行是该行的第一个字符,则仅用星号 表示注释行.我该如何在ANTLR4中正式制定这样的规则?我考虑过使用:

Simply put, one of the rules is that comment lines are denoted by an asterisk only if that asterisk is the first character of the line. How might I go about formalising such a rule in ANTLR4? I thought about using:

START_LINE_COMMENT: '\n*' .*? '\n' -> skip; 

但是我敢肯定这不会连续使用多行注释,因为末尾的换行符将作为START_LINE_COMMENT令牌的一部分使用,这意味着以后的任何注释行都将缺少必需的注释初始换行符,它将不起作用.有没有一种方法可以检查行是否以'*'开头而不需要消耗先前的'\n'?

But I am certain this won't work with more than one line comment in a row, as the newline at the end will be consumed as part of the START_LINE_COMMENTtoken, meaning any subsequent comment lines will be missing the required initial newline character, which won't work. Is there a way I can perhaps check if the line starts with a '*' without needing to consume the prior '\n'?

推荐答案

匹配注释行并不容易.每年写一本语法时,我不得不抓住权威ANTLR参考刷新我的大脑.试试这个:

Matching a comment line is not easy. As I write one grammar per year, I had to grab to The Definitive ANTLR Reference to refresh my brain. Try this :

grammar Question;

/* Comment line having an * in column 1. */

question
    :   line+
    ;

line
//    :   ( ID | INT )+
    :   ( ID | INT | MULT )+
    ;

LINE_COMMENT
    :   '*' {getCharPositionInLine() == 1}? ~[\r\n]* -> channel(HIDDEN) ;
ID  :   [a-zA-Z]+ ;
INT :   [0-9]+ ;
//WS  :   [ \t\r\n]+ -> channel(HIDDEN) ;
WS  :   [ \t\r\n]+ -> skip ;
MULT : '*' ;

编译并执行:

$ echo $CLASSPATH
.:/usr/local/lib/antlr-4.6-complete.jar:
$ alias
alias a4='java -jar /usr/local/lib/antlr-4.6-complete.jar'
alias grun='java org.antlr.v4.gui.TestRig'
$ a4 Question.g4 
$ javac Q*.java
$ grun Question question -tokens data.txt 
[@0,0:3='line',<ID>,1:0]
[@1,5:5='1',<INT>,1:5]
[@2,9:12='line',<ID>,2:2]
[@3,14:14='2',<INT>,2:7]
[@4,16:26='* comment 1',<LINE_COMMENT>,channel=1,3:0]
[@5,32:35='line',<ID>,4:4]
[@6,37:37='4',<INT>,4:9]
[@7,39:48='*comment 2',<LINE_COMMENT>,channel=1,5:0]
[@8,51:78='* comment 3 after empty line',<LINE_COMMENT>,channel=1,7:0]
[@9,81:81='*',<'*'>,8:1]
[@10,83:85='not',<ID>,8:3]
[@11,87:87='a',<ID>,8:7]
[@12,89:95='comment',<ID>,8:9]
[@13,97:100='line',<ID>,9:0]
[@14,102:102='9',<INT>,9:5]
[@15,107:107='*',<'*'>,9:10]
[@16,109:110='no',<ID>,9:12]
[@17,112:118='comment',<ID>,9:15]
[@18,120:119='<EOF>',<EOF>,10:0]

具有以下data.text文件:

with the following data.text file :

line 1
        line 2
* comment 1
    line 4
*comment 2

* comment 3 after empty line
 * not a comment
line 9    * no comment

请注意,解析器规则中某处没有MULT令牌或'*'时,令牌中未列出星号,但解析器会抱怨:

Note that without the MULT token or '*' somewhere in a parser rule, the asterisk is not listed in the tokens, but the parser complains :

line 8:1 token recognition error at: '*'

如果显示解析树

$ grun Question question -gui data.txt

您将看到整个文件被一条线规则吸收.如果您需要识别线条,请像这样更改线条和空白规则:

you'll see that the whole file is absorbed by one line rule. If you need to recognize lines, change the line and white space rules like so :

line
    :   ( ID | INT | MULT )+ NL
    |   NL
    ;

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

这篇关于如何检查行的第一个字符是否为"*"?在ANTLR4中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆