10
while(cond) // fine
for(;cond;) //fine

しかし、条件付き部分を削除すると

while() //syntax compilation error 
for(;;) //Infinite loop

これらのループはどのように内部的に実装されていますか?whileまたは、コンパイラ(パーサー)は、の空の条件がエラーであり、forInfiniteであることをどのように認識しますか?

特にこれについては何も見つかりませんでした。Cの私のような人(初心者)も同じ混乱を抱えているのではないかと思います。

4

5 に答える 5

12

The standard requires that the omitted condition for for loop is replaced by a non-zero constant:

From C11 6.8.5.3: (emphasis mine)

6.8.5.3 The for statement

1 The statement for ( clause-1 ; expression-2 ; expression-3 ) statement behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.134)

2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

Since there's no such requirement for while loop (if the condition is omitted), I believe, it's left to the implementation of compiler.

于 2012-10-30T20:24:41.723 に答える
3

...コンパイラ (パーサー) は、while の空の条件がエラーであり、 for が Infinite であることをどのように認識しますか?

言語定義は、構文 (文法) とセマンティクスの両方でそれを指定しているためです。

whileループの構文は次のとおりです。

while ( expression ) statement

forループの構文は次のとおりです( C2011の時点):

for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement

ステートメント内のeachの添字optは、対応する式がオプションであることを示します。これは、テキストによって強化されます。expressionoptfor

6.8.5.3 for ステートメント

...
2文節 1式 3は省略できます。省略された式-2は、ゼロ以外の定数に置き換えられます。

対照的に、whileステートメントの制御式はオプションとしてマークされておらず、テキストでも強化されています。

6.8.5.1 while ステートメント

1 制御式の評価は、ループ本体の各実行前に行われます。

制御式が省略される可能性があると解釈する余地はあまりありません。

于 2012-10-30T21:44:59.290 に答える
3

一方が機能し、他方が機能しない理由を説明する技術的な理由はありません。これは、言語設計者の人的要因に関する考慮事項です。for (;;)彼らは、 を使用した無限ループの方が より理にかなっていると感じましたwhile ()。彼らは、穴居人が使用する言語である ALGOL の影響を受けている可能性があります。

于 2012-10-30T20:11:31.963 に答える
2

プログラムの構文と意味の正確さを決定する条件は、言語文法にエンコードされます。言語文法は言語作成者によって作成され、Cの場合のように、言語のルックアンドフィールを決定します。私は背後にある基本的な直感を推測し、for(;;)無限ループを作成するには完全に十分ですがwhile(1)、の任意の部分をfor(;;)省略できます。たった1つの狭いケースのハッキーなコーナーケースになります。while(1)while()

于 2012-10-30T20:17:33.600 に答える
2

これは C 構文の一部です。すべてのプログラミング言語には、正式な文法仕様があります (ここに BNF の C の正式な文法があります。つまり、構文的に正しいものとそうでないものがあります。C の正式な文法では、whileは次のように見える必要があることがわかります。

'while' '(' exp ')' stat

一重引用符 (終端記号) 内の単語/記号は必須です: 'while'、'(' および ')'。引用符で囲まれていない単語 (非終端記号) は、形式文法でも指定されているものです。C の形式文法を分析すると、exp が何もないことがわかります。一方、forを見ると、次のようになります。

'for' '(' exp ';' exp ';' exp ')' stat

| | 'for' '(' exp ';' exp ';' ')' stat

| | 'for' '(' exp ';' ';' exp ')' stat

| | 'for' '(' exp ';' ';' ')'stat

| | 'for' '(' ';' exp ';' exp ')' stat

| | 'for' '(' ';' exp ';' ')'stat

| | 'for' '(' ';' ';' exp ')' stat

| | 'for' '(' ';' ';' ')' stat

( | は OR を意味します)。

プログラムをコンパイルすると、字句解析器 (コンパイラの一部) がコードが構文的に正しいかどうか (つまり、形式的な文法に準拠しているかどうか) をチェックし、ソース コードに応じて他のアクションを実行します。

于 2012-10-30T20:47:44.513 に答える