Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- ///To store the data of Symbol Table using structure
- struct symbol{
- string variable;
- string type;
- int index_number;
- }arr[100]; ///for several input
- ///To print the Symbol Table
- void display_symbol_table(int n)
- {
- cout << "Index\t\tVariable Name\tVariable Type\n";
- for(int i=0;i<n;i++)
- cout << arr[i].index_number << "\t\t" << arr[i].variable << "\t\t" << arr[i].type << endl;
- }
- ///For indexing & insert the variable name & type
- void insert_in_symbol_table(string t, int n)
- {
- string vname = "", vtype = "";
- for(int i=0;i<t.size();i++){
- if(t[i] == ' ')
- break;
- else
- vtype += t[i];
- }
- for(int i=vtype.size()+1;i<t.size();i++){
- if(t[i] == ' ')
- break;
- else
- vname += t[i];
- }
- arr[n].index_number = n;
- arr[n].variable = vname;
- arr[n].type = vtype;
- }
- void symbol_table()
- {
- int index = 0;
- cout << "Input for Symbol Table: ";
- string s;
- getline(cin, s);
- string t = "";
- for(int i=0; i<s.size()+1; i++){
- if(s[i] == ';'){
- insert_in_symbol_table(t, index); ///using ';' differ one from another
- i++;
- index++;
- t = "";
- }
- else
- t += s[i];
- }
- display_symbol_table(index);
- }
- void to_print(string t, int n)
- {
- bool ok = true;
- for(int i=0;i<10;i++){
- if(arr[i].variable == t){
- cout << "<id, " << arr[i].index_number << ">";
- ok = false;
- }
- }
- if(ok)
- cout << "<" << t << ">";
- if(n)
- cout << ", ";
- }
- void display_laxical_analyzing(string t, int n)
- {
- cout << "For Statement " << n << ": ";
- int cnt = 0;
- for(int i=0;i<t.size();i++){
- if(t[i] == ' ')
- cnt++;
- }
- string tt = "";
- for(int ii=0; ii<t.size(); ii++){
- if(t[ii] == ' '){
- to_print(tt, cnt);
- cnt--;
- tt = "";
- }
- else
- tt += t[ii];
- }
- ///for the last part every statement
- to_print(tt, cnt);
- cout << endl;
- }
- void lexical_analyzing()
- {
- cout << "\n\nInput for Lexical Analyzing: ";
- string s;
- getline(cin, s);
- string t = "";
- int statement_no = 1;
- for(int i=0; i<s.size()+1; i++){
- if(s[i] == ';'){
- display_laxical_analyzing(t, statement_no);
- i++;
- statement_no++;
- t = "";
- }
- else
- t += s[i];
- }
- }
- int main()
- {
- ///int x = 12; float z = 4.5; char c = 'Z'; string s = "Imran Emu";
- ///x = z; c = s;
- ///Always use a space after the word/symbol/number/semicolon
- symbol_table();
- lexical_analyzing();
- }
- /// Sample Input
- /// int x = 12; float z = 4.5; char c = 'Z'; string s = "Imran Emu";
- /// x = z; c = s;
- ///
- /// int value = 12; float value2 = 4.5; char ch = 'Z'; string str = "Imran Emu";
- /// value = value2; ch = str;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement