User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,979 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,852 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Jun 30th, 2007
Views: 1,675
This Program Implements The SLR Parsing method
Last edited : Jun 30th, 2007.
c Syntax | 5 stars
  1. int axn[][6][2]={
  2. {{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
  3. {{-1,-1},{100,6},{-1,-1},{-1,-1},{-1,-1},{102,102}},
  4. {{-1,-1},{101,2},{100,7},{-1,-1},{101,2},{101,2}},
  5. {{-1,-1},{101,4},{101,4},{-1,-1},{101,4},{101,4}},
  6. {{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
  7. {{-1,-1},{101,6},{101,6},{-1,-1},{101,6},{101,6}},
  8. {{100,5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
  9. {{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
  10. {{-1,-1},{100,6},{-1,-1},{-1,-1},{100,11},{-1,-1}},
  11. {{-1,-1},{101,1},{100,7},{-1,-1},{101,1},{101,1}},
  12. {{-1,-1},{101,3},{101,3},{-1,-1},{101,3},{101,3}},
  13. {{-1,-1},{101,5},{101,5},{-1,-1},{101,5},{101,5}}
  14. };//Axn Table
  15. int gotot[12][3]={1,2,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,8,2,3,-1,-1,-1,
  16. -1,9,3,-1,-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; //GoTo table
  17. int a[10];
  18. char b[10];
  19. int top=-1,btop=-1,i;
  20. void push(int k)
  21. {
  22. if(top<9)
  23. a[++top]=k;
  24. }
  25.  
  26. void pushb(char k)
  27. {
  28. if(btop<9)
  29. b[++btop]=k;
  30. }
  31.  
  32. char TOS()
  33. {
  34. return a[top];
  35. }
  36.  
  37. void pop()
  38. {
  39. if(top>=0)
  40. top--;
  41. }
  42.  
  43. void popb()
  44. {
  45. if(btop>=0)
  46. b[btop--]='\0';
  47. }
  48.  
  49. void display()
  50. {
  51. for(i=0;i<=top;i++)
  52. printf("%d%c",a[i],b[i]);
  53. }
  54.  
  55. void display1(char p[],int m) //Displays The Present Input String
  56. {
  57. int l;
  58. printf("\t\t");
  59. for(l=m;p[l]!='\0';l++)
  60. printf("%c",p[l]);
  61. printf("\n");
  62. }
  63.  
  64. void error()
  65. {
  66. printf("Syntax Error");
  67. }
  68.  
  69. void reduce(int p)
  70. {
  71. int len,k,ad;
  72. char src,*dest;
  73. switch(p)
  74. {
  75. case 1:dest="E+T";
  76. src='E';
  77. break;
  78. case 2:dest="T";
  79. src='E';
  80. break;
  81. case 3:dest="T*F";
  82. src='T';
  83. break;
  84. case 4:dest="F";
  85. src='T';
  86. break;
  87. case 5:dest="(E)";
  88. src='F';
  89. break;
  90. case 6:dest="i";
  91. src='F';
  92. break;
  93. default:dest="\0";
  94. src='\0';
  95. break;
  96. }
  97. for(k=0;k<strlen(dest);k++)
  98. {
  99. pop();
  100. popb();
  101. }
  102. pushb(src);
  103. switch(src)
  104. {
  105. case 'E':ad=0;
  106. break;
  107. case 'T':ad=1;
  108. break;
  109. case 'F':ad=2;
  110. break;
  111. default: ad=-1;
  112. break;
  113. }
  114. push(gotot[TOS()][ad]);
  115. }
  116. int main()
  117. {
  118. int j,st,ic;
  119. char ip[20]="\0",an;
  120. clrscr();
  121. printf("Enter any String");
  122. gets(ip);
  123. push(0);
  124. display();
  125. printf("\t%s\n",ip);
  126. for(j=0;ip[j]!='\0';)
  127. {
  128. st=TOS();
  129. an=ip[j];
  130. if(an>='a'&&an<='z') ic=0;
  131. else if(an=='+') ic=1;
  132. else if(an=='*') ic=2;
  133. else if(an=='(') ic=3;
  134. else if(an==')') ic=4;
  135. else if(an=='$') ic=5;
  136. else {
  137. error();
  138. break;
  139. }
  140. if(axn[st][ic][0]==100)
  141. {
  142. pushb(an);
  143. push(axn[st][ic][1]);
  144. display();
  145. j++;
  146. display1(ip,j);
  147. }
  148. if(axn[st][ic][0]==101)
  149. {
  150. reduce(axn[st][ic][1]);
  151. display();
  152. display1(ip,j);
  153. }
  154. if(axn[st][ic][1]==102)
  155. {
  156. printf("Given String is accepted");
  157. break;
  158. }
  159. }
  160. return 0;
  161. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 12:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC