Thread: can some one help me???

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    can some one help me???

    Hello
    I asked to write a program works as parser This program implements an interpreter in the C language,which will read a sentence at atime from the user and print :Good:<n> words when the input noun phrase is grammatically correct,and <n> is the total number of words in the phrase.If the input is grammatically incorrect,it will print :Bad phrase to the user.

    The sentence is correct(according to this program) if it contains
    1.article(a,the)
    2.adjective {hungry,black,white , thirsty}
    3.noun {cat,dog}

    the sentence may contain more than one objective or no objective at all and the program must contain the foolowing function getnexttoken(); noun_phrase();
    article(); noun();
    example:


    input output
    The cat Good<2>words
    The black dog. Good<3>words
    The white hungry cat. Good<4>words
    a hungry Bad phrase

    my code has many problems which i could not solve them .
    ,so,help me pleeeeeeease !!!!!!

    Code:
    #nclude<stdio.h>
    #nclude<conio.h>
    #include<ctype.h>
    
    typedef char* token_t ;
    typedef token_t* token;
    
    
    int get_next_token();
    int article (token str);
    int adjective(token  str);
    int noun(token str);
    
    int count=0  ;
    //-------------------------------------------------------------------------//
    void main()
    {int y;
     y=get_next_token();
     if(y==0)
     printf("BAD PHRASE \n");
     else
     printf("GOOD PHRASE %d",count);
     }
    //-----------------------------------------------------------------------//
    
    
    int get_next_token()
    
    {
     token string[30];
     token str1[10],str2[10],str3[10];
     int i=0,j=0,k=0,x,y,z;
     printf("Enter the sentence\n");
     scanf("%s",string);
    
     while(isspace(string[i]))
     { str1[i]=string[i] ;
       i++;
     }
    
     count++;
     x=article(str1);
     if(x==0)
     return(0);
    
     else
     { i++;
    
      while(!(isspace(string[i])))
      {
    
       str2[j]=string[i];
       i++;
       j++;
      }
    
     count++;
    y=adjective(str2);
    
    while(y!=0)
    {  i++;
    	while(!(isspace(string[i])))
       {
    	str3[k]=string[i];
       i++;
       k++;
       }
    count++;
    
     y=adjective(str3);
    }
    z=noun(str3);
    if(z==1)
    return(1);
    else
    return(0);
    }
    //------------------------------------------------------------------------------//
    /* article()                                                             */
    
    int article (token str)
    {
    	if(str=="The" || str== "a")
       return(1);
       else
       return(0);
    }
    
    //----------------------------------------------------------------------------//
    /* adjective()                                                           */
    
    int adjective(token str)
    {
     if(str=="white" || str=="hungry" || str=="black" || str=="thirsty")
     return (1);
     else
     return  (0);
     }
    
    /---------------------------------------------------------------------------/
    /* noun()                                                               */
    int noun(token str)
    {
     if(str=="cat"|| str=="dog")
     return(1);
     else
     return(0);
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Flow chart your program and make sure the logic is correct. Next, don't try to solve all the problems. Pick the easiest and solve just that one. Recompile, solve the next, etc.

    Sometimes, just solving one problem will correct others.

    small steps, grashopper; small steps...
    It is not the spoon that bends, it is you who bends around the spoon.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Let me solve your first 2 problems:

    1. void main is wrong, use int main
    2. don't use the == operator to compare 2 strings but use the strcmp function.

    And, like Sayeh already told you, start with something small that works...
    Last edited by Monster; 11-13-2002 at 08:13 AM.

Popular pages Recent additions subscribe to a feed