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);
}
[code][/code]tagged by Salem