检测 ml-lex 中的单词。 (正则表达式)

发布于 2025-01-03 21:06:13 字数 865 浏览 2 评论 0原文

我正在尝试用 ml-lex 编写一个程序来检测整数和实数。我的程序是这样的:

datatype lexresult = INTEGER of string | REAL of string | EOF
val linenum = ref 1;
val error = fn x => output(stdOut,x^"\n");
val eof = fn () => EOF;
fun inc(j) = j := !(j) + 1;
%%
%structure Something
num=[1-9];
zero=[0];
%%
\n => (inc linenum; lex());
^({num}+)({num}* | {zero}*)* => (INTEGER yytext);
^({num}+)({num}* | {zero}*)*(".")({zero} | ({zero}* | {num}+)) => (REAL yytext);
. => (error ("ignoring bad character "^yytext); lex());

但问题是它只检测从新行开始的整数和实数。我尝试在 start 中给出 (^ | " "+) 代替 ^ 但随后 ml-lex 给出错误 ml-lex: 语法错误,第 15 行: 未处理的异常:错误。我可以进行哪些更改来检测“我有 5 本书”之类的句子之间的整数和实数。并且程序应忽略所有字符并仅检测整数 5。

我还有一个问题。我想我已经为实数定义了正则表达式,就像它应该说,只有当数字在 . (点)或 0-9 的某个数字序列之后只有一个零时,某些东西才是真实的,但是不以零结尾。但我的程序也将 5.00 和 5.600 检测为实数。

I am trying to write a program in ml-lex which will detect integers and reals. My program is something like this:

datatype lexresult = INTEGER of string | REAL of string | EOF
val linenum = ref 1;
val error = fn x => output(stdOut,x^"\n");
val eof = fn () => EOF;
fun inc(j) = j := !(j) + 1;
%%
%structure Something
num=[1-9];
zero=[0];
%%
\n => (inc linenum; lex());
^({num}+)({num}* | {zero}*)* => (INTEGER yytext);
^({num}+)({num}* | {zero}*)*(".")({zero} | ({zero}* | {num}+)) => (REAL yytext);
. => (error ("ignoring bad character "^yytext); lex());

But the problem is that it detects only integers and reals starting in new line. I tried to give (^ | " "+) in start in place of ^ but then ml-lex gives error ml-lex: syntax error, line 15:
unhandled exception: Error
. What changes can I make to detect integers and reals in between the sentence like "I have 5 books." and the program should ignore all characters and should detect integer 5 only.

I also have one more problem. I think I have defined regular expression for real number something like that it should say that some thing is real only if number has only one zero after . (dot) or some sequence of number from 0-9 but doesn't end in zero. But my program also detecting 5.00 and 5.600 as real number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2025-01-10 21:06:13

我不了解 mllex,但我玩过一点 GNU Flex 和 ocamllex。我要做的就是添加这样的规则:

" " => (lex());

这样,空白就会被默默地跳过。如果您想跳过所有字母,可以在 . 规则中删除对 error 的调用。

我认为你的程序将 5.600 检测为实数没有问题,因为它有小数部分。要强制将 5.000 词法为整数,您可以在当前返回 REAL 的规则的 RHS 中执行其他测试。

I don't know mllex, but I've played a little with GNU Flex and ocamllex. What I would do is add a rule like this:

" " => (lex());

That way, whitespace is silently skipped. If you want to skip all letters, you can remove the call to error in your rule for ..

I see no problem with your program detecting 5.600 as a real number as it has a decimal component. To force 5.000 to be lexed as an integer, you can do additional tests in the RHS of your rule that currently returns REAL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文