简体   繁体   English

这个语法是递归还是因子?

[英]Is this grammar left recursive or factor?

I am new to this topic of left recursion and left factoring, please help me in determining whether this grammar is left recursive or left factored, if it is then why ? 我对左递归和左分解这个主题还不熟悉,请帮助我确定该语法是左递归还是左分解,如果是,为什么呢?

S-> aAd | S-> aAd | bBd | bBd | aBe | 阿贝| bAe | 婴儿| cA | cA | cB B

Is it Left recursive? 它是左递归的吗? Answer: No. 答:不可以。

By definition, "A grammar is left-recursive if we can find some non-terminal A which will eventually derive a sentential form with itself as the left-symbol." 按照定义,“如果我们能找到一些非终结符A,那么语法最终将是左递归的,它最终会得出一个以其自身为左符号的句子形式。”

example: Immediate left recursion occurs in rules of the form 示例:立即左递归发生在以下形式的规则中

A -> Aa | b

where a and b are sequences of nonterminals and terminals, and b doesn't start with A . 其中ab是非终结点和终结点的序列,而b不是以A开头。

Clearly not the case in: 显然情况并非如此:

S-> aAd | S-> aAd | bBd | bBd | aBe | 阿贝| bAe | 婴儿| cA | cA | cB B

Is it Left factored? 是左因素吗? Answer: Yes. 答:可以。

By definition, in left factoring, it is not clear which two alternative production to choose, to expand a non-terminal. 根据定义,在左分解中,不清楚要扩展非终端的两个替代生产。

This ambiguity occurs, when you have two alternative production that starts with same terminal/non-terminal. 当您有两个以相同终端/非终端开始的替代产品时,就会出现这种歧义。 In your case, I can see that thrice, two alternative paths: 在您的情况下,我可以看到两条替代路径:

S-> aAd | aBe 

S-> bBd |  bAe 

S-> cA | cB

If I remove the left factoring then the grammar becomes: 如果删除左因子,则语法变为:

S-> aA'

A'-> Ad | Be

S-> bB'

B'-> Bd | Ae

S-> cC'

C'-> A | B

This slide explains the same in simpler words 这张幻灯片用简单的语言解释了同样的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM