我猜两种方法都应用了相同的逻辑,即用生产规则中提供的相应非终端元素替换匹配的字符串。
为什么他们会对LL as top down
和LR as bottom-up
进行分类?
答案 0 :(得分:4)
自下而上解析(也称为 shift-reduce parsing)是一种策略 用于分析未知数据 尝试的关系 确定最基本的单位 首先,然后推断更高阶 他们的结构。它试图 从一开始就向上建树 符号
自上而下的解析是一种策略 分析未知数据关系 通过假设一般的解析树 结构,然后考虑 是否已知基础 结构与之相容 假设
答案 1 :(得分:0)
自上而下解析 涉及从第一个非终端生成字符串。 示例:递归下降解析,非递归下降解析,LL解析等。 左递归和左保理的语法不起作用。 可能会发生回溯。 使用最左派的
答案 2 :(得分:-1)
兴趣博客 自上而下解析和自下而上解析之间的区别
给定一个正式的语法和该语法产生的字符串,解析就是找出该字符串的生成过程。
在无上下文语法的情况下,生产过程采用解析树的形式。在我们开始之前,我们总是知道关于解析树的两件事:根节点,它是最初从中派生字符串的初始符号,以及叶节点,它们是按顺序排列的字符串的所有字符。我们不知道的是它们之间的节点和分支的布局。
例如,如果字符串是acddf,我们已经知道了很多:
S
/ | \
???
| | | | | a c d d f
本文中使用的示例语法
S → xyz | aBC
B → c | cd
C → eg | df
自下而上解析
这种方法与解决拼图游戏并无二致。我们从包含单个字符的解析树底部开始。然后我们使用规则将字符连接成更大的标记。在字符串的末尾,一切都应该组合成一个大的S,而S应该是我们唯一留下的东西。如果没有,则需要回溯并尝试以不同方式组合令牌。
通过自下而上的解析,我们通常会维护一个堆栈,这是我们到目前为止看到的字符和令牌列表。在每一步中,我们将一个新字符移到堆栈上,然后通过将字符组合成更大的标记来尽可能地减少。 实施例
字符串是acddf。 步骤
ε can't be reduced
a can't be reduced
ac can be reduced, as follows:
reduce ac to aB
aB can't be reduced
aBd can't be reduced
aBdd can't be reduced
aBddf can be reduced, as follows:
reduce aBddf to aBdC
aBdC can't be reduced
End of string. Stack is aBdC, not S. Failure! Must backtrack.
aBddf can't be reduced
ac can't be reduced
acd can be reduced, as follows:
reduce acd to aB
aB can't be reduced
aBd can't be reduced
aBdf can be reduced, as follows:
reduce aBdf to aBC
aBC can be reduced, as follows:
reduce aBC to S
End of string. Stack is S. Success!
解析树
| 一个
| | a c
乙 | | a c
乙 | | | a c d
乙 | | | | a c d d
乙 | | | | | a c d d f
B C. | | | | \ a c d d f
| | a c
| | | a c d
B
| / | a c d
B
| / | | a c d d
B
| / | | | a c d d f
B C
| / | | \ a c d d f
S
/ | \ / | | / B C. | / | | \ a c d d f
示例2
如果所有组合都失败,则无法解析字符串。
字符串是acdg。 步骤
ε can't be reduced
a can't be reduced
ac can be reduced, as follows:
reduce ac to aB
aB can't be reduced
aBd can't be reduced
aBdg can't be reduced
End of string. Stack is aBdg, not S. Failure! Must backtrack.
ac can't be reduced
acd can be reduced, as follows:
reduce acd to aB
aB can't be reduced
aBg can't be reduced
End of string. stack is aBg, not S. Failure! Must backtrack.
acd can't be reduced
acdg can't be reduced
End of string. Stack is is acdg, not S. No backtracking is possible. Failure!
解析树
| 一个
| | a c
乙 | | a c
乙 | | | a c d
乙 | | | | a c d g
| | a c
| | | a c d
B
| / | a c d
B
| / | | a c d g
| | | a c d
| | | | a c d g
自上而下解析
对于这种方法,我们假设字符串与S匹配,并查看此假设的内部逻辑含义。例如,字符串逻辑上匹配S的事实意味着(1)字符串匹配xyz或(2)字符串匹配aBC。如果我们知道(1)不是真的,那么(2)必须是真的。但是(2)有其自身的进一步逻辑含义。必须在必要时检查这些以证明基本断言。 实施例
字符串是acddf。 步骤
Assertion 1: acddf matches S
Assertion 2: acddf matches xyz:
Assertion is false. Try another.
Assertion 2: acddf matches aBC i.e. cddf matches BC:
Assertion 3: cddf matches cC i.e. ddf matches C:
Assertion 4: ddf matches eg:
False.
Assertion 4: ddf matches df:
False.
Assertion 3 is false. Try another.
Assertion 3: cddf matches cdC i.e. df matches C:
Assertion 4: df matches eg:
False.
Assertion 4: df matches df:
Assertion 4 is true.
Assertion 3 is true.
Assertion 2 is true.
Assertion 1 is true. Success!
解析树
S
|
S
/ | \ 一个B C. | |
S
/ | \ 一个B C. | | ç
S
/ | \ 一个B C. / | | c d
S
/ | \ 一个B C. / | | \ c d d f
示例2
如果在遵循每个逻辑引导之后,我们无法证明基本假设("字符串匹配S")则无法解析字符串。
字符串是acdg。 步骤
Assertion 1: acdg matches S:
Assertion 2: acdg matches xyz:
False.
Assertion 2: acdg matches aBC i.e. cdg matches BC:
Assertion 3: cdg matches cC i.e. dg matches C:
Assertion 4: dg matches eg:
False.
Assertion 4: dg matches df:
False.
False.
Assertion 3: cdg matches cdC i.e. g matches C:
Assertion 4: g matches eg:
False.
Assertion 4: g matches df:
False.
False.
False.
Assertion 1 is false. Failure!
解析树
S
|
S
/ | \ 一个B C. | |
S
/ | \ 一个B C. | | ç
S
/ | \ 一个B C. / | | c d
为什么左递归是自上而下解析器的问题
如果我们的规则是左递归的,例如:
S → Sb
然后注意我们的算法的行为: 步骤
Assertion 1: acddf matches S:
Assertion 2: acddf matches Sb:
Assertion 3: acddf matches Sbb:
Assertion 4: acddf matches Sbbb:
...and so on forever
解析树
取值 |
取值 | \ S b |
取值 | \ S b | \ S b |
取值 | \ S b | \ S b | \ S b |
...