2011-03-03 93 views
0
//complete 
start ::= template. 

//template 
template ::= template_elements. 
template ::= template template_elements. 
template ::= . 

//template elements 
template_elements(res) ::= COMMENT. 
template_elements(res) ::= tag(t). 

//tag 
tag(res) ::= LDEL exp(e) RDEL. 

//exp 
exp(res) ::= value(v). 
exp(res) ::= exp(e1) OP(o) exp(e2). 

//value 
value(res) ::= variable(v). 

//variable 
variable(res) ::= DOLLAR ID(i). 

任何人都知道衝突的位置在哪裏?爲什麼在我的小語法中有3個解析衝突?

UPDATE

如果我刪除exp(res) ::= exp(e1) OP(o) exp(e2).,就會有隻有兩個矛盾,但我不知道這是爲什麼造成衝突......

UPDATE2

爲什麼沒關係here

template ::= template_elements. 
template ::= template template_elements. 
template ::= . 
+0

嘗試一次刪除一條規則,看看會發生什麼。 – 2011-03-03 05:06:43

+1

Err,這個問題究竟與yacc有什麼關係?你似乎引用了一些先進的BNF參數化語法語言...... – DigitalRoss 2011-03-03 05:09:40

+0

我記得我在CS的舊日子裏認爲'exp(e1)OP(o)exp(e2)'可能會導致類似表達式的歧義exp(e1)OP(o)exp(e2)OP(o)exp(e3)'。不知道這是什麼意思? – mellamokb 2011-03-03 05:11:50

回答

0

要解決不確定性,請設置優先級和關聯性 - 請參閱優先級規則下的documentation

檸檬可處理左遞歸,但你的template規則應該是

template ::= template template_elements. 
template ::= . 

因爲template可以是空的,你不需要template ::= template_elements情況。

做出這些更改後會出現什麼錯誤?

+0

經過'%left'和這個變化,現在只有1個衝突..'template :: = template_elements.' – yoyo 2011-03-03 05:28:57

+0

嘗試擺脫空的模板規則,'template: := .'並允許'template_elements'爲空。 – tgdavies 2011-03-03 05:30:17

+0

如果我這樣做,它會起作用,但它爲什麼會導致衝突? – yoyo 2011-03-03 05:32:02

相關問題