Context-free grammar and its grammar tree, sentence structure analysis, harmful rules and redundant rules
Article Directory
- (1) Context-free grammar
- 1. Examples:
- (2) Normative derivation and normative sentence patterns
- 1. Definition:
- 2. Note:
- 3. Example: (must write again)
- (3) Syntax tree
- 1. Function:
- 2. Examples:
- 3. Definition: (should be useless, just look at it)
- (4) Ambiguity of grammar
- 1. Definition:
- 2. Example:
- 3. Example 2 (Ambiguous grammar becomes unambiguous grammar)
- 4. Note:
- (5) Analysis of sentence patterns (generalization)
- 1. Definition:
- 2. Classification: (just take a look)
- (6) Top-down analysis method
- 1. Definition:
- 2. Example: (feeling to take the test)
- 3. The main problem of the top-down approach: (is a choice production)
- (7) Bottom-up analysis method
- 1. Definition:
- 2. Examples:
- 3. The main problem of bottom-up analysis: (Is to determine the "reducible string")
- (8) Harmful rules and redundant rules
- 1. Definition of harmful rules: U->U, useless, and cause grammatical ambiguity
- 2. Redundant rule definition: rules that are not used in all sentence derivation
- Unreachable: non-terminal symbols that do not appear on the right side of any production
- Non-terminal: the non-terminal symbol of the terminal string cannot be deduced
- 3. Example: (important)
- 4. ε Rule
(1) Context-free grammar
1. Examples:
(2) Normative derivation and normative sentence patterns
1. Definition:
If in any part of the derivation α⇒β, where α and β are sentence patterns, they replace the leftmost (rightmost) nonterminal symbol in α, then this derivation is called the leftmost (rightmost) derivation
2. Note:
①The rightmost derivation is called the normative derivation
②The sentence pattern derived from the norm is called the normative sentence pattern
3. Example: (must write again)
From the example in the figure above:
①The leftmost derivation is to replace the leftmost nonterminal derivation on the right of the production .
②The rightmost derivation is to replace the rightmost nonterminal derivation on the right of the production .
(3) Syntax tree
1. Function:
Intuitively describe the derivation process of context-free grammar.
2. Examples:
3. Definition: (should be useless, just look at it)
(4) Ambiguity of grammar
1. Definition:
If a sentence in a grammar corresponds to two different grammar trees , then the grammar is said to be ambiguous . Ambiguous grammar exists in a sentence, and it has two different left-most (right) derivations.
For a programming language, it is hoped that its grammar is unambiguous, because it is hoped that the analysis of each sentence in it is unique.
2. Example:
3. Example 2 (Ambiguous grammar becomes unambiguous grammar)
4. Note:
If the same grammar, the derivation process is different, but the result is two identical grammar trees, it is also unambiguous
(the derivation process is different, but the results are equivalent)
(5) Analysis of sentence patterns (generalization)
1. Definition:
The process of identifying whether the input symbol string is a grammatically correct program. (Judging whether it conforms to the grammar)
2. Classification: (just take a look)
(6) Top-down analysis method
1. Definition:
Starting from the beginning symbol of the grammar, we repeatedly use the production of the grammar to find the derivation that matches the symbol string.
The syntax tree structure: the grammar start symbol number as a syntax tree roots , and gradually establish a syntax tree down, so that the end symbol string syntax tree node is exactly the input symbol string.
2. Example: (feeling to take the test)
3. The main problem of the top-down approach: (is a choice production)
(7) Bottom-up analysis method
1. Definition:
Starting from the input symbol string, the reduction is carried out step by step , until the reduction to the beginning symbol of the grammar .
Syntax tree construction: start from the input symbol string and use it as the end node symbol string of the syntax tree to construct the syntax tree from the bottom up
2. Examples:
3. The main problem of bottom-up analysis: (Is to determine the "reducible string")
(8) Harmful rules and redundant rules
1. Definition of harmful rules: U->U, useless, and cause grammatical ambiguity
2. Redundant rule definition: rules that are not used in all sentence derivation
Manifestations:
Unreachable: non-terminal symbols that do not appear on the right side of any production
Non-terminal: the non-terminal symbol of the terminal string cannot be deduced
3. Example: (important)
Among them (2) is also non-terminating
4. ε Rule
Definition: A->ε production is allowed in context-free grammar, and A->ε is called the ε rule