Next: Notes on the Implementation, Previous: The Database Language, Up: Another Example of Flex and Bison [Contents][Index]
¾å¤ÎÀá¤Ç¡¢ ¾®µ¬ÌϤʸÀ¸ì¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤·¤¿¡£ ¼¡¤Ë¤½¤ì¤ò¼ÂÁõ¤·¤Æ¤ß¤ë¤³¤È¤Ë¤·¤Þ¤·¤ç¤¦¡£ °Ê²¼¤Î¥Õ¥¡¥¤¥ë¤¬¤³¤ì¤ò¼Â¸½¤·¤Þ¤¹¡£
Ãí¡§¤³¤ì¤Ï¤¢¤¯¤Þ¤Ç¤â£±¤Ä¤ÎÎã¤È¤·¤Æ¸«¤Æ¤¯¤À¤µ¤¤¡£ ÆÃ¤Ëʸˡ¤ÎÉôʬ¤Ï¡¢ ±Ñ¸ì¤Î¥Ñ¡¼¥¹½èÍý¤È¤·¤Æ¤Ï¤¢¤Þ¤êÎɤ¤Îã¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£
°Ê²¼¤ÏBison¤Î¥Õ¥¡¥¤¥ë¤Ç¤¹¡£
%union
¤ÎÉôʬ¡¢
¤ª¤è¤Ó¡¢
yylval
¤Ë¥¢¥¯¥»¥¹¤¹¤ë¤¿¤á¤Ë$$
¤È$
n¤ò»È¤¦ÊýË¡¤ËÃíÌܤ·¤Æ¤¯¤À¤µ¤¤¡£
/* C¥³¡¼¥É¤Ï¥Õ¥¡¥¤¥ë¤ÎÀèÆ¬¤ÇÄ󶡤¹¤ë */ %{ #include <stdio.h> #include <string.h> extern int yylexlinenum; /* lex.yy.c¤Ë¸ºß¤¹¤ë */ extern char *yytext; /* ¥«¥ì¥ó¥È¡¦¥È¡¼¥¯¥ó */ %}
/* ¥¡¼¥ï¡¼¥É¤ÈͽÌó¸ì¤¬¤³¤³¤«¤é»Ï¤Þ¤ë */ %union{ /* ¤³¤ì¤Ï¥Ç¡¼¥¿¤Î¶¦ÍÑÂÎ */ char name[128]; /* ̾Á° */ }
/*------------- ͽÌó¸ì ------------------*/ %token PERIOD %token NEWLINE %token POSITIONAL
%token VERB %token ADVERB
%token PROPER_NOUN %token NOUN
%token DECLARATIVE %token CONDITIONAL
%type <name> declarative %type <name> verb_phrase %type <name> noun_phrase %type <name> position_phrase %type <name> adverb
%type <name> POSITIONAL VERB ADVERB PROPER_NOUN %type <name> NOUN DECLARATIVE CONDITIONAL %%
sentence_list : sentence | sentence_list NEWLINE sentence ;
sentence : verb_phrase noun_phrase position_phrase adverb period { printf("I understand that sentence.\n"); printf("VP = %s \n",$1); printf("NP = %s \n",$2); printf("PP = %s \n",$3); printf("AD = %s \n",$4); } | { yyerror("That's a strange sentence!"); } ;
position_phrase : POSITIONAL declarative PROPER_NOUN { sprintf($$,"%s %s %s",$1,$2,$3); } | /* ¶õ */ { strcpy($$,""); } ;
verb_phrase : VERB { strcpy($$,$1); strcat($$," "); } | adverb VERB { sprintf($$,"%s %s",$1,$2); } ;
adverb : ADVERB { strcpy($$,$1); } | /* ¶õ */ { strcpy($$,""); } ;
noun_phrase : DECLARATIVE NOUN { sprintf($$,"%s %s",$1,$2); }
| CONDITIONAL declarative NOUN { sprintf($$,"%s %s %s",$1,$2,$3); } | NOUN { strcpy($$,$1); strcat($$," "); } ;
declarative : DECLARATIVE { strcpy($$,$1); } | /* ¶õ */ { strcpy($$,""); } ;
period : /* ¶õ */ | PERIOD ; %%
/* main()¤ª¤è¤Óyyerror()´Ø¿ô¤òÄ󶡤¹¤ë */ void main(int argc, char **argv) { yyparse(); /* ¥Õ¥¡¥¤¥ë¤ò¥Ñ¡¼¥¹¤¹¤ë */ }
int yyerror(char *message) { extern FILE *yyout; fprintf(yyout,"\nError at line %5d. (%s) \n", yylexlinenum,message); }
°Ê²¼¤ÏFlex¤Î¥Õ¥¡¥¤¥ë¤Ç¤¹¡£ ʸ»úÎó¤¬ÅϤµ¤ì¤ëÊýË¡¤ËÃí°Õ¤·¤Æ¤¯¤À¤µ¤¤¡£ ¤³¤ì¤ÏºÇŬ²½¤µ¤ì¤¿ÊýË¡¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¤¬¡¢ ºÇ¤âÍý²ò¤·¤ä¤¹¤¤ÊýË¡¤Ç¤¹¡£
%{ #include <stdio.h> #include <string.h> #include "y.tab.h" /* ¤³¤ì¤ÏBison¤Ë¤è¤êÀ¸À®¤µ¤ì¤ë */
#define TRUE 1 #define FALSE 0
#define copy_and_return(token_type) \ { \ strcpy(yylval.name,yytext);\ return(token_type); \ } int yylexlinenum = 0; /* ¹Ô¿ô¥«¥¦¥ó¥ÈÍÑ */ %}
%% /* »ú¶ç²òÀϥ롼¥ë¤¬¤³¤³¤«¤é»Ï¤Þ¤ë */ MEN|WOMEN|STOCKS|TREES copy_and_return(NOUN) MISTAKES|GNUS|EMPLOYEES copy_and_return(NOUN) LOSERS|USERS|CARS|WINDOWS copy_and_return(NOUN)
DATABASE|NETWORK|FSF|GNU copy_and_return(PROPER_NOUN) COMPANY|HOUSE|OFFICE|LPF copy_and_return(PROPER_NOUN)
THE|THIS|THAT|THOSE copy_and_return(DECLARATIVE) ALL|FIRST|LAST copy_and_return(CONDITIONAL)
FIND|SEARCH|SORT|ERASE|KILL copy_and_return(VERB) ADD|REMOVE|DELETE|PRINT copy_and_return(VERB) QUICKLY|SLOWLY|CAREFULLY copy_and_return(ADVERB)
IN|AT|ON|AROUND|INSIDE|ON copy_and_return(POSITIONAL) "." return(PERIOD); "\n" yylexlinenum++; return(NEWLINE); . %%
¤³¤ì¤é¤Î¥Õ¥¡¥¤¥ë¤Ï¡¢ °Ê²¼¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¥³¥ó¥Ñ¥¤¥ë¤Ç¤¤Þ¤¹¡£
% bison -d front.y % flex -I front.lex % cc -o front alloca.c front.tab.c lex.yy.c
¤Þ¤¿¤Ï¡¢ ¤³¤ÎÎã¤Î¥½¡¼¥¹¤¬¼ê¸µ¤Ë¤¢¤ì¤Ð¡¢ examples¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤ª¤¤¤Æ‘make front’¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¤â¥³¥ó¥Ñ¥¤¥ë¤Ç¤¤Þ¤¹¡£
Ãí¡§Bison¥Ñ¡¼¥µ¤Ïalloca.c¤È¤¤¤¦¥Õ¥¡¥¤¥ë¤òɬÍפȤ·¤Þ¤¹¡£
¤³¤Î¥Õ¥¡¥¤¥ë¤Ïexamples¥µ¥Ö¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤¢¤ê¤Þ¤¹¡£
Bison¤ÎÂå¤ï¤ê¤Ëyacc
¤ò»È¤¦¤Î¤Ç¤¢¤ì¤Ð¡¢
¤³¤Î¥Õ¥¡¥¤¥ë¤ÏɬÍפ¢¤ê¤Þ¤»¤ó¡£
Next: Notes on the Implementation, Previous: The Database Language, Up: Another Example of Flex and Bison [Contents][Index]