Guest User

Untitled

a guest
Aug 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. use bison to make executable file but error
  2. snazzle.tab.c: In function ‘int yyparse()’:
  3. snazzle.tab.c:1403: warning: deprecated conversion from string constant to ‘char*’
  4. snazzle.tab.c:1546: warning: deprecated conversion from string constant to ‘char*’
  5. /tmp/ccFyBCBm.o: In function `yyparse':
  6. snazzle.tab.c:(.text+0x1e0): undefined reference to `yylex'
  7. collect2: ld returned 1 exit status
  8.  
  9. snazzle.l
  10. %{
  11. #include <cstdio>
  12. #include <iostream>
  13. using namespace std;
  14. #include "snazzle.tab.h"
  15. %}
  16. %%
  17. [ t] ;
  18. [0-9]+.[0-9]+ { yylval.fval = atof(yytext); return FLOAT; }
  19. [0-9]+ { yylval.ival = atoi(yytext); return INT; }
  20. [a-zA-Z0-9]+ {
  21. // we have to copy because we can't rely on yytext not changing underneath us:
  22. yylval.sval = strdup(yytext);
  23. return STRING;
  24. }
  25. . ;
  26. %%
  27.  
  28. snazzle.y
  29. %{
  30. #include <cstdio>
  31. #include <iostream>
  32. using namespace std;
  33.  
  34. extern "C" int yylex();
  35. extern "C" int yyparse();
  36. extern "C" FILE *yyin;
  37.  
  38. void yyerror(char *s);
  39. %}
  40.  
  41. %union {
  42. int ival;
  43. float fval;
  44. char *sval;
  45. }
  46.  
  47. %token <ival> INT
  48. %token <fval> FLOAT
  49. %token <sval> STRING
  50.  
  51. %%
  52.  
  53. snazzle:
  54. INT snazzle { cout << "bison found an int: " << $1 << endl; }
  55. | FLOAT snazzle { cout << "bison found a float: " << $1 << endl; }
  56. | STRING snazzle { cout << "bison found a string: " << $1 << endl; }
  57. | INT { cout << "bison found an int: " << $1 << endl; }
  58. | FLOAT { cout << "bison found a float: " << $1 << endl; }
  59. | STRING { cout << "bison found a string: " << $1 << endl; }
  60. ;
  61. %%
  62.  
  63. main() {
  64. // open a file handle to a particular file:
  65. FILE *myfile = fopen("a.snazzle.file", "r");
  66. // make sure it is valid:
  67. if (!myfile) {
  68. cout << "I can't open a.snazzle.file!" << endl;
  69. return -1;
  70. }
  71. // set flex to read from it instead of defaulting to STDIN:
  72. yyin = myfile;
  73.  
  74. // parse through the input until there is no more:
  75. do {
  76. yyparse();
  77. } while (!feof(yyin));
  78. }
  79.  
  80. void yyerror(char *s) {
  81. cout << "EEK, parse error! Message: " << s << endl;
  82. // might as well halt now:
  83. exit(-1);
  84. }
Add Comment
Please, Sign In to add comment