简体   繁体   English

数字flex&bison

[英]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. 我正在编写我的第一个flex and bison程序,这是一个简单的计算器,我希望计算器能够支持复数运算。

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. 但是,我无法插入甚至处理其中的任何复杂数字,因为我需要flex以某种方式同时处理复数的实际值和图像值并将其传递给野牛,而且我也不知道野牛如何处理2个参数。

This is my flex file: 这是我的flex文件:

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. 只是为了记录,复杂的数字看起来像1.000 + 2.111i,3i等。

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)". 如果你想要对你想象的数字进行算术运算,你可能想要创建一个结构,比如“img_t”,它有两个字段,比如,real和img,然后编写具有签名的函数,例如“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. 你的yyunion应该有一个字段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. 关键是你需要一个包含虚数的yyunion结构。

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? 如果你在lex中看到一个整数或两个整数,你想把它变成一个虚数吗? 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. 如果是这种情况,那么你可以取消使用expr_int和expr_double并让你所有的lex标记返回img_t。

If you post some of the expressions you want to parse and evaluate I can make further comments. 如果你发布一些你要解析和评估的表达式,我可以做进一步的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM