Advertisement
sak1b

flexfile.l

Apr 16th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. %{
  2. #include <bits/stdc++.h>
  3. #define dbg(x) printf("\n--bug: %d --\n",x)
  4. #define  MXX 53
  5.  
  6. using namespace std;
  7.  
  8. struct tokens{
  9.  
  10.     string id;
  11.     string key;
  12.     struct tokens *next;
  13.  
  14. }*heads[MXX],*temp,*prv,*dummy;
  15.  
  16. int bg=1;
  17. int line_cnt=1;
  18. bool search(string x,string y)
  19. {
  20.     for(int i=0;i<MXX;i++)
  21.     {
  22.         temp=heads[i];
  23.         while(temp!=NULL)
  24.         {
  25.             if(temp->id==x && temp->key==y)
  26.             {
  27.                 return true;
  28.             }
  29.             temp=temp->next;
  30.         }
  31.     }
  32.     return false;
  33. }
  34.  
  35. int hashkey(string x)
  36. {
  37.     int sum=0;
  38.     for(int i=0;i<x.size();i++)
  39.     {
  40.         sum+=x[i];
  41.     }
  42.     return sum%MXX;
  43. }
  44.  
  45.  
  46. void insert(string x,string y)
  47. {
  48.     bool exist=search(x,y);
  49.     if(exist==false)
  50.     {
  51.         int position=hashkey(x);
  52.         temp=heads[position];
  53.  
  54.         if(temp==NULL)   //if list is empty
  55.         {
  56.             dummy = new tokens;
  57.  
  58.             dummy->id=x;
  59.             dummy->key=y;
  60.             dummy->next=NULL;
  61.  
  62.             heads[position]=dummy;
  63.  
  64.         }
  65.         else
  66.         {
  67.  
  68.             dummy=new tokens;
  69.             dummy->id=x;
  70.             dummy->key=y;
  71.             dummy->next= heads[position];
  72.             heads[position]=dummy;
  73.  
  74.         }
  75.  
  76.     }
  77.     else {
  78.  
  79.         printf("Already exits\n\n");
  80.     }
  81. }
  82.  
  83.  
  84. void del(string x,string y)
  85. {
  86.     int i=hashkey(x);
  87.     if(heads[i]==NULL) return;
  88.     temp=heads[i];
  89.     prv=heads[i];
  90.  
  91.       if(heads[i]->id==x && heads[i]->key==y)
  92.       {
  93.         heads[i]=heads[i]->next;
  94.         delete(temp);
  95.         return;
  96.       }
  97.  
  98.  
  99.         while(temp!=NULL)
  100.         {
  101.             if(temp->id==x && temp->key==y)
  102.             {
  103.                 printf("Successfully Deleted\n");
  104.                 prv->next=temp->next;
  105.                 delete(temp);
  106.                 return;
  107.             }
  108.             prv=temp;
  109.             temp=temp->next;
  110.         }
  111.  
  112.  
  113. }
  114.  
  115. void update(string x, string y, string nw)
  116. {
  117.         int i=hashkey(x);
  118.         temp=heads[i];
  119.  
  120.         while(temp!=NULL)
  121.         {
  122.             if(temp->id==x && temp->key==y)
  123.             {
  124.                 printf("Successfully Updated\n");
  125.                 temp->key=nw;
  126.             }
  127.  
  128.             temp=temp->next;
  129.         }
  130.  
  131.  
  132. }
  133.  
  134. void show()
  135. {
  136.     printf("======Showing the contents======\n\n");
  137.  
  138.  
  139.      for(int i=0;i<MXX;i++)
  140.     {
  141.         temp=heads[i];
  142.         if(temp==NULL) continue;
  143.         printf("Link %d : ",i);
  144.  
  145.         while(temp!=NULL)
  146.         {
  147.             cout<<"< "<<temp->id<<" , "<<temp->key <<" >  ";
  148.             temp=temp->next;
  149.         }
  150.         cout<<endl;
  151.         cout<<endl;
  152.     }
  153. }
  154.  
  155. %}
  156. see (dekhao)
  157. keyword (program)|(if)|(not)|(end)|(begin)|(else)|(then)|(do)|(while)|(funtion)|(Procedure)|(integer)|(real)|(var)|(oh)|(array)|(write)
  158. variable        [a-zA-Z_]{1}[a-zA-Z0-9_]{0,31}
  159. integer         [+-]?[1-9][0-9]*
  160. float           [+-]?[0-9]*\.[0-9]*
  161. exponent    [+-]?[1-9](\.[0-9]+)[E][-+]?[0-9]+
  162. single_comment      (\/\/).*
  163. multi_comment       \/[*].*[*]\/|\/[*](.*(\n).*)[*]\/
  164. pascal_comment       \{(.*)\}
  165. string              \".*\"
  166. newline            [\n]
  167. relop              (=)|(==)|(<>)|(<)|(<=)|(>=)|(>)
  168. addop              [+-]|(or)
  169. mulop              [*/]|(div)|(mod)|(and)
  170. assignop           :=
  171. dotdot             (\.\.)
  172. comma              ,
  173. semicolon          ;
  174. colon              :
  175.  
  176.  
  177. %%
  178. {see}           {show();}
  179. {newline}       {line_cnt++;}
  180.  
  181. {single_comment}    {cout<<"Single line C/C++ Comment"<<endl;}
  182. {multi_comment}     {cout<<"Multiline C/C++ Comment"<<endl;}
  183. {pascal_comment}    {cout<<"Pascal Comment"<<endl;}
  184.  
  185. {keyword}       {cout<<"Keyword: "<<yytext<<" at line: "<<line_cnt<<endl;}
  186. {variable}      {cout<<"Varibale: "<<yytext<<endl; insert(yytext,"KEYWORD"); }
  187. {string}        {cout<<"String: "<<yytext<<" at line: "<<line_cnt<<endl;}
  188.  
  189. {integer}       {cout<<"Integer number: "<<yytext<<endl; insert(yytext,"integer");}
  190. {float}         {cout<<"Float number: "<<yytext<<endl; insert(yytext,"float");}
  191. {exponent}      {cout<<"Exponent number: "<<yytext<<endl; insert(yytext,"exponent");}
  192.  
  193. {relop}         {cout<<"Relational Operator: "<<yytext<<endl; insert(yytext,"RELOP");}
  194. {addop}         {cout<<"Addition Operator: "<<yytext<<endl; insert(yytext,"ADDOP");}
  195. {mulop}         {cout<<"Multiplier Operator: "<<yytext<<endl; insert(yytext,"MULOP");}
  196. {assignop}      {cout<<"Assign Operator: "<<yytext<<endl;insert(yytext,"ASSIGNOP");}
  197. {dotdot}        {cout<<"DOT DOT: "<<yytext<<endl;insert(yytext,"DOTDOT");}
  198.  
  199. {comma}             {cout<<"Comma: "<<yytext<<endl;}
  200. {semicolon}         {cout<<"semicolon: "<<yytext<<endl;}
  201. {colon}             {cout<<"colon: "<<yytext<<endl;}
  202.  
  203. %%
  204. int main()
  205. {
  206.     yylex();
  207.     cout<<"\nTotal Lines: "<<line_cnt<<endl;
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement