如何重写程序,这样我就不必调用'flex',而只需调用'bison'和'cc'?

如何重写程序,这样我就不必调用'flex',而只需调用'bison'和'cc'?,c,command-line-arguments,bison,flex-lexer,C,Command Line Arguments,Bison,Flex Lexer,我已经有了一个基于bison和flex的计算器程序,它从命令行参数获取输入 现在我如何重写程序,这样我就不必调用flex,而只需在构建过程中调用bison和cc?取得类似的成就 生成文件: fb1-5: fb1-5.l fb1-5.y bison -d fb1-5.y flex fb1-5.l cc -o $@ fb1-5.tab.c lex.yy.c -lfl fb1-5.y /* simplest version of calculator */ %{ # i

我已经有了一个基于bison和flex的计算器程序,它从命令行参数获取输入

现在我如何重写程序,这样我就不必调用flex,而只需在构建过程中调用bison和cc?取得类似的成就

生成文件:

fb1-5:  fb1-5.l fb1-5.y
    bison -d fb1-5.y
    flex fb1-5.l
    cc -o $@ fb1-5.tab.c lex.yy.c -lfl
fb1-5.y

/* simplest version of calculator */

%{
#  include <stdio.h>
%}

/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token OP CP

%%

calclist: /* nothing */
 | calclist exp { printf("= %d\n> ", $2); }
 ;

exp: factor
 | exp ADD exp { $$ = $1 + $3; }
 | exp SUB factor { $$ = $1 - $3; }
 | exp ABS factor { $$ = $1 | $3; }
 ;

factor: term
 | factor MUL term { $$ = $1 * $3; }
 | factor DIV term { $$ = $1 / $3; }
 ;

term: NUMBER
 | ABS term { $$ = $2 >= 0? $2 : - $2; }
 | OP exp CP { $$ = $2; }
 ;
%%
int main(int argc, char** argv)
{
  // printf("> ");
  if(argc > 1) {
    if(argv[1]){
      yy_scan_string(argv[1]);
    }
  }

  yyparse();
  return 0;
}

yyerror(char *s)
{
  fprintf(stderr, "error: %s\n", s);
}
更新:

我试图按照回复中的建议进行操作,请参见下面修改的代码。大体上,为什么在printfargv[%d]之前调用yyerror:%s,n,argv[n]?yyerror不是仅由yyparse调用,yyparse不是仅在printfargv[%d]:%s,n,argv[n]在main中的main之后调用

fb1-5.y:


中有一个词法扫描程序的基本实现。本手册稍后将介绍稍微不太基本的版本


这不会直接帮助您,因为它基于fscanf,这意味着它可以在输入流上工作。大多数C库都包含一些函数,可以将字符串作为文件处理。例如,请参见Posix标准。如果做不到这一点,您将不得不用基于字符串的替代方法替换getc和scanf调用,这意味着您将需要跟踪某个地方的缓冲区和输入指针。strtoul或strtod将被证明是有用的,因为第二个参数可以帮助您跟踪数字使用了多少字符串。

flex读取输入。野牛没有:它只是叫flex。您无需对bison执行任何操作即可从命令行获取输入。语法也与此无关。移除flex并不能实现您的目标。你的问题没有道理。如果你真正想要的是从命令行arg之类的字符串而不是文件中读取,你可以使用flex的yy_scan_string函数从字符串而不是文件中读取…谢谢。换车后你能给我看一下代码吗?我还在读《野牛手册》。@蒂姆:我想如果你至少自己写代码,你会学到更多。其实没那么复杂,谢谢。查看我的更新。我搞不懂bison是如何使用yyflex的,我的修改也不起作用。你为什么要打这些ungetc电话?你没有解开你处理过的令牌。。。Bison在需要令牌时调用yylex。Yylex返回一个令牌。就这样。没有比这更复杂的了。而且,使用单字符标记,就像《野牛手册》中的例子一样,确实让一切变得更简单。但你也可以用你自己的方式。
/* simplest version of calculator */

%{
#  include <stdio.h>
%}

/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token OP CP

%%

calclist: /* nothing */
 | calclist exp { printf("= %d\n> ", $2); }
 ;

exp: factor
 | exp ADD exp { $$ = $1 + $3; }
 | exp SUB factor { $$ = $1 - $3; }
 | exp ABS factor { $$ = $1 | $3; }
 ;

factor: term
 | factor MUL term { $$ = $1 * $3; }
 | factor DIV term { $$ = $1 / $3; }
 ;

term: NUMBER
 | ABS term { $$ = $2 >= 0? $2 : - $2; }
 | OP exp CP { $$ = $2; }
 ;
%%
int main(int argc, char** argv)
{
  // printf("> ");
  if(argc > 1) {
    if(argv[1]){
      yy_scan_string(argv[1]);
    }
  }

  yyparse();
  return 0;
}

yyerror(char *s)
{
  fprintf(stderr, "error: %s\n", s);
}
/* recognize tokens for the calculator and print them out */

%{
# include "fb1-5.tab.h"
%}

%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|"     { return ABS; }
"("     { return OP; }
")"     { return CP; }
[0-9]+  { yylval = atoi(yytext); return NUMBER; }

"//".*  
[ \t]   { /* ignore white space */ }
.   { yyerror("Mystery character %c\n", *yytext); }
%%
$ ./fb1-5  2*4
2*4error: �
= 8
/* simplest version of calculator */

%{
#  include <stdio.h>
  FILE * fin;
  int yylex (void);
  void yyerror(char *s);  
  %}

/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token OP CP

%%

calclist: /* nothing */
 | calclist exp { printf("= %d\n", $2); }
 ;

exp: factor
 | exp ADD exp { $$ = $1 + $3; }
 | exp SUB factor { $$ = $1 - $3; }
 | exp ABS factor { $$ = $1 | $3; }
 ;

factor: term
 | factor MUL term { $$ = $1 * $3; }
 | factor DIV term { $$ = $1 / $3; }
 ;

term: NUMBER
 | ABS term { $$ = $2 >= 0? $2 : - $2; }
 | OP exp CP { $$ = $2; }
 ;
%%




/* The lexical analyzer returns a double floating point
   number on the stack and the token NUM, or the numeric code
   of the character read if not a number.  It skips all blanks
   and tabs, and returns 0 for end-of-input.  */

#include <ctype.h>
#include <string.h>

int yylex (void)
{
  char c;

/* Skip white space.  */
  while ((c = getc(fin)) == ' ' || c == '\t'){
    continue;
  }

  // printf("%s", &c);

  /* Process numbers.  */
  if (c == '.' || isdigit (c))
    {
      ungetc(c, fin);
      fscanf (fin, "%d", &yylval);
      return NUMBER;
    }

  /* Process addition.  */
  if (c == '+')
    {
      return ADD;
    }

  /* Process sub.  */
  if (c == '-')
    {
      return SUB;
    }

  /* Process mult.  */
  if (c == '*')
    {
      return MUL;
    }

  /* Process division.  */
  if (c == '/')
    {
      return DIV;
    }

  /* Process absolute.  */
  if (c == '|')
    {
      return ABS;
    }

   /* Process left paren.  */
   if (c == '(')
    {
      return OP;
    }

  /* Process right paren.  */
  if (c == ')')
    {
      return CP;
    }

  /* Return a single char.  */
  yyerror(&c);
  return c;
}


int main(int argc, char** argv)
{
  // evaluate each command line arg as an arithmetic expression
  int n=1;
  while (n < argc) {
    if(argv[n]){
      // yy_scan_string(argv[n]);
      // fin = stdin;
      fin = fmemopen(argv[n], strlen (argv[n]), "r");
      printf("%s ",argv[n]);
      fflush(stdout);
      yyparse();
    }
    n++;
  }

  return 0;
}

void yyerror(char *s)
{
  fprintf(stderr, "error: %s\n", s);
}