Advertisement
r4lovets

Untitled

Oct 2nd, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.39 KB | None | 0 0
  1. program ConsoleMetrology;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.   System.SysUtils, System.RegularExpressions,
  8.   uFiles in 'uFiles.pas';
  9.  
  10. const
  11.   N = 1000;
  12.  
  13. type
  14.   TRec = record
  15.     id: string[255];
  16.     count: Integer;
  17.   end;
  18.  
  19.   TArray = array [1 .. N] of TRec;
  20.  
  21. const
  22.   stringPattern = '"[^"]*"';
  23.  
  24.   charPattern = '\x27(.?|\\u[0-9a-fA-F]*)\x27';
  25.  
  26.   floatPattern = '[0-9]+\.[0-9]+((E|e)?\-?[0-9]+)?';
  27.  
  28.   numPattern = '\W([0-9]([xX][0-9a-fA-F]*|[bB][0-1]*|[0-9]*))';
  29.  
  30.   identsPattern = 'null|true|false|([A-Za-z_]\w*)';
  31.  
  32.   deletePattern = 'public|private|:|(\[ *\])|\)|\}|protected|static|([A-Za-z_]\w*\.)+([A-Za-z_]\w*)?';
  33.  
  34.   fnDeclarationPattern =
  35.     '(byte|short|int|long|float|double|String|char|boolean|void) +[A-Za-z_]\w* *\(';
  36.  
  37.   fnPattern = '([A-Za-z_]\w* *\()|break|continue|goto|return';
  38.  
  39.   bracketsPattern = '\)|\}|\]';
  40.  
  41.   o2perationsPattern = '(\+\+)|(\-\-)|(\+=)|(\*=)|(\-=)|(<=)|(>=)|(==)|(!=)|(%=)|(/=)|(%=)|(&&)|(\|\|)';
  42.  
  43.   o1perationsPattern = '[\+\-=\*/&\|!%\~\>\<\,\;\(\{(\[)]';
  44.  
  45.   typesPattern = '[\W](byte |short |int |long |float |double |String |char |boolean |void )';
  46.  
  47. var
  48.   Operators, Operands: TArray;
  49.   s: string;
  50.  
  51.   StringRE: TRegEx;
  52.   CharRE: TRegEx;
  53.   NumRE: TRegEx;
  54.   DeleteRE: TRegEx;
  55.   OtherLiteralsRE: TRegEx;
  56.   FnDeclarationRE: TRegEx;
  57.   FnRE: TRegEx;
  58.   BracketsRE: TRegEx;
  59.   IdentsRE: TRegEx;
  60.   TypesRE: TRegEx;
  61.   O1perationsRE: TRegEx;
  62.   O2perationsRE: TRegEx;
  63.   FloatRE: TRegEx;
  64.  
  65.   StringCollection: TMatchCollection;
  66.   CharCollection: TMatchCollection;
  67.   NumCollection: TMatchCollection;
  68.   OtherLiteralsCollection: TMatchCollection;
  69.   FnDeclarationCollection: TMatchCollection;
  70.   FnCollection: TMatchCollection;
  71.   IdentsCollection: TMatchCollection;
  72.   TypesCollection: TMatchCollection;
  73.   O1perationsCollection: TMatchCollection;
  74.   O2perationsCollection: TMatchCollection;
  75.   FloatCollection: TMatchCollection;
  76.  
  77.   i: Integer;
  78.   buf: string;
  79.  
  80. procedure AddToArray(str: string; var arr: TArray);
  81. var
  82.   i: Integer;
  83.   Flag: Boolean;
  84. begin
  85.   i := 1;
  86.   Flag := True;
  87.   while Flag and (i <= N) do
  88.   begin
  89.     if arr[i].id = '' then
  90.     begin
  91.       arr[i].id := str;
  92.       arr[i].count := 1;
  93.       Flag := False;
  94.     end
  95.     else if str = arr[i].id then
  96.     begin
  97.       Flag := False;
  98.       Inc(arr[i].count);
  99.     end;
  100.     Inc(i);
  101.   end;
  102.  
  103. end;
  104.  
  105. begin
  106.   GetCodeFromFile(s);
  107.  
  108.   StringRE := TRegEx.Create(stringPattern);
  109.   CharRE := TRegEx.Create(charPattern);
  110.   NumRE := TRegEx.Create(numPattern);
  111.   IdentsRE := TRegEx.Create(identsPattern);
  112.   DeleteRE := TRegEx.Create(deletePattern);
  113.   FnDeclarationRE := TRegEx.Create(fnDeclarationPattern);
  114.   FnRE := TRegEx.Create(fnPattern);
  115.   BracketsRE := TRegEx.Create(bracketsPattern);
  116.   IdentsRE := TRegEx.Create(identsPattern);
  117.   TypesRE := TRegEx.Create(typesPattern);
  118.   O2perationsRE := TRegEx.Create(o2perationsPattern);
  119.   O1perationsRE := TRegEx.Create(o1perationsPattern);
  120.   FloatRE := TRegEx.Create(floatPattern);
  121.  
  122.   // Строковые литералы
  123.   StringCollection := StringRE.Matches(s);
  124.   s := StringRE.Replace(s, ' ');
  125.  
  126.   // Символьные литералы
  127.   CharCollection := CharRE.Matches(s);
  128.   s := CharRE.Replace(s, ' ');
  129.  
  130.  
  131.   // Объявления функций
  132.   FnDeclarationCollection := FnDeclarationRE.Matches(s);
  133.   s := FnDeclarationRE.Replace(s, ' ');
  134.  
  135.  
  136.   // Вызовы функций / операторы
  137.   FnCollection := FnRE.Matches(s);
  138.   s := FnRE.Replace(s, ' ');
  139.  
  140.   s := DeleteRE.Replace(s, '');
  141.   s := BracketsRE.Replace(s, ' ');
  142.   // Знаки операций
  143.   O2perationsCollection := O2perationsRE.Matches(s);
  144.   s := O2perationsRE.Replace(s, ' ');
  145.  
  146.   O1perationsCollection := O1perationsRE.Matches(s);
  147.   s := O1perationsRE.Replace(s, ' ');
  148.  
  149.   // Чыселки
  150.   FloatCollection := FloatRE.Matches(s);
  151.   s := FloatRE.Replace(s, ' ');
  152.  
  153.   NumCollection := NumRE.Matches(s);
  154.   s := NumRE.Replace(s, ' ');
  155.  
  156.   s := TypesRE.Replace(s, '');
  157.  
  158.   // Идентификаторы
  159.   IdentsCollection := IdentsRE.Matches(s);
  160.   s := IdentsRE.Replace(s, ' ');
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.   // Сбор коллекций по порядку!!!
  169.  
  170.   Writeln('Строки:');
  171.   for i := 0 to StringCollection.count - 1 do
  172.   begin
  173.     Writeln(StringCollection.Item[i].Value);
  174.     AddToArray(StringCollection.Item[i].Value, Operands);
  175.   end;
  176.  
  177.  
  178.   Writeln(#10#13'Символы:');
  179.   for i := 0 to CharCollection.count - 1 do
  180.   begin
  181.     Writeln(CharCollection.Item[i].Value);
  182.     AddToArray(CharCollection.Item[i].Value, Operands);
  183.   end;
  184.  
  185.  
  186. //  Writeln(#10#13'Другое:');
  187.   for i := 0 to OtherLiteralsCollection.count - 1 do
  188.   begin
  189.     Writeln(OtherLiteralsCollection.Item[i].Value);
  190.   end;
  191.  
  192.  
  193.   Writeln(#10#13'Функции (объявление):');
  194.   for i := 0 to FnDeclarationCollection.count - 1 do
  195.   begin
  196.     Writeln(FnDeclarationCollection.Item[i].Value + ')');
  197.   end;
  198.  
  199.  
  200.  
  201.   Writeln(#10#13'Функции (вызов):');
  202.   for i := 0 to FnCollection.count - 1 do
  203.   begin
  204.     if (FnCollection.Item[i].Value <> 'break') and
  205.       (FnCollection.Item[i].Value <> 'continue') and
  206.       (FnCollection.Item[i].Value <> 'goto') then
  207.     begin
  208.       Writeln(FnCollection.Item[i].Value + ')');
  209.       AddToArray(FnCollection.Item[i].Value + ')', Operators);
  210.     end
  211.     else
  212.     begin
  213.       Writeln(FnCollection.Item[i].Value);
  214.       AddToArray(FnCollection.Item[i].Value, Operators);
  215.     end;
  216.   end;
  217.  
  218.  
  219.   Writeln(#10#13'Операции:');
  220.   for i := 0 to O2perationsCollection.count - 1 do
  221.   begin
  222.     Writeln(O2perationsCollection.Item[i].Value);
  223.     AddToArray(O2perationsCollection.Item[i].Value, Operators);
  224.   end;
  225.   for i := 0 to O1perationsCollection.count - 1 do
  226.   begin
  227.     Writeln(O1perationsCollection.Item[i].Value);
  228.     AddToArray(O1perationsCollection.Item[i].Value, Operators);
  229.   end;
  230.  
  231.  
  232.   Writeln(#10#13'Идентификаторы:');
  233.   for i := 0 to IdentsCollection.count - 1 do
  234.   begin
  235.     Writeln(IdentsCollection.Item[i].Value);
  236.     AddToArray(IdentsCollection.Item[i].Value, Operands);
  237.   end;
  238.  
  239.   Writeln(#10#13'Числа:');
  240.   for i := 0 to NumCollection.count - 1 do
  241.   begin
  242.     buf := NumCollection.Item[i].Value;
  243.     Delete(buf, 1, 1);
  244.     Writeln(buf);
  245.     AddToArray(buf, Operands);
  246.   end;
  247.  
  248.   for i := 0 to FloatCollection.count - 1 do
  249.   begin
  250.     Writeln(FloatCollection.Item[i].Value);
  251.     AddToArray(FloatCollection.Item[i].Value, Operands);
  252.   end;
  253.  
  254.   Readln;
  255.  
  256. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement