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 402,643 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 2,195 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: Programming Forums
Jun 30th, 2007
Views: 2,157
This Program implements the Predictive Parsing Of the grammar
E->E+T/T
F->F*T/F
F->id(Identifier)
c Syntax | 5 stars
  1. //To Implement Predictive Parsing
  2. #include<string.h>
  3. #include<conio.h>
  4. char a[10];
  5. int top=-1,i;
  6. void error(){
  7. printf("Syntax Error");
  8. }
  9. void push(char k[]) //Pushes The Set Of Characters on to the Stack
  10. {
  11. for(i=0;k[i]!='\0';i++)
  12. {
  13. if(top<9)
  14. a[++top]=k[i];
  15. }
  16. }
  17. char TOS() //Returns TOP of the Stack
  18. {
  19. return a[top];
  20. }
  21. void pop() //Pops 1 element from the Stack
  22. {
  23. if(top>=0)
  24. a[top--]='\0';
  25. }
  26. void display() //Displays Elements Of Stack
  27. {
  28. for(i=0;i<=top;i++)
  29. printf("%c",a[i]);
  30. }
  31. void display1(char p[],int m) //Displays The Present Input String
  32. {
  33. int l;
  34. printf("\t");
  35. for(l=m;p[l]!='\0';l++)
  36. printf("%c",p[l]);
  37. }
  38. char* stack(){
  39. return a;
  40. }
  41. int main()
  42. {
  43. char ip[20],r[20],st,an;
  44. int ir,ic,j=0,k;
  45. char t[5][6][10]={"$","$","TH","$","TH","$",
  46. "+TH","$","e","e","$","e",
  47. "$","$","FU","$","FU","$",
  48. "e","*FU","e","e","$","e",
  49. "$","$","(E)","$","i","$"};
  50. clrscr();
  51. printf("\nEnter any String(Append with $)");
  52. gets(ip);
  53. printf("Stack\tInput\tOutput\n\n");
  54. push("$E");
  55. display();
  56. printf("\t%s\n",ip);
  57. for(j=0;ip[j]!='\0';)
  58. {
  59. if(TOS()==an)
  60. {
  61. pop();
  62. display();
  63. display1(ip,j+1);
  64. printf("\tPOP\n");
  65. j++;
  66. }
  67. an=ip[j];
  68. st=TOS();
  69. if(st=='E')ir=0;
  70. else if(st=='H')ir=1;
  71. else if(st=='T')ir=2;
  72. else if(st=='U')ir=3;
  73. else if(st=='F')ir=4;
  74. else {
  75. error();
  76. break;
  77. }
  78. if(an=='+')ic=0;
  79. else if(an=='*')ic=1;
  80. else if(an=='(')ic=2;
  81. else if(an==')')ic=3;
  82. else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
  83. else if(an=='$')ic=5;
  84. strcpy(r,strrev(t[ir][ic]));
  85. strrev(t[ir][ic]);
  86. pop();
  87. push(r);
  88. if(TOS()=='e')
  89. {
  90. pop();
  91. display();
  92. display1(ip,j);
  93. printf("\t%c->%c\n",st,238);
  94. }
  95. else{
  96. display();
  97. display1(ip,j);
  98. printf("\t%c->%s\n",st,t[ir][ic]);
  99. }
  100. if(TOS()=='$'&&an=='$')
  101. break;
  102. if(TOS()=='$'){
  103. error();
  104. break;
  105. }
  106. }
  107. k=strcmp(stack(),"$");
  108. if(k==0 && i==strlen(ip))
  109. printf("\n Given String is accepted");
  110. else
  111. printf("\n Given String is not accepted");
  112. return 0;
  113. }
Comments (Newest First)
hariharanb | Newbie Poster | Feb 18th, 2008
For which input will this program work?
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 2:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC