简体   繁体   English

编译器:不包含epsilon的语法的第一套和第二套

[英]Compilers: First and Follow Sets of a grammar that does not contain epsilon

In my current compilers course, I've understood how to find the first and follow sets of a grammar, and so far all of the grammars I have dealt with have contained epsilon. 在当前的编译器课程中,我已经了解了如何查找语法的第一套和第二套,到目前为止,我所处理的所有语法都包含epsilon。 Now I am being asked to find the first and follow sets of a grammar without epsilon, and to determine whether it is LR(0) and SLR. 现在,我被要求查找没有epsilon的第一个和第二个语法集,并确定它是LR(0)和SLR。 Not having epsilon has thrown me off, so I don't know if I've done it correctly. 没有epsilon会让我失望,所以我不知道我做得是否正确。 I would appreciate any comments on whether I am on the right track with the first and follow sets, and how to begin determining if it is LR(0) 对于我是否在第一个和第二个集合上都处在正确的轨道上,以及如何开始确定它是否为LR(0),我将不胜感激。

Consider the following grammar describing Lisp arithmetic: 考虑以下描述Lisp算术的语法:

S -> E // S is start symbol, E is expression S-> E // S是开始符号,E是表达式

E -> (FL) // F is math function, L is a list E->(FL)// F是数学函数,L是列表

L -> LI | L-> LI | I // I is an item in a list 我//我是列表中的一个项目

I -> n | I-> n | E // an item is a number n or an expression E E //一个项目是数字n或表达式E

F -> + | F-> + | - | -| * *

FIRST: 第一:

FIRST(S)= FIRST(E) = {(} FIRST(S)= FIRST(E)= {(}

FIRST(L)= FIRST(I) = {n,(} FIRST(L)= FIRST(I)= {n,(}

FIRST(F) = {+, -, *} FIRST(F)= {+,-,*}

FOLLOW: 跟随:

FOLLOW(S) = {$} 跟随= {$}

FOLLOW(E) = FOLLOW(L) = {), n, $} FOLLOW(E)= FOLLOW(L)= {),n,$}

FOLLOW(I) = {),$} FOLLOW(I)= {),$}

FOLLOW(F) = {),$} FOLLOW(F)= {),$}

The FIRST sets are right, but the FOLLOW sets are incorrect. 第一组是正确的,但跟随组是不正确的。

The FOLLOW(S) = {$} is right, though technically this is for the augmented grammar S' -> S$ . FOLLOW(S)= {$}是正确的,尽管从技术上讲这是针对扩展语法S'-> S $。

E appears on the right side of S -> E and I -> E, both of which mean that the follow of that set is in the follow of E, so: FOLLOW(E) = FOLLOW(S) ∪ FOLLOW(I) . E出现在S-> E和I-> E的右侧,这两者都意味着该集合的跟随在E的跟随中,因此:FOLLOW(E)= FOLLOW(S)∪FOLLOW(I) 。

L appears on the right hand side of L -> LI, which gives FOLLOW(L) ⊇ FIRST(I) , and E -> (FL), which gives FOLLOW(L) ⊇ {)} . L出现在L-> LI的右侧,给出FOLLOW(L)⊇FIRST(I),而E->(FL)给出FOLLOW(L)⊇{)}。

I appears on the right side of L -> LI | 我出现在L-> LI |的右侧。 I , which gives FOLLOW(I) = FOLLOW(L) . I,它给出FOLLOW(I)= FOLLOW(L)。

F appears on the right side in E -> (FL) , which gives FOLLOW(F) = FIRST(L) F出现在E->(FL)的右侧,这给出FOLLOW(F)= FIRST(L)

Solving for these gives: 解决这些问题得到:

FOLLOW(F) = {n, (} FOLLOW(F)= {n,(}

FOLLOW(L) = FIRST(I) ∪ {)} = {n, (, )} FOLLOW(L)= FIRST(I)∪{}} = {n,(,)}

FOLLOW(I) = {n, (, )} FOLLOW(I)= {n,(,)}

FOLLOW(E) = {$} ∪ {n, (, )} = {n, (, ), $} FOLLOW(E)= {$}∪{n,(,)} = {n,(,),$}

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

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