Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <regex>
  4. #include <fstream>
  5.  
  6. std::string IntToString(int a) {
  7.    std::string result;
  8.    int number = abs(a);
  9.    while (number) {
  10.       result += (number % 10) + 48;
  11.       number /= 10;
  12.    }
  13.    if (a < 0) result += '-';
  14.    std::reverse(result.begin(), result.end());
  15.    return result;
  16. }
  17.  
  18. /////////////////////////////ЛЕКСИЧЕСКИЙ АНАЛИЗ////////////////////////////////
  19. //список лексем
  20. struct node {
  21.     std::string id;
  22.     int n;
  23.     node *next;
  24. };
  25.  
  26. node *head;
  27. node *tail;
  28.  
  29. void addToList (std::string value, int i) {
  30.     if (head == NULL) {
  31.             head = new node();
  32.             head->n = i;
  33.             head->id = value;
  34.             head->next = NULL;
  35.             tail = head;
  36.         }
  37.         else {
  38.             tail->next = new node();
  39.             tail = tail->next;
  40.             tail->n = i;
  41.             tail->id = value;
  42.             tail->next = NULL;
  43.         }
  44. }
  45.  
  46. int searchInList (std::string value) {
  47.     //bool b;
  48.     node *point = head;
  49.     while (point != NULL) {
  50.         if (value == point->id) {
  51.             return point->n;
  52.         }
  53.         point = point->next;
  54.     };
  55.     return 0;
  56. }
  57.  
  58. void removeList () {
  59.     node *point = head;
  60.     node *pBuf;
  61.     while (point != NULL) {
  62.         pBuf = point->next;
  63.         delete point;
  64.         point = pBuf;
  65.     }
  66. }
  67.  
  68. void searchLexemes(std::string& input, int& i, int type, std::ofstream& fout)
  69. {
  70.     std::string strBuf;
  71.     switch (type) {
  72.         case 1: { //переменные
  73.             strBuf = "[a-z][a-z0-9]*";
  74.             break;
  75.         }
  76.         case 2: { //числа с плавающей точкой
  77.             strBuf = "\\d(\\.?\\d+)?E[+-]\\d+";
  78.             break;
  79.         }
  80.         case 3: { //числа с фиксированной точкой
  81.             strBuf = "[0-9]+\\.[0-9]+";
  82.             break;
  83.         }
  84.         case 4: { //целые числа
  85.             strBuf = "[0-9]+";
  86.             break;
  87.         }
  88.     }
  89.  
  90.     std::cout << "Start search" << std::endl;
  91.  
  92.     std::smatch m;
  93.     std::regex rExp(strBuf);
  94.     strBuf = input;
  95.     input = "";
  96.     bool foundAny = false;
  97.     int i2;
  98.     while (std::regex_search (strBuf, m, rExp)) {
  99.         auto x = m.str();
  100.         std::cout << "Found " << x << std::endl;
  101.         i2 = searchInList(x);
  102.         std::cout << "i2 = " << i2 << std::endl;
  103.         if (i2 == 0) { //новая лексема
  104.             i2 = i;
  105.             addToList(x, i);
  106.             i++;
  107.         }
  108.         input += m.prefix();
  109.         input += "i" + IntToString(i2);
  110.         strBuf = m.suffix();
  111.         foundAny = true;
  112.         }
  113.     foundAny ? input += m.suffix() : input = strBuf;
  114. }
  115.  
  116. void main() {
  117.     std::string input;
  118.     input = "fgs1=(b+a*5.01E+10)+a*10+(c*10.5)*51E-10";
  119.  
  120.     std::ofstream fout;
  121.     fout.open("OUTPUT.TXT");
  122.  
  123.     int intBuf;
  124.     int i = 1; //номер последней лексеммы
  125.    
  126.     std::cout << input << std::endl;
  127.    
  128.     searchLexemes(input, i, 1, fout);
  129.     searchLexemes(input, i, 2, fout);
  130.     searchLexemes(input, i, 3, fout);
  131.     searchLexemes(input, i, 4, fout);
  132.    
  133.     std::cout << input << std::endl;
  134.     //ожидаемый результат - "i1=(i2+i3*i5)+i3*i8+(i4*i7)*i6"
  135.  
  136.     removeList();
  137.     system ("pause");
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement