i am making a programming language using bison/flex but i get errors i just found a simple programming language tutorial in dev.to then made some changes now im making a variable system or anything else. but i get a error
how the variable should look like:
the end is just example but i want the first one to work; i done the lexer with no error
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; }
and my parser.y code:
%{
#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;
}