numbers flex & bison
I'm writing my first flex and bison program which is a simple calculator, and I'd like the calculator to support complex number operations.
But, I cannot manage to insert or even handle any complex numbers in it, since I need the flex to somehow handle both real value and image value of the complex number in the same time and pass it to the bison, and also I dont know how the bison could handle 2 parameters.
This is my flex file:
As you can see, I tried something but this doesnt even compile (with all the complex number procedure). And just for the record, a complex number would look like 1.000+2.111i, 3i, etc.
If you are going to want to perform arithmetic computations on your imaginary numbers you probably want to create a struct say, "img_t" which has two fields, say, real and img and then write functions that have signatures like "img_t img_add(img_t a, img_t b)". Then your bison productions will call these functions for each operator. Your yyunion should have a field img_t img. You could also do these functions inlne in the bison if you wanted. The key point is that you need a struct in your yyunion that holds an imaginary number.
typedef struct {
double real, img;
} img_t;
%yyunion {
:
:
img_t img;
};
:
:
expr_complex: expr_complex '+' expr_complex { $$ = img_add($1, $3); }
:
:
If you see an integer or double alone in the lex, do you want to turn that into an imaginary number? If that's the case, then you can do away with expr_int and expr_double and have all your lex tokens return img_t.
If you post some of the expressions you want to parse and evaluate I can make further comments.
链接地址: http://www.djcxy.com/p/41740.html上一篇: Flex / Bison没有正确评估
下一篇: 数字灵活和野牛