1

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.

lex ファイルで次の正規表現を使用しています。

"//"[^\n]*|"\/\*".*"\*\/"
"\/\*"([^\n])*  
(.)*"\*\/"  

複数行のコメントが一致しない理由がわかりません。誰かが洞察を提供してもらえますか?

4

2 に答える 2

6

.flexの文字は、改行を除く任意の文字に一致します (したがって、 と同じ[^\n]です)。その結果、改行を含むコメントと一致する正規表現はありません。

C スタイルのコメントの通常の正規表現は次のとおりです。

"/*"([^*]|\*+[^*/])*\*+"/"

これは、コメント マーカー内の 0 個以上の「* 以外のすべて」または「1 つ以上の * の後に * または / が続かないもの」に一致します。

于 2012-11-26T18:24:41.230 に答える