Advertisement
apl-mhd

compiler symboltable

Jul 21st, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. using  namespace std;
  5.  
  6.  
  7. int serial=1;
  8.  
  9. struct  table{
  10.     char varName[100];
  11.     char varType[100];
  12.     char varValue[100];
  13.  
  14. };
  15. struct  table st[100];
  16.  
  17.  
  18.  
  19.  
  20. void  insert();
  21. void  showall();
  22. void  showSpec();
  23. int findSerial();
  24.  
  25.  
  26. int main() {
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.     bool decision = true;
  36.  
  37.     while(decision){
  38.  
  39.         cout<<endl<<"1. Insert :\n";
  40.         cout<<"2. Delete :\n";
  41.         cout<<"3. Show specefic :\n";
  42.         cout<<"4. Show all :\n";
  43.         cout<<"5. Update :\n"<<endl;
  44.  
  45.  
  46.         string n;
  47.         cin>>n;
  48.  
  49.         if(n == "1" ){
  50.  
  51.  
  52.             insert();
  53.  
  54.  
  55.         }
  56.  
  57.         else if(n == "2"){
  58.  
  59.  
  60.  
  61.  
  62.  
  63.         }
  64.         else if(n == "3"){
  65.  
  66.  
  67.  
  68.             showSpec();
  69.  
  70.  
  71.  
  72.         }
  73.         else if(n == "4"){
  74.  
  75.  
  76.             showall();
  77.  
  78.  
  79.         }
  80.  
  81.         else{
  82.             cout<<"Invalid input, enter again \n";
  83.         }
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
  93. void  insert(){
  94.  
  95.  
  96.  
  97.  
  98.  
  99.     cin.ignore();
  100.  
  101.     string input;
  102.     getline(cin, input);
  103.  
  104.     int findEqual;
  105.     findEqual =  input.find('=');
  106.  
  107.     int splt=0;
  108.     int n=0;
  109.  
  110.  
  111.     if(findEqual == -1) {
  112.  
  113.         for (int i = 0; '\0' != input[i]; ++i) {
  114.             // cout<<"first"<<endl;
  115.  
  116.             if (splt == 0) {
  117.  
  118.                 if (input[i] == ' ') {
  119.  
  120.                     st[serial].varType[n] = '\0';
  121.                 } else {
  122.  
  123.                     st[serial].varType[n] = input[i];
  124.                     n++;
  125.                 }
  126.  
  127.             }
  128.             if (splt == 1) {
  129.  
  130.                 if (input[i] == ';') {
  131.  
  132.                     st[serial].varName[n] = '\0';
  133.                 } else {
  134.  
  135.                     st[serial].varName[n] = input[i];
  136.                     n++;
  137.                 }
  138.             }
  139.  
  140.             if (input[i] == ' ') {
  141.  
  142.                 splt++;
  143.                 n = 0;
  144.             }
  145.  
  146.         }
  147.  
  148.         st[serial].varValue[0] ='N';
  149.         st[serial].varValue[1] ='U';
  150.         st[serial].varValue[2] ='L';
  151.         st[serial].varValue[3] ='L';
  152.         st[serial].varValue[4] ='\0';
  153.  
  154.         serial++;
  155.  
  156.  
  157.     } else{ // for float amount=2.25
  158.  
  159.         //cout<<"second"<<endl;
  160.  
  161.         for (int i = 0; '\0' != input[i]; ++i) {
  162.  
  163.             if (splt == 0) {
  164.  
  165.                 if (input[i] == ' ') {
  166.  
  167.                     st[serial].varType[n] = '\0';
  168.                 } else {
  169.  
  170.                     st[serial].varType[n] = input[i];
  171.                     n++;
  172.                 }
  173.  
  174.             }
  175.             if (splt == 1) {
  176.  
  177.                 if (input[i] == '=') {
  178.  
  179.                     st[serial].varName[n] = '\0';
  180.                 } else {
  181.  
  182.                     st[serial].varName[n] = input[i];
  183.                     n++;
  184.                 }
  185.             }
  186.  
  187.             if(splt == 2){
  188.  
  189.                 if (input[i] == ';') {
  190.  
  191.                     st[serial].varValue[n] = '\0';
  192.                 } else {
  193.  
  194.                     st[serial].varValue[n] = input[i];
  195.                     n++;
  196.                 }
  197.  
  198.             }
  199.  
  200.             if (input[i] == '=' || input[i] == ' ') {
  201.  
  202.                 splt++;
  203.                 n = 0;
  204.             }
  205.  
  206.         }
  207.  
  208.  
  209.         serial++;
  210.     }
  211.  
  212.  
  213.  
  214. }
  215.  
  216.  
  217.  
  218.  
  219. void showall(){
  220.  
  221.  
  222.  
  223.         for (int i = 1; i <serial; ++i) {
  224.  
  225.             cout<<i<<" "<<st[i].varName<<" "<<st[i].varType<<" "<<st[i].varValue<<endl;
  226.  
  227.  
  228.         }
  229.  
  230. }
  231.  
  232. void  showSpec(){
  233.  
  234.    int id = findSerial();
  235.  
  236.    if (id == -1){
  237.        cout<<" Name not found"<<endl;
  238.    } else {
  239.  
  240.        cout << id << " " << st[id].varName << " " << st[id].varType << " " << st[id].varValue << endl;
  241.    }
  242.  
  243.  
  244. }
  245.  
  246. int  findSerial(){
  247.  
  248.  
  249.     string input;
  250.  
  251.     cin>>input;
  252.  
  253.     char x[100];
  254.  
  255.     int j;
  256.  
  257.     for (j = 0; j <input.length(); ++j) {
  258.  
  259.         x[j] = input[j];
  260.  
  261.     }
  262.  
  263.     x[j] = '\0';
  264.  
  265.  
  266.  
  267.     for (int i = 1; i <serial ; ++i) {
  268.  
  269.  
  270.             if(strcmp(st[i].varName, x) == 0)
  271.  
  272.                 return i;
  273.  
  274.     }
  275.  
  276.     return  -1;
  277.    
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement