context free grammars in javamop
play

context free grammars in JavaMOP CS 119 a property can be seen as - PowerPoint PPT Presentation

context free grammars in JavaMOP CS 119 a property can be seen as a language defined by a grammar 2 http://fsl.cs.uiuc.edu/index.php/Special:CFGPlugin 3 Instances of MOP MOP JavaMOP BusMOP HardwareMOP logic plugins MOP CFG


  1. context free grammars in JavaMOP CS 119 a property can be seen as a language defined by a grammar

  2. 2

  3. http://fsl.cs.uiuc.edu/index.php/Special:CFGPlugin 3

  4. Instances of MOP MOP JavaMOP BusMOP HardwareMOP … logic plugins MOP … CFG ptLTL ptCaRet ERE LTL languages JavaMOP BusMOP … today 4

  5. the 4 elements context free languages pushdown context free Automata grammars (theoretical concept) parsers 5

  6. Chomsky’s language hierarchy http://en.wikipedia.org/wiki/Chomsky_hierarchy 6

  7. example context free languages and pushdown automata l n r n S ! ² | lSr input: llll rrrr ² q 1 ² ! push($) l l q 4 q 2 l ! push( l ) l l ² top()=$ r top()= l ! pop() $ ! pop() q 3 r top()= l ! pop() 7

  8. example CFL parsers grammar: S ! Ax Consider the term: ax A ! a A ! b • top-down parser – expands non-terminals into RH-sides S ! Ax ! ax • bottom-up parser – reduces RH-sides to non-terminals – LALR(1) ½ LR(1) ½ DCFL ½ CFL JavaMOP ax ! Ax ! S 8

  9. structure of a table-driven bottom-up parser • an input buffer • a stack of states visited • an action-table giving a grammar rule to apply given the current state and current terminal in input buffer • a goto-table describing which new state it should go to 9

  10. actions • Shift - push token onto stack • Reduce - remove handle from stack and push on corresponding nonterminal • Accept - recognize sentence when stack contains only the distinguished symbol and input is empty • Error - happens when none of the above is possible; means original input was not a sentence! 10

  11. abstract algorithm • start with an empty stack • a "shift" action corresponds to pushing the current input symbol onto the stack • a "reduce" action occurs when we have a handle on top of the stack. To perform the reduction, we pop the handle off the stack and replace it with the terminal on the LHS of the corresponding rule. 11

  12. the lock/release example specification S ! lock S release S ! epsilon () lock lock lock release release release SHIFT (lock) lock lock release release release SHIFT (lock lock) lock release release release SHIFT (lock lock lock) release release release SHIFT (lock lock lock release) release release RED(S) (lock lock S) release release SHIFT (lock lock S release) release RED(S) (lock S) release SHIFT (lock S release) RED(S) (S) ACC 12

  13. expressions example 13

  14. recall structure of a table-driven bottom-up parser 14

  15. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 0 15

  16. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 1 0 16

  17. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 2 1 0 17

  18. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 2 2 1 0 18

  19. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 5 2 2 1 0 19

  20. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 5 2 7 2 2 1 1 0 0 20

  21. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 9 7 2 1 0 21

  22. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 9 7 2 6 1 1 0 0 22

  23. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 8 6 1 0 23

  24. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 8 6 1 3 0 0 24

  25. JavaMOP specification input event lock lock event release lock lock productions : release S -> lock S release | epsilon release release $ 8 6 1 3 0 0 25

  26. JavaMOP algorithm 8 6 1 0 26

  27. properties of Java library APIs properties of Java library APIs R 1 : There should be no two calls to next() without a call to hasNext() in between, on the same iterator . 27

  28. an example an example class class Test { public public static tatic void oid main(String[] args) { Vector<Integer> v1 = new ew Vector(); Vector<Integer> v2 = new ew Vector(); v1.add(1); v1.add(3); v2.add(5); v2.add(7); Iterator it1 = v1.iterator(); Iterator it2 = v2.iterator(); int int sum = 0; should have been: if if(it2.hasNext()) if(it2 if it2.hasNext()) sum += (Integer)it2.next(); if if(it1.hasNext()) sum += (Integer)it2.next(); ) System. out .println(”sum(v2) = " + sum); } unguarded call: } it2 it2.next() 28

  29. recall the recall the regular expression specification egular expression specification /*@ partial, matching partial centralized against suffix trace scope = global logic = ERE HasNext(Iterator i) { event hasnext<i> : end(call(* i.hasNext())); event next<i> : begin(call(* i.next())); formula : next next } validation validation handler{ (not violation) System.err.println("*** call hasNext() before next()"); } @*/ 29

  30. CFG Property in FG Property in JavaMOP avaMOP (trying to (trying to replicate the RE solution) eplicate the RE solution) /*@ partial centralized scope = global logic = CFG HasNext(Iterator i) { event hasnext<i> : end(call(* i.hasNext())); event next<i> : begin(call(* i.next())); productions : in this case Pattern -> next next we can write } the same spec more or less validation handler{ System.err.println("*** call hasNext() before next()"); } @*/ 30

  31. recall recall this other regular expression his other regular expression (hasNext hasNext* next)* which was slightly too strong, but let’s try to emulate it i) total trace semantics ii)looking for violation 31

  32. CFG Property in CFG Property in JavaMOP avaMOP /*@ no longer partial centralized scope = global logic = CFG HasNext(Iterator i){ event hasnext<i> : end(call(* i.hasNext())); event next<i> : begin(call(* i.next())); productions : Iterate -> hasnext HasnextStar next Iterate | epsilon, HasnextStar -> hasnext HasnextStar | epsilon } violation handler{ System.err.println("*** call hasNext() before next()"); } @*/ 32

  33. DEMO ON SLIDES 33

  34. 34

  35. 35

  36. 36

  37. 37

  38. 38

  39. 39

  40. 40

  41. 41

  42. 42

  43. END OF DEMO ON SLIDES 43

  44. CFG Property in JavaMOP CFG Property in avaMOP as a state machine as a state machine /*@ centralized scope = global logic = CFG HasNext(Iterator i) { event hasnext<i> : end(call(* i.hasNext())); event next<i> : begin(call(* i.next())); productions : Iterate -> hasnext HasnextStar next Iterate | epsilon, epsilon needed HasnextStar -> hasnext HasnextStar | epsilon } | epsilon State0 -> hasnext State1 , State1 -> hasnext State1 | next State0 violation handler{ System.err.println("*** call hasNext() before next()"); state machine } like notation @*/ 44

  45. suppose we changed the example suppose we changed the example class Test { class public public static tatic void oid main(String[] args) { Vector<Integer> v1 = new ew Vector(); Vector<Integer> v2 = new ew Vector(); v1.add(1); v1.add(3); v2.add(5); v2.add(7); Iterator it1 = v1.iterator(); Iterator it2 = v2.iterator(); change from: int int sum = 0; if(it2 if it2.hasNext()) if if(it1.hasNext()) sum += (Integer)it2.next(); to: if if(it1.hasNext()) if if(it1 it1.hasNext()) sum += (Integer)it2.next(); System. out .println(”sum(v2) = " + sum); } } program still has the same error! does our latest spec still catch the error? 45

  46. no: CFG property will not catch error no: CFG property ill not catch error /*@ centralized scope = global logic = CFG HasNext(Iterator i) { the monitor event hasnext<i> : end(call(* i.hasNext())); generating event event next<i> : begin(call(* i.next())); is hasnext since it occurs first productions : (and not next!!) Iterate -> hasnext HasnextStar next Iterate | epsilon, HasnextStar -> hasnext HasnextStar | epsilon Since only operation } on it2 is next, a monitor violation handler{ is not created. System.err.println("*** call hasNext() before next()"); } @*/ 46

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend