+ All Categories
Home > Documents > A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing...

A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing...

Date post: 22-Dec-2015
Category:
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
41
APRIL 05, 2006 BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" Analyzing Ambiguity of Context-Free Grammars BRICS, Department of Computer Science University of Aarhus, Denmark Claus Brabrand [email protected] Anders Møller [email protected]
Transcript
Page 1: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

APRIL 05, 2006BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS"

Analyzing Ambiguity of Context-Free Grammars

BRICS, Department of Computer Science

University of Aarhus, Denmark

Claus [email protected]

Anders Mø[email protected]

Page 2: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 2 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

“Analyzing Ambiguity of Context-Free Grammars”“Analyzing Ambiguity of Context-Free Grammars”

// Abstract

Context-free grammar ambiguity is undecidable.

However, just because the problem is undecidable does not mean that there are no useful approximations to the problem.

We exhibit a linguistic characterization of context-free ambiguity which transforms the problem of checking a grammar for ambiguity from a grammatical or structural property to a linguistic property, dealing solely with the set of strings in specific parts of the grammar.

We then show how to exploit this characterization to obtain a conservative approximation to the problem (based on the [Mohri-Nederhof, 2000] regular approximation of context-free grammars). The resulting approximation is capable of acquitting unambiguous grammars that the LR(k) cannot (for any k).

Finally, we give some pointers on how to boost the precision of the analysis even further.

Context-free grammar ambiguity is undecidable.

However, just because the problem is undecidable does not mean that there are no useful approximations to the problem.

We exhibit a linguistic characterization of context-free ambiguity which transforms the problem of checking a grammar for ambiguity from a grammatical or structural property to a linguistic property, dealing solely with the set of strings in specific parts of the grammar.

We then show how to exploit this characterization to obtain a conservative approximation to the problem (based on the [Mohri-Nederhof, 2000] regular approximation of context-free grammars). The resulting approximation is capable of acquitting unambiguous grammars that the LR(k) cannot (for any k).

Finally, we give some pointers on how to boost the precision of the analysis even further.

Page 3: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 3 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Outline

Introduction Vertical / Horizontal Ambiguity Characterization of Ambiguity (Over-)Approximation Approximation (AMN) Examples and Assessment Related Work Conclusion

Page 4: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 4 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Context-Free Grammar

N finite set of nonterminals finite set of terminals s N start nonterminal : N P(E*) production function, E = N

G = N, , s,

Assume: All nN reachable (from s) All nN derive some (finite) string

L : G P(*) language of G, L(G)

Page 5: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 5 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// CFG Decision Problems Decidable:

Membership: L(GCFG) !

Emptiness: L(GCFG) = !

Intersection (w/ REG): L(GCFG) L(RREG) = ! …

Undecidable: Intersection (w/ CFG): L(GCFG) L(G’CFG) = ? …

Ambiguity: *: multiple derivation trees ?

Page 6: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 6 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Ambiguity: Undecidable!

Undecidable!

However…

T

s

T’

s

=

unambiguous ambiguous

Ambiguity: *: multiple derivation trees ?

?

Page 7: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 7 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// “Side-Stepping Undecidability”

Unsafe approximation:

Safe approximation:

However, just because it’s undecidable, doesn’t mean there aren’t (good) approximations! Indeed, the whole area of static analysis works on “side-stepping undecidability”.

However, just because it’s undecidable, doesn’t mean there aren’t (good) approximations! Indeed, the whole area of static analysis works on “side-stepping undecidability”.

unambiguous ambiguous

safe (over-)approximation

unambiguous ambiguous

safe (under-)approximation

unambiguous ambiguous

unsafe approximation

Page 8: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 8 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Motivation

Use safe (over-)approximation:

“Yes!” “G guaranteed unambiguous”!!! Safely use any GLR parser on G

Because: we never get two parses at runtime!

Hence: dynamic parse ambiguity static parse ambiguity

unambiguous ambiguous

Yes!

.

Page 9: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 9 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Motivation (cont’d)

Undecidability means: “there’ll always be a slack”:

However, still useful! Possible interpretations of “No?”:

Treat as error (reject grammar): “Please redesign your grammar” (as in LR(k))

Treat as warning: “Here are some potential problems”

unambiguous ambiguous

No?

. .

Page 10: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 10 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Vertical Unambiguity

“Vertical unambiguity”:

Example:

n N : , ’ (n) : ’ L() L(’) =

xay

Z : x A y : x B y A : aB : a

Ambiguous string:

~ “reduce/reduce conflict” in [Yacc]

G

Page 11: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 11 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Horizontal Unambiguity

“Horizontal unambiguity”:

where:

Example:

n N: (n): i [1..||-1]: L(0 .. i-1) L(i .. ||-1 ) =

: P(*) P(*) P(*)

X Y = { xay | x,y* a+ x,xaL(X) y,ayL(Y) }

xay

Z : A B A : x a : xB : a y : y

Ambiguous string:

~ “shift/reduce conflict” in [Yacc]

G

Page 12: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 12 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Characterization of Ambiguity

Theorem 1:

Lemma 1a: (“”)

Lemma 1b: (“”)

G G G unambiguous

G G G unambiguous

G G G unambiguous

Page 13: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 13 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1a): “”

Lemma 1a:

…contrapositively:

Proof: Assume G ambiguous (i.e. 2 der. trees for )

Show: by induction in max height of the 2 derivation trees

G G G unambiguous

G ambiguous G G

G G

Page 14: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 14 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1a): “” (Base)

Base case (height 1): The ambiguity means that:

However, this means that = t0 t1 ... t||-1 = '(i.e. the two trees must be the same); and so the result holds vacuously

N

’1N

1

=

Page 15: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 15 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1a): “” (I.H.)

Induction step (height n): Assume induction hypothesis (for height n-1)

The ambiguity means:

N

n-1

N

n-1

i ’i’

… …i … …’i’

11

||-1= ’0 ’|’|-10.. .. .. ..

=

Page 16: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 16 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1a): “” (’)

Case (i.e. different production):

…but then i.e., we have a vertical ambiguity:

L() L(’) {}

G

N

n-1

N

n-1

i ’i’

… …i … …’i’

11

||-1= ’0 ’|’|-10.. .. .. ..

=

Page 17: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 17 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1a): “” (=’,1)

Case (i.e., same prod. ): i.e. “the top of the trees are the same”

Case : ambiguity in subtreei (i.e. & ambiguously derive same i): Induction hypothesis (on these subtrees)

i : i = ’i

i : i = ’i

N

n-1

N

n-1

i i

… …i … …i’

11

||-1= 0 ||-10.. .. .. ..

=

G G

= ’

Ti

Ti T'i

T'i

Page 18: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 18 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

Case (i.e., same prod. ): Case (i.e., ):

Now let k = min{ i | i = 'i }

...then:

N

n-1

N

n-1

x

...k

i : i ’i

11

i : i = ’i

=

y x'

...k

y'

L(0 .. k) L(k+1 .. || ) {xay'} G

// Proof (Lemma 1a): “” (=’,2)

i : i = ’i = ’

k 'k

= xay'

Page 19: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 19 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1b): “”

Lemma 1b:

...contrapositively:

Proof: Assume “ ” (vertical conflict):

But then derive (using reachability + derivability of N):

s * x N x * x a * x a y

s * x N x ’ * x a * x a y

N * a, N ’ * a, L() L(’) {a}

G G G unambiguous

G ambiguous G G

for some N

G a

mb

igu

ou

s

Page 20: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 20 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Proof (Lemma 1b): “” (cont’d)

Assume “ ” (horizontal conflict): Then for some NN:

But then derive (using reachability + derivability of N):

s * v N v * v x * v x a y * v x a y w

s * v N v * v x a * v x a y * v x a y w

N , L() L()

x,y * : a + : x,xa L() y,ay L()

i.e.

G a

mb

igu

ou

s

Page 21: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 21 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// (Over-)Approximation (A)

(Over-)Approximation A : E* P(*)

A decidable “ ” and “ ” decidable on co-dom(A)

Approximated vertical unambiguity:

Approximated horizontal unambiguity:

E* : L() A()

n N : , ’ (n) : A() A(’) =

A

A

n N: (n): i [1..||-1]: A(0 .. i-1) A(i .. ||-1) =

G

G

Page 22: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 22 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Ambiguity Approximation

Theorem 2:

Proof:

“Conflicts w/ smaller sets conflicts w/ larger sets”:

G unambiguous

A() A() = L() L() =

A() A() = L() L() =

AA

AA

G G

G G G G

Page 23: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 23 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Compositionality (of A’s)

Proposition 3:

Proof: Follows from definition [proof omitted]

i.e. “Approximations are compositional”!:

A, A’ decidable (over-)approximations A A’ decidable (over-)approximation

unambiguous ambiguous

unambiguous ambiguous

unambiguous ambiguous

A

A’

A A’

Page 24: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 24 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Choice(s) of A?

A*() = * (constant) Worst approximation... …but safe!

Useless: “Cannot determine that any grammars are unambiguous”

unambiguous ambiguous

worst approximation

Page 25: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 25 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Choice(s) of A? (cont’d)

AMN() = [Mohri-Nederhof]() CFG REGDFA (Over-)Approximation

Properties of this “ ”: Good (over-)approximation! Now we deal with regular languages:

Almost everything is decidable!

Works on language, L(G); i.e. not on the structure of the grammar, G

“Regular Approximation of Context-Free Grammars through Transformation”[Mohri-Nederhof, 2000]

Black-box

Page 26: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 26 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Decidability (of AMN)

“” decidable (constructively, using DFAs)

O(|XNFA||YNFA|)

“ ” decidable (constructively, using DFAs)

O(|XNFA||YNFA|)

AMN decidable

With potential counterexamples (DFAs)

X Y =

X Y =

G unambiguous

AMNAMN

Page 27: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 27 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

For X,Y regular languages (NFAs):

All overlappings, “xay”, as DFAs; variant of “” construction!

// Decision Algorithm for (X Y)

XNFA YNFA

[X;Y]NFA

a path :

XNFA YNFA

[X Y]NFA

a a

x y

x a y

a

a

X Y

YX

X Y = Ø

~ ~~ ~

~ ~

Page 28: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 28 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

[Mohri-Nederhof]: O(n2vh) Vertical Amb: O(n3v4h4) Horizontal Amb: O(n3v3h5) Total: O(n3v3h4(v+h)) O(g5)

// Asymptotic (Time) Complexity

N1 : e1,1 … ea,1

: … : e1,p … ea,p

h

n

v

n = |N| v = max{|(N)|, NN} h = max{||, (N), NN} g = nvh = |G|

Page 29: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 29 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Implementation

Java impl.: "grambiguity" (510 lines, using): "dk.brics.automaton" [ http://www.brics.dk/automaton/ ] "dk.brics.grammar" [ http://www.brics.dk/grammar/ ] Java String Analyzer [ http://www.brics.dk/JSA/ ]

%> java –jar Grambiguity.jar G1.cfg

*** (potential) vertical ambiguity detected: 'S[aS]' vs. 'S[Sa]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[aa]' vs. 'S[SS]' shortest ambiguous string: "()"

*** (potential) vertical ambiguity detected: 'S[aS]' vs. 'S[SS]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[Sa]' vs. 'S[SS]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[SS]' vs. 'S[empty]' shortest ambiguous string: ""

*** (potential) horizontal ambiguity detected: 'S[SS:0..0]' vs. 'S[SS:1..1]' shortest ambiguous string: "."

*** (potentially) ambiguous grammar: 5 (potential) vertical ambiguities 1 (potential) horizontal ambiguities

/* ambiguous */

S[aa] : "(" S2 ")" ; [aS] : "." S ; [Sa] : S "." ; [SS] : S S ; [empty] : ;

/* unambiguous */

P[aPa] : "a" P "a" ; [a] : "a" ; [empty] : ;

unambiguous grammar!

P

G1

Page 30: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 30 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: Palindromes Palindromic examples:

P : "a" P "a" ; : "a" ; : ;

unambiguous grammar!

P : "a" P "a" ; : "b" P "b" ; : "b" ; : "a" ; : ;

unambiguous grammar!

P : "a" P "a" ; : ;

unambiguous grammar!

R : "a" R "b" ; : "b" R "a" ; : "a" "b" ; : "b" "a" ;

unambiguous grammar!

R : "a" R "b" ; : "b" R "a" ; : ;

unambiguous grammar!

Note: these are allnon-LR(k)

grammars !!!

Page 31: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 31 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: Odd/Even Keeping track of parity (odd/even):

Start : Even ; : Odd ;

Even : "(" "(" Odd ")" ")" ; : ;

Odd : "(" "(" Odd ")" ")" ; : "(" ")" ;

unambiguous grammar!

A(Even) = A(Odd) =

Page 32: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 32 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: RNA Analysis (G1) RNA Analysis (G1):

%> java –jar Grambiguity.jar G1.cfg

*** (potential) vertical ambiguity detected: 'S[aS]' vs. 'S[Sa]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[aa]' vs. 'S[SS]' shortest ambiguous string: "()"

*** (potential) vertical ambiguity detected: 'S[aS]' vs. 'S[SS]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[Sa]' vs. 'S[SS]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[SS]' vs. 'S[empty]' shortest ambiguous string: ""

*** (potential) horizontal ambiguity detected: 'S[SS:0..0]' vs. 'S[SS:1..1]' shortest ambiguous string: "."

*** (potentially) ambiguous grammar: 5 (potential) vertical ambiguities 1 (potential) horizontal ambiguities

/* ambiguous */

S[aa] : "(" S ")" ; [aS] : "." S ; [Sa] : S "." ; [SS] : S S ; [empty] : ;

G1

Page 33: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 33 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: RNA Analysis (G2) RNA Analysis (G2):

*** (potential) vertical ambiguity detected: 'S[aS]' vs. 'S[Sa]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[aPa]' vs. 'S[SS]' shortest ambiguous string: "()"

*** (potential) vertical ambiguity detected: 'S[aS]' vs. 'S[SS]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[Sa]' vs. 'S[SS]' shortest ambiguous string: "."

*** (potential) vertical ambiguity detected: 'S[SS]' vs. 'S[empty]' shortest ambiguous string: ""

*** (potential) vertical ambiguity detected: 'P[aPa]' vs. 'P[S]' shortest ambiguous string: "()"

*** (potential) horizontal ambiguity detected: 'S[SS:0..0]' vs. 'S[SS:1..1]' shortest ambiguous string: "."

*** (potentially) ambiguous grammar: 6 (potential) vertical ambiguities 1 (potential) horizontal ambiguities

/* ambiguous */

S[aPa] : "(" P ")" ; [aS] : "." S ; [Sa] : S "." ; [SS] : S S ; [empty] : ;

P[aPa] : "(" P ")" ; [S] : S ;

G2

Page 34: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 34 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: RNA Analysis... RNA Analysis (G3,G4,G5,G6):

S[aS] : "." S ; [T] : T ; [empty] : ;

T[Ta] : T "." ; [aSa] : "(" S ")" ; [TaSa] : T "(" S ")" ;

S[LS] : L S ; [L] : L ;

L[aFa] : "(" F ")" ; [a] : "." ;

F[aFa] : "(" F ")" ; [LS] : L S ;

S[aS] : "." S ; [aSaS] : "(" S ")" S ; [empty] : ;

S[aPa] : "(" P ")" ; [aL] : "." L ; [Ra] : R "." ; [LS] : L S ;

L[aPa] : "(" P ")" ; [aL] : "." L ;

R[Ra] : R "." ; [empty] : ;

P[aPa] : "(" P ")" ; [aNa] : "(" N ")" ;

N[aL] : "." L ; [Ra] : R "." ; [LS] : L S ;

unambiguous grammar!

G3

G4

G5

G6

Page 35: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 35 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: RNA Analysis (G7) RNA Analysis (G7,G8):

*** (potential) vertical ambiguity detected: 'P[aPa]' vs. 'P[aNa]' shortest ambiguous string: "(((.)"

*** (potentially) ambiguous grammar: 1 (potential) vertical ambiguity 0 (potential) horizontal ambiguities

S[aPa] : "(" P ")" ; [aL] : "." L ; [Ra] : R "." ; [LS] : L S ;

L[aPa] : "(" P ")" ; [aL] : "." L ;

R[Ra] : R "." ; [empty] : ;

P[aPa] : "(" P ")" ; [aNa] : "(" N ")" ;

N[aL] : "." L ; [Ra] : R "." ; [LS] : L S ;

G7

*** (potential) vertical ambiguity detected: 'P[aPa]' vs. 'P[aNa]' shortest ambiguous string: "(((.)"

*** (potentially) ambiguous grammar: 1 (potential) vertical ambiguity 0 (potential) horizontal ambiguities

S[aS] : "." S ; [T] : T ; [empty] : ;

T[Ta] : T "." ; [aPa] : "(" P ")" ; [TaPa] : T "(" P ")" ;

P[aPa] : "(" P ")" ; [aNa] : "(" N ")" ;

N[aS] : "." S ; [Ta] : T "." ; [TaPa] : T "(" P ")" ;

G8Note: these are all

spurious errorsdue to imprecisions

in the analysis

Page 36: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 36 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: Expressions Expressions:E[term] : T ; [plus] : E "+" T ; T[x] : "x" ; [par] : "(" E ")" ;

*** (potential) vertical ambiguity detected: 'E[term]' vs. 'E[plus]' shortest ambiguous string: "x+x"

*** (potential) horizontal ambiguity detected: 'E[plus:0..0]' vs. 'E[plus:1..2]' shortest ambiguous string: "x+x+x"

*** (potential) horizontal ambiguity detected: 'E[plus:0..1]' vs. 'E[plus:2..2]' shortest ambiguous string: "x+x+x"

*** (potentially) ambiguous grammar: 1 (potential) vertical ambiguity 2 (potential) horizontal ambiguities

Note: General problem with non-linear recursive structures

However, there's a trick...

Page 37: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 37 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Examples: Expressions (cont'd) Expressions:E[term] : T ; [plus] : E "+" T ; T[x] : "x" ; [par] : "(" E ")" ;

E[term] : T ; [plus] : E "+" T ; T[x] : "x" ; [par] : "(" E ")" ;

E[term] : T ; [plus] : E "+" T ; T[x] : "x" ; [par] : "(" E ")" ; unambiguous grammar!

unfold

w = x+(x+(x+x)+x)+x

w = x+(x+(x+x)+x)+x

E : T : E "+" T T : "x" : "(" E ")"

E : T : E "+" T T : "x" : "(" E ")"

E : T : E "+" T T : "x" : "(" E ")"

E

E

<=

>

unfold trick:(inside/outside)parentheses

Page 38: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 38 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Benchmark Grammars

LALR(1)LR(1)LR(2)LR(3)LR(4)LR(5)LR(6)LR(7)LR(8)

LR(k)..

G3

G7

G4

G5

G8

G6

AMBIGUOUS

P

[OUR]

UNAMBIGUOUS

G1

G2

R

Page 39: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 39 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Related Work (Dynamic)

Dynamic disambiguation: “Disambiguation-by-convention”:

Longest match, most specific match, …

Customizable: [Bison v. 1.5+]: %dprec, %merge [ASF+SDF]: “disambiguation filters”

Dynamic ambiguity interception: GLR ([Tomita], [Early], [Bison], [ASF+SDF], …)

Page 40: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 40 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Related Work (Static)

Static disambiguation: “Disambiguation-by-convention”:

First match, most specific match, …

Customizable: [Yacc]: %left, %right, %nonassoc, %prec

Static ambiguity interception: LL(k), LALR(1), LR(k), … Our work goes here

Page 41: A PRIL 05, 2006 B IELEFELD, G ERMANY "A NALYZING A MBIGUITY OF C ONTEXT-FREE G RAMMARS " Analyzing Ambiguity of Context-Free Grammars BRICS, Department.

[ 41 ]BIELEFELD, GERMANY "ANALYZING AMBIGUITY OF CONTEXT-FREE GRAMMARS" APRIL 05, 2006

// Conclusion

“Approximating Context-Free Grammar Ambiguity”“Approximating Context-Free Grammar Ambiguity”Context-free grammar ambiguity is undecidable.

However, just because the problem is undecidable does not mean that there are no useful /approximations/ to the problem.

We exhibit a linguistic characterization of context-free ambiguity which transforms the problem of checking a grammar for ambiguity from a grammatical or structural property to a linguistic property, dealing solely with the set of strings in specific parts of the grammar.

We then show how to exploit this characterization to obtain a conservative approximation to the problem (based on the [Mohri-Nederhof, 2000] regular approximation of context-free grammars). The resulting approximation is capable of acquitting unambiguous grammars that the LR(k) cannot (for any k).

Finally, we give some pointers on how to boost the precision of the analysis even further.

Context-free grammar ambiguity is undecidable.

However, just because the problem is undecidable does not mean that there are no useful /approximations/ to the problem.

We exhibit a linguistic characterization of context-free ambiguity which transforms the problem of checking a grammar for ambiguity from a grammatical or structural property to a linguistic property, dealing solely with the set of strings in specific parts of the grammar.

We then show how to exploit this characterization to obtain a conservative approximation to the problem (based on the [Mohri-Nederhof, 2000] regular approximation of context-free grammars). The resulting approximation is capable of acquitting unambiguous grammars that the LR(k) cannot (for any k).

Finally, we give some pointers on how to boost the precision of the analysis even further.


Recommended