Skip to content

Instantly share code, notes, and snippets.

@vnkdj5
Created May 3, 2019 06:51
Show Gist options
  • Save vnkdj5/c1daa7cd24070f60fd709ba74a3ea7d8 to your computer and use it in GitHub Desktop.
Save vnkdj5/c1daa7cd24070f60fd709ba74a3ea7d8 to your computer and use it in GitHub Desktop.
YACC specifications to implement syntax analysis phase of compiler to recognize simple and compound sentences given in input file.
%{
#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;
}
%{
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment