语法:自上而下和自下而上的区别? (例子)

发布于 2024-09-08 10:14:42 字数 848 浏览 8 评论 0原文

这是来自 语法:自上而下之间的差异的后续问题和自下而上?

我从这个问题中了解到:

  • 语法本身不是自上而下或自下而上的,解析器存在
  • 可以被其中一个解析但不能被另一个解析的语法
  • (感谢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末骤雨初歇 2024-09-15 10:14:42

这种语法很愚蠢,因为它将词法分析和解析合而为一。但好吧,这是一个学术例子。

自下而上和自上而下的问题是,有一些特殊的极端情况,很难用正常的第一眼来实现。我可能认为你应该检查它是否有任何问题并更改语法。

为了理解你的语法,我写了一个正确的 EBNF,

expr:
    expr op expr |
    '(' expr ')' |
    number;

op:
    '+' |
    '-' |
    '*' |
    '/';

number:
    '0' |
    digit digits;

digits:
    '0' |
    digit |
    digits digits;

digit:
    '1' | 
    '2' | 
    '3' | 
    '4' | 
    '5' | 
    '6' | 
    '7' | 
    '8' | 
    '9'; 

我特别不喜欢规则 digits:digitsdigits。目前尚不清楚第一个数字从哪里开始,第二个数字从哪里结束。我会将规则实施为

digits:
    '0' |
    digit |
    digits digit;

另一个问题是 number: '0' | digital digitals; 这与 digits: '0'digits: digital; 冲突。事实上,这是重复的。我会将规则更改为(删除数字):

number:
    '0' |
    digit |
    digit zero_digits;

zero_digits:
    zero_digit |
    zero_digits zero_digit;

zero_digit:
    '0' |
    digit;

这使得语法 LR1(左递归,向前看)和上下文无关。这是您通常会提供给解析器生成器(例如 bison)的内容。由于 bison 是自下而上的,因此这是自下而上解析器的有效输入。

对于自上而下的方法,至少对于递归体面来说,左递归有点问题。如果您愿意,可以使用回滚,但对于这些,您需要 RR1(右递归前瞻)语法。为此,请交换递归:

zero_digits:
    zero_digit |
    zero_digit zero_digits;

我不确定这是否回答了您的问题。我认为这个问题的表述很糟糕并且具有误导性;我以编写解析器为生......

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

expr:
    expr op expr |
    '(' expr ')' |
    number;

op:
    '+' |
    '-' |
    '*' |
    '/';

number:
    '0' |
    digit digits;

digits:
    '0' |
    digit |
    digits digits;

digit:
    '1' | 
    '2' | 
    '3' | 
    '4' | 
    '5' | 
    '6' | 
    '7' | 
    '8' | 
    '9'; 

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 as

digits:
    '0' |
    digit |
    digits digit;

An other problem is number: '0' | digit digits; This conflicts with digits: '0' and digits: digit;. As a matter of fact that is duplicated. I would change the rules to (removing digits):

number:
    '0' |
    digit |
    digit zero_digits;

zero_digits:
    zero_digit |
    zero_digits zero_digit;

zero_digit:
    '0' |
    digit;

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:

zero_digits:
    zero_digit |
    zero_digit zero_digits;

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文