Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <bits/stdc++.h>
- #define dbg(x) printf("\n--bug: %d --\n",x)
- #define MXX 53
- using namespace std;
- struct tokens{
- string id;
- string key;
- struct tokens *next;
- }*heads[MXX],*temp,*prv,*dummy;
- int bg=1;
- int line_cnt=1;
- bool search(string x,string y)
- {
- for(int i=0;i<MXX;i++)
- {
- temp=heads[i];
- while(temp!=NULL)
- {
- if(temp->id==x && temp->key==y)
- {
- return true;
- }
- temp=temp->next;
- }
- }
- return false;
- }
- int hashkey(string x)
- {
- int sum=0;
- for(int i=0;i<x.size();i++)
- {
- sum+=x[i];
- }
- return sum%MXX;
- }
- void insert(string x,string y)
- {
- bool exist=search(x,y);
- if(exist==false)
- {
- int position=hashkey(x);
- temp=heads[position];
- if(temp==NULL) //if list is empty
- {
- dummy = new tokens;
- dummy->id=x;
- dummy->key=y;
- dummy->next=NULL;
- heads[position]=dummy;
- }
- else
- {
- dummy=new tokens;
- dummy->id=x;
- dummy->key=y;
- dummy->next= heads[position];
- heads[position]=dummy;
- }
- }
- else {
- printf("Already exits\n\n");
- }
- }
- void del(string x,string y)
- {
- int i=hashkey(x);
- if(heads[i]==NULL) return;
- temp=heads[i];
- prv=heads[i];
- if(heads[i]->id==x && heads[i]->key==y)
- {
- heads[i]=heads[i]->next;
- delete(temp);
- return;
- }
- while(temp!=NULL)
- {
- if(temp->id==x && temp->key==y)
- {
- printf("Successfully Deleted\n");
- prv->next=temp->next;
- delete(temp);
- return;
- }
- prv=temp;
- temp=temp->next;
- }
- }
- void update(string x, string y, string nw)
- {
- int i=hashkey(x);
- temp=heads[i];
- while(temp!=NULL)
- {
- if(temp->id==x && temp->key==y)
- {
- printf("Successfully Updated\n");
- temp->key=nw;
- }
- temp=temp->next;
- }
- }
- void show()
- {
- printf("======Showing the contents======\n\n");
- for(int i=0;i<MXX;i++)
- {
- temp=heads[i];
- if(temp==NULL) continue;
- printf("Link %d : ",i);
- while(temp!=NULL)
- {
- cout<<"< "<<temp->id<<" , "<<temp->key <<" > ";
- temp=temp->next;
- }
- cout<<endl;
- cout<<endl;
- }
- }
- %}
- see (dekhao)
- keyword (program)|(if)|(not)|(end)|(begin)|(else)|(then)|(do)|(while)|(funtion)|(Procedure)|(integer)|(real)|(var)|(oh)|(array)|(write)
- variable [a-zA-Z_]{1}[a-zA-Z0-9_]{0,31}
- integer [+-]?[1-9][0-9]*
- float [+-]?[0-9]*\.[0-9]*
- exponent [+-]?[1-9](\.[0-9]+)[E][-+]?[0-9]+
- single_comment (\/\/).*
- multi_comment \/[*].*[*]\/|\/[*](.*(\n).*)[*]\/
- pascal_comment \{(.*)\}
- string \".*\"
- newline [\n]
- relop (=)|(==)|(<>)|(<)|(<=)|(>=)|(>)
- addop [+-]|(or)
- mulop [*/]|(div)|(mod)|(and)
- assignop :=
- dotdot (\.\.)
- comma ,
- semicolon ;
- colon :
- %%
- {see} {show();}
- {newline} {line_cnt++;}
- {single_comment} {cout<<"Single line C/C++ Comment"<<endl;}
- {multi_comment} {cout<<"Multiline C/C++ Comment"<<endl;}
- {pascal_comment} {cout<<"Pascal Comment"<<endl;}
- {keyword} {cout<<"Keyword: "<<yytext<<" at line: "<<line_cnt<<endl;}
- {variable} {cout<<"Varibale: "<<yytext<<endl; insert(yytext,"KEYWORD"); }
- {string} {cout<<"String: "<<yytext<<" at line: "<<line_cnt<<endl;}
- {integer} {cout<<"Integer number: "<<yytext<<endl; insert(yytext,"integer");}
- {float} {cout<<"Float number: "<<yytext<<endl; insert(yytext,"float");}
- {exponent} {cout<<"Exponent number: "<<yytext<<endl; insert(yytext,"exponent");}
- {relop} {cout<<"Relational Operator: "<<yytext<<endl; insert(yytext,"RELOP");}
- {addop} {cout<<"Addition Operator: "<<yytext<<endl; insert(yytext,"ADDOP");}
- {mulop} {cout<<"Multiplier Operator: "<<yytext<<endl; insert(yytext,"MULOP");}
- {assignop} {cout<<"Assign Operator: "<<yytext<<endl;insert(yytext,"ASSIGNOP");}
- {dotdot} {cout<<"DOT DOT: "<<yytext<<endl;insert(yytext,"DOTDOT");}
- {comma} {cout<<"Comma: "<<yytext<<endl;}
- {semicolon} {cout<<"semicolon: "<<yytext<<endl;}
- {colon} {cout<<"colon: "<<yytext<<endl;}
- %%
- int main()
- {
- yylex();
- cout<<"\nTotal Lines: "<<line_cnt<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement