c - 如何重写程序,以便不必调用 `flex` 而只调用 `bison` 和 `cc` ?

标签 c command-line-arguments bison flex-lexer

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

现在我该如何重写程序,以便在构建过程中不必调用 flex 而只调用 bisoncc过程? (实现类似于 https://unix.stackexchange.com/questions/499190/where-is-the-official-documentation-debian-package-iproute-doc#comment919875_499225 的效果)。

$ ./fb1-5 '1+3'
= 4

生成文件:

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);
}

fb1-5.l:

/* 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); }
%%
<小时/>

更新:

我尝试按照回复中的建议进行操作,请参阅下面修改后的代码。在 main() 中,为什么在 printf("argv[%d]: %s ", n, argv[n])< 之前调用 yyerror()/? yyerror() 不是仅由 yyparse() 调用,并且 yyparse 不是仅在 printf("argv[ %d]: %s ", n, argv[n])main() 中,在 main() 中。

$ ./fb1-5  2*4
2*4error: �
= 8

fb1-5.y:

/* 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);
}

最佳答案

examples section of the bison manual 中有词法扫描器的基本实现。 。 (手册后面有稍微不太基础的版本。)

这不会直接帮助您,因为它基于 fscanf,这意味着它在输入流上工作。大多数 C 库包含可让您将字符串视为 FILE* 的函数(例如,请参阅 Posix 标准 fmemopen )。如果做不到这一点,您就必须将 getc 和 scanf 调用替换为基于字符串的替代方案,这意味着您将需要在某处跟踪缓冲区和输入指针。 strtoul(或strtod)将证明很有用,因为第二个参数可以帮助您跟踪数字使用了多少字符串。

关于c - 如何重写程序,以便不必调用 `flex` 而只调用 `bison` 和 `cc` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600702/

相关文章:

Python – 验证输入是真实文件夹而不是根目录

c++ - C/C++ 中的搜索字符串解析器

Bison 冲突转移/减少

c - C 中删除 else 语句是否更有效?

c - C语言中如何判断输入的内容是否为数字?

c - 在 Linux 中测量时间 - 时间 vs 时钟 vs getrusage vs clock_gettime vs gettimeofday vs timespec_get?

c - 尝试检查命令行参数时出现段错误(在 C 中)

c - 绑定(bind)失败 : Cannot assign requested address

c - 我如何应对/创建/规避 Yacc/Bison 中处理多个 %types 的规则?

printf 可以改变它的参数吗?