دا واجب لمعمل الكومبالير بس ماني عارفه فين الغلط فممكن الي يعرف فين الخطا يقولي
yacc
كود:
%{
#include <stdio.h>
int yylex(void);
void yyerror(char *);
%}
%token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION
%%
sentence: simple_sentence { printf("parsed a simple sentence.\n", $1); }
| compound_sentence { printf("parsed a compound sentence.\n", $1); }
;
simple_sentence: subject verb object
| subject verb object prep_phrase
;
compound_sentence: simple_sentence CONJUNCTION simple_sentence
| compound_sentence CONJUNCTION simple_sentence
;
subject: NOUN
| PRONOUN
| ADJECTIVE subject
;
verb: VERB
| ADVERB VERB
| verb VERB
;
object: NOUN
| ADJECTIVE object
;
prep_phrase: PREPOSITION NOUN
;
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
int main(void) {
printf("Please enter any NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION or sentence\n");
yyparse();
return 0;
}
lex
كود:
%{
#include "y.tab.h"
#include <stdlib.h>
void yyerror(char *);
#define LOOKUP 0
int state;
%}
%%
\n { state = LOOKUP ;}
\.\n { state = LOOKUP ;
return 0 ;}
^noun { state = NOUN ; }
^pronoun { state = PRONOUN ; }
^verb { state = VERB ; }
^adverb { state = ADVERB ; }
^adjective { state = ADJECTIVE ;}
^preposition { state = PREPOSITION ;}
^conjunction { state = CONJUNCTION ;}
[a-zA-Z]+ {
if (state != LOOKUP)
{
add_word(state, yytext);
}
else
{
switch (lookup_word(yytext))
{
case NOUN : { printf(" %s : NOUN\n",yytext);return (NOUN) ;}
case PRONOUN : {printf(" %s : PRONOUN\n",yytext); return (PRONOUN) ;}
case VERB : { printf(" %s : VERB\n",yytext); return (VERB) ;}
case ADVERB :{printf(" %s : ADVERB\n",yytext);return (ADVERB) ;}
case ADJECTIVE :{ printf(" %s : ADJECTIVE\n",yytext);return (ADJECTIVE) ;}
case PREPOSITION :{printf(" %s : PREPOSITION\n",yytext);return (PREPOSITION) ;}
case CONJUNCTION :{printf(" %s : CONJUNCTION\n",yytext);return (CONJUNCTION) ; }
default : printf(" %s : don't recognize\n",yytext);
}
}
}
%%
struct word
{
char *word_name;
int word_type;
struct word *next;
};
struct word *word_list;
extern void *malloc();
int add_word( int type, char *word)
{
struct word *wp;
if(lookup_word(word) != LOOKUP)
{
printf(" !!! warning: word %s already defined \n",word);
return 0;
}
wp = (struct word *) malloc( sizeof(struct word)) ;
wp->next = word_list;
wp->word_name = (char *) malloc( strlen(word)+1);
strcpy(wp->word_name, word);
wp->word_type = type;
word_list = wp ;
return 1;
}
int lookup_word(char *word)
{
struct word *wp = word_list;
for( ; wp; wp= wp->next)
{
if(strcmp(wp->word_name, word) == 0)
{
return wp->word_type;
}
}
return LOOKUP;
}
int yywrap(void) {
return 1;
}