简体   繁体   English

flex 词法分析器中的定义 - 如何定义科学记数法

[英]Definitions in flex lexical analyzer - How to define scientific notation

I'm new to using flex and need to define real literal numbers, ie.我是使用 flex 的新手,需要定义实数,即。 3.1 or 3.0e-10 as acceptable numbers. 3.1 或 3.0e-10 作为可接受的数字。 Here is what I have so far:这是我到目前为止所拥有的:

digit       [0-9]
int     {digit}+
real_literal  ({digit}+)("."{digit}*)

From my understanding this works for decimals without accepting something like 12.52.23根据我的理解,这适用于小数而不接受像 12.52.23 这样的东西

How would I define numbers that accept scientific notation, such as 3.0e-10 like mentioned above?我将如何定义接受科学记数法的数字,例如上面提到的 3.0e-10?

Would it be something like this?会是这样吗?

real_literal  ({digit}+)("."{digit}*)|({digit}+)("."{digit})[Ee]["-""+"]{digit}+

Yay, for once I get to say RTFM !是的,这一次我可以说RTFM The flex manual has the very pattern you want in appendix A. flex 手册在附录 A 中有您想要的模式。

A possible solution, a bit shorter than yours, would be:一个可能的解决方案,比你的短一点,是:

{digit}+"."{digit}*([eE][-+]?{digit}+)?

Break down分解

  • {digit}+"."{digit} matches positive real numbers (it won't recognize integers). {digit}+"."{digit}匹配正实数(它不会识别整数)。
  • (...)? enclosing the next part in parenthesis followed by a question mark makes what's inside optional.将下一部分括在括号中,后跟一个问号,使里面的内容成为可选的。
  • [eE][-+]?{digit}+ match the exponent. [eE][-+]?{digit}+匹配指数。 Note that the - is not escaped within squared brackets in this case because it's the first or last character of the list, as @rici pointed out.请注意,在这种情况下-不会在方括号内转义,因为它是列表的第一个或最后一个字符,正如@rici 指出的那样。

I just want to point out that you're not recognizing negative numbers here, but I don't know if that's intentional.我只是想指出您在这里没有识别负数,但我不知道这是不是故意的。

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

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