使用Flex和Bison输出到两个不同的文件

时间:2019-04-10 03:02:11

标签: c bison flex-lexer

我想在文件_1中打印由Flex生成的令牌,并在文件_2中打印由Bison生成的已解析输出。目前,我将它们都打印到一个文件中,以便将它们混合在一起。如何将打印分为两个不同的文件?

我尝试通过依次调用yylex和yyparse来分别打印它们,但是没有用。

下面是我的Flex和Bison文件。

Flex文件

%{
#include <stdio.h>
#include <string.h>
#include "Parser.tab.h"
%}

Number [0-9]*[\.0-9]*
Ident  [A-Za-z][A-Za-z0-9]*
Tagident  <[A-Za-z]+
Whitespace  [ \t\n]+
Comment  <!--(.*?)-->
Plus  \+
Minus  \-
Mult  \*
Divide  \/
Modulo  \%
Exp  [*]{2}
Lparen  \(
Rparen  \)
Equals  \=
Lthan  \<
Gthan  \>
Colon  \:
Comma  \,
Scolon \;
Period \s+\.\s+
Quote \'                                                                                                                            
Dquote \"                                                                                                                                           
Endtaghead <[/]                                                                                                             

%%
\n  {/* Do nothing */}
{Comment}   {/* Do nothing */}
{Whitespace}    {/* Do nothing */}
{Number} {printf("%s%s%s", "Number\t\t", yytext,"\n"); yylval.ival= atoi(yytext); return number; }
{Plus}  {printf("%s%s%s", "Plus\t\t" ,yytext,"\n"); yylval.sval = strdup(yytext); return plus; }
{Minus} {printf("%s%s%s", "Minus \t", yytext,"\n"); yylval.sval = strdup(yytext); return minus; }
{Mult}  {printf("%s%s%s", "Mult \t", yytext,"\n"); yylval.sval = strdup(yytext); return mult; }
{Divide} {printf("%s%s%s", "Divide\t\t", yytext,"\n"); yylval.sval = strdup(yytext); return divide; }
{Modulo} {printf("%s%s%s", "Modulo \t", yytext,"\n"); yylval.sval = strdup(yytext); return modulo; }
{Exp} {printf("%s%s%s", "Exp \t", yytext,"\n"); yylval.sval = strdup(yytext); return exp;}
{Lparen} {printf("%s%s%s", "Lparen \t", yytext,"\n"); yylval.sval = strdup(yytext); return left; }
{Rparen} {printf("%s%s%s", "Rparen \t", yytext,"\n"); yylval.sval = strdup(yytext); return right; }
{Equals} {printf("%s%s%s", "Equals \t", yytext,"\n"); yylval.sval = strdup(yytext); return equals; }
{Lthan} {printf("%s%s%s", "Lthan\t\t", yytext,"\n"); yylval.sval = strdup(yytext);return lthan; }
{Gthan} {printf("%s%s%s", "Gthan\t\t", yytext,"\n"); yylval.sval = strdup(yytext); return gthan; }
{Colon} {printf("%s%s%s", "Colon \t", yytext,"\n"); yylval.sval = strdup(yytext);return colon;}
{Comma} {printf("%s%s%s", "Comma \t", yytext,"\n"); yylval.sval = strdup(yytext);return comma; }
{Scolon} {printf("%s%s%s", "Scolon \t", yytext,"\n"); yylval.sval = strdup(yytext);return scolon; }
{Period} {printf("%s%s%s", "Period \t", yytext,"\n"); yylval.sval = strdup(yytext);return period;}
{Quote} {printf("%s%s%s", "Quote \t", yytext,"\n"); yylval.sval = strdup(yytext);return quote; }
{Dquote} {printf("%s%s%s", "Dquote \t", yytext,"\n"); yylval.sval = strdup(yytext);return dquote;}
{Ident} {printf("%s%s%s", "Ident\t\t", yytext,"\n"); yylval.sval = strdup(yytext);return ident; }
{Tagident} {printf("%s%s%s", "Tagident \t", yytext,"\n"); yylval.sval = strdup(yytext);return tagident;}
{Endtaghead} {printf("%s%s%s", "Endtaghead \t", yytext,"\n"); yylval.sval = strdup(yytext);return endtaghead; }
. {printf("%s%s%s", "Invalid character \t", yytext,"\n"); }

%%
int yywrap (void ) 
{
    return 1;
}

解析器文件

%{
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  extern int yylex();
  extern int yyparse();
  extern FILE *yyin;
  extern FILE *yyout;
  void yyerror(const char *s);
%}

%union{
  int ival;
  char *sval; 
}

/* Some tokens are unused since program is not yet complete */
%token <ival> number
%token <sval> ident
%token <sval> tagident
%token <sval> endtaghead
%token <sval> plus 
%token <sval> minus
%token <sval> mult
%token <sval> divide
%token <sval> modulo
%token <sval> left
%token <sval> right 
%token <sval> equals
%token <sval> lthan
%token <sval> gthan
%token <sval> colon
%token <sval> comma 
%token <sval> scolon
%token <sval> period
%token <sval> quote
%token <sval> dquote
%token <sval> exp


%error-verbose

%start summary
%%

summary: opener rows closer
;

opener: tagident gthan
;

closer: endtaghead ident gthan
;

rows: row | row rows
;

row: opener cells closer
;

cells: cell | cell cells
;

cell: opener contents closer
;

contents: content | content contents
;

content: ident 
{
  printf("%s",$1);
}
;


content: number
{
  printf("%d",$1);
}
;


%%
int main(int argc, char *argv[])
{
    #ifdef YYDEBUG
    yydebug = 1;
    #endif
    yyin = fopen(argv[1], "r");
    yyout = freopen(argv[2], "w",stdout);
    fprintf(yyout, "%s%s","TOKEN\t\t","LEXEME\n");   
    fprintf(yyout, "-------------------------------------------------\n");
    if(yyparse() ==0)
    {
        fprintf(yyout,"Parse success");
    }
    fclose(yyin);
    fclose(yyout);
}
void yyerror(const char *s) {
    fprintf(stderr, "%s\n", s);     
}

0 个答案:

没有答案