Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and...

44
Programming Languages Sem inar, Spring 2004 1 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th , 2004

Transcript of Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and...

Page 1: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

1

Grammar Adaptation (Ralf Lämmel, CWI)

Presentation and slides by:Faizan Javed

March 9th, 2004

Page 2: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

2

1. Introduction

Grammar Adaptation:1. Model Restructuring (fold unfold) and

Local Changes (removal of phrases, restriction)

2. Performed by grammar programmers manually, otherwise.

3. Transformations relevant for grammar development, maintenance, re-engineering and recovery.

Page 3: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

3

1. Introduction (contd.)

Grammar Recovery1. Concerned with derivation of a

languages’ grammar.2. Grammar transformations can be used

to facilitate grammar recovery.3. Important for software re-engineering

- Y2K problem, Euro-conversion problem

Page 4: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

4

1. Introduction (contd..)

Grammar recovery is hard:1. ancient languages (ex: Cobol dialects)2. in-house languages and language

extensions COBOL example [Lämmel, Verhoef]:

1. Grammar transformations made the process:- accessible, traceable and measurable.

Page 5: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

5

1. Introduction (contd…) Sample Adaptation (VS Cobol II):1. Problems faced were errors, omissions

and use of informal comments.

2. Reason: lack of use of formal methods

3. Example: VS Cobol II REDEFINES clause

Page 6: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

6

1. Introduction (contd….) REDEFINES clause is

actually of type: “REDEFINES” data-name

Sample defines a structure of a data item with a REDEFINES clause.

Use “delete” transformation operator:

delete level-number (data-name | “FILLER")? in REDEFINES-clause

Page 7: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

7

1. Introduction (contd..)

Transformational approach benefits:

1. Adds Traceability: changes can be recorded

2. Reusable adaptation scripts: useful for dialects of the same grammar

3. Relaxed notions for non-semantics preserving operators

Page 8: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

8

2. Grammar Fragments Grammar fragments are a variant of

context-free grammars used for grammar transformations. In grammar adaptation, grammars “evolve”.

“Evolving” grammars rather than “reduced” grammars: reduced grammars assume that each production can be used in some derivation of a terminal string from the start symbol.

Page 9: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

9

2.1 Standard Context-Free grammars

Standard CFG definition: <N, T, s, P>

1. N and T are disjoint finite sets of nonterminals and terminals respectively.

2. s is the start symbol,3. P is a finite set of productions or

(context-free) rules with4. A production <n, u> P with n 2 N and u 2 (N U T)* is also written as n → u.

Page 10: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

10

2.2 Deviation Start symbol not required for incomplete

grammars No explicit declaration of non-terminals and

terminals – grammar represented just by productions

Non-terminals from and terminals from Bottom non-terminals: non-terminals which are not

terminated This deviation known as grammar fragments. Domain defined as:

Page 11: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

11

2.2 Deviation (contd..) Relevant set of grammar symbols:

Page 12: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

12

2.2 Deviation (contd..) Bottom non-terminals ( ):1. Non-terminal lacking a definition, OR2. Indicates a connectivity problem; defined

with a different left-hand side.

Top non-terminals ( ):1. Non-terminals defined, but not used.2. Start symbols usually meet this criteria.

Page 13: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

13

2.3 Semantics Language generated by a common CFG

defined as: Terminal strings can also be generated

from an arbitrary non-terminal n; the semantics of n w.r.t. a grammar fragment are:

Page 14: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

14

2.3 Semantics (contd..) Terminal strings not sufficient since

grammar fragments are not necessarily terminated (ex: productions with bottom non-terminals)

Incomplete grammar :

Page 15: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

15

2.3 Semantics (contd..) Sentential forms provide an upper

bound on the denotation.

However, sentential forms don’t provide a basis to state the semantics preservation of fold/unfold modulations.

Page 16: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

16

2.3 Semantics (contd..) Observable non-terminals: restrict

sentential forms so that only particular non-terminals are observable.

means semantics restricted to sentential forms consisting solely of bottom non-terminals and terminals.

Page 17: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

17

2.3 Semantics (contd..) Looping non-terminals: non-terminal “b” lacks

a base case

“Ultimate” denotation of n w.r.t :

Page 18: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

18

3. Formal Reasoning

Need to compare grammars, and characterize the properties of grammar transformations.

Certain relations on the grammar fragments are defined.

Page 19: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

19

3.1 Equivalent grammars Equivalent grammars:

Useful in fold/unfold manipulations

Page 20: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

20

3.1 Equivalent Grammars Equivalent grammars example:

Using equivalence properties,

Page 21: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

21

3.2 Beyond Equivalence Equivalence often too restrictive to

characterize related grammars! Some “relaxations” on the relations

between two grammars:

Page 22: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

22

3.2 Beyond Equivalence (contd.)

Equivalence modulo renaming:

In above fig, it holds that

Page 23: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

23

3.2 Beyond Equivalence (contd.)

Sub-grammar relation:

In above fig., it holds that but not vice versa.

Page 24: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

24

3.2 Beyond Equivalence (contd.)

Enrichment relation:

In above fig, it holds that

Page 25: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

25

3.2 Beyond Equivalence (contd.)

Instance relation:

In above fig., it holds that

Page 26: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

26

3.3 Grammar Transformers Partial grammar transformers: Grammar relations

can be used to define various preservation properties for grammar transformations.

Page 27: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

27

4. Transformation Framework

Define a framework for grammar transformations offering transformation primitives and combinators.

Discuss supplementary concepts like focus, constraints and symbolic operands.

Page 28: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

28

4.1 Primitives id: identity function fail: undefined grammar transformation reset: returning empty set of rules add/subtract: add/subtract a rule from a

grammar replace: replace a phrase by a phrase in a

grammar substitute: substitute non-terminal by a non-

terminal

Page 29: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

29

4.1 Primitives (contd.)

Page 30: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

30

4.2 Combinators

Page 31: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

31

4.3 Constraints

Page 32: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

32

4.4 Symbolic operands Introduces higher level of abstraction? Examples:1. definition of n : Denotes RHS of n.

Useful in unfolding operation; no need to point out definition explicitly.

2. all: focus operand. Denotes all non-terminals defined in a grammar. Useful if an operator expecting a focus parameter should be applied globally.

Page 33: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

33

5.1 Operator Suite - Overview Stepwise adaptation: application of a

sequence of transformation operators from the operator suite, T1;..;Tm.

Transformational grammar programmer: uses only operators of the suite, not the combinators or the primitives.

Three groups of operators: 1. Refactoring 2. Construction3. Destruction

Page 34: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

34

5.2 Refactoring Restructure grammar so that:1. Comprehensibility is improved2. Subsequent adaptation steps are easier to

perform

Semantics-preserving in the narrow sense.

Use of pre- and post- conditions in operators.

Page 35: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

35

5.2 Refactoring (contd.)

Page 36: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

36

5.3 Construction Facilitate grammar substitution,

extension and completion.

Page 37: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

37

5.3 Construction (contd.) Unify example:1. Useful if a bottom non-terminal should be

resolved interms of an existing definition2. Or, if two bottom non-terminals intentionally

coincide.

Page 38: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

38

5.3 Construction (contd.) Useful in grammar completion and

connection:1. Missing rules added by include.2. Too restrictive phrases generalised by

generalise.3. Missing definition of non-terminals

established using resolve.4. Non-terminals unified with unify.

Page 39: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

39

5.4 Destruction Essentially inverse of construction

(except delete operator)

Page 40: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

40

5.4 Destruction (contd.) Separate

example:

Page 41: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

41

5.4 Destruction (contd.)

Useful for correction or revision:1. Too general phrases can be

restricted.2. Superfluous rules or definitions can be

excluded or rejected, respectively.3. Accidentally unified phrases can be

separated by introducing new non-terminals in certain occurrences.

Page 42: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

42

5.5 Discussion

Page 43: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

43

5.5 Discussion (contd.) Some semantics preservation

examples:1. preserve: strictly preserving –

equivalence of phrases implies equivalent grammars.

2. unfold: strictly preserving – unfold defined interms of preserve.

3. introduce: introducing – adds a rule for a new non-terminal

Page 44: Programming Languages Seminar, Spring 20041 Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.

Programming Languages Seminar, Spring 2004

44

6.0 Conclusion Towards proper grammar re-engineering:

-viewed as coding work-not a research focus anymore…more research needed!-paper contributes by defining the foundations of an adaptive style of grammar development.

Semantics preservation and relaxation:-refactoring operators are semantics preserving only.-paper introduces a set of weaker preservation notions, suitable to characterise revisions and extensions.

Perspective:- more global notions than just preservation – can the grammar be improved with the transformation sequence?-Open research problem: how clients of a grammar such as compiler compiler inputs, rewrite rules have to be adapted if the grammar serving as contract changes.