Write a context free grammar for structure of a english sentence, where a pronoun is followed by a verb and verb is followed by determiner. Determiner is followed by noun and noun can be followed by noun. Statement can begin with noun or a proper noun. **following program is written for example sentence"I prefer a morning flight from New Delhi".

LEX FILE:

%{
#include "y.tab.h"
%}
%%
I {return pronoun};
prefer {return verb;}
a{return determiner;}
morning|flight {return noun;}
from {return preposition;}
NewDelhi {return propernoun;}
%%

YACC FILE:

%{
#include<stdio.h>
%}
%token noun pronoun propernoun determiner verb preposition
%%
S : NP VP {printf ("Sentence accepted");}
   ;
NP : pronoun
      | propernoun
      | determiner nominal
      ;
nominal : noun nominal
               | noun
               ;
VP : verb
      | verb NP
      | verb NP PP
      | verb PP
      ;
PP : preposition NP
      ;
%%

main()
{
yyparse();
}
yyerror(char * arg)
{
printf("invalid sentence");
}

Comments

Popular posts from this blog

Lex program for counting keyword and identifier

A program to find FIRST of non-terminals of the given grammar

There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump. The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time?