2010-11-01 51 views
1

(这一切首先是不是硬件,我知道所有的答案)BNF语法和操作关联性

我有一个简单的BNF语法

<UNIT> ::= (<CLAUSE>) | a | b | c 
<ITEM> ::= not <UNIT> | <UNIT> 
<CLAUSE> ::= <CLAUSE> and <PHRASE> | <PHRASE> 
<PHRASE> ::= <ITEM> | <ITEM> or <PHRASE> 

and运算符是左关联的(左手递归) or运算符是右结合(这一次,它是右手递归)

鉴于表达c and b or not a and (not b or c),为什么是最合适的“和”是在分析树高?
的方式,我看到c **and** b or not a and (not b or c)左边最高应该是在解析树中更高。

我们的教授提供了这样的回答:

这里是在lispy符号解析树。

(clause (clause (clause (phrase (item (unit 'c')))) 
'and' 
(phrase (item (unit 'b')) 
'or' 
(phrase (item 'not' 
(unit 'a'))))) 
**'and'** // is higher in parse tree 
(phrase (item (unit '(' 
(clause (phrase (item 'not’(unit 'b')) 
'or' 
(phrase (item (unit 'c'))))) 
')')))) 

回答

1

给出的BNF语法看起来与解析树一致,并且与“and”应该是左关联的说法一致。如果你想制作“a和b和c”使用这种语法,从“条款”,你开始是这样的:

  1. 条款
  2. 条款短语

在其中点,短语不能成为“b和c”(无括号),因为只有子句可以产生“和”。短语必须发展为“c”,而第二行的条款可以变成“a和b”。这将迫使最右边的“和”在解析树中更高。

由于解析树中的更高元素是最后一个评估对象,这与运算符“和”保持关联的说法是一致的。