语法:自上而下和自下而上的区别? (例子)
我从这个问题中了解到:
- 语法本身不是自上而下或自下而上的,解析器存在
- 可以被其中一个解析但不能被另一个解析的语法
- (感谢 Jerry Coffin
所以对于这个语法(所有可能数学公式):
E -> E T E
E -> (E)
E -> D
T -> + | - | * | /
D -> 0
D -> L G
G -> G G
G -> 0 | L
L -> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
这可以被自上而下和自下而上的解析器读取吗?
能说这是自上而下的语法还是自下而上的语法(或两者都不是)?
你
“为包含所有...的语言编写自上而下和自下而上的语法”(不同的问题)
我不确定这是否正确,因为似乎不存在自上而下和自下而上的语法语法。有人能澄清一下吗?
This is a follow up question from Grammar: difference between a top down and bottom up?
I understand from that question that:
- the grammar itself isn't top-down or bottom-up, the parser is
- there are grammars that can be parsed by one but not the other
- (thanks Jerry Coffin
So for this grammar (all possible mathematical formulas):
E -> E T E
E -> (E)
E -> D
T -> + | - | * | /
D -> 0
D -> L G
G -> G G
G -> 0 | L
L -> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Would this be readable by a top down and bottom up parser?
Could you say that this is a top down grammar or a bottom up grammar (or neither)?
I am asking because I have a homework question that asks:
"Write top-down and bottom-up grammars for the language consisting of all ..." (different question)
I am not sure if this can be correct since it appears that there is no such thing as a top-down and bottom-up grammar. Could anyone clarify?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种语法很愚蠢,因为它将词法分析和解析合而为一。但好吧,这是一个学术例子。
自下而上和自上而下的问题是,有一些特殊的极端情况,很难用正常的第一眼来实现。我可能认为你应该检查它是否有任何问题并更改语法。
为了理解你的语法,我写了一个正确的 EBNF,
我特别不喜欢规则
digits:digitsdigits
。目前尚不清楚第一个数字从哪里开始,第二个数字从哪里结束。我会将规则实施为另一个问题是
number: '0' | digital digitals;
这与digits: '0'
和digits: digital;
冲突。事实上,这是重复的。我会将规则更改为(删除数字):这使得语法 LR1(左递归,向前看)和上下文无关。这是您通常会提供给解析器生成器(例如 bison)的内容。由于 bison 是自下而上的,因此这是自下而上解析器的有效输入。
对于自上而下的方法,至少对于递归体面来说,左递归有点问题。如果您愿意,可以使用回滚,但对于这些,您需要 RR1(右递归前瞻)语法。为此,请交换递归:
我不确定这是否回答了您的问题。我认为这个问题的表述很糟糕并且具有误导性;我以编写解析器为生......
That grammar is stupid, since it unites lexing and parsing as one. But ok, it's an academic example.
The thing with bottoms-up and top-down is that is has special corner cases that are difficult to implement with you normal 1 look ahead. I probably think that you should check if it has any problems and change the grammar.
To understand you grammar I wrote a proper EBNF
I especially don't like the rule
digits: digits digits
. It is unclear where the first digits starts and the second ends. I would implement the rule asAn other problem is
number: '0' | digit digits;
This conflicts withdigits: '0'
anddigits: digit;
. As a matter of fact that is duplicated. I would change the rules to (removing digits):This makes the grammar LR1 (left recursive with one look ahead) and context free. This is what you would normally give to a parser generator such as bison. And since bison is bottoms up, this is a valid input for a bottoms-up parser.
For a top-down approach, at least for recursive decent, left recursive is a bit of a problem. You can use roll back, if you like but for these you want a RR1 (right recursive one look ahead) grammar. To do that swap the recursions:
I am not sure if that answers you question. I think the question is badly formulated and misleading; and I write parsers for a living...