Thursday, April 10 2025

Header Ads

YACC program to recognize simple and compound sentences given in input file.

Problem Statement:-

Write a program using YACC specifications to implement syntax analysis phase of compiler to recognize simple and compound sentences given in input file.

Source Code:-

%{
#include "y.tab.h" //Contains Token Definiation
%}
%%
[\t ] ; //IGNORE WHITE SPACES
am|is|are|have|has|can|will|shall|eat|sing|go|goes { printf("VERB\t==>%s\n",yytext);return VERB;}
very|simply|gently { printf("VERB\t==>%s\n",yytext);return(ADVERB); }
and|or|also|so|but|if|then {printf("CONJUNCTION\t==>%s\n",yytext);return (CONJUNCTION);}
fast|good|honest {printf("ADJECTIVE\t==>%s\n",yytext);return (ADJECTIVE);}
I|he|she|we|they|you|this {printf("PRONOUN\t==>%s\n",yytext);return (PRONOUN);}
in|on|to {printf("PREPOSITION\t==>%s\n",yytext);return (PREPOSITION);}
[a-zA-Z]+ {printf("NOUN\t==>%s\n",yytext);return (NOUN);}
. ; //IGNORE ANYTHING ELSE
%%
int yywrap()
{
return 1;
}
view raw sentence.l hosted with ❤ by GitHub
%{
#include<stdio.h>
void yyerror(char*);
int yylex();
FILE* yyin;
%}
%token NOUN PRONOUN ADJECTIVE VERB ADVERB CONJUNCTION PREPOSITION
%%
sentence: compound { printf("COMPOUND SENTENCE\n");}
|
simple {printf("SIMPLE SENTENCE\n");}
;
simple: subject VERB object;
compound: subject VERB object CONJUNCTION subject VERB object;
subject: NOUN|PRONOUN;
object: NOUN|ADJECTIVE NOUN|ADVERB NOUN|PREPOSITION NOUN;
%%
void yyerror(char *s)
{
printf("ERROR:%s",s);
}
int main(int argc,char* argv[])
{
yyin=fopen(argv[1],"r");
yyparse();
fclose(yyin);
return 0;
}
view raw sentence.y hosted with ❤ by GitHub

Input:-

vaibhav is in PICT

Output:-


Source Code:-  B5_SPOS

2 comments:

  1. whenever I run code and give output of compound sentence it gives me syntax error. why so please tell

    ReplyDelete

Powered by Blogger.