Rscript in a Nutshell Rscript: examples Set: Basic types: - - PowerPoint PPT Presentation

rscript in a nutshell rscript examples
SMART_READER_LITE
LIVE PREVIEW

Rscript in a Nutshell Rscript: examples Set: Basic types: - - PowerPoint PPT Presentation

Rscript in a Nutshell Rscript: examples Set: Basic types: , , , (text location in specific file with comparison operators) type:


slide-1
SLIDE 1

Introduction to ASF+SDF

Rscript in a Nutshell

  • Basic types: , , , (text location in

specific file with comparison operators)

  • Sets, relations and associated operations (domain,

range, inverse, projection, ...)

  • Comprehensions
  • User-defined types
  • Fully typed
  • Functions and sets of equations over the above

Introduction to ASF+SDF

Rscript: examples

  • Set:

– type:

  • Set:

– type:

  • Relation:

– type:

Introduction to ASF+SDF

Rscript: examples

  • !

domain: all elements in lhs of pairs range: all elements in rhs of pairs carrier: all elements in lhs or rhs

  • f pairs

Introduction to ASF+SDF

Comprehensions

  • Comprehensions: "#$%&%'(((

– A generator is an enumerator or a test – Enumerators: )*+"# or )&)'*,"# – Tests: any predicate – consider all combinations of values in %&%'((( – if some % is false, reject that combination – compute "# for all legal combinations

slide-2
SLIDE 2

Introduction to ASF+SDF

Comprehensions

  • -$-*&'.

– yields &'.

  • -$-*&'.-

– yields .

  • /-$-/*&&0''0

– yields &0&'0'

Introduction to ASF+SDF

Functions

  • ,

/-$-/*,

– &&0''0yields &0&'0'

  • 12131312,

/-$13-12/*,

– &!'yields !&'

13, 12 indicate any type and are used to define polymorphic functions

Introduction to ASF+SDF

Toy program

4!*!5!*!5! *!5!6 *6 78 *9

  • *.

7 *

  • is undefined

may be undefined

Introduction to ASF+SDF

Toy program

4!*!5!*!5! *!5!6 *6 78 *9

  • *.

7 *

  • &

'

  • .
  • :";+&.

+"+ <,":0&&'''. .

slide-3
SLIDE 3

Introduction to ASF+SDF

Finding uninitialized variables

Use of x Def 2 of x Def 1 of x Start of program Along these path, we encounter a definition Along this path, we can reach a use without passing a definition Value of x may be undefined here

Introduction to ASF+SDF

Intermezzo: !8-

  • Reachability with exclusion of certain elements
  • 1=!8-

– 1=+! – 1=" – 1=1=,

  • !8-&'&'&'..

yields .

& '

  • .

Introduction to ASF+SDF

The undefined query

:";+((( +"+((( <,":((( >?>?= >)$>)*+"+>!8-0:";+@)<,":

Start from the root There is a path from the root to >: ) is not initialized Reach exclude Use the <,": relation Exclude all definitions of )

Introduction to ASF+SDF

Applying the undefined query

4!*!5!*!5! *!5!6 *6 78 *9

  • *.

7 *

  • &

'

  • .
  • Result:

is undefined may be undefined

slide-4
SLIDE 4

Introduction to ASF+SDF

Some Questions

  • There are several additional questions:

– In the example so far we have worked with statement

numbers but how do we make a connection with the source text?

– How do we extract relations like <,": and +"

from the source text?

Introduction to ASF+SDF

Use locations to connect with the source text

:";+((( +"+((( <,":(((

:";+ +"+ <,": ABB,+ Use location instead of number

Variable occurrence in a statement

Introduction to ASF+SDF

Example Rstore

<,": !!@@7CD8 D(((D! #(#C!!.'.EE.F !!@@7CD8 D(((D! #(#C!!'EG.F !!@@7CD8 D(((D! #(#C!!'EG.F !!@@7CD8 D(((D! #(#C!!F'&0.&0.F ((( :";+ ABB,+ CC!!@@7CD8 D(((D! #(#C!!&&'&&&F.& CC!!@@7CD8 D(((D! #(#C!!&&H&&E&FG& (((

  • Introduction to ASF+SDF

Extracting Facts

  • Goal: extract facts from source code and use as

input for queries

  • How should fact extraction be organized?
  • How to write a fact extractor?
slide-5
SLIDE 5

Introduction to ASF+SDF

Workflow Fact Extraction

Obtain sources of SUI Obtain sources of SUI Obtain grammar for source language of SUI Obtain grammar for source language of SUI Validate grammar Validate grammar Improve Improve Write queries Write queries Determine needed facts Determine needed facts Obtain fact extractor Obtain fact extractor Validate extracted facts Validate extracted facts Improve Improve Execute queries Execute queries Validate answers Validate answers Use answers Use answers Improve Improve Improve Improve Grammar Facts Queries

SUI = System Under Investigation

Introduction to ASF+SDF 18

Extracting Facts using ASF+SDF

  • Dump-and-Merge: Facts can be extracted per file

and be merged later

  • Extract-and-Update: Facts are extracted per file

and merged with previously extracted RStore

  • Both styles can be used, matter of taste

Introduction to ASF+SDF 19

Extracting Facts using ASF+SDF

  • Write traversal functions that extract facts from

source file

  • All-in-One: one function extracts all facts in one

traversal

– typically an accumulator that returns an Rstore – makes contribution to named relations in Rstore

  • Separation-of-Concerns: separate function for

each fact to be extracted

  • SoC is more modular and preferred

Introduction to ASF+SDF 20

All-in-One Strategy

Parse source file Parse source file Return partial RStore

  • r

merge with previous RStore Return partial RStore

  • r

merge with previous RStore Extract contribution to R1 and add to RStore ... Extract contribution to Rn and add to RStore Extract contribution to R1 and add to RStore ... Extract contribution to Rn and add to RStore

slide-6
SLIDE 6

Introduction to ASF+SDF 21

Separation of Concerns Strategy

Parse source file Parse source file Extract contribution to R1 Extract contribution to R1 Extract contribution to Rn Extract contribution to Rn

Combine into contribution to RStore Combine into contribution to RStore Return partial RStore

  • r

merge with previous RStore Return partial RStore

  • r

merge with previous RStore

Introduction to ASF+SDF 22

Extracting Facts from Pico Programs

  • Use ,+ for creating and extending Rstores
  • Use 5D<?7+ for getting position

information for specific sorts

  • Use 5D<!+ for unparsing a tree to

a string (unparse-to-string)

  • Write (example) functions

– 7I for extracting control flow – 5+! for making a statement histogram

Introduction to ASF+SDF 23

Fact extractor

!45!4D#D!D< !45!4D#D!D< 5D,+ !D?4 5D<?7 5D<!4

Introduction to ASF+SDF 24

RStores

  • A set of typed set/relational variables (see Rscript)
  • Primitives for

– Creating a new Rstore (!@) – Declaring a new variable with its type (!) – Setting/getting the value of a variable (/4) – Modifying the value of a variable by inserting, deleting

  • r replacing elements (, #!, , J5#)

– Changing integer variables (, )

slide-7
SLIDE 7

Introduction to ASF+SDF 25

Sets-and-Relations

  • Provides most of the Rscript functionality inside

ASF+SDF

  • Uses one common element type: ,

– less type-safe than Rscript

  • Provides all Rscript primitives:

– 5, 77, – 5, 5#, @7, ...

Introduction to ASF+SDF 26

Fact extractor

5!45!4D#D!D< #5D,+ #!45!4D#D!D< #!D?4 #5D<!4<?BA@?: #5D<!4+=3="K">= #5D<!4"-< #5D<?7+=3="K">= #5D<?7"-< 8 @7! ;I<,A%,3K,+@,+ ! L4! <,A%,3K,+@,+

Declare the two extraction functions

Introduction to ASF+SDF 27

Fact extractor

@7! 5+! <,A%,3K,+@,+ !!!5 @5#5 5+! +=3="K">=,+@,+ !!!5 @5#5 7I+=3="K">=C6CM@," ," ," @7!@ <,A%,3K,+,"

5+! is defined as traversal function

that will be applied to the sorts<,A%,3K and

+=3="K">=; It accumulates an,+ 7I is an ordinary function that returns triples for

each Pico language construct of the form: <entry points, internal connections , exit points>

Introduction to ASF+SDF 28

Fact extractor

!! C<4! C0@GM@<,A%,3K C:C0@GM@:"BN+ C+!C0@GM@+=3="K">= C+!MC0@GM@+=3="K">=C6CM C+!9C0@GM@+=3="K">=C6C9 C"#C0@GM@"-< C?C0@GM@<?BA@?: C"C0@GM@," C"C0@GM@," C,C0@GM@," CBC0@GM@," !! C+C0@GM@,+ C?C0@GM@?4

slide-8
SLIDE 8

Introduction to ASF+SDF 29

Histogram

O5! 8! L4! <4! + 5+! <4! !+ +! L4!

  • O5!

5+! ?*"#+ ++! L4! C34 C 5+! 7"#8+!M&+!M'7+ ++! L4! CB!C 5+! I8"#+!M+ ++! L4! CN#C

Declare the variable

+! L4! and

apply 5+! Only declare the cases

  • f interest and increment

relevant counter

Introduction to ASF+SDF 30

Cflow: series

O5! 74+&*!+B;I ","*7I+!M

  • ;I4:+!M+

+&B;I, 74@& "&,&"&*7I+! "','"'*7I+!9

  • 7I+!6+!9

"&5,&5,'#5"&"'"' 74@'7I

Declare the variable

B;I and

apply 7I Compute controlflow for a series of statements: <Entry1, Rel1 Rel2 (Exit1 Entry2), Exit2>

Introduction to ASF+SDF 31

Cflow: while

O5! 74@ ","*7I+!M B*5#!@@4"#4@!"#

  • 7II8"#+!M
  • B

5#5B"5,#5"B B

  • Compute controlflow for a while statement:

<{Control}, ({Control} Entry) Rel (Exit {Control}), {Control}>

Introduction to ASF+SDF 32

Cflow: if

74@. "&,&"&*7I+!M& "','"'*7I+!M' B*5#!@@4"#4@!"#

  • 7I7"#8+!M&+!M'7

B 5#5B"& 5#5B"' 5,&,' 5"&"' 7!5@74 B*5#!@@4+!4@!+!

  • 7I+!BB

Compute controlflow for an if statement: <{Control}, ({Control} Entry1) ({Control} Entry2) Rel1 Rel2, Exit1 Exit2>

slide-9
SLIDE 9

Introduction to ASF+SDF 33

Connecting the pieces ...

!+&*!@ +'*! L4! <4! +& +*;I<4! +'

  • !<,A%,3K<4! !,++

Create a new RStore Add controlflow facts Extraction of a given<,A%,3K will result in an,+ Add histogram facts

Introduction to ASF+SDF 34

Graph view (factorial)

Introduction to ASF+SDF 35

Barchart view (factorial)

Introduction to ASF+SDF 36

Further reading

  • www.meta-environment.org (select Documentation):

– Guided Tour: Playing with Booleans (flash) – ASF+SDF by Example – Writing Language Definitions in ASF+SDF – The Language Specification Formalism ASF+SDF – The Syntax Definition Formalism SDF – An Explanation of Error Messages of SDF (draft) – An Explanation of Error Messages of ASF (draft) – The Architecture of The Meta-Environment