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;
| ^~~~~~