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");
}
%{
#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
Post a Comment