1 /* $NetBSD: front.y,v 1.1.1.1 2009/10/26 00:28:33 christos Exp $ */ 2 3 /* C code supplied at the beginning of the file. */ 4 5 %{ 6 7 #include <stdio.h> 8 #include <string.h> 9 10 extern int yylexlinenum; /* these are in YYlex */ 11 extern char *yytext; /* current token */ 12 13 14 %} 15 16 /* Keywords and reserved words begin here. */ 17 18 %union{ /* this is the data union */ 19 char name[128]; /* names */ 20 } 21 22 /*-------------------- the reserved words -----------------------------*/ 23 24 %token PERIOD 25 %token NEWLINE 26 %token POSITIONAL 27 28 %token VERB 29 %token ADVERB 30 31 %token PROPER_NOUN 32 %token NOUN 33 34 %token DECLARATIVE 35 %token CONDITIONAL 36 37 38 %type <name> declarative 39 %type <name> verb_phrase 40 %type <name> noun_phrase 41 %type <name> position_phrase 42 %type <name> adverb 43 44 %type <name> POSITIONAL VERB ADVERB PROPER_NOUN 45 %type <name> NOUN DECLARATIVE CONDITIONAL 46 47 %% 48 49 sentence_list : sentence 50 | sentence_list NEWLINE sentence 51 ; 52 53 54 sentence : verb_phrase noun_phrase position_phrase adverb period 55 { 56 printf("I understand that sentence.\n"); 57 printf("VP = %s \n",$1); 58 printf("NP = %s \n",$2); 59 printf("PP = %s \n",$3); 60 printf("AD = %s \n",$4); 61 } 62 | { yyerror("That's a strange sentence !!"); } 63 ; 64 65 position_phrase : POSITIONAL declarative PROPER_NOUN 66 { 67 sprintf($$,"%s %s %s",$1,$2,$3); 68 } 69 | /* empty */ { strcpy($$,""); } 70 ; 71 72 73 verb_phrase : VERB { strcpy($$,$1); strcat($$," "); } 74 | adverb VERB 75 { 76 sprintf($$,"%s %s",$1,$2); 77 } 78 ; 79 80 adverb : ADVERB { strcpy($$,$1); } 81 | /* empty */ { strcpy($$,""); } 82 ; 83 84 noun_phrase : DECLARATIVE NOUN 85 { 86 sprintf($$,"%s %s",$1,$2); 87 } 88 | CONDITIONAL declarative NOUN 89 { 90 sprintf($$,"%s %s %s",$1,$2,$3); 91 } 92 | NOUN { strcpy($$,$1); strcat($$," "); } 93 ; 94 95 declarative : DECLARATIVE { strcpy($$,$1); } 96 | /* empty */ { strcpy($$,""); } 97 ; 98 99 period : /* empty */ 100 | PERIOD 101 ; 102 103 104 %% 105 106 /* Supplied main() and yyerror() functions. */ 107 108 int main(int argc, char *argv[]) 109 { 110 yyparse(); /* parse the file */ 111 return(0); 112 } 113 114 int yyerror(char *message) 115 { 116 extern FILE *yyout; 117 118 fprintf(yyout,"\nError at line %5d. (%s) \n", 119 yylexlinenum,message); 120 } 121