473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to build a predictive parser in c?

Below is the code for doing the syntax analysis of an expression using Predictive parser in c and it is neither displaying any output nor is it working correctly ,it simply crashes after running , so please clarify it .

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  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.   printf("\nEnter any String(Append with $)");
  51.   gets(ip);
  52.   printf("Stack\tInput\tOutput\n\n");
  53.   push("$E");
  54.   display();
  55.   printf("\t%s\n",ip);
  56.   for(j=0;ip[j]!='\0';)
  57.   {
  58.   if(TOS()==an)
  59.       {
  60.     pop();
  61.     display();
  62.     display1(ip,j+1);
  63.     printf("\tPOP\n");
  64.     j++;
  65.       }
  66.     an=ip[j];
  67.     st=TOS();
  68.       if(st=='E')ir=0;
  69.       else if(st=='H')ir=1;
  70.       else if(st=='T')ir=2;
  71.       else if(st=='U')ir=3;
  72.       else if(st=='F')ir=4;
  73.       else {
  74.         error();
  75.         break;
  76.         }
  77.       if(an=='+')ic=0;
  78.       else if(an=='*')ic=1;
  79.       else if(an=='(')ic=2;
  80.       else if(an==')')ic=3;
  81.       else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
  82.       else if(an=='$')ic=5;
  83.       strcpy(r,strrev(t[ir][ic]));
  84.       strrev(t[ir][ic]);
  85.       pop();
  86.       push(r);
  87.       if(TOS()=='e')
  88.       {
  89.     pop();
  90.     display();
  91.     display1(ip,j);
  92.     printf("\t%c->%c\n",st,238);
  93.       }
  94.       else{
  95.       display();
  96.       display1(ip,j);
  97.       printf("\t%c->%s\n",st,t[ir][ic]);
  98.       }
  99.       if(TOS()=='$'&&an=='$')
  100.       break;
  101.       if(TOS()=='$'){
  102.     error();
  103.     break;
  104.     }
  105.       }
  106.       k=strcmp(stack(),"$");
  107.       if(k==0 && i==strlen(ip))
  108.     printf("\n Given String is accepted");
  109.     else
  110.     printf("\n Given String is not accepted");
  111.   return 0;
  112. }
Jul 8 '15 #1
1 7846
donbock
2,426 Expert 2GB
Here are some general comments from a quick scan of your program.
  1. The size of the a array appears on lines 4 and 13. Program is correct as is, but it would be more maintainable if array size could be altered by changing only a single line rather than two.
  2. stdio typically uses buffered I/O. Thus you might want to terminate output strings with a newline to insure they print right away.
  3. Function TOS() will fail if the stack is empty (top < 0). Perhaps return '\0' if the stack is empty.
  4. an is uninitialized at line 58 the first time through the loop.
  5. Are you sure display1() doesn't need to trap error cases: m indexes past end of p[]; or p[] is not null-terminated?
Jul 8 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Himanshu Garg | last post by:
Hello, I am trying to pinpoint an apparent bug in HTML::Parser. The encoding of the text seems to change incorrectly if the locale isn't set properly. However Parser.pm in the directory...
9
by: Cesar A. K. Grossmann | last post by:
Hi I'm trying to build a parser for a file I create. The file format is as follow: IDENTIFIER = NUMBER STRING STRING; COMPOSITE = STRING { ITEM }; ITEM = NUMBER IDENTIFIER|COMPOSITE
6
by: Hervé Piedvache | last post by:
Hi, Does anyone know a predictive or a database scoring solution for PostgreSQL ? I'm looking for a system able to take a database with for example 100 000 records in total, inside them we...
0
by: YellowFin Announcements | last post by:
Yellowfin has announced a new partnership with Extol, one of Malaysia's largest ICT security solutions providers, in which Extol will integrate their predictive analytics application into...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
1
by: Priyasal | last post by:
Hi, I just need a parser to parse some text files which splits the strings as tokens and convert the dictionary to ASCII, then parse those ASCII codes and give the output in standard format.All these...
8
by: Filipe Fernandes | last post by:
I have a project that uses a proprietary format and I've been using regex to extract information from it. I haven't hit any roadblocks yet, but I'd like to use a parsing library rather than...
1
by: sunil | last post by:
Hello, I am working on a problem where I will have a boolean expression with upto four variables: A,B,C,D and connected by basic operator &&,||and may be XOR and NOT in future AND has higher...
4
by: Bartc | last post by:
"vaib" <vaibhavpanghal@gmail.comwrote in message news:26a44cc5-0f08-41fe-859b-0d27daf3ca1d@f24g2000prh.googlegroups.com... I don't know the formal approach to these things but I haven't come...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.