• 沒有找到結果。

Design Grammars I

N/A
N/A
Protected

Academic year: 2022

Share "Design Grammars I"

Copied!
12
0
0

加載中.... (立即查看全文)

全文

(1)

Example 2.4 I

The following CFG handles mathematical expressions

G4 = (V , Σ, R, hexpri)

V = {hexpri, htermi, hfactori}

Σ = {a, +, ×, (, )}

R:

hexpri → hexpri + htermi | htermi htermi → htermi × hfactori | hfactori hfactori → (hexpri) | a

Fig 2.5: check a + a × a

(2)

Example 2.4 II

E

E

T

F

a +

T

T

F

a × F

a

check (a + a) × a

(3)

Example 2.4 III

E T

T F

F E

E T

T F

F a

( + a ) × a

(4)

Design Grammars I

{0n1n | n ≥ 0} ∪ {1n0n | n ≥ 0}

S1 → 0S11 |  S2 → 1S20 |  S → S1 | S2 CFG versus DFA

For a CFG that is regular, it can be recognized by DFA

(5)

Design Grammars II

Rules of CFG can be

Ri → aRj if δ(qi, a) = qj

Ri →  if qi ∈ F

We see the main difference between CFG and DFA CFG: a rule can be like

Ri → aRjb

(6)

Design Grammars III

DFA: a rule can only be

Ri → aRj,

where we treat each Ri as a state and let δ(Ri, a) = Rj

(7)

Ambiguity I

The same string but obtained in different ways For the example of mathematical expressions discussed earlier, what if we consider the following rules?

hexpri → hexpri + hexpri | hexpri × hexpri | (hexpri)|a We see the following ways to parse

a + a × a

(8)

Ambiguity II

Fig 2.6

E

E

E

a + E

a × E

a

E

E

a +

E

E

a × E

a

This CFG does not give the precedence relation We want that a × a is done first

(9)

Ambiguity III

By the more complicated CFG earlier, the parsing is unambiguous

Question: how to formally define the ambiguity?

We need to define “leftmost derivation” first

(10)

Leftmost derivation I

Even for an unambiguous CFG we may have the same parse tree, but different derivations For the CFG discussed earlier we can do

hexpri ⇒ hexpri + htermi

⇒ hexpri + htermi × hfactori where the second part is expanded.

On the other hand, we can handle the first one first.

hexpri ⇒ hexpri + htermi

⇒ htermi + htermi

(11)

Leftmost derivation II

This is not considered ambiguous

Definition of leftmost derivation: at every step the leftmost remaining variable is the one replaced.

(12)

Formal definition of ambiguity I

w ambiguous if there exist

two leftmost derivations

Some context-free languages can be generated by ambiguous & unambiguous grammars

We say a CFG is inherently ambiguous if it only has ambiguous grammars

See prob 2.29 in the textbook. Details not given here.

參考文獻

相關文件

Remember the definition of Laurent series of a function about an isolated singularity, and the principal part and the analytic part of the Laurent series.. Remember the definition

The slope of the tangent line to C 1 at the point A is, by definition, the partial derivative with respective

If vertex i is a universal sink according to the definition, the i-th row of the adjacency-matrix will be all “0”, and the i-th column will be all “1” except the a ii.. entry,

You need to show the formal definition of the TM, but you can draw a state diagram instead of giving a formal definition of the transition function δ... You do not have give a

Note that the definition for a set having content (or the definition of a set being measurable) is well defined since the the following properties hold.... the definition of content

32.Start from the definition of derivative, then find the tangent line equation.. 46.Find the product of their slopes at there

The fist step in this endeavor is to identify the possible outcomes or, in statistical terminology, the sample space.. Definition 1.1 The set, S, of all possible outcomes of

The molal-freezing-point-depression constant (Kf) for ethanol is 1.99 °C/m. The density of the resulting solution is 0.974 g/mL.. 21) Which one of the following graphs shows the