Skip to Content

Introduction

Sometimes ABAP programs can encounter a scenario where some particular syntax needs to identify and require a replacement dynamically. This situation is very common in SAP upgrade projects where obsolete syntaxes and Function modules need a replacement which is a tedious task if done manually.

Significance of SCAN ABAP Statement

SCAN ABAP Statement provides an easy way to do the same. It scans the whole program at one go and provides the whole code semantics and syntaxes in tabular format which can be used later. Lets checkout the SCAN ABAP statement in detail:

SCAN ABAP-SOURCE is the command to scan the complete code.

SCAN ABAP-SOURCE Itab_source levels into i_levels TOKENS INTO i_tokens
STATEMENTS INTO i_statements.

where

 Itab_source = contain Program  source code

I_levels = contains levels in a Program(Level of includes if exists )

I_tokens = contains all tokens like obsolete syntaxes in a tabular format

I_statements = contains information regarding indexes of syntaxes

I_levels, i_statements and i_tokens are the most important tables, as these tables contain all the information of source code and can be used at any point of time for analysis.

Detail Description of Tables:

I_levels(Table which captures Levels details):-

As shown in below diagram, i_levels table describes the levels in a main program. Levels are basically the depth of include files in a program. If main program has one include file so its depth is 2 and level is 1 but if this include file contains another include file in itself then its depth would be 3 and level would be 2.But this table is significant only when scan abap-source command is used with includes addition which also scans all the includes available in the main program.

 For e.g.: As depicted in below picture, main program contains one include file, therefore i_levels table is displaying the depth 2 and level 1 along with the name of include file.

 

I_statements (Table which captures Row wise details):-

This table contains level wise row and column information as shown in below diagram. It shows at a particular level there is a block  of code which is of what type like K, I or S and having  a terminator ‘.’ Or ‘,’.

For E.g.: As shown below that at level 1 from row no 5 to row no 8, there is terminator ‘.’ With type S (String, i.e. character literal).

I_TOKENS( Table which captures actual code in the form of Tokens):-

I_tokens table contains all the scanned code in token form which has a relation with i_statements table.As i_statements table tell that from row no.  5 to 8 there is an end terminator, here from table i_tokens, Index 5 to 8, actual code of those lines can be accessed under column STR.If One source code token is larger the 30 characters the OVFL Field sets value as 1 and actual row value can be accessed by adding offset values from OFF1, OFF2 and OFF3 table fields consecutively.

In this way, whole source code is scanned in a single go and put in i_tokens table, where one can directly use Find statement on STR Column to identify a particular syntax and replace it with some other syntax at same position with the help of program name, its row no and the syntax to replace .

There are certain additions in this command which can be used as per requirement.

Additions

Effect

FROM n1,To n2

Breaks down the source code table Itab_source into tokens not from start to finish, but only from line n1 to line n2.

KEYWORDS FROM itab4

Does not return all statements, only those specified in the key word table itab4

LEVELS INTO itab5

Contains  all the includes and their levels information

OVERFLOW INTO c1

If a token is too large to be stored in the token table in the field STR , it is placed in the overflow area c1

 WITH ANALYSIS

WITH ANALYSIS , the token table itab_tokens  must have the structure STOKEX,means all structure detail level information

WITH COMMENTS

Returns comments also, with each individual comment representing a token.

WITH INCLUDES

Scans the source code available in includes also

WITHOUT TRMAC

use the addition WITHOUT TRMAC if the source code to be scanned contains unknown statements

 

For Detail description of all additions checks out following link:

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/scan.htm

How to use Scan ABAP-Source:-

Scan abap-source can be used as per user requirement differently in different scenarios. Below is an example to show how one can use it in a simplest manner.

Code Snippet:

Following code snippet  will scan through the ABAP Program for the Keywords provided like ‘DATA’, ‘Tables’, ‘TYPES’ and provide the details like row no and program name  in which they are identified.

REPORT  Y_SCAN_SOURCE line-size 400.

* Data declarations
  DATA: BEGIN OF I_REPORT OCCURS 0,
            line(256) TYPE c,
          END OF I_REPORT.

   DATA:  i_statements LIKE  sstmnt OCCURS 0 WITH HEADER LINE,
             i_levels         LIKE  slevel OCCURS 0 WITH HEADER LINE,
             i_keywords   LIKE  I_REPORT OCCURS 0 WITH HEADER LINE,
             i_tokens       LIKE  stokex OCCURS 0 WITH HEADER LINE.

* Selection screen
   PARAMETERS: progName     LIKE sy-repid,
                          keyword LIKE stokex-str. start-of-selection.

   Append: keyword to i_keywords.

* Get source code to be scanned
  read report progName into I_REPORT.

  clear: i_keywords, I_REPORT.

* Scan for statements with keywords
     SCAN ABAP-SOURCE I_REPORT
        TOKENS        INTO i_tokens
        STATEMENTS INTO i_statements
        LEVELS          INTO i_levels
        KEYWORDS   FROM i_keywords
        WITH INCLUDES
        WITH ANALYSIS.

End-of-selection.

* Display details
  Loop at i_statements.
      refresh I_REPORT.
      clear   I_REPORT.

      read table i_levels index i_statements-level.

      if i_levels-level > 0.
        read report i_levels-name into I_REPORT.
        read table I_REPORT index i_statements-trow.
        Write:/ keyword, i_levels-name, I_REPORT.
      else.
        read report progName into I_REPORT.
        read table I_REPORT index i_statements-trow.
        Write:/ keyword, progName, I_REPORT.
      endif.
  endloop.

top-of-page.
* Display header
  Format color col_heading.
  write:/ ‘Keyword’,32 ‘Program/include name’,73 ‘ABAP line text’.
  Format color off.
  Skip.

Conclusion:Scan Abap-source is a powerful command which can not only scan the abap source dynamically but also can be used for changing the existing code, inserting new code .It can also be used for dynamic code comparison for various programs.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Uthaman Palaniappan
    Good effort. But the help for Scan in SAP Help says that,

    “This statement is for internal use only.
    It cannot be used in application programs. “

    Anyway, jus brought to your notice..

    thxs,
    Uth

    (0) 

Leave a Reply