next up previous contents index
Next: Fortran declarations Up: Statements Previous: Marking and finding nodes

   
Use the pattern-matching facilities in scripts

In addition to use the pattern-matching mechanism for finding some specifics FORTRAN constructions (see chapter 11), some script statements have been add in order to ease the comparison of AST at the script level.

  $\blacktriangleright$ GETPAT(astOrstring)
 
    This function allows to create a pattern from a string or from a FORTRAN AST. In this preliminary version, if you need to create a pattern from a string, it is recommend to use the pattern editor (see chapter 5.2) in order to ease this step.

  $\blacktriangleright$ MATCH(pattern,ast)
 
    This function checks the pattern against an AST. It returns a boolean value according to the result of the comparison. In the case where the comparison is true, the matched variables are automatically add to the script's symbol table (status global, see chapter 6.1 for more details).

  $\blacktriangleright$ NEXTMATCH(pattern,ast)
 
    This function launch the research of the pattern from the specified position by the ast argument. In the case where the pattern-matching is successful, the new ast found is return. () otherwise. Warning, the patterns variables are not updated by this function.


$\vartriangleright$ Example 19: Create a pattern from a string.
SCRIPT UsePattern ()
  pat := GETPAT ("(do ?v144 ?v145 (bounds ?index ?start ?end ?step) 
                  (?? ?body))") 

  IF (MATCH (pat,$cstat)) THEN
      PRINT "the selection matches the pattern"
      PRINT ?index
      PRINT ?start
      PRINT ?end
      PRINT ?step
      PRINT ?body
   ELSE
        PRINT "the selection is different from the pattern"
   ENDIF
ENDSCRIPT

Fortran code selection Results
      do 100 i = 1,ndim1
        a(i) = var1
        b(i) = var2
        c(i) = var2
 100  continue
Display message
?index
?start
?end
?step
?body
the selection matches the pattern
i
1
ndim1

a(i) = var1
b(i) = var2
c(i) = var2
100  continue
 



$\vartriangleright$ Example 20: Create a pattern from a FORTRAN AST.
SCRIPT UsePattern ()
   pattern := GETPAT(PARSESTAT("               c2(i,j) = 0"))
   PRINT pattern

   IF (MATCH (pat2,$csel)) THEN
        PRINT "the selection is equal to the pattern"
   ELSE
        PRINT "the selection is different from the pattern"
   ENDIF        
ENDSCRIPT

Fortran code selection Results
     c2(i,j) = 0
Display pattern
Display message
(ass (vardim c2 (l_exp i j)) 0)
the selection matches the pattern
 



$\vartriangleright$ Example 21: Use the NEXTMATCH function to go over the current unit.
SCRIPT SearchAllMatch ()
  FIRST // set $cstat
  
   // pattern created from the pattern editor
   // we are looking for something like: 
   //      ?array1(?v117,?index) ?op ?array2(?index, ?v121)
   // we are interested by finding some expressions which involved two arrays 
   // with the same indice between their rows and their columns
  pat := GETPAT ("(?op (vardim ?array1 (l_exp ?v117 ?index)) 
                       (vardim ?array2 (l_exp ?index ?v121)))")

  newMatch := NEXTMATCH(pat,$cstat)
  WHILE (newMatch)
    PRINT ""
    PRINT "A new match has been found"
    PRINT newMatch
    PRINT newMatch.ENCLOSINGSTAT // Get the enclosing statement

    newMatch := NEXTMATCH(pat,newMatch)
  ENDWHILE

  PRINT "No more match" 
 // Note: can also use the Motif Builtin Dialog functions
 // to display message
ENDSCRIPT
 

Fortran code Results
 ...

 do k = 1,ndim2
   c2(i,j) = a2(i,k)*b2(k,j)
           + c2(i,j)
 end do
 ...

 do 10 i = 1,ndim1,2
  b(i) = c2(var2,i) + b2(i,1)
  c(i) = var2
0   continue
 ...
First 
match




Second 
match


End of  
script
A new match has been found
a2(i,k)*b2(k,j)
c2(i,j) = a2(i,k)*b2(k,j) + c2(i,j)



A new match has been found
c2(var2,i)+b2(i,1)
b(i) = c2(var2,i) + b2(i,1)

No more match
 



next up previous contents index
Next: Fortran declarations Up: Statements Previous: Marking and finding nodes
Yann Mevel
1999-04-30