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:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#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; | |
} |
Input:-
vaibhav is in PICT
whenever I run code and give output of compound sentence it gives me syntax error. why so please tell
ReplyDeletethanks
ReplyDelete