] > Interfacing Bison with Flex

9.5 Interfacing Bison with Flex

   prog.l             prog.y
 -----|----|    -------|-----|
 fleFxL pErXog.l |    |bisonBI-SOdN prog.y|
 ----------     -------------
   lex.yy.c          prog.tab.c
      -----|     |------
-----------|--C--||---------|
-cc--o prog-lex.yy.c-prog.tab.c--ll
da   ----------||--------|   an
ta--||     application     |--|s⋅
⋅i   -prog-< data.in-> ans.out  out
n

<..prog.y..>
 %{
 
 void yyerror( char * );
 %}
 %token NUM
 %token PLUS MINUS MULT DIV
 %token LPAR RPAR
 %%
 input:    expr ’\n’        { printf ("ans = %d\n", $1); }
         | input ’\n’
         |
 ;
 expr:     expr PLUS term   { $$ = $1 + $3; }
         | expr MINUS term  { $$ = $1 - $3; }
         | term
 ;
 term:     term MULT factor { $$ = $1 * $3; }
         | term DIV factor  { $$ = $1 / $3; }
         | factor
 ;
 factor:   LPAR expr RPAR   { $$ = $2; }
         | NUM              { $$ = $1; }
 ;
 %%
 int main ()
 {
    yyparse ();
    return 0;
 }
 void yyerror(char * e)
 {
    printf ("--- Error --- %s\n", e);
 }
-_-_-

<..prog.l..>
 %{
 #include "prog.tab.h"
 %}
 %%
 "\n"    return ’\n’;
 "+"     return PLUS;
 "-"     return MINUS;
 "*"     return MULT;
 "/"     return DIV;
 "("     return LPAR;
 ")"     return RPAR;
 [0-9]  { sscanf(yytext,"%d",&yylval);
          return NUM;}
-_-_-