从 lex 解析器确定十六进制值
Determining a hex value from a lex parser
我目前正在尝试解析一个文本文件并在其中查找任何十六进制数字。如果十六进制数不正确,我将其显示为非数字。
input:
-0xA98F
0XA98H
0x123
0xabc
expected output:
-0xA98F valid
0x123 valid
0xabc not valid
0xA98H not valid
我的问题是,如果我得到类似 0xA98H 的内容,它将输出为 0xA98 并显示为数字。我的目标是让我的输出像我的例子一样,但是我没有看到任何解决我的问题的方法。
[-]?[0][x|X][0-9A-F]+ {cout << yytext << " Number" << endl; }
以下示例代码根据 OP 的要求接受十六进制数:
%{
#include <iostream>
#include <string>
using namespace std;
static bool error = false;
static string buffer;
%}
HEX "-"?"0"[xX][0-9A-F]+
EOL (\n|\r|\r\n)
%%
{HEX} { buffer += yytext; }
" " { /* ignore spaces */ }
. { buffer += yytext; error = true; }
{EOL}+ {
cout << buffer << '\t' << (error ? "not valid" : "valid") << endl;
buffer.clear();
error = false;
}
%%
int main(int argc, char **argv) { return yylex(); }
int yywrap() { return 1; }
使用 flex 和 g++ 编译并在 cygwin 上测试:
$ flex -otest-hex.cc test-hex.l ; g++ -o test-hex test-hex.cc
$ echo '-0xA98F
> 0XA98H
> 0x123
> 0xabc
>' | ./test-hex
-0xA98F valid
0XA98H not valid
0x123 valid
0xabc not valid
$
忽略空格和空行。
(\n|\r|\r\n)
表示匹配类 Unix 行尾、类 MacOS 行尾和 DOS/Windows-like 行尾(按此顺序)的模式。
我目前正在尝试解析一个文本文件并在其中查找任何十六进制数字。如果十六进制数不正确,我将其显示为非数字。
input:
-0xA98F
0XA98H
0x123
0xabc
expected output:
-0xA98F valid
0x123 valid
0xabc not valid
0xA98H not valid
我的问题是,如果我得到类似 0xA98H 的内容,它将输出为 0xA98 并显示为数字。我的目标是让我的输出像我的例子一样,但是我没有看到任何解决我的问题的方法。
[-]?[0][x|X][0-9A-F]+ {cout << yytext << " Number" << endl; }
以下示例代码根据 OP 的要求接受十六进制数:
%{
#include <iostream>
#include <string>
using namespace std;
static bool error = false;
static string buffer;
%}
HEX "-"?"0"[xX][0-9A-F]+
EOL (\n|\r|\r\n)
%%
{HEX} { buffer += yytext; }
" " { /* ignore spaces */ }
. { buffer += yytext; error = true; }
{EOL}+ {
cout << buffer << '\t' << (error ? "not valid" : "valid") << endl;
buffer.clear();
error = false;
}
%%
int main(int argc, char **argv) { return yylex(); }
int yywrap() { return 1; }
使用 flex 和 g++ 编译并在 cygwin 上测试:
$ flex -otest-hex.cc test-hex.l ; g++ -o test-hex test-hex.cc
$ echo '-0xA98F
> 0XA98H
> 0x123
> 0xabc
>' | ./test-hex
-0xA98F valid
0XA98H not valid
0x123 valid
0xabc not valid
$
忽略空格和空行。
(\n|\r|\r\n)
表示匹配类 Unix 行尾、类 MacOS 行尾和 DOS/Windows-like 行尾(按此顺序)的模式。