Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. using namespace std;
  2.  
  3. int main()
  4. {
  5.  
  6. int i=0,j=0,k=0,m=0,n=0,o=0,o1=0,var=0,l=0,f=0,c=0,f1=0;
  7. char str[30],str1[40]="E",temp[20],temp1[20],temp2[20],tt[20],t3[20];
  8. strcpy(temp1,'');
  9. strcpy(temp2,'');
  10. char t[10];
  11. char array[6][5][10] = {
  12. "NT", "<id>","+","*",";",
  13. "E", "Te","Error","Error","Error",
  14. "e", "Error","+Te","Error","",
  15. "T", "Vt","Error","Error","Error",
  16. "t", "Error","","*Vt","",
  17. "V", "<id>","Error","Error","Error"
  18. };
  19. cout << "ntLL(1) PARSER TABLE n";
  20. for(i=0;i<6;i++)
  21. {
  22. for(j=0;j<5;j++)
  23. {
  24. cout.setf(ios::right);
  25. cout.width(10);
  26. cout<<array[i][j];
  27. }
  28. cout<<endl;
  29. }
  30. cout << endl;
  31. cout << "ntENTER THE STRING :";
  32. gets(str);
  33. if(str[strlen(str)-1] != ';')
  34. {
  35. cout << "END OF STRING MARKER SHOULD BE ';'";
  36. getch();
  37. exit(1);
  38. }
  39. cout << "ntCHECKING VALIDATION OF THE STRING ";
  40. cout <<"nt" << str1;
  41. i=0;
  42.  
  43. while(i<strlen(str))
  44. {
  45. again:
  46. if(str[i] == ' ' && i<strlen(str))
  47. {
  48. cout << "ntSPACES IS NOT ALLOWED IN SOURSE STRING ";
  49. getch();
  50. exit(1);
  51. }
  52. temp[k]=str[i];
  53. temp[k+1]='';
  54. f1=0;
  55. again1:
  56. if(i>=strlen(str))
  57. {
  58. getch();
  59. exit(1);
  60. }
  61. for(int l=1;l<=4;l++)
  62. {
  63. if(strcmp(temp,array[0][l])==0)
  64. {
  65. f1=1;
  66. m=0,o=0,var=0,o1=0;
  67. strcpy(temp1,'');
  68. strcpy(temp2,'');
  69. int len=strlen(str1);
  70. while(m<strlen(str1) && m<strlen(str))
  71. {
  72. if(str1[m]==str[m])
  73. {
  74. var=m+1;
  75. temp2[o1]=str1[m];
  76. m++;
  77. o1++;
  78. }
  79. else
  80. {
  81. if((m+1)<strlen(str1))
  82. {
  83. m++;
  84. temp1[o]=str1[m];
  85. o++;
  86. }
  87. else
  88. m++;
  89. }
  90.  
  91. }
  92. temp2[o1] = '';
  93. temp1[o] = '';
  94. t[0] = str1[var];
  95. t[1] = '';
  96. for(n=1;n<=5;n++)
  97. {
  98. if(strcmp(array[n][0],t)==0)
  99. break;
  100. }
  101. strcpy(str1,temp2);
  102. strcat(str1,array[n][l]);
  103. strcat(str1,temp1);
  104. cout << "nt" <<str1;
  105. getch();
  106.  
  107. if(strcmp(array[n][l],'')==0)
  108. {
  109. if(i==(strlen(str)-1))
  110. {
  111. int len=strlen(str1);
  112. str1[len-1]='';
  113. cout << "nt"<<str1;
  114. cout << "nntENTERED STRING IS VALID";
  115. getch();
  116. exit(1);
  117. }
  118. strcpy(temp1,'');
  119. strcpy(temp2,'');
  120. strcpy(t,'');
  121. goto again1;
  122. }
  123. if(strcmp(array[n][l],"Error")==0)
  124. {
  125. cout << "ntERROR IN YOUR SOURCE STRING";
  126. getch();
  127. exit(1);
  128. }
  129. strcpy(tt,'');
  130. strcpy(tt,array[n][l]);
  131. strcpy(t3,'');
  132. f=0;
  133. for(c=0;c<strlen(tt);c++)
  134. {
  135. t3[c]=tt[c];
  136. t3[c+1]='';
  137. if(strcmp(t3,temp)==0)
  138. {
  139. f=0;
  140. break;
  141. }
  142. else
  143. f=1;
  144. }
  145.  
  146. if(f==0)
  147. {
  148. strcpy(temp,'');
  149. strcpy(temp1,'');
  150. strcpy(temp2,'');
  151. strcpy(t,'');
  152. i++;
  153. k=0;
  154. goto again;
  155. }
  156. else
  157. {
  158. strcpy(temp1,'');
  159. strcpy(temp2,'');
  160. strcpy(t,'');
  161. goto again1;
  162. }
  163. }
  164. }
  165. i++;
  166. k++;
  167. }
  168. if(f1==0)
  169. cout << "nENTERED STRING IS INVALID";
  170. else
  171. cout << "nntENTERED STRING IS VALID";
  172.  
  173. NT <id> + * ;
  174. E Te Error Error Error
  175. e Error +Te Error
  176. T Vt Error Error Error
  177. t Error *Vt
  178. V <id> Error Error Error
  179.  
  180. ENTER THE STRING :<id>+<id>*<id>;
  181. CHECKING VALIDATION OF THE STRING
  182. E
  183. Te
  184. Vte
  185. <id>te
  186. <id>e
  187. <id>+Te
  188. <id>+Vte
  189. <id>+<id>te
  190. <id>+<id>*Vte
  191. <id>+<id>*<id>te
  192. <id>+<id>*<id>e
  193. <id>+<id>*<id>
  194. ENTERED STRING IS VALID
  195.  
  196. return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement