检测 ml-lex 中的单词。 (正则表达式)
我正在尝试用 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:
. 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.
unhandled exception: Error
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不了解 mllex,但我玩过一点 GNU Flex 和 ocamllex。我要做的就是添加这样的规则:
这样,空白就会被默默地跳过。如果您想跳过所有字母,可以在
.
规则中删除对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:
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
.