extensible syntax analyzers pavel egorov [email protected]

22
Extensible syntax analyzers Pavel Egorov [email protected]

Upload: philip-henry

Post on 17-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Extensible syntax analyzers

Pavel [email protected]

Page 2: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Problem statements

L – some formal language; T : L R – it’s translator;Lext L – some extended language;

Text : Lext Rext – it’s extended translator.

L Text () = T() We want compatibility!

Page 3: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Disclaimer

• Consider syntax analyzers only.

• Fixed algorithm of syntax analysis.

• Extension due to modification of the grammar only.

Page 4: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

What is syntax analyzer?

• SA converts a word to a syntax tree.

• What is syntax tree?– Root is labeled by the axiom of the grammar– Inner nodes are labeled by non-terminals– Leaf nodes are labeled by terminals or non-

terminals or

Page 5: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Classic parse tree

Grammar:A aBd B bB A

Word: “aabdd”Derivation:A aBd aAd

aaBdd aabdd

A

B

A

B

b

da

da

Page 6: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Classic parse treeIs it good enough?

G1:A abc

Extended G2:A aBcB b

Word: “abc”

A

B

b

ca

A

b ca

They are different!

Page 7: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Classic parse treeIs it good enough?

G1:A a

Extended G2:A aEE

Word: “a”

A

E

a

A

a

They are different!

Page 8: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Cancelled tree

• Driven by labeled grammar• Cut off some inner nodes from parse tree• Cut off -leafs from parse tree

Is much better for parser extension!

Page 9: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Cancelled tree example

Labeled grammar:A a1B0d0B1

B b1E0

B x1

E 0

• Symbols labeled by 0 are not present in tree

• -leafs are always labeled by 0

A

B

b

da

A

ba

parse tree

cancelled tree

B

x

B

x

E

Page 10: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Intermediate results

• Labeled grammar• Syntax analyzer driven by labeled grammar• Canceled tree as a result of syntax analysis

So how can we extend grammars without breaking compatibility?

Compatibility: L SAext () = SA ()

Page 11: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Extending transformations of grammars

f – extending grammar transformationG - grammar

L(f(G)) L(G)

Page 12: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Extending transformations

• ADD-transform adds some fixed new production to a grammar.

• EXTRACT-transform:before: A xyz

after:A xB0z

B y

Page 13: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Extending transformations

AE* all transforms, composed from arbitrary number of ADD- and EXTRACT-transforms.

Page 14: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Results

Page 15: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Compatibility

1. Any transform from AE* doesn’t break compatibility of corresponding syntax analyzers.

2. A grammar extended by AE* transform has the same production labeling as initial grammar has on the common productions. (AE* doesn’t change labeling of unchanged productions)

Page 16: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Independent transforms

G

G2G1

G12 ???

When it is possible?How can we do it?

AE*

G2Domain() ?G1Domain() ?

Page 17: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Independent transforms

What if G1 Domain(2)?

Sometimes we can solve this problem!A 1: A C; C

2: A B; B

2/1: A B; B C

Conflict!

Decision!

Page 18: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

afixed transform

• “fixed” , which can be applied after • Sometimes we can’t fix

AAX; X AY; Y

• Recursive definition of the function for and from AE*.

• Sometimes is not defined.

Page 19: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Independent transforms

G

G2G1

G12

/ /

/ ? /

G21?

Page 20: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Independent transforms

G

G2G1

G12

/ /

/ = /

Page 21: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Conclusion• Syntax analyzers – driven by labeled grammars– cancelled trees as a result

• AE* - a way to extend grammars safelly• – a way to compose independent

grammar transforms• Order of application of the extending

transforms doesn’t matter!

Page 22: Extensible syntax analyzers Pavel Egorov pe@skbkontur.ru

Thank you!Questions?