220 likes | 324 Views
Extensible syntax analyzers. Pavel Egorov pe@skbkontur.ru. Problem statements. L – some formal language; T : L R – it’s translator; L ext L – some extended language; T ext : L ext R ext – it’s extended translator. L T ext ( ) = T( ) We want compatibility!.
E N D
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!
Disclaimer • Consider syntax analyzers only. • Fixed algorithm of syntax analysis. • Extension due to modification of the grammar only.
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 e
Classic parse tree A Grammar: AaBd B b B A Word: “aabdd” Derivation: A aBd aAd aaBdd aabdd a B d A a B d b
Classic parse treeIs it good enough? G1: Aabc Extended G2: AaBc Bb Word: “abc” A a b c They are different! A a B c b
Classic parse treeIs it good enough? G1: Aa Extended G2: AaE Ee Word: “a” A a They are different! A a E e
Cancelled tree • Driven by labeled grammar • Cut off some inner nodes from parse tree • Cut off e-leafs from parse tree Is much better for parser extension!
Cancelled tree example parse tree A Labeled grammar: A a1B0d0B1 B b1E0 B x1 E e0 • Symbols labeled by 0 are not present in tree • e-leafs are always labeled by 0 B a B d x b E e A B a b cancelled tree x
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 ()
Extending transformations of grammars f – extending grammar transformation G - grammar L(f(G))L(G)
Extending transformations • ADD-transform adds some fixed new production to a grammar. • EXTRACT-transform: before: Aaxbygz after: AaxB0gz Bby
Extending transformations AE* all transforms, composed from arbitrary number of ADD- and EXTRACT-transforms.
Compatibility • Any transform from AE* doesn’t break compatibility of corresponding syntax analyzers. • 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)
Independent transforms G y1, y2AE* y1 y2 G2 Domain(y1) ? G1 Domain(y2) ? G1 G2 G12??? When it is possible? How can we do it?
Independent transforms What if G1 Domain(y2)? Sometimes we can solve this problem! Aabgd y1: AabCd; C g y2: AaBd; Bbg y2/y1: A aBd; BbC Conflict! Decision!
y/f - afixed transform • y/f - “fixed” y, which can be applied after f • Sometimes we can’t fix y... Aabg y: AXg; X ab f: AaY; Y bg • Recursive definition of the function y/f for y and f from AE*. • Sometimes y/f is not defined.
Independent transforms G y1 y2 G1 G2 y2/ y1 y1/ y2 ? G12 G21 y2/ y1y1? y1/ y2y2
Independent transforms G y1 y2 G1 G2 y2/ y1 y1/ y2 G12 y2/ y1y1 = y1/ y2y2
Conclusion • Syntax analyzers • driven by labeled grammars • cancelled trees as a result • AE* - a way to extend grammars safelly • y/f – a way to compose independent grammar transforms • Order of application of the extending transforms doesn’t matter!