next up previous contents index
Next: Marking and finding nodes Up: Statements Previous: IO statement

   
Browsing in the source code

The fortran code is described in a tree called an AST. The first node of a AST is called the root node. In practice, we use the term node or root node to reference the first node. In order to move in a AST, we introduce the following attributes and functions:

  $\blacktriangleright$ t.VARIANT
 
The   expression returns a string that describe the nature of the node t (see the attribute AST to obtain more informations about variants). For example, the variant "ass" is used for assignment statements. 
    $\vartriangleright$ Example 16:Verify that the selected statement is a DO loop.

   IF ($cstat.VARIANT!="do") THEN 
      ERROR(1,"Please, select a DO statement") 
   ENDIF

  $\vartriangleright$ Remark: In some previous versions of TSF, a comparison such as ($cstat.VARIANT!=do) was possible. The nature of the identifier do is ambiguous ; is do a variant/string or a TSF variable ? This syntax is not supported in by new versions of TSF.

  $\blacktriangleright$ t.AST
 
The   expression returns a string that describe the structure of the AST t. The table 2 shows some examples. The following rules can be used to read those descriptions
var ( p1,p2,..,pn) : The AST of variant var has n children p1..pn. The number of arguments is constant.
var [p1,p2,..,pn] : The AST of variant var has n children p1..pn. The number of arguments is not constant.
( var   v ) : The AST is a terminal node (i.e. no child) of variant var associated to the value v. The nature of v depends of the variant. (see CHILD(0)).
$node\{ ... \}$ : An attribute attached to the node. For example, a comment attached to a statement.


 
Table 2: a few examples of AST structures
Description Fortran expression the result given by AST
A variable x name x
A real constant 0.5 real_cst 0.5
A binary operator i*2 mul(name i,int_cst 2)
An expression with several operators (i*2)+j/3-k

sub(
  add(
    parenth(mul(name i,
                int_cst 2)),
    div(name j,int_cst 3)),
  name k)



An assignment i=j ass(name i,name j)
A conditionnal statement, a function call and some sequences of statements

IF (x .GE. 0) THEN
  y = x
ELSE
  PRINT *, 'Warning'
  y = abs(x)
ENDIF





struct_if(
  none(), 
  ge(name x, int_cst 0), 
  l_stat[ass(name y, name x)],
  l_stat[
    print(star(), 
         l_ioexp[
           fstring_cst[
            atom_string_cst Warning]
           ]),
    ass(name y, 
        func_call(name abs, 
                  l_exp[name x]))
    ])



A DO statement with bounds, a labeled statement and a comment

C A COMMENT
   DO 10 i=1,n,2 
     PRINT *,i
10 CONTINUE





do (
  none(), 
  int_cst 10, 
  bounds(name i, 
         int_cst 1, 
         name n, 
         int_cst 2),
  l_stat[
      print(star(), l_ioexp[name i]), 
      labstat(int_cst 10, continue())
      ]
  ){^prefix
      comment_s[comment A COMMENT]}



 

  $\blacktriangleright$ t.NBCHILD
 
The   expression returns the number of children of the AST t.
  $\blacktriangleright$ t.CHILD(n)
 
The   expression returns the child of rank n of the AST t. The rank of the first child is 1 and the rank of the last one is t.NBCHILD. The rank 0 is used to reference the value associated to an terminal AST.

  $\blacktriangleright$ t.PARENT
 
The   expression is the opposite of CHILD.

  $\blacktriangleright$ t.RANK
 
The expression returns the index of t in list of children of its parent.


\begin{displaymath}\textit{t} \equiv \textit{t}.\textbf{PARENT}.\textbf{CHILD}(\textit{t}.\textbf{RANK})\end{displaymath}

  $\blacktriangleright$ t.FIRST
 
The   expression returns the first statement in the AST t. When applied to $cunit,the FIRST attribute returns the first statement of the fortran function or subroutine. When applied to a loop of to a conditional statement, it returns the first statement of the body.
  $\blacktriangleright$ t.LAST
 
The   expression is the opposite of t.FIRST. Its returns the last statement in the AST t.
  $\blacktriangleright$ t.SKIP
 
The   expression returns the next instruction at the same level of the AST t. (see figure 8 for more details).

  $\blacktriangleright$ t.NEXT
 
The   expression returns to the next instruction whatever its level may be in the code.
  $\blacktriangleright$ t.PREVIOUS
 
The   expression returns the previous instruction whatever its level may be in the code. It is the opposite of t.NEXT.

The figures 8 illustrates the use of these functions. Note the presence of the keyword header which enables us any manipulations (cut or paste for instance) at any level in the fortran code without being worried by losing the current level.


  
Figure 8: Browse operators in a program
\includegraphics[width=14cm]{Figures/moveOperatorsBW.eps}

The figure 9 illustrates the same functions on a fortran AST view.


  
Figure 9: Browse operators on a fortran AST
\includegraphics[width=16cm]{Figures/moveOperatorsTreeBW.eps}

  $\vartriangleright$ Remark: The attributes FIRST, LAST, SKIP, NEXT and PREVIOUS are also available as obsolete statements:

$\bullet$
FIRST is equivalent to $cstat:=$cunit.FIRST
$\bullet$
LAST is equivalent to $cstat:=$cstat.LAST
$\bullet$
SKIP is equivalent to $cstat:=$cstat.SKIP
$\bullet$
NEXT is equivalent to $cstat:=$cstat.NEXT
$\bullet$
PREVIOUS is equivalent to $cstat:=$cstat.PREVIOUS


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