0

im trying to make a programming language like basic or else i used a tutorial to make a simple programming language but it only can do calculation like x + y , y - x , and more, but i added a token for a variables such as x = 5; but when i tried to run code code throws error.

here is scanner.l:

%{
  extern "C" int yylex();
  #include "parser.tab.c"
%}

%%
[0-9]+        { yylval.intVal = atoi(yytext); return INTEGER_LITERAL; }
[0-9]+.[0-9]+ { yylval.floatVal = atof(yytext); return FLOAT_LITERAL; }
"="           { return EQUALS; }
[A-Z]+|[a-z]+ { yylval.stringVal = yytext; return VAR; }
"+"           { return PLUS; }
"-"           { return MINUS; }
"*"           { return MULT; }
"/"           { return DIV; }
";"           { return SEMI; }
[\t\r\n\f]    ;

and the parser.y:

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

%union{
  int intVal;
  float floatVal;
  string stringVal; // the error, here im just setting the var for stringVal
}

%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; } // here is the variable
    | 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;
}

also the error:

In file included from scanner.l:3:
parser.tab.c:974:9: error: use of deleted function 'YYSTYPE::YYSTYPE()'
  974 | YYSTYPE yylval;
      |         ^~~~~~
In file included from scanner.l:3:
parser.tab.c:120:7: note: 'YYSTYPE::YYSTYPE()' is implicitly deleted because the default definition would be ill-formed:
  120 | union YYSTYPE
      |       ^~~~~~~
In file included from scanner.l:3:
parser.y:12:10: error: union member 'YYSTYPE::stringVal' with non-trivial 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
   12 |   string stringVal;
      |          ^~~~~~~~~
In file included from scanner.l:3:
parser.tab.c: In function 'int yyparse()':
parser.tab.c:1003:30: error: use of deleted function 'YYSTYPE::YYSTYPE()'
 1003 |     YYSTYPE yyvsa[YYINITDEPTH];
      |                              ^
parser.tab.c:1003:30: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
In file included from scanner.l:3:
parser.tab.c:120:7: note: 'YYSTYPE::~YYSTYPE()' is implicitly deleted because the default definition would be ill-formed:
  120 | union YYSTYPE
      |       ^~~~~~~
In file included from scanner.l:3:
parser.y:12:10: error: union member 'YYSTYPE::stringVal' with non-trivial 'std::basic_string<_CharT, _Traits, _Alloc>::~basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
   12 |   string stringVal;
      |          ^~~~~~~~~
In file included from scanner.l:3:
parser.tab.c:1003:30: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
 1003 |     YYSTYPE yyvsa[YYINITDEPTH];
      |                              ^
parser.tab.c:1015:11: error: use of deleted function 'YYSTYPE::YYSTYPE()'
 1015 |   YYSTYPE yyval;
      |           ^~~~~
parser.tab.c:1015:11: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
parser.tab.c:1181:14: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)'
 1181 |   *++yyvsp = yylval;
      |              ^~~~~~
In file included from scanner.l:3:
parser.tab.c:120:7: note: 'YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)' is implicitly deleted because the default definition would be ill-formed:
  120 | union YYSTYPE
      |       ^~~~~~~
In file included from scanner.l:3:
parser.y:12:10: error: union member 'YYSTYPE::stringVal' with non-trivial 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
   12 |   string stringVal;
      |          ^~~~~~~~~
In file included from scanner.l:3:
parser.tab.c:1212:24: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)'
 1212 |   yyval = yyvsp[1-yylen];
      |                        ^
In file included from scanner.l:3:
parser.y:37:36: error: cannot convert 'std::string' {aka 'std::basic_string<char>'} to 'float' in assignment
   37 |   | VAR        { $$ = $1; }
      |                          ~~        ^
      |                                    |
      |                                    std::string {aka std::basic_string<char>}
In file included from scanner.l:3:
parser.tab.c:1287:14: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)'
 1287 |   *++yyvsp = yyval;
      |              ^~~~~
In file included from scanner.l:3:
parser.tab.c:1317:20: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 1317 |       yyerror (YY_("syntax error"));
      |                    ^~~~~~~~~~~~~~
parser.tab.c:198:22: note: in definition of macro 'YY_'
  198 | #  define YY_(Msgid) Msgid
      |                      ^~~~~
In file included from scanner.l:3:
parser.tab.c:1431:14: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)'
 1431 |   *++yyvsp = yylval;
      |              ^~~~~~
In file included from scanner.l:3:
parser.tab.c:1461:16: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 1461 |   yyerror (YY_("memory exhausted"));
      |                ^~~~~~~~~~~~~~~~~~
parser.tab.c:198:22: note: in definition of macro 'YY_'
  198 | #  define YY_(Msgid) Msgid
      |                      ^~~~~
parser.tab.c: In function 'void __static_initialization_and_destruction_0(int, int)':
parser.tab.c:974:9: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
  974 | YYSTYPE yylval;
      |         ^~~~~~
rici
  • 234,347
  • 28
  • 237
  • 341
  • 1
    If a union have more complex members (like `std::string`) then it must have a (default) constructor that explicitly construct the complex member. – Some programmer dude Dec 27 '20 at 13:19
  • what do you mean the error seams to be from the union i searched everywhere and didn't find anything everyone said i cant use string in union anymore – Ariya1234gamer Dec 27 '20 at 13:23
  • 1
    Actually before the C++11 standard you couldn't use complex (*non-trivial* in C++-speak) structures at all inside unions. Since the C++11 standard it's possible, but you need to provide a union constructor. And before you use the `std::string` member you must explicitly "construct" (initialize) it. Please see e.g. [this `union` reference](https://en.cppreference.com/w/cpp/language/union). – Some programmer dude Dec 27 '20 at 13:27
  • i still get error i use "bison parser.y" and "flex scanner.l" then "g++ lex.yy.c -lfl" – Ariya1234gamer Dec 27 '20 at 13:44
  • i didn't think you read all the question – Ariya1234gamer Dec 27 '20 at 13:49
  • 1
    This has little to do with flex or bison, and much to do with a `union` that has `std::string` as a member. [This simple program](https://godbolt.org/z/6nzjM7) already generates some of the errors you observe. Before you even start with flex/bison, you need to get `YYSTYPE` definition to the point where you can create, destroy, copy-construct and assign instances of that type. – Igor Tandetnik Dec 27 '20 at 14:01
  • Possible duplicate: https://stackoverflow.com/questions/51390618/pass-stl-containers-from-flex-to-bison. I removed [tag:c] because nothing in this question uses C. – rici Dec 27 '20 at 16:11

0 Answers0