无法在分配 flex/bison 中将“char*”转换为“float”

问题描述

我正在使用 bison/flex 制作一种编程语言,但出现错误 我刚刚在 dev.to 中找到了一个简单的编程语言教程 然后进行了一些更改,现在我正在制作一个可变系统或其他任何东西。但我得到一个错误

变量应该是什么样子:

结尾只是示例,但我希望第一个起作用;我没有错误地完成词法分析

varX = 5

varX + 3
In file included from scanner.l:3:
parser.y: In function 'int yyparse()':
parser.y:38:36: error: cannot convert 'char*' to 'float' in assignment
   38 |   | VAR        { $$ = $1; }

和我的 parser.y 代码

%{
  #include <iostream>
  #include <string>
  #include <vector>
  using namespace std;
  extern "C" void yyerror(char *s);
  extern "C" int yyparse();
%}

%union{
  char* stringVal;
  int intVal;
  float floatVal;
}

%start program

%token <intVal> INTEGER_LIteraL
%token <floatVal> FLOAT_LIteraL
%token <stringVal> VAR
%token EQUALS
%token SEMI
%type <floatVal> exp
%type <floatVal> statement
%left PLUS MINUS
%left MULT DIV

%%
program: /* empty */
    | program statement { cout << "" << $2 << endl; }
    ;

statement: exp SEMI

exp:
    INTEGER_LIteraL { $$ = $1; }
    | FLOAT_LIteraL { $$ = $1; }
  | VAR        { $$ = $1; }
    | exp PLUS exp  { $$ = $1 + $3; }
    | exp MINUS exp { $$ = $1 - $3; }
    | exp MULT exp  { $$ = $1 * $3; }
    | exp DIV exp   { $$ = $1 / $3; }
    ;
%%
int main(int argc,char **argv) {
  if (argc < 2) {
    cout << "Provide a filename to parse!" << endl;
    exit(1);
  }
  FILE *sourceFile = fopen(argv[1],"r");

  if (!sourceFile) {
    cout << "Could not open source file " << argv[1] << endl;
    exit(1);
  }

  yyin = sourceFile;
  yyparse();
}

void yyerror(char *s) {
  cerr << s << endl;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)