0

I'm trying to build a scanner-parser to handle input like this x1+x2+x3+x4; and I'm blocked by this error. I understand that when you declare a union in the flex flex2.y file the yylval is connected with this union and after you compile the file flex2.y 2 files are created on the same folder. The one is flex2.tab.y and the other is flex2.tab.h. So after that I have to include the flex2.tab.h file in the flex2.l file so that I can use yylval in the flex2.l file to return the value of the tokens. But for some reason the .l file doesn't recognise the yylval. If someone can point my mistake i would be really grateful.

flex2.y code:

%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int yyerror(char *message)
{
    return 0;
}
%}

%union {
 double val;
 char sym;
}   
%token <val> NUMBER
%token <sym> PLUS Q_MARK
%type <val> addition_list addition
%start addition_list
%%

addition_list : addition Q_MARK  {printf("apotelesma: %d\n", $1);}
| addition_list addition Q_MARK  {}
;

addition : NUMBER PLUS NUMBER {$$ = $1 + $3;}
| addition PLUS NUMBER {$$ = $1 + $3;}
;

%%
void main(int argc, char *argv[])
{
    yyparse();
}

flex2.l code:

%option noyywrap
%{
#include "flex2.tab.h"
%}

%%
\+ { yylval.sym = yytext[0]; return PLUS; }
; { yylval.sym = yytext[0]; return Q_MARK; }
0|([-+]?(([1-9][0-9]*)|(0\.[0-9]+)|([1-9][0-9]*\.[0-9]+))) {yylval.val = atof(yytext); return NUMBER; }
%%

And here is the flex2.tab.h content where I notice that it sais typedef YYSTYPE somewhere but from what I've read the union is managed by yylval.

#ifndef YYTOKENTYPE
# define YYTOKENTYPE
   /* Put the tokens into the symbol table, so that GDB and other debuggers
      know about them.  */
   enum yytokentype {
     NUMBER = 258,
     PLUS = 259,
     Q_MARK = 260
   };
#endif



#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{

/* Line 1676 of yacc.c  */
#line 14 ".\\flex2.y"

 double val;
 char sym;



/* Line 1676 of yacc.c  */
#line 64 "flex2.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif

extern YYSTYPE flex2lval;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
captain monk
  • 719
  • 4
  • 11
  • 34
  • What options are you giving to bison? It seems like you are specifying `-p flex2` (or `--name-prefix flex2`). – rici Feb 19 '14 at 23:30
  • Hello, where can i check that?(maybe properties on the bison file?) – captain monk Feb 19 '14 at 23:35
  • Bison Parser->Command line-> there is only this line in there bison.exe -b flex2 -d -p flex2 – captain monk Feb 19 '14 at 23:37
  • Like I said, you're specifying `-p flex2`. Why are you doing that? – rici Feb 19 '14 at 23:43
  • i've changed nothing at the visual studio options. I'm actually trying to solve an exercise from my university. There are guidelines on how to do it. They say nothing about messing with the specific option so i hasn't change it. I have to change something so that it can work?As you understand i have no idea what -p means :/ i am only used to write code. – captain monk Feb 19 '14 at 23:48
  • If you specify `-p flex2`, then bison will rename all variables whose names start with `yy` to start with `flex2`, so that `yylval` will be renamed to `flex2lval`. If that's what you want, you need to use the correct name. Search for name-prefix in this page in the bison manual: https://www.gnu.org/software/bison/manual/html_node/Decl-Summary.html – rici Feb 19 '14 at 23:53
  • It's just that? i just have to remove the -p flex2 part from bison.exe -b flex2 -d -p flex2 and it will work? Thnx so much..i'm searching for it for more than 3 hours and i couldn't continue without it. Honestly thnx! – captain monk Feb 19 '14 at 23:56
  • It seems that i can't delete any of this line..i can only add more. Any ideas? – captain monk Feb 19 '14 at 23:59
  • Then you'll have to use the name `flex2lval` instead of `yylval` (and the other renamings indicated in the bison manual). Seems strange to me. If I were you, I'd ask my instructor or lab assistant why it's set up that way. – rici Feb 20 '14 at 00:00
  • Also since that happens why yytext works and i don't have to name it flex2texxt? – captain monk Feb 20 '14 at 00:01
  • Anyway thank you again i can move on at last! :) – captain monk Feb 20 '14 at 00:02
  • Did you read the documentation I pointed you to? The sentence starting "The precise list of names..."? – rici Feb 20 '14 at 00:06
  • Just read it actually..and i understand it now. But what i don't understand actually is how i could understand from this that -p name does it, since it sais %name-prefix – captain monk Feb 20 '14 at 00:12
  • https://www.gnu.org/software/bison/manual/html_node/Bison-Options.html Look for `-p prefix`. If you have three hours to spare, you'd be better off using them to read the manuals for the tools you want to use, than doing random internet searches. IMHO. :) – rici Feb 20 '14 at 00:15

0 Answers0