Ale*_*ren 1 regex compiler-construction lex bison
我正在使用Lex和Yacc构建计算器编译器.该想法基于以下资源:http://epaperpress.com/lexandyacc/index.html.
对于给定的输入文件,我需要识别所有注释:
//.TEST -- JWJ
//.Step final -- testing all requirements
//.source: test-1m.cal
//.expected output: test-1m_expected.out
/**
* This program will use Newton's method to estimate the roots of
This should be a comment as well, but does not get picked up
* f(x) = x^3 - 3*x
*/
float xn;
float xo;
// int num_iterations;
xo = 3.0;
xn = 3.0;
num_iterations = 1;
/* A do-while loop */
do {
print xo;
xo = xn;
xn = xo - ( xo * xo * xo - 3.0 * xo ) / ( 3.0 * xo * xo - 3.0);
num_iterations = num_iterations + 1;
} while ( num_iterations <= 6 )
print xn; // The root found using Newton's method.
print (xo * xo * xo - 3.0 * xo ); // Print f(xn), which should be 0.
Run Code Online (Sandbox Code Playgroud)
我在我的lex文件中使用以下正则表达式:
"//"[^\n]*|"\/\*".*"\*\/"
"\/\*"([^\n])*
(.)*"\*\/"
Run Code Online (Sandbox Code Playgroud)
我不明白为什么多行评论不匹配?有人可以提供一些见解吗?
.flex中的字符匹配除换行符之外的任何字符(因此它与之相同[^\n]).因此,您的正则表达式都不会匹配包含换行符的任何评论.
C风格评论的常用正则表达式是:
"/*"([^*]|\*+[^*/])*\*+"/"
Run Code Online (Sandbox Code Playgroud)
这与评论标记内的0或更多"除*之外的任何内容"或"1或更多*s后面没有*或/"匹配.
| 归档时间: |
|
| 查看次数: |
4722 次 |
| 最近记录: |