问题描述
我正在尝试制作一种像 basic 这样的编程语言,否则我使用教程来制作一种简单的编程语言,但它只能进行 x + y 、 y - x 等计算, 但我为变量添加了一个标记,例如 x = 5; 但是当我尝试运行代码时,代码抛出错误。
这里是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] ;
和 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;
}
还有错误:
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,_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,_Alloc>& std::basic_string<_CharT,_Alloc>::operator=(const std::basic_string<_CharT,_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;
| ^~~~~~
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)