home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / TURBOPAS / CROSSREF.ARK / CROSSREF.PAS < prev    next >
Pascal/Delphi Source File  |  1987-04-18  |  8KB  |  159 lines

  1. {****************************************************************************
  2.  
  3.  PROGRAM        : Cross Reference
  4.  VERSION        : 4
  5.  MODIFIED       : 1/31/87
  6.  
  7.  AUTHOR         : Milton Hom
  8.  
  9.  DESCRIPTION:
  10.  
  11.    This a program will produce a cross-reference table of identifiers from a
  12.    Pascal program.  Each identifier will have a list of line numbers indicating
  13.    its location throughout the program.  Also, all identifiers, different
  14.    identifier, code lines, comment lines, & blank lines will be counted.  The
  15.    most & least used identifier will be counted.
  16.  
  17.  INPUT:
  18.  
  19.    An ASCII file in Pascal.  Include file(s) are not recognized by this
  20.    program.  Therefore, each file(s) must be cross referenced separately.
  21.  
  22.  OUTPUT:
  23.  
  24.    A table with all identifiers sorted from A to Z along with all line
  25.    numbers associated with it and statistics of the program.  This table
  26.    will be sent to an output file specified by the user.
  27.  
  28. *****************************************************************************}
  29. {.pa}
  30. program CrossReference(input,output);
  31.  
  32. {$A-}      { compiler directive for recursion (default: A+) }
  33. const
  34.    MaxIdentLength    = 15;     { max. length of identifier }
  35.    MaxLengthOfLine   = 96;     { max. length of line in input file }
  36.    MaxReservedWords  = 40;     { max. no.# of reserved & semi-reserved words }
  37.  
  38. { do not change following constants. }
  39.    MaxReservedLength = 9;      { max. length of reserved word }
  40.  
  41. type
  42.    Index = -1..10000;                             { subrange for array index }
  43.    IdentType = array[1..MaxIdentLength] of char;  { ident type }
  44.    WordType  = array[1..MaxLengthOfLine] of char; { max. size of word type }
  45.    RWord     = array[1..MaxReservedLength] of char; { max. size of reserved }
  46.    QueuePointer = ^QueueItem;
  47.    QueueItem    = record
  48.                      LineNumber :Index;             { line number of ident }
  49.                      Next       :QueuePointer;
  50.                   end;
  51.  
  52.    EntryType    = record
  53.                      UpIdent,                       { Upper case ident }
  54.                      Ident   :IdentType;            { Identifier }
  55.                      Head,                          { queue Head }
  56.                      Tail    :QueuePointer;         { queue Tail }
  57.                   end;
  58.  
  59.    TreePointer  = ^TreeType;
  60.    TreeType     = record
  61.                      Entry :EntryType;
  62.                      Left,                          { pointer to left node  }
  63.                      Right :TreePointer;            { pointer to right node }
  64.                   end;
  65.  
  66.    LineType =                                       { record of line info }
  67.       record
  68.          Line    :WordType;                         { line of input file }
  69.          Len     :0..MaxLengthOfLine;               { length of line }
  70.       end;
  71.  
  72.    Info = record
  73.              TotalLines,                { total number of lines }
  74.              CommentLines,              { total number of comment lines }
  75.              BlankLines,                { total number of blank lines }
  76.              Comments,                  { total number of comments }
  77.              TotalIdents,               { total number of identifiers }
  78.              DifferentIdents,           { total number of different identifiers }
  79.              TotalReserved,             { total number of reserved words }
  80.              MostUsedNumber,            { number of identifier used the most }
  81.              LeastUsedNumber: Index;    { number of identifier used the least }
  82.              AvgIdentLength: real;      { average length of identifier }
  83.              MostUsedIdent,             { identifier used the most }
  84.              LeastUsedIdent: IdentType; { identifier used the least }
  85.              UsedReserved:              { number of each reserved word used }
  86.                  array[1..MaxReservedWords] of Index;
  87.           end;
  88.  
  89.    ReservedType = array[1..MaxReservedWords] of RWord;
  90.    NameType     = string[14];
  91.    Condition    = (CCopy,SkipComment,SkipString);
  92.    SetType      = set of char;
  93. {.pa}
  94. var
  95.    Tree: TreePointer;      { binary tree of idents, line no & lower case idents }
  96.    Reserved: ReservedType; { reserved & semi-reserved words }
  97.    LineText: LineType;     { one line of text from input file }
  98.    Status: Info;           { information on cross-referenced table }
  99.    State: Condition;       { diff. states for building words }
  100.    SourceName,             { name of source file }
  101.    TableName: NameType;    { name of table }
  102.    InFile,                 { source file var of input }
  103.    TableFile: text;        { output file var of Table }
  104.    Stop: boolean;          { don't cross-reference a program }
  105.    Number: Index;          { total line numbers in input file }
  106.    Alpha,                  { alphabet }
  107.    AlphaNumeric,           { alphabet & number }
  108.    EndWord: SetType;       { char. showing end of word }
  109.  
  110. {$I cr-m01.inc}     { BuildTree, BinarySearch, InsertTree, EnQueue & FindWord }
  111. {$I cr-m02.inc}     { ReadLine, ClearLine, ReadInReserved, & DisplayStatus }
  112. {$I cr-m03.inc}     { PrintTree }
  113. {$I cr-m04.inc}     { Compare, OpenFile, AnotherCross, InitializeTree, }
  114.                     { InitializeStatus & Title }
  115. {.pa}
  116. {****************************** main program *********************************}
  117.  
  118. begin
  119.    Alpha        := ['A'..'Z' , 'a'..'z'];
  120.    AlphaNumeric := ['A'..'Z' , 'a'..'z' , '0'..'9' , '_'];
  121.    EndWord      := ['^' , '*' , '(' , ')' , '-' , '+' , '=' , '[' , ']' , ';',
  122.                     ':' , '''', '"' , ',' , '.' , '/' , '<' , '>' , ' ' , '{',
  123.                     '}'                                                      ];
  124.  
  125.    ReadInReserved (Reserved);                { read in reserved words }
  126.    repeat
  127.       Titles;                                { display titles }
  128.       Stop := false;
  129.       State := CCopy;                        { set state of building words }
  130.       Number := 0;
  131.       OpenFile (InFile,TableFile,Stop,       { open source file & output }
  132.                 SourceName,TableName);
  133.       if not Stop then
  134.          begin
  135.             gotoxy(1,13); write('Scanning ',SourceName,' on line ');
  136.             InitializeStatus (Status);       { initialize status }
  137.             InitializeTree (Tree);           { initialize tree }
  138.             while not eof(InFile) do         { find identifier loop }
  139.                begin
  140.                   ClearLine (LineText);              { blank out line buffer }
  141.                   ReadLine (InFile,LineText);        { take 1 line from input file }
  142.                   Number := Number + 1;              { count lines of input file }
  143.                   gotoxy(19+length(SourceName),13);  { display line number }
  144.                   write(Number);
  145.                   if LineText.Len = 0 then           { if blank line then skip it }
  146.                      with Status do
  147.                         BlankLines := BlankLines + 1 { count blank lines }
  148.                   else
  149.                      FindWord (LineText,Number,      { get words from line of text }
  150.                                State,Tree,Status);
  151.                end;  { while }
  152.             Status.TotalLines := Number;             { total lines in ref program }
  153.             PrintTree (Tree,TableFile,Status,        { write a table of identifiers }
  154.                        SourceName,TableName  );      { with line numbers to disk }
  155.             close(InFile)
  156.          end;  { if not Stop }
  157.    until not AnotherCross;                           { prompt user with question }
  158. end.
  159.