0

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;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    `exp` is `floatVal`, but `VAR` is `stringVal`. How `VAR` can be `exp` in this case? – MikeCAT Dec 27 '20 at 14:19
  • `$$ = atof($1);` – Tumbleweed53 Dec 27 '20 at 14:32
  • @Tumbleweed53 it returns number im asking how should i use the VAR i dont know im new to bison/flex – Ariya1234gamer Dec 27 '20 at 14:36
  • @MikeCAT you mean i should add two more %type for stringVal? – Ariya1234gamer Dec 27 '20 at 14:52
  • It seems your purpose is getting the calculation result (one float value) as the result of parsing. Then, variables should be parsed to float values assigned to the variables. This means that `VAR` also should be `stringVal` and instead of direct assignment `$$ = $1;` you will have to write some code to obtain the assigned value of the variables from the variable names. – MikeCAT Dec 27 '20 at 14:54
  • a new function right? adding %type expStr and %type statementStr ?? – Ariya1234gamer Dec 27 '20 at 14:56
  • The [bison manual](https://www.gnu.org/software/bison/manual/bison.html#Examples) contains a series of simple examples; if you work through them, you will end up with a calculator which understands named variables and functions. It's probably a better introduction to parsing than most tutorials you'll find on the web. But please note: it shows you how to build a *calculator* – that is, an immediate interpreter – not a compiler. A compiler has a similar (although probably more complicated) grammar, but what it does with the parsed input is completely different... – rici Dec 27 '20 at 16:22
  • .... because the goal of the compiler is completely different. A calculator interprets and evaluates the input once, producing an immediate result. A compiler, on the other hand, produces an executable object; a representation of how to later evaluate the parsed program. – rici Dec 27 '20 at 16:27
  • thank you but the bison tutorial dont have any lex file provided i should write it? – Ariya1234gamer Dec 27 '20 at 20:10
  • If you want to. Bison doesn't require you to use (f)lex to implement the scanner, but it's usually the simplest. However, it's also good to understand the interface which `yylex` implements, and in simple cases like a calculator it's not much work to write the scanner by hand, as the examples show. What is most important is to make an attempt to understand the examples, which will help you learn how to use bison for your own needs. Rewriting the scanners using (f)lex is probably a good exercise. – rici Dec 28 '20 at 00:38
  • If you just want a calculator then use [this](http://manual.freeshell.org/bison/examples/calc++/) but there are no float value acceptance so modify it. – Suvajit Patra Jan 02 '21 at 14:56

0 Answers0