Jump to content

flex/bison passing variable

- - - - -

  • Please log in to reply
6 replies to this topic

#1
babno

babno

    Newbie

  • Members
  • Pip
  • 6 posts
I'm writing a flex/bison program that involves asking the user for a multiple of things, one of which would be an int which would indicate a spot in an array to acsess. I am able to recognize when its an int, but I need the specific value that is entered put into a variable so I can use it. I tried $$, $1, and so forth but it says its undecalred variable. is there something I need to include for them?

my code to recognize and int
[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+(\.[0-9]+)?)?

#2
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
double value;
%%
[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+(\.[0-9]+)?)? {
// write you code to convert yytext to its double represention here
value = to_double( yytext ); // yytext contains the text of the float value
// that has been recognized. to_double is to be define by you
// and suppose you have a double value in global scope.

}

%%

#3
babno

babno

    Newbie

  • Members
  • Pip
  • 6 posts
Undefined symbols:
"_to_double", referenced from:
_yylex in ccGRo4nZ.o
ID: symbol(s) not found
collect2: Id returned 1 exit status


if it would help I could put the array acsess at the end and just give a kill at the end of my other conditions and I would then be able to return the value itself rather than the DECNUM I currently use to identify the course of action to be taken

#4
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Sorry, probably I should be more specific.

The string you specific and thus recognized by the parser is pointed by yytext as a NULL TERMINATED ASCII string. (C-string) You can printed out like:

double value;
%%
[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+(\.[0-9]+)?)? {
                  // write your code to convert yytext to its double represention  here
         //         value = to_double( yytext ); // yytext contains the text of the  float value 
                                                  // that has been recognized.  to_double is to be define by you
                                                // and suppose you have a double  value in global scope.

       printf("A float point number is recognized: %s\n", yytext);
}


But if you want to make use of the value of the input float point number, you need to convert it to a float or double. You should have noticed that in the comment I wrote "write your code to ....", that's something to be done by you, or maybe the libary function 'atof' might be of some help to you.

#5
babno

babno

    Newbie

  • Members
  • Pip
  • 6 posts
double value;
%%
[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+(\.[0-9]+)?)? {
         value = to_double( yytext ); 
       return DECNUM;
}
yes, this is exactly what I wrote (DECNUM indicates I got a number and I should deal with as such, not all that nessesary), and I got the error

Undefined symbols:
"_to_double", referenced from:
_yylex in ccGRo4nZ.o
ID: symbol(s) not found
collect2: Id returned 1 exit status

what I believe that means is that to_double needs to be included at the top in some library, so if you know what library that is would be helpful, or some other way to deal with the problem. I need that specific value (and subsequent values) that the use entered (say 3) placed in a value such that I can say array[value] and have it give me the value in array[3] or whatever the user entered.

#6
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
There is no such library function as to_double, you need to implemented it yourself!

Or change it for atof, see if it works for you.

#7
babno

babno

    Newbie

  • Members
  • Pip
  • 6 posts
thank you kindly. sorry I didn't understand about to_double. used atof and worked fine.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users