[編譯原理]2.語法分析(syntax analysis)


There are three general types of parsers for grammars

universal top-down bottom-up
Cocke-Younger-Kasami algorithm, Earley's algorithm
parse any grammar, inefficient work only for subclasses of grammars work only for subclasses of grammars

strategies for error recovery:panic-modephrase-level recovery,error-productions,global-correction
a compiler is expected to assist the programmer in locating and tracking down errors

error handler in a parser has goals:

  • Report the presence of errors clearly and accurately.
  • Recover from each error quickly enough to detect subsequent errors.
  • Add minimal overhead to the processing of correct programs.

一、context-free grammar

由一系列production組成

  • terminal symbols
  • nonterminals
  • productions
  • start symbol

Derivation: sentential form, sentence, left-sentential form, leftmost derivation, rightmost derivation

Every construct that can be described by a regular expression can be described by a grammar, but not vice-versa.

Immediate left recursion can be eliminated by the following technique

Algorithm 4.19, below, systematically eliminates left recursion from a grammar.


二、top-down parsing

top-down parsing method 有3種: recursive-descent parsing, predictive parsing 和 nonrecursive predictive parsing,
predictive parsing 是一種特殊的 recursive-descent parsing。
有一類 predictive parsing 一定能夠解析的 grammar 稱為 LL(1) grammar, left-recursive grammar 和 ambiguous grammar 一定不是 LL(1) grammar。

當且僅當一個 grammar \(G\) 滿足下列條件時,\(G\) 才能成為一個 LL(1) grammar:
\(A \to \alpha | \beta\) 是兩個不同的 productions, 則
1)\(FIRST(\alpha) \cap FIRST(\beta) = \emptyset\)
2)若 \(\epsilon \in FIRST(\beta)\), 則 \(FIRST(\alpha) \cap FOLLOW(\beta) = \emptyset\); \(\epsilon \in FIRST(\alpha)\) 同理。

為predictive parsing method 構造 parsing table 的算法:
輸入為 grammar \(G\),
輸出為 parsing table \(M\)
對 grammar \(G\) 中的所有 production \(A \to \alpha\) 執行以下兩步操作:

  1. \(\forall a \in FIRST(\alpha)\), 將\(A \to \alpha\)添加到 \(M[A,a]\)
  2. \(\epsilon \in FIRST(\alpha)\), 則 \(\forall b \in FOLLOW(\alpha)\), 將 \(A \to \alpha\)添加到 \(M[A,b]\)
    \(\exists A, a, s.t. M[A, a] = \emptyset\) , 則令\(M[A, a] =\) error

注意,對於 LL(1) grammar, table \(M\) 中的每一個 entry 至多包含一條 production。
考慮\(A \to \alpha | \beta\),我們來說明它們不可能出現在同一個 entry 中。
LL(1) grammar 的條件 1 說明,這兩條 production 經過步驟 1 ,不可能出現在同一個 entry 中
LL(1) grammar 的條件 1 說明\(FIRST(\alpha), FIRST(\beta)\) 不可能同時包含 \(\epsilon\), 所以兩條 production 至多只有一條能經過步驟 2 添加到 \(M\) 中,
而LL(1) grammar 的條件 2 又說明,經過步驟 2 添加到 \(M\) 中的那條 production 與另外一條 production 不可能出現在同一個 entry 中。

計算FIRST的方法

計算FOLLOW的方法


三、bottum-up parsing

下面介紹 3 種 bottum-up 方法,SLR, canonical LR (LR for short) 和 LALR, 它們都基於 shift-reduce 方法

我們先看一下 3 種 LR 型 parser 的共同點,然后再看它們之間的差異

1.LR 型 parser 的構造過程

\(item \to DFA \to parsing\) \(table\)
\(DFA\) 的一個 \(state\) 對應一個由若干 \(item\) 構成的一個 \(set\)

2.LR型 parser 的結構和工作原理


LR 型的 parser 結構如上圖所示,有一個存儲 state 的棧, 一塊存儲輸入的緩沖區, 還有一張用來做決策的 parsing table。
1)parser 每次都根據棧頂的 state 和緩沖區中下一個輸入的 terminal 去查詢 parsing table 的 action 區域,決定接下來是 shift, reduce, accept 還是 error。
2)如果是 reduce, 則要彈出棧頂代表 handle 的若干 state, 再根據新的棧頂 state 和用來代替 handle 的 nonterminal 去查詢 parsing table 的 goto 區域,並將跳轉的下一個 state 壓入棧頂或者是 error。
ps:從任意其它 state 進入 state j 一定是通過相同的 grammar symbol X。
pss:所有的 LR 型 grammar 都是 unambiguous 的。

3.SLR, LR 和 LALR 的區別

三種 paser 使用了不同的 item,

item parser grammar state size
LR(0) SLR SLR small
LR(1) LR LR large
LALR(1) LALR LALR small

An \(LR(0)\) \(item\) of a grammar \(G\) is a production of G with a dot at some position of the body. Thus, production \(A \to XYZ\) yields the four items
\(A \to ·XYZ\)
\(A \to X·YZ\)
\(A \to XY·Z\)
\(A \to XYZ·\)

An \(LR(1)\) \(item\) has the form \([A \to \alpha·\beta, a]\) , where \(A \to \alpha\beta\) is a production and \(a\) is a terminal or the right endmarker \(\$\).


Recall that in the SLR method, state $i$ calls for reduction by $A \to \alpha$ if the set of items $I_i$ contains item $[A \to \alpha·]$ and $a$ is in FOLLOW($A$). In some situations, however, when state $i$ appears on top of the stack, the viable prefix $\beta\alpha$ on the stack is such that $\beta A$ cannot be followed by $a$ in any right-sentential form.

maybe Follow(\(A\)) != Follow(\(\beta A\))

Sets of LR(1) items 構造方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM