473,480 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

k&r chapter-1 question, creating a syntax checker

3 New Member
hey it is required to create a syntax checker which reports about common errors like unbalanced paranthesis etc. i hav created d following code, which complies finely and produces correct output for generally, but when i enter d source code as input, it produces wrong input, so csn u help me find d bugs:-
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #define IN 1
  3. #define OUT 0
  4. int main()
  5. {
  6.     int c=0,prevc=0,prevprevc=0,sstate=OUT,dstate=OUT,cstate=OUT,count[256],boo=OUT,i=0,asciieq=0,line=0;
  7.     char escape[]={'a','b','\\','0','t','n','\"','\''};
  8.     for(i=0;i<=255;i++)
  9.     {
  10.         count[i]=0;
  11.     }
  12.  
  13.     while((c=getchar())!=EOF)
  14.     {
  15.        if(c=='\n')
  16.        line++;
  17.        asciieq=c;                                                               /*ASCII EQ. OF C*/
  18.        if(c=='\"' && prevc!='\\' && cstate==OUT && sstate==OUT)                                /*DOUBLE QUOTE TOGGLE*/
  19.        dstate=!dstate;
  20.  
  21.        if(c=='\'' && prevc!='\\' && cstate==OUT && dstate==OUT)                                /*SINGLE QUOTE TOGGLE*/
  22.        sstate=!sstate;
  23.  
  24.        if(sstate==OUT && dstate==OUT)                                           /*comment toggle*/
  25.        { 
  26.             if(cstate==OUT && c=='*' && prevc=='/')
  27.             cstate=IN;
  28.             if(cstate==IN && c=='/' && prevc=='*')
  29.             cstate=OUT;
  30.        }
  31.        if(cstate==OUT)                                                          /*escape sequences*/
  32.        {
  33.            if(prevc=='\\' && prevprevc!='\\')
  34.            {
  35.                for(i=0;i<sizeof(escape);i++)
  36.                {
  37.                     if(c==escape[i])
  38.                     {
  39.                         boo=IN;
  40.                         break;
  41.                     }
  42.                 }
  43.                 if(boo!=IN)
  44.                 {
  45.                     printf("Incorrect escape sequence entered\n");              /*Error 1*/
  46.                 }
  47.             }
  48.         }
  49.  
  50.        if(cstate==OUT && dstate==OUT && sstate==OUT)
  51.        {count[asciieq]++; }
  52.  
  53.         prevprevc=prevc;
  54.         prevc=c;
  55.     }  
  56.  
  57.     int a='[', b=']', cx='{', d='}', e='(', f=')', g='/' ;            /*error generation*/
  58.     boo=0;
  59.     if(count[a]!=count[b])
  60.     {printf("unbalanced brackets\n");boo=1;}
  61.  
  62.     if(count[cx]!=count[d])
  63.     {printf("unbalanced braces\n");boo=1;}
  64.  
  65.     if(count[e]!=count[f])
  66.     {printf("unbalanced paranthesis\n"); boo=1;}
  67.  
  68.     if(count[g]%2!=0)
  69.    { printf("Uneven / \n");boo=1;}
  70.  
  71.     if(sstate==IN)
  72.     {printf("Single character string i.e ' , not closed \n");boo=1;}
  73.  
  74.     if(dstate==IN)
  75.     {printf("Character string i.e \" , not closed \n");boo=1;}
  76.  
  77.     if(cstate==IN)
  78.     {printf("Comments not closed \n");boo=1;}
  79.  
  80.     if(boo==0)
  81.     {printf("no errors encountered\n");}
  82.  
  83.     system("pause");
  84.     return 0;
  85. }
  86.  
I suspect probably something is not right with the toggle for '," or comments that i have created!
Dec 30 '11 #1
2 1971
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Please tell us what you have done so far to debug your code, and in particular which lines we should be looking at.

-Stewart
Dec 30 '11 #2
karan173
3 New Member
hmm, thanks for ur response, solved myself after some effort, as i said the toggling code was not correct
Dec 30 '11 #3

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

Similar topics

192
8712
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
5
8623
by: Jukka K. Korpela | last post by:
The HTML specifications define the entities &zwj;, &zwnj;, &lrm;, &rlm; as denoting zero-width joiner, zero-width non-joiner, left to right mark, and right to left mark. Is there any evidence of...
35
1772
by: sathya | last post by:
Q. Write a program to print all input lines that are longer than 80 characters. Code taken from http://users.powernet.co.uk/eton/kandr2/krx117.html ...
10
2584
by: Eric | last post by:
Hello, I have been working through the K&R book (only chapter one so far) examples and exercises. After much sweat and hair pulling, I think I have a solution for ex 1-18 on page 31. It...
9
3007
by: JFS | last post by:
I know most of you have probably read "The C Programming Language" (K&R) at some point. Well here is something that is driving me crazy. The exercises are impossible (most of them) for me to do....
0
7041
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
6908
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
7084
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...
1
6739
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
6929
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
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.