简体   繁体   English

左递归文法的 LR(1) 项集

[英]LR(1) item sets for left recursive grammar

I read several papers about creating LR(1) item sets, but none of them pertained to left recursive grammars, such as those used for parsing expressions.我阅读了几篇关于创建 LR(1) 项集的论文,但没有一篇与左递归文法有关,例如用于解析表达式的文法。 If I had the following grammar,如果我有以下语法,

E -> E + T | T
T -> T * F | F
F -> (E) | NUMBER

How would I go about creating the LR(1) item sets?我 go 如何创建 LR(1) 项集?

Left recursion isn't inherently a problem for LR(1) parsers, and the rules for determining the configurating sets and lookaheads is the same regardless of whether your grammar has left recursion.左递归本质上不是 LR(1) 解析器的问题,无论您的语法是否左递归,确定配置集和前瞻的规则都是相同的。

In your case, we begin by augmenting the grammar with our new start symbol:在您的情况下,我们首先使用新的开始符号扩充语法:

S -> E
E -> E + T | T
T -> T * F | F
F -> (E) | NUMBER

Our initial configurating set corresponds to looking at the production S -> E with a lookahead of $ .我们的初始配置集对应于使用$的前瞻性查看生产S -> E Initially, that gives us the following:最初,这为我们提供了以下内容:

(1)
    S -> .E     [$]

We now need to expand out what E could be.我们现在需要扩展 E 可能是什么。 That gives us these new items:这给了我们这些新项目:

(1)
    S -> .E     [$]
    E -> .E + T [$]
    E -> .T     [$]

Now, let's look at the item E ->.E + T [$] .现在,让我们看看项目E ->.E + T [$] We need to expand out what E could be here, and the rules for doing so are the same as in the non-left-recursive case: we list all productions for E with the dot at the front, with a lookahead given by what follows the E in the production E ->.E + T [$] .我们需要扩展E在这里可能是什么,这样做的规则与非左递归情况相同:我们列出E的所有产生式,前面有一个点,并通过以下内容给出前瞻性产生式E E ->.E + T [$]中的 E。 In this case we're looking for an E with a lookahead of + because that's what follows is in the production.在这种情况下,我们正在寻找具有+前瞻性的E ,因为这就是生产中的内容。 That adds these items:这增加了这些项目:

(1)
    S -> .E     [$]
    E -> .E + T [$]
    E -> .T     [$]
    E -> .E + T [+]
    E -> .T     [+]

From here, we expand out all the cases where there's a dot before a T , which gives the following:从这里开始,我们扩展了T之前有一个点的所有情况,它给出了以下内容:

(1)
    S -> .E     [$]
    E -> .E + T [$]
    E -> .T     [$]
    E -> .E + T [+]
    E -> .T     [+]
    T -> .T * F [$]
    T -> .F     [$]
    T -> .T * F [+]
    T -> .F     [+]

We now have to expand out the T s in the context of T ->.T * F [$] , and we do so by listing all productions of T followed by what the T is followed by in T ->.T * F [$] (namely, * ).我们现在必须在T ->.T * F [$]的上下文中扩展T s,我们通过列出 T 的所有产生式以及TT ->.T * F [$] T紧随其后的内容来实现T ->.T * F [$] (即* )。 That gives us the following:这给了我们以下内容:

(1)
    S -> .E     [$]
    E -> .E + T [$]
    E -> .T     [$]
    E -> .E + T [+]
    E -> .T     [+]
    T -> .T * F [$]
    T -> .F     [$]
    T -> .T * F [+]
    T -> .F     [+]
    T -> .T * F [*]
    T -> .F     [*]

And from here we'd expand out the productions that have a dot before the F .从这里我们将扩展在F之前有一个点的产品。 Do you see how to do that based on how things have played out so far?根据目前的情况,您是否知道如何做到这一点?

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

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