我试图理解“确定性上下文无关语法”等表达中的“确定性”是什么意思。(在该字段中还有更多确定性的“事物”)。除了最详尽的解释之外,我将不胜感激一个例子!如果可能的话。
我困惑的主要根源在于无法分辨语法的这种性质与(非)歧义有何不同。
我最想找到的意思是D. Knuth 在论文《语言从左到右的翻译》中的这句话:
Ginsburg和Greibach(1965)定义了确定性语言的概念。我们在第五节中表明,这些正是存在LR(k)语法的语言
一旦到达Section V
,它就变成循环了,因为那里说LR(k)解析器可以解析的是确定性语言...
下面是一个示例,可以帮助我理解“歧义”的含义,请看一下:
onewartwoearewe
可以将其解析为- one war two ear ewe
或o new art woe are we
-如果语法允许(例如,它列出了我刚刚列出的所有单词)。
我需要做些什么才能使示例语言具有不确定性?(例如,我可以o
从语法中删除单词,以使语法不模糊)。
以上语言是确定性的吗?
PS。这个例子来自《哥德尔,埃舍尔,巴赫:永恒的金辫子》一书。
假设我们为示例语言定义了语法,如下所示:
S -> A 'we' | A 'ewe'
A -> B | BA
B -> 'o' | 'new' | 'art' | 'woe' | 'are' | 'one' | 'war' | 'two' | 'ear'
通过关于必须解析整个字符串的争论,这种语法是否会使语言具有不确定性?
let explode s =
let rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) [];;
let rec woe_parser s =
match s with
| 'w' :: 'e' :: [] -> true
| 'e' :: 'w' :: 'e' :: [] -> true
| 'o' :: x -> woe_parser x
| 'n' :: 'e' :: 'w' :: x -> woe_parser x
| 'a' :: 'r' :: 't' :: x -> woe_parser x
| 'w' :: 'o' :: 'e' :: x -> woe_parser x
| 'a' :: 'r' :: 'e' :: x -> woe_parser x
(* this line will trigger an error, because it creates
ambiguous grammar *)
| 'o' :: 'n' :: 'e' :: x -> woe_parser x
| 'w' :: 'a' :: 'r' :: x -> woe_parser x
| 't' :: 'w' :: 'o' :: x -> woe_parser x
| 'e' :: 'a' :: 'r' :: x -> woe_parser x
| _ -> false;;
woe_parser (explode "onewartwoearewe");;
- : bool = true
| Label | Pattern |
|---------+--------------|
| rule-01 | S -> A 'we' |
| rule-02 | S -> A 'ewe' |
| rule-03 | A -> B |
| rule-04 | A -> BA |
| rule-05 | B -> 'o' |
| rule-06 | B -> 'new' |
| rule-07 | B -> 'art' |
| rule-08 | B -> 'woe' |
| rule-09 | B -> 'are' |
| rule-10 | B -> 'one' |
| rule-11 | B -> 'war' |
| rule-12 | B -> 'two' |
| rule-13 | B -> 'ear' |
#+TBLFM: @2$1..@>$1='(format "rule-%02d" (1- @#));L
Generating =onewartwoearewe=
First way to generate:
| Input | Rule | Product |
|-------------------+---------+-------------------|
| '' | rule-01 | A'we' |
| A'we' | rule-04 | BA'we' |
| BA'we' | rule-05 | 'o'A'we' |
| 'o'A'we' | rule-04 | 'o'BA'we' |
| 'o'BA'we' | rule-06 | 'onew'A'we' |
| 'onew'A'we' | rule-04 | 'onew'BA'we' |
| 'onew'BA'we' | rule-07 | 'onewart'A'we' |
| 'onewart'A'we' | rule-04 | 'onewart'BA'we' |
| 'onewart'BA'we' | rule-08 | 'onewartwoe'A'we' |
| 'onewartwoe'A'we' | rule-03 | 'onewartwoe'B'we' |
| 'onewartwoe'B'we' | rule-09 | 'onewartwoearewe' |
|-------------------+---------+-------------------|
| | | 'onewartwoearewe' |
Second way to generate:
| Input | Rule | Product |
|-------------------+---------+-------------------|
| '' | rule-02 | A'ewe' |
| A'ewe' | rule-04 | BA'ewe' |
| BA'ewe' | rule-10 | 'one'A'ewe' |
| 'one'A'ewe' | rule-04 | 'one'BA'ewe' |
| 'one'BA'ewe' | rule-11 | 'onewar'A'ewe' |
| 'onewar'A'ewe' | rule-04 | 'onewar'BA'ewe' |
| 'onewar'BA'ewe' | rule-12 | 'onewartwo'A'ewe' |
| 'onewartwo'A'ewe' | rule-03 | 'onewartwo'B'ewe' |
| 'onewartwo'B'ewe' | rule-13 | 'onewartwoearewe' |
|-------------------+---------+-------------------|
| | | 'onewartwoearewe' |
B -> 'o'
,,则它将不再是模棱两可的……
S
。通过应用该规则S := ...
,我们得到...
……”