本书以 编译原理及实践 为textbook
Chapter Three. Context-Free Grammers and Parsing
context-free grammer
pasre工作时需要一个character就调用scanner读入一个,而不是scanner结束后运行
error in the scanner:generator an error token and consume the offending character
error in the parser:report an error message,recover from the error and continue parsing
BNF范式:backus-Naur form
Context-Free Grammer:
(
V
,
Σ
,
S
,
→
)
(V,\Sigma,S,\rightarrow)
(V,Σ,S,→)
V
V
V:finite set of nonterminal symbols
Σ
\Sigma
Σ:finite set of terminal symbols
S
∈
V
S\in V
S∈V::distinguished nonterminal,the start symbol
→
⫅
V
×
(
V
∪
Σ
)
∗
\rightarrow \subseteqq V \times (V \cup \Sigma)^{*}
→⫅V×(V∪Σ)∗:finite relation,productions
Derivations:a sequence of replacements of structure by choices on the right-hand sides of grammer rules
L
(
G
)
=
{
s
∣
e
x
p
⇒
∗
s
}
L(G)=\{s|exp\Rightarrow ^{*} s\}
L(G)={s∣exp⇒∗s}
left recursive:the nonterminal A appears as the first symbol on the right-hand size of the rule defining A.
right recursive:the nonterminal A appears as the last symbol on the right-hand size of the rule defining A.
the leftmost derivation
the rightmost derivation
neither leftmost or rightmost derivation
abstract syntax tree
s;s;s;类似的抽象语法树为leftmost-child right-sibling
left associative
right associative
extended BNF(用于直接表达重复项,etc)
A
→
A
α
∣
β
A\rightarrow A \alpha | \beta
A→Aα∣β(left recursive) transform to
A
→
β
{
α
}
A\rightarrow \beta \{\alpha\}
A→β{α}
A
→
α
A
∣
β
A\rightarrow \alpha A| \beta
A→αA∣β(right recursive) transform to
A
→
{
α
}
β
A\rightarrow \{\alpha\} \beta
A→{α}β