问题描述
我正在尝试使用以下规则集创建 ANTLR v4 语法:
I'm trying to create an ANTLR v4 grammar with the following set of rules:
1.如果一行以@开头,就被认为是一个标签:
1.In case a line starts with @, it is considered a label:
@label
2.如果该行以cmd开头,则视为命令
2.In case the line starts with cmd, it is treated as a command
cmd param1 param2
3.如果一行以空格开头,则视为字符串.应提取所有文本.字符串可以是多行的,所以它们以空行结束
3.If a line starts with a whitespace, it is considered a string. All the text should be extracted. Strings can be multiline, so they end with an empty line
A long string with multiline support
and any special characters one can imagine.
<-empty line here->
4.最后,如果一行以空格开头,@
和 cmd
,它的第一个词应该被视为标题.
4.Lastly, in case a line starts with anything but whitespace, @
and cmd
, it's first word should be considered a heading.
Heading A long string with multiline support
and any special characters one can imagine.
<-empty line here->
处理标签和命令很容易.但我对字符串和标题一无所知.分隔 whitespace word whitespacewhat doubleNewline
和 whatever doubleNewline
的最佳方法是什么?我见过很多带有空格的示例,但它们都不适用于随机文本和换行符.我不希望你为我编写实际的代码.建议一种方法就行了.
It was easy to handle lables and commands. But I am clueless about strings and headings.What is the best way to separate whitespace word whitespace whatever doubleNewline
and whatever doubleNewline
? I've seen a lot of samples with whitespaces, but none of them works with both random text and newlines. I don't expect you to write actual code for me. Suggesting an approach will do.
推荐答案
这样的事情应该可以解决问题:
Something like this should do the trick:
lexer grammar DemoLexer;
LABEL
: '@' [a-zA-Z]+
;
CMD
: 'cmd' ~[\r\n]+
;
STRING
: ' ' .*? NL NL
;
HEADING
: ( ~[@ \t\r\nc] | 'c' ~'m' | 'cm' ~'d' ).*? NL NL
;
SPACE
: [ \t\r\n] -> skip
;
OTHER
: .
;
fragment NL
: '\r'? '\n'
| '\r'
;
这并不强制要求行的开头";要求.如果这是你想要的东西,你就必须在你的语法中添加语义谓词,这将它与目标语言联系起来.对于 Java,这看起来像这样:
This does not mandate the "beginning of the line" requirement. If that is something you want, you'll have to add semantic predicates to your grammar, which ties it to a target language. For Java, that would look like this:
LABEL
: {getCharPositionInLine() == 0}? '@' [a-zA-Z]+
;
见:
这篇关于处理以空格开头的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!