Successfully reported this slideshow.
Dynamic Grammars
 Lukas Renggli, Stéphane Ducasse,
   Tudor Gîrba, Oscar Nierstrasz
grammar
a set of rules governing what
  strings are valid or allowable
  in a [formal] language.




                    [...
dynamic grammar
a high-level grammar that
  executes at runtime behaviors
  that other grammars perform
  during compilati...
Dynamic Grammars
Dynamic Languages

‣ Late-bound behavior
‣ First-class representation
‣ On-the-fly transformation
‣ Intros...
Why would
we want that?
Language and Tool Extensions


Language Boxes




Host Environment
Language and Tool Extensions


Language Boxes




                               Grammars
                               D...
Language
               *
                    Change


Language       *   Language
  Box               Concern
           ...
Language
 Change
           =
               δ   Host Language
                     Grammar
Language          Grammar
           =
 Change        Transformation
scanIdentifier
	 self step.	
	 ((currentCharacter between: $A and: $Z) or:
[ currentCharacter between: $a and: $z ]) ifTrue...
✖
scanIdentifier
	 self step.	
	 ((currentCharacter between: $A and: $Z) or:
[ currentCharacter between: $a and: $z ]) ifTr...
#(	#[1 0 9 0 25 0 13 0 34 0 17 0 40 0 21 0 41]
	 #[1 0 9 0 25 0 13 0 34 0 93 0 76 0 157 0 112]
	 #[1 2 38 0 21 2 38 0 25 2...
✖
#(	#[1 0 9 0 25 0 13 0 34 0 17 0 40 0 21 0 41]
	 #[1 0 9 0 25 0 13 0 34 0 93 0 76 0 157 0 112]
	 #[1 2 38 0 21 2 38 0 25...
ID ::= letter { letter | digit } ;




    a..z             a..z
                     0..9
id := #letter , (#letter / #digit) star



            sequence

       letter         star

                     choice

...
id := #letter asParser ,
(#letter asParser / #digit asParser) star


               sequence

          letter        star...
Scannerless Parser
Parsing Expression
     Grammar
Packrat Parser
Parser Combinator
Grammar
Transformation
Graph
Rewriting
#letter → #letter / $_



     sequence

letter        star

             choice

         letter      digit
#letter → #letter / $_



         sequence

  choice            star

letter     _      choice

           choice        ...
Optimizations
(`a / `b) / `c → `a / `b / `c



          sequence

   choice            star

 letter     _      choice

            cho...
(`a / `b) / `c → `a / `b / `c



          sequence

   choice           star

 letter    _       choice

          letter...
(`a / `b) / `c → `a / `b / `c



in general             sequence
 a graph
                choice           star

         ...
Conflicts?
SELECT * FROM users
|r|
r := SELECT * FROM users .
^ User fromRow: r
expr | sql
<SQL: SELECT * FROM users>
ordered



expr / sql
No Conflicts
No Ambiguities
surprise



expr / sql
surprise



sql / expr
!expr sql / !sql expr



expr | sql
!expr sql / !sql expr / ui



expr $ sql
Dynamic grammars
Dynamic grammars
Upcoming SlideShare
Loading in …5
×

Dynamic grammars

958 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Dynamic grammars

  1. 1. Dynamic Grammars Lukas Renggli, Stéphane Ducasse, Tudor Gîrba, Oscar Nierstrasz
  2. 2. grammar a set of rules governing what strings are valid or allowable in a [formal] language. [Adapted from The New Oxford American Dictionary on Grammar]
  3. 3. dynamic grammar a high-level grammar that executes at runtime behaviors that other grammars perform during compilation, see dynamic languages. [Adapted from Wikipedia on Dynamic Programming Languages]
  4. 4. Dynamic Grammars Dynamic Languages ‣ Late-bound behavior ‣ First-class representation ‣ On-the-fly transformation ‣ Introspection and reflection
  5. 5. Why would we want that?
  6. 6. Language and Tool Extensions Language Boxes Host Environment
  7. 7. Language and Tool Extensions Language Boxes Grammars Dynamic Host Environment
  8. 8. Language * Change Language * Language Box Concern * * Language Scope
  9. 9. Language Change = δ Host Language Grammar
  10. 10. Language Grammar = Change Transformation
  11. 11. scanIdentifier self step. ((currentCharacter between: $A and: $Z) or: [ currentCharacter between: $a and: $z ]) ifTrue: [ [ self recordMatch: #IDENTIFIER. self step. (currentCharacter between: $0 and: $9) or: [ (currentCharacter between: $A and: $Z) or: [ currentCharacter between: $a and: $z ] ] ] whileTrue. ^ self reportLastMatch ]
  12. 12. ✖ scanIdentifier self step. ((currentCharacter between: $A and: $Z) or: [ currentCharacter between: $a and: $z ]) ifTrue: [ [ self recordMatch: #IDENTIFIER. self step. (currentCharacter between: $0 and: $9) or: [ (currentCharacter between: $A and: $Z) or: [ currentCharacter between: $a and: $z ] ] ] whileTrue. ^ self reportLastMatch ]
  13. 13. #( #[1 0 9 0 25 0 13 0 34 0 17 0 40 0 21 0 41] #[1 0 9 0 25 0 13 0 34 0 93 0 76 0 157 0 112] #[1 2 38 0 21 2 38 0 25 2 38 0 26 0 13 0 34] #[0 1 154 0 16 0 21 0 25 0 26 0 34 0 40 0 41] #[0 1 210 0 76 0 81] #[0 1 214 0 76 0 81] #[1 0 173 0 76 0 177 0 81] #[0 1 134 0 16 0 21 0 25 0 26 0 34 0 40 0 41] #[1 1 46 0 21 1 46 0 25 1 46 0 26 1 69] #[1 1 54 0 21 1 54 0 25 1 54 0 26 1 54 0 34] #[0 2 102 0 21 0 25 0 26 0 34 0 40 0 41 0 76] #[0 2 50 0 21 0 25 0 26 0 76 0 79] #[1 1 13 0 76 2 85 0 124 1 21 0 125] #[1 2 89 0 17 2 30 0 21 2 30 0 82] #[1 2 93 0 21 2 97 0 82] )
  14. 14. ✖ #( #[1 0 9 0 25 0 13 0 34 0 17 0 40 0 21 0 41] #[1 0 9 0 25 0 13 0 34 0 93 0 76 0 157 0 112] #[1 2 38 0 21 2 38 0 25 2 38 0 26 0 13 0 34] #[0 1 154 0 16 0 21 0 25 0 26 0 34 0 40 0 41] #[0 1 210 0 76 0 81] #[0 1 214 0 76 0 81] #[1 0 173 0 76 0 177 0 81] #[0 1 134 0 16 0 21 0 25 0 26 0 34 0 40 0 41] #[1 1 46 0 21 1 46 0 25 1 46 0 26 1 69] #[1 1 54 0 21 1 54 0 25 1 54 0 26 1 54 0 34] #[0 2 102 0 21 0 25 0 26 0 34 0 40 0 41 0 76] #[0 2 50 0 21 0 25 0 26 0 76 0 79] #[1 1 13 0 76 2 85 0 124 1 21 0 125] #[1 2 89 0 17 2 30 0 21 2 30 0 82] #[1 2 93 0 21 2 97 0 82] )
  15. 15. ID ::= letter { letter | digit } ; a..z a..z 0..9
  16. 16. id := #letter , (#letter / #digit) star sequence letter star choice letter digit
  17. 17. id := #letter asParser , (#letter asParser / #digit asParser) star sequence letter star choice letter digit
  18. 18. Scannerless Parser
  19. 19. Parsing Expression Grammar
  20. 20. Packrat Parser
  21. 21. Parser Combinator
  22. 22. Grammar Transformation
  23. 23. Graph Rewriting
  24. 24. #letter → #letter / $_ sequence letter star choice letter digit
  25. 25. #letter → #letter / $_ sequence choice star letter _ choice choice digit letter _
  26. 26. Optimizations
  27. 27. (`a / `b) / `c → `a / `b / `c sequence choice star letter _ choice choice digit letter _
  28. 28. (`a / `b) / `c → `a / `b / `c sequence choice star letter _ choice letter _ digit
  29. 29. (`a / `b) / `c → `a / `b / `c in general sequence a graph choice star letter _ choice letter _ digit
  30. 30. Conflicts?
  31. 31. SELECT * FROM users
  32. 32. |r| r := SELECT * FROM users . ^ User fromRow: r
  33. 33. expr | sql
  34. 34. <SQL: SELECT * FROM users>
  35. 35. ordered expr / sql
  36. 36. No Conflicts
  37. 37. No Ambiguities
  38. 38. surprise expr / sql
  39. 39. surprise sql / expr
  40. 40. !expr sql / !sql expr expr | sql
  41. 41. !expr sql / !sql expr / ui expr $ sql

×