Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use bison to make executable file but error
- snazzle.tab.c: In function ‘int yyparse()’:
- snazzle.tab.c:1403: warning: deprecated conversion from string constant to ‘char*’
- snazzle.tab.c:1546: warning: deprecated conversion from string constant to ‘char*’
- /tmp/ccFyBCBm.o: In function `yyparse':
- snazzle.tab.c:(.text+0x1e0): undefined reference to `yylex'
- collect2: ld returned 1 exit status
- snazzle.l
- %{
- #include <cstdio>
- #include <iostream>
- using namespace std;
- #include "snazzle.tab.h"
- %}
- %%
- [ t] ;
- [0-9]+.[0-9]+ { yylval.fval = atof(yytext); return FLOAT; }
- [0-9]+ { yylval.ival = atoi(yytext); return INT; }
- [a-zA-Z0-9]+ {
- // we have to copy because we can't rely on yytext not changing underneath us:
- yylval.sval = strdup(yytext);
- return STRING;
- }
- . ;
- %%
- snazzle.y
- %{
- #include <cstdio>
- #include <iostream>
- using namespace std;
- extern "C" int yylex();
- extern "C" int yyparse();
- extern "C" FILE *yyin;
- void yyerror(char *s);
- %}
- %union {
- int ival;
- float fval;
- char *sval;
- }
- %token <ival> INT
- %token <fval> FLOAT
- %token <sval> STRING
- %%
- snazzle:
- INT snazzle { cout << "bison found an int: " << $1 << endl; }
- | FLOAT snazzle { cout << "bison found a float: " << $1 << endl; }
- | STRING snazzle { cout << "bison found a string: " << $1 << endl; }
- | INT { cout << "bison found an int: " << $1 << endl; }
- | FLOAT { cout << "bison found a float: " << $1 << endl; }
- | STRING { cout << "bison found a string: " << $1 << endl; }
- ;
- %%
- main() {
- // open a file handle to a particular file:
- FILE *myfile = fopen("a.snazzle.file", "r");
- // make sure it is valid:
- if (!myfile) {
- cout << "I can't open a.snazzle.file!" << endl;
- return -1;
- }
- // set flex to read from it instead of defaulting to STDIN:
- yyin = myfile;
- // parse through the input until there is no more:
- do {
- yyparse();
- } while (!feof(yyin));
- }
- void yyerror(char *s) {
- cout << "EEK, parse error! Message: " << s << endl;
- // might as well halt now:
- exit(-1);
- }
Add Comment
Please, Sign In to add comment