Advertisement
warrior98

Analizor

Nov 10th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMAX 1005
  3. #define pb push_back
  4. #define ll long long
  5. #define INF 0x3f3f3f3f
  6. #define EPS 1e-3
  7.  
  8. using namespace std;
  9.  
  10. bool is_type(string symbol);
  11. bool is_identifier(string symbol);
  12. bool is_constant(string symbol);
  13. bool is_expression(string symbol);
  14. bool is_term(string symbol);
  15. bool is_factor(string symbol);
  16.  
  17. bool is_declaration(string symbol);
  18.  
  19. int main() {
  20.     char separatori[] = ",{};<>=![]";
  21.     string line;
  22.     int nr_lines = 0;
  23.  
  24.     ifstream t("fisier.in");
  25.     stringstream buffer;
  26.     buffer << t.rdbuf();
  27.     string code = buffer.str();
  28.     ofstream fout("fisier.out");
  29.  
  30.     assert(is_identifier("abfes") == true);
  31.     assert(is_identifier("ab__1s") == true);
  32.     assert(is_identifier("a1235") == true);
  33.     assert(is_identifier("1fesv54") == false);
  34.     assert(is_identifier("fes+bgdf") == false);
  35.  
  36.     assert(is_constant("1245439") == true);
  37.     assert(is_constant("100004") == true);
  38.     assert(is_constant("+0") == true);
  39.     assert(is_constant("-41656") == true);
  40.     assert(is_constant("01312") == false);
  41.     assert(is_constant("+012") == false);
  42.  
  43.     assert(is_expression("432+654"));
  44.  
  45.  
  46. /*
  47.     ++nr_lines;
  48.  
  49.     while() {
  50.         string symbol = ""
  51.         while(pos < line.size() && strchr(separatori, line[pos])) {
  52.             symbol.push_back(line[pos]);
  53.             ++pos;
  54.         }
  55.  
  56.         if(is_identificator(symbol)) {
  57.  
  58.         }
  59.  
  60.         if(is_constant())
  61.  
  62.  
  63.         while(line[pos] is in sper)
  64.     }
  65. */
  66.     return 0;
  67. }
  68.  
  69. bool is_type(string symbol) {
  70.     return symbol == "int" && symbol == "bool" && symbol == "char" && symbol == "double";
  71. }
  72.  
  73. bool is_identifier(string symbol) {
  74.     if(isalpha(symbol[0]) == 0)
  75.         return false;
  76.  
  77.     for(int i =1;i<symbol.size();++i)
  78.         if(isalnum(symbol[i]) == 0 && symbol[i] != '_')
  79.             return false;
  80.  
  81.     return true;
  82. }
  83.  
  84. bool is_constant(string symbol) {
  85.     int start = 0;
  86.     if(symbol[0] == '+' || symbol[0] == '-')
  87.         start = 1;
  88.  
  89.     if(symbol[start] == '0' && symbol.size() > 1+start) return false;
  90.  
  91.     for(int i=start;i<symbol.size();++i)
  92.         if(isdigit(symbol[i]) == 0)
  93.             return false;
  94.  
  95.     return true;
  96. }
  97.  
  98. bool is_expression(string symbol) {
  99.     if (is_term(symbol))
  100.         return true;
  101.  
  102.     string expression = "";
  103.     string term = "";
  104.     int i;
  105.     for(i=0;i<symbol.size() && symbol[i] != '+';++i)
  106.         expression.push_back(symbol[i]);
  107.  
  108.     cout << expression << ' ' << term;
  109.     for(i=i+1;i<symbol.size();++i) // Aici trebuie schimbat
  110.         term.push_back(symbol[i]);
  111.  
  112.     cout << expression << ' ' << term;
  113.     return is_expression(expression) && is_term(term);
  114. }
  115.  
  116. bool is_term(string symbol) {
  117.     if (is_factor(symbol))
  118.         return true;
  119.  
  120.     string term = "";
  121.     string factor = "";
  122.     int i;
  123.     for(i=0;i<symbol.size() && symbol[i] != '*';++i)
  124.         term.push_back(symbol[i]);
  125.  
  126.     for(i=i+1;i<symbol.size();++i) // Aici trebuie schimbat
  127.         factor.push_back(symbol[i]);
  128.  
  129.     return is_term(term) && is_factor(factor);
  130. }
  131.  
  132. bool is_factor(string symbol) {
  133.     if(is_constant(symbol))
  134.         return true;
  135.  
  136.     return is_expression(symbol) || is_identifier(symbol); // sa am grija cu parantezele
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement