Advertisement
Imran1107048

Symbol Table & Syntax Analyzer

Apr 6th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ///To store the data of Symbol Table using structure
  5. struct symbol{
  6. string variable;
  7. string type;
  8. int index_number;
  9. }arr[100]; ///for several input
  10.  
  11. ///To print the Symbol Table
  12. void display_symbol_table(int n)
  13. {
  14. cout << "Index\t\tVariable Name\tVariable Type\n";
  15.  
  16. for(int i=0;i<n;i++)
  17. cout << arr[i].index_number << "\t\t" << arr[i].variable << "\t\t" << arr[i].type << endl;
  18. }
  19.  
  20. ///For indexing & insert the variable name & type
  21. void insert_in_symbol_table(string t, int n)
  22. {
  23. string vname = "", vtype = "";
  24.  
  25. for(int i=0;i<t.size();i++){
  26. if(t[i] == ' ')
  27. break;
  28. else
  29. vtype += t[i];
  30. }
  31.  
  32. for(int i=vtype.size()+1;i<t.size();i++){
  33. if(t[i] == ' ')
  34. break;
  35. else
  36. vname += t[i];
  37. }
  38.  
  39. arr[n].index_number = n;
  40. arr[n].variable = vname;
  41. arr[n].type = vtype;
  42. }
  43.  
  44. void symbol_table()
  45. {
  46. int index = 0;
  47. cout << "Input for Symbol Table: ";
  48. string s;
  49. getline(cin, s);
  50.  
  51. string t = "";
  52. for(int i=0; i<s.size()+1; i++){
  53. if(s[i] == ';'){
  54. insert_in_symbol_table(t, index); ///using ';' differ one from another
  55. i++;
  56. index++;
  57. t = "";
  58. }
  59. else
  60. t += s[i];
  61. }
  62.  
  63. display_symbol_table(index);
  64. }
  65.  
  66. void to_print(string t, int n)
  67. {
  68. bool ok = true;
  69. for(int i=0;i<10;i++){
  70. if(arr[i].variable == t){
  71. cout << "<id, " << arr[i].index_number << ">";
  72. ok = false;
  73. }
  74. }
  75. if(ok)
  76. cout << "<" << t << ">";
  77.  
  78. if(n)
  79. cout << ", ";
  80. }
  81.  
  82. void display_laxical_analyzing(string t, int n)
  83. {
  84. cout << "For Statement " << n << ": ";
  85.  
  86. int cnt = 0;
  87. for(int i=0;i<t.size();i++){
  88. if(t[i] == ' ')
  89. cnt++;
  90. }
  91.  
  92. string tt = "";
  93. for(int ii=0; ii<t.size(); ii++){
  94. if(t[ii] == ' '){
  95. to_print(tt, cnt);
  96. cnt--;
  97. tt = "";
  98. }
  99. else
  100. tt += t[ii];
  101. }
  102. ///for the last part every statement
  103. to_print(tt, cnt);
  104.  
  105. cout << endl;
  106.  
  107. }
  108.  
  109. void lexical_analyzing()
  110. {
  111. cout << "\n\nInput for Lexical Analyzing: ";
  112. string s;
  113. getline(cin, s);
  114. string t = "";
  115. int statement_no = 1;
  116. for(int i=0; i<s.size()+1; i++){
  117. if(s[i] == ';'){
  118. display_laxical_analyzing(t, statement_no);
  119. i++;
  120. statement_no++;
  121. t = "";
  122. }
  123. else
  124. t += s[i];
  125. }
  126. }
  127.  
  128. int main()
  129. {
  130. ///int x = 12; float z = 4.5; char c = 'Z'; string s = "Imran Emu";
  131. ///x = z; c = s;
  132. ///Always use a space after the word/symbol/number/semicolon
  133.  
  134. symbol_table();
  135. lexical_analyzing();
  136. }
  137.  
  138. /// Sample Input
  139. /// int x = 12; float z = 4.5; char c = 'Z'; string s = "Imran Emu";
  140. /// x = z; c = s;
  141. ///
  142. /// int value = 12; float value2 = 4.5; char ch = 'Z'; string str = "Imran Emu";
  143. /// value = value2; ch = str;
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement