[Compilation Principles] -- Chapter 2 (2) (phrases, simple phrases, handles, grammatical ambiguity, syntax trees, examples)

Table of contents

1. Analysis of sentence patterns

1. Norm derivation and norm reduction

2. Phrases, simple phrases and handles

3. Syntax tree

4. Find phrases, simple phrases, and handles through trees

2. Grammatical ambiguity

1. Definition of grammatical ambiguity

2. Elimination of grammatical ambiguity

(1) Define regulations or rules

(2) Rewrite the grammar

3. Example questions

1. Language L={ambn,m>=1,n>=1}, try to write the grammar.

2. Language L={anbncm,m>=1,n>=1}, try to write the grammar.

3. Language L={anbbn,n>=1}, try to write the grammar.

4. Language L={anbmcmdn ,m>=1,n>=1}, try to write the grammar.

5. Language L={ambn ,n>=m>=1}, try to write the grammar.

 


1. Analysis of sentence patterns

1. Norm derivation and norm reduction

The leftmost (right) derivation:In any step of the derivation v=>w, the leftmost (right) non-terminal symbol of the symbol string v is replaced, which is called The leftmost (right) derivation.

Normal derivation: is the rightmost derivation.

Normal sentence pattern:The sentence pattern derived from the norm.

Canonical reduction:The reverse process of canonical derivation is called canonical reduction or leftmost reduction.

example:

G[<identifier>]
<identifier>→<letter>|<identifier><letter> <number>→0|1|2|...|9 <Letter>→a|b|...|z|A|...|Z
|<Identifier><Number>

Normal derivation
<Identifier>
=><Identifier> <Letter>< a i=3> =><Identifier>y =><Identifier> <Number>y => <identifier>4y =><letter>4y =>a4y




Canonical reduction
a4y
<≠<letter>4y
<≠ < identifier>4y
<≠ <identifier><number>y
<≠<identifier>y <≠ <identifier>
<≠ <identifier><letter>

2. Phrases, simple phrases and handles

Phase: Grammar G[Z], ω=xuy is a sentence pattern, x, y∈V*, if there is Z=*>xUy, and U=+> ;u, U∈Vn, u ∈V+, it is said that u is a phrase relative to the sentence pattern ω of the non-terminal symbol U.

Simple phrase:Grammar G[Z], ω=xuy is a sentence pattern, if there is Z=*>xUy, and U=>u, U∈Vn, u ∈V+, we say u is a simple phrase relative to the sentence pattern ω of the non-terminal symbol U.

Handle:The leftmost simple phrase of the sentence pattern is the handle of the sentence pattern, and a sentence pattern has only one handle.

illustrate:

  • Phrases and simple phrases must be for a certain sentence pattern and be a string of words for that sentence pattern.
  • Phrases and simple phrases must be relative to some nonterminal symbol.
  • Both conditions are indispensable.
  • A sentence pattern can have several phrases and simple phrases.
  • A current sentence pattern has only one handle (unambiguous grammar)
  • The handle of the current sentence pattern reduced by the leftmost reduction.

example:

G[S]:
S→AB
A→Aa|bB
B→a|Sb

Question: Give the phrases, simple phrases and handles of the sentence pattern baSb.

(1) S=>AB=>bBB =>baB=>baSb and B=>Sb
(2) S=>AB => ;ASb =>bBSb=>baSb and B=>a
(3) S=>AB =+>baSb and A=+>ba

Sb is a phrase relative to B, sentence pattern baSb, and is a simple phrase,
a is a phrase relative to B, sentence pattern baSb, and is a simple phrase,
ba is relative to A, and the phrase handle of sentence type baSb is a.

3. Syntax tree

Syntax tree:A graphical representation of a sentence pattern or sentence derivation process, forming a syntax tree.

Root:Start sign.

Subtree:A certain non-terminal symbol (the root of the subtree) and its branches below.

Leaf:The terminal node of the tree.

All end nodes of the syntax tree (from left to right) form the current sentence pattern.

example:

G[S]:
S→AB
A→Aa|bB
B→a|Sb

Leftmost direction        
S=>AB
=>bBB
=>baB
=>baSb

 

illustrate:

Assuming grammar G = (Vn, Vt, P, S), for any sentence pattern of G, a lesson grammar tree associated with it and satisfying the following conditions can be constructed.

  • Each node has a label, which is a symbol in V=Vn∪Vt∪ε.
  • The root of the tree is marked by the starting symbol S of the grammar.
  • If a node has at least one branch node, the mark on the node must be a non-terminal symbol.
  • If the node of A has k branch nodes, and the labels of the branch nodes are A1, A2,...,Ak, then A→A1A2...Ak must be a rule of G.

4. Find phrases, simple phrases, and handles through trees

Phrase: A string of symbols formed by the end nodes of the subtree.

Simple subtree:A subtree with only one level of branches.

Simple phrase:A symbol string formed by the terminal nodes of a simple subtree.

example:

          Syntax tree of sentence pattern baSb

There are three subtrees in total,

Three phrases: ba,a, Sb, baSb
Simple phrase: a, Sb
Handle: a

 1. How to find a phrase?

Starting from the tree root S, find the end node of S: baSb (that is, the sentence pattern itself)

Then go down and find the end node of A: ba

                  The terminal node of B is: Sb

Further down, only B has an end node: a

2. How to find simple phrases?

First find a simple subtree, that is, a subtree with only one level of branches;

Then the symbol string formed by the terminal nodes of the simple subtree is a simple phrase.

3. How to find the handle?

Find the simple phrase on the left

in conclusion:

  • For a certain sentence pattern, each derivation has a corresponding syntax tree; but different derivation may also have the same syntax tree.
  • The end nodes of the tree form the sentence pattern to be deduced.
  • But a certain sentence pattern may also correspond to two different grammar trees. This is the problem of grammatical ambiguity.

2. Grammatical ambiguity

1. Definition of grammatical ambiguity

  • If there are two or more different syntax trees for a certain sentence of grammar G, the sentence is said to be ambiguous.
  • If a grammar contains ambiguous sentences, the grammar is said to be ambiguous; otherwise, the grammar is unambiguous.

 illustrate:

  • Grammatical ambiguity: a sentence has two different leftmost (right) derivations, or two different leftmost (canonical) reductions.
  • Grammatical ambiguity is undecidable: there is no algorithm.
  • Proving the ambiguity of grammar can only try to find a sentence that has two different syntax trees or two different leftmost (right) derivation.
  • Special case: If a grammar G contains both left recursion and right recursion, then G must be an ambiguous grammar. (This is experience)

example:

Prove that the grammar G[S]: S→aSb|Sb|b is an ambiguous grammar.

Proof: There are two different leftmost derivations for the sentence abbb
Leftmost derivation 1: S=>aSb=>aSbb=>abbb< a i=2> Leftmost derivation 2: S=>Sb=>aSbb=>abbb Therefore, the sentence abbb is ambiguous. Due to the grammar of ambiguous sentences, So grammar G[S] is ambiguous.

2. Elimination of grammatical ambiguity

(1) Define regulations or rules

example:

G1[E]:E→E+E|E*E|(E)|i

Provision: The four arithmetic rules form an unambiguous grammar.

G[S]: S →if B then S else S|if B then S

Provision: else matches the then that it has not recently matched, forming an unambiguous grammar

(2) Rewrite the grammar

example:

G1[E]:E→E+E|E*E|(E)|i

Ambiguous grammar has no precedence relationship information and cannot be used directly.

G2[E]:
E→E+T|T
T→T*F|F
F→(E)|i

Unambiguous grammar, including four arithmetic operations information, used in syntax analysis.

3. Example questions

1. Language L={ambn,m>=1,n>=1}, try to write the grammar.

G[S]:
S→AB
A→Aa|a
B→bB|b
或G[S]:
S→AB
A→aA|a
B→bB|b

2. Language L={anbncm,m>=1,n>=1}, try to write the grammar.

G[S]:
S→AB
A→aAb|ab
B→cB|c

3. Language L={anbbn,n>=1}, try to write the grammar.

G[S]:
S→aAb
A→aAb|b

or G[S]:
S→aSb|A
A→abb

4. Language L={anbmcmdn ,m>=1,n>=1}, try to write the grammar.

G[S]:
S→aSd|aAd
A→bAc|bc

5. Language L={ambn ,n>=m>=1}, try to write the grammar.

Solution: Rewritten into equivalent language L={ambmbk ,m>=1,k>=0}

G1[S]:S→AB
A→aAb|ab
B→bB|ε

G2[S]:
S→aAb
A→aAb|Ab|ε

Or rewritten into equivalent language L={ambkbm ,m>=1,k>=0}

G3[S]:
S→aSb|aAb
A→bA|ε


Guess you like

Origin blog.csdn.net/Tir_zhang/article/details/126890577