G'day!如何在ANTLR中构造一个干净的,像Python一样的语法?
如何构造一个简单的ANTLR语法来处理多行表达式而不需要分号或反斜线?
我试图写一个简单的DSL的表达式:
# sh style comments
ThisValue = 1
ThatValue = ThisValue * 2
ThisOtherValue = (1 + 2 + ThisValue * ThatValue)
YetAnotherValue = MAX(ThisOtherValue, ThatValue)
总之,我希望我的应用程序提供了一些初步命名值的脚本,然后拉出最后的结果。然而,我越来越挂在语法上。我想支持像多行表达式如下:
# Note: no backslashes required to continue expression, as we're in brackets
# Note: no semicolon required at end of expression, either
ThisValueWithAReallyLongName = (ThisOtherValueWithASimilarlyLongName
+AnotherValueWithAGratuitouslyLongName)
我开始了与ANTLR语法是这样的:
exprlist
: (assignment_statement | empty_line)* EOF!
;
assignment_statement
: assignment NL!?
;
empty_line
: NL;
assignment
: ID '=' expr
;
// ... and so on
这看似简单,但我已经与烦恼换行:
warning(200): StackOverflowQuestion.g:11:20: Decision can match input such as "NL" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
图形,在org.antlr.works.IDE:
我已经戒了周围的语法,但总是与违反预期的行为的结束:
- 在文件到底是不是需要一个换行符
- 空行是可以接受的
- 从英镑符号开始的一行中的所有内容都将作为注释被丢弃
- 赋值以行尾而非分号结尾
- 如果包含在括号中,表达式可以跨越多行ets
我可以找到具有许多这些特征的示例ANTLR语法。我发现,当我削减他们的表现力,以限制他们的需求时,我最终打破了一些东西。其他人太简单了,当我添加表现力的时候我会打破他们。
这个语法应该采用哪个角度?你能指出任何既不平凡又不完整的图灵完整语言的例子吗?
现在我需要弄清楚如何让tokenizer完成那个繁重的工作。回到文档,我想。 :) – 2009-07-23 11:45:50