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