Compilation Principle: From Getting Started to Giving Up
1、 What is compilation (understanding)
1.1 Computer programming language and compilation
1.2 The compiler's position in the language processing system
1.3 Compiling System Structure
1.4 Examples of manual English Chinese translation
1.5 Compiler structure
2、 Grammar (Mastery)
-
Understanding Terminators and Non Terminators -
Types of grammar -
Judge whether a string is a sentence pattern of a grammar
2.1 Understanding terminators and non terminators
Grammar G2 [S] is: S->Ap S->Bq A->a A->cA B->b B->dB
2.2 Types of grammar
2.2.1 Understanding of Several Grammars
-
Type-0 grammar
-
Type 1 grammar
-
Type 2 Grammar
-
Type 3 Grammar
A-> ε| aB B->Ab|a
2.2.2 Relationship of several grammars
3、 Normal form (master)
3.1 Conversion between formal form and formal grammar
|Grammar production| | ----- | ---------- | ------ | |Rule 1 | A ->xB, B ->y | A=xy| |Rule 2 | A ->xA | y | A=x ^ * y| |Rule 3 | A ->x, A ->y | A=x | y|
Grammar G [S]: S ->xSx | y describes the language _____ (n>=0) A.(xyx)^* B.xyx^* C.xy^*x D.x^*yx^*
The regular expression of language L={a ^ mb ^ n | m>=0, n>=1} is _____. A.a^*bb^* B.aa^*bb^* C.aa^*b^* D.a^*b^*
4、 Finite automaton (master)
-
Definition of NFA and DFA -
NFA converted to DFA -
Transformation between regular expressions and finite automata
4.1 Definition of NFA and DFA
4.1.1 Definition of Determined Finite Automata (DFA)
S is a finite set of states
∑ is an alphabet, and each element of it is called an input character
F is a slave S ✖∑ Single value partial mapping to S, f (S, a)=s' means that when the current status is s and the input character is a, the status will be transferred to the next status s'. We call s' a successor state of s.
S0 ∈ S is the only initial state.
Z ⊆ S is a final state set.
DFA=({S, A, B, C, f}, {1,0}, F, S, {f}). To avoid confusion, F is represented by K below. Where: K (S, 0)=B, K (S, 1)=A, K (A, 0)=f, K (A, 1)=C, K (B, 0)=C, K (B, 1)=f, k (C, 1)=f;
4.1.2 Definition of uncertain finite automata (NFA)
S is a finite set of states
∑ is an alphabet, and each element of it is called an input character
F is a slave S ✖∑ Single value partial mapping to S, f (S, a)=s' means that when the current status is s and the input character is a, the status will be transferred to the next status s'. We call s' a successor state of s.
S0 ⊆ S is a non empty initial state set.
Z ⊆ S is a final state set.
4.2 Conversion of NFA to DFA
| | |
---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
(1)A.2 B.4 C.3 D.5 (2)A.1,3,4,5,Z B.2,3 C.6 D.4,5,Z (3)A. {Z} B.{6} C.{4,5,Z} D.{}
4.3 Conversion between normal form and finite automata
(_|a)(_|a|d)^*
A.0*|(0|1)0 B.(0|10)* C.0*((0|1)0)* D.0* (10)*
5、 Syntax derivation tree (master)
5.1 Syntax tree
Each node has a mark, which is a symbol of V; The root is marked with S; If a node n has at least one descendant except itself, and is marked with A, then A must be in Vn; If the direct descendants of node n, from left to right, are nodes n1, n2... nk, and their marks are A1, A2... Ak, then A ->A1, A2... Ak, must be a production formula in P.
If grammar G={{a, b}, {S, A}, S, P}, where: S ->aAS | a; A->SbA|SS|ba; Please construct the derivation tree of sentence pattern aabAa.
5.2 Phrases, simple phrases, handles
answer:
Phrase: a1 ɛ、 b1、b2、a2、a3
Direct phrase: because the leaf node of this tree is derived through several steps without one step, there is no direct phrase.
Handle: find the leftmost direct phrase from these direct phrases, that is, handle. The handle of this question is a1
6、 LL (1) Grammar (Mastery)
-
Judge whether it is LL (1) grammar -
Find the FIRST, FOLLOW and SELECT sets of the generative expression
6.1 What is LL (1) grammar
(1) If α、β Neither can be derived ε, FIRST( α) ∩ FIRST( β) = ∅。
(2) α And β At most one can be deduced ε。
(3) If β * ═> ε, FIRST( α) ∩ FOLLOW(A) = ∅。
The grammar that meets the above conditions is called LL (1) grammar.
6.2 Find FIRST set
1. If X is a terminal, FIRST (X)=X. 2. If X is a nonterminal symbol and X ->Y1Y2... Yk is a production, where k ≥ 1, then if for an i, a is in FIRST (Yi) and ε In all FIRST (Y1), FIRST (Y2) In FIRST (Yi-1), add a to FIRST (X). In other words, Y1... Yi-1=>* ε。 If more than all j=1,2,3, k , ε In FIRST (Yj), then ε Add to FIRST (X). For example, all symbols in FIRST (Y1) must be in FIRST (X). If Y1 cannot be deduced ε , Then, we will not add any symbols to FIRST (X), but if Y1=>* ε , Then we will add FIRST (Y2), and so on. 3. If X -> ε Is a production, then ε Add to FIRST (X).
E → TE' E' → +TE' | ε T → FT ' T' → *FT ' | ε F → (E)|id
FIRST (E): FIRST (E)=FIRST (T) can be obtained from E ->TE '
FIRST (T): FIRST (T)=FIRST (F) can be obtained from T ->FT '
FIRST (F): from F ->(E) and F ->id, FIRST (F)={(, id};
FIRST (T '): By T' -> FT 'and T' -> ε The available FIRST (T ')={ , ε };
FIRST (E '): from E' ->+TE 'and E' -> ε We can get FIRST (E ')={+, ε };
FIRST ( E ) = { ( id }
FIRST ( E' ) = { + ε }
FIRST ( T ) = { ( id }
FIRST ( T' ) = { * ε }
FIRST ( F ) = { ( id }
6.3 Finding the FILLOW set
1. Put $into FOLLOW (S), where S is the start symbol and $is the end mark on the right end of input. 2. If there is a production A -> α B β , Then FIRST( β) Middle division ε All symbols except are in FLOW (B). 3. If there is a production A -> α B. Or there is production A -> α B β And FIRST( β) contain ε , Then all symbols in FOLLOW (A) are in FOLLOW (B).
E → TE' E' → +TE' | ε T → FT ' T' → *FT ' | ε F → (E)|id
FOLLOW (E): obtained from F ->(E), FOLLOW (E)={), $}
FOLLOW (E '): obtained from E ->TE' and E '->+TE', FOLLOW (E ')=FOLLOW (E)
FILLOW (T): From E ->TE ', E' ->+TE 'and E' -> ε Yes, FILLOW (T)=FIRST (E ')/ ε + FOLLOW(E) + FOLLOE(E’ )
FOLLOW (T '): available from T ->FT' and T '->* FT', FOLLOW (T ')=FOLLOW (T)
FLOW (F): From T ->FT ', T' ->* FT 'and T' -> ε Yes, FILLOW (F)=FIRST (T ')/ ε + FOLLOW(T) + FOLLOW(T’)
FOLLOW(E) = { ) ,$}
FOLLOW(E’) = FOLLOW(E) = { ) ,$}
FOLLOW(T) = FIRST(E’) / ε + FOLLOW(E) + FOLLOE(E’) = {+ , ) , $}
FOLLOW(T’) = FOLLOW(T) = {+ , ) , $}
FOLLOW(F) = FIRST(T’) / ε + FOLLOW(T) + FOLLOW(T’) = {* , + , ) , $}
6.4 Finding the SELECT set
1. If ε∉ FIRST (a), then SELECT (A → a)=FIRST (a) 2. If ε∈ EFIRST (a), then SELECT (A → a)=(FIRST (a) - {}) U FLOW (A)
(1) E → T E ' (2) E '→ + T E ' (3) E '→ ε (4) T → F T ' (5) T ' → * F T ' (6) T ' → ε (7) F → ( E ) (8) F → id
| | |
---|---|---|
| | |
| | |
| | |
| | |
| | |
SELECT (1)= { ( id }
SELECT (2)= { + }
SELECT (3)= { $ ) }
SELECT (4)= { ( id }
SELECT (5)= { * }
SELECT (6)= { + ) $ }
SELECT (7)= { ( }
SELECT (8)= { id }