📜  FOLLOW 语法分析中的设置

📅  最后修改于: 2021-09-28 11:04:50             🧑  作者: Mango

我们已经讨论了以下有关语法分析的主题。

语法分析简介
为什么先跟后?
语法分析中的第一个集合

在这篇文章中,讨论了 FOLLOW 集。

Follow(X)是可以以某种句子形式直接出现在非终端 X 右侧的终端集。
例子:

S ->Aa | Ac
A ->b  

      S                  S  
     /  \              /   \
    A    a            A     C  
    |                 |
    b                 b   

Here, FOLLOW (A) = {a, c}

计算 FOLLOW 集的规则:

1) FOLLOW(S) = { $ }   // where S is the starting Non-Terminal

2) If A -> pBq is a production, where p, B and q are any grammar symbols,
   then everything in FIRST(q)  except Є is in FOLLOW(B).

3) If A->pB is a production, then everything in FOLLOW(A) is in FOLLOW(B).

4) If A->pBq is a production and FIRST(q) contains Є, 
   then FOLLOW(B) contains { FIRST(q) – Є } U FOLLOW(A) 

示例 1:

Production Rules:
E -> TE’
E’ -> +T E’|Є
T -> F T’
T’ -> *F T’ | Є
F -> (E) | id

FIRST set
FIRST(E) = FIRST(T) = { ( , id }
FIRST(E’) = { +, Є }
FIRST(T) = FIRST(F) = { ( , id }
FIRST(T’) = { *, Є }
FIRST(F) = { ( , id }

FOLLOW Set
FOLLOW(E)  = { $ , ) }  // Note  ')' is there because of 5th rule
FOLLOW(E’) = FOLLOW(E) = {  $, ) }  // See 1st production rule
FOLLOW(T)  = { FIRST(E’) – Є } U FOLLOW(E’) U FOLLOW(E) = { + , $ , ) }
FOLLOW(T’) = FOLLOW(T) =      { + , $ , ) }
FOLLOW(F)  = { FIRST(T’) –  Є } U FOLLOW(T’) U FOLLOW(T) = { *, +, $, ) }

示例 2:

Production Rules:
S -> aBDh
B -> cC
C -> bC | 
D -> EF
E -> g | Є
F -> f | Є

FIRST set
FIRST(S) = { a }
FIRST(B) = { c }
FIRST(C) = { b , Є }
FIRST(D) = FIRST(E) U FIRST(F) = { g, f, Є }
FIRST(E) = { g , Є }
FIRST(F) = { f , Є }

FOLLOW Set
FOLLOW(S) = { $ } 
FOLLOW(B) = { FIRST(D) – Є } U FIRST(h) = { g , f , h }
FOLLOW(C) = FOLLOW(B) = { g , f , h }
FOLLOW(D) = FIRST(h) = { h }
FOLLOW(E) = { FIRST(F) – Є } U FOLLOW(D) = { f , h }
FOLLOW(F) = FOLLOW(D) = { h } 

示例 3:

Production Rules:
S -> ACB|Cbb|Ba
A -> da|BC
B-> g|Є
C-> h| Є

FIRST set
FIRST(S) = FIRST(A) U FIRST(B) U FIRST(C) = { d, g, h, Є, b, a}
FIRST(A) = { d } U {FIRST(B)-Є} U FIRST(C) = { d, g, h, Є }
FIRST(B) = { g, Є }
FIRST(C) = { h, Є }

FOLLOW Set
FOLLOW(S) = { $ }
FOLLOW(A)  = { h, g, $ }
FOLLOW(B) = { a, $, h, g }
FOLLOW(C) = { b, g, $, h }

笔记 :

  1. Є 作为 FOLLOW 并不意味着什么(Є 是一个空字符串)。
  2. $ 称为结束标记,表示输入字符串的结束,因此在解析时用于指示输入字符串已被完全处理。
  3. 上面使用的语法是上下文无关语法(CFG)。可以使用 CFG 指定编程语言的语法。
  4. CFG 的形式为 A -> B ,其中 A 是单个非终端,而 B 可以是一组语法符号(即终端和非终端)