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

时间:2019-02-08 21:47:44

标签: 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

Makefile:

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()中,为什么yyerror()printf("argv[%d]: %s ", n, argv[n])之前被调用? yyerror()不仅yyparse()被调用,yyparseprintf("argv[%d]: %s ", n, argv[n])的{​​{1}}之后仅main()也被调用,谢谢。 / p>

main()

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

fb1-5.y

1 个答案:

答案 0 :(得分:1)

examples section of the bison manual中有一个词法扫描器的基本实现。 (本手册稍后会介绍一些基本版本。)

这对您没有直接帮助,因为它基于fscanf,这意味着它可以在输入流上使用。大多数C库都包含使您可以将字符串视为FILE*的函数(例如,参见Posix标准fmemopen)。否则,您必须将getc和scanf调用替换为基于字符串的替代方案,这意味着您将需要在某个位置跟踪缓冲区和输入指针。 strtoul(或strtod)将很有用,因为第二个参数可帮助您跟踪数字使用了多少字符串。