在loop/switch语句之外的break语句是否是语法错误? [英] Is a break statement outside of loop/switch statement a syntax error?

查看:104
本文介绍了在loop/switch语句之外的break语句是否是语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

break语句通常在循环或switch case语句中使用.如果有人在这些陈述之外使用它,那将构成什么样的错误?
这是句法还是语义错误?

感谢您的时间!

The break statement is usually used inside loops or switch case statements. If one used it outside of these statements what kind of error would this constitute?
Would that be a syntactic or rather a semantic error?

Thanks for your time!

推荐答案

我花了一段时间检查一下,因为我首先必须找到C#的语法文件,但这绝对是一个语义错误.语法指定什么是语言中的有效构造.如果书面程序遵循语法,那么它在语法上是正确的.

我只会发布语法的相关部分,以阐明为什么C#程序中的任意位置中断在语法上是正确的,但在语义上是错误的,即中断,所以在这里中断是没有意义的..

C#语法文件的摘录:
I took me a while to check since I first had to find a grammar file for C#, but it is definitely a semantic error. The grammar specifies what are valid constructs in a language. If the written program follows the grammar then it is syntactically correct.

I''ll only post the relevant parts of the grammar that will make it clear why a break anywhere in a C# program is syntactically correct, but is semantically wrong here in the sense of "There is nothing to break out of, so it does not make sense to break here.".

Excerpt of C# grammar file:
block:
	';'
	| '{'   statement_list?   '}';

statement_list:
	statement+ ;

statement:
    (declaration_statement) => declaration_statement
    | (identifier   ':') => labeled_statement
    | embedded_statement
    ;

embedded_statement:
    block
    | selection_statement   // if, switch
    | iteration_statement   // while, do, for, foreach
    | jump_statement        // break, continue, goto, return, throw
    | try_statement
    | checked_statement
    | unchecked_statement
    | lock_statement
    | using_statement
    | yield_statement
    | unsafe_statement
    | fixed_statement
    | expression_statement  // expression!
    ;

jump_statement:
	break_statement
	| continue_statement
	| goto_statement
	| return_statement
	| throw_statement ;

break_statement:
	'break'   ';' ;



链是:



The chain is:

block -> statement_list -> statment -> embedded_statement -> jump_statement -> break_statement -> "break"



从语法文件中,我们可以推断在块内的任何地方都可以使用break语句.因此,具有不在循环构造或switch语句中的break语句的程序在语法上仍然是正确的.编译器具有足够的内在智能,尽管可以看到上述语句中未包含的break语句没有任何意义,并且会引发编译时错误.

该错误是语义错误,因为该语法允许break语句出现在块({})内的任何位置.

希望我对它的解释足够好,如果您仍有问题,请给我留言.

[更新]
忘记了在哪里可以找到所说语法文件的链接.在此站点上可以下载zip文件: http://antlrcsharp.codeplex.com/ [



From the grammar file we can infer that the break statement would be allowed anywhere inside a block. Thus programs that have a break statement that is not within a looping construct or a switch statement are still syntactically correct. The compiler has enough cleverness built in though to see that the break statement which is not enclosed in the aforementioned statements does not make any sense and throws a compile time error.

The error is a semantic error because the grammar allows for break statements to appear anywhere inside a block ({})

I hope I explained it well enough, if you still have questions leave me a comment.

[Update]
Forgot the link where to find said grammar file. On this site there is a download of a zip file: http://antlrcsharp.codeplex.com/[^]. In the folder UnitTest there is a file called cs.g which contains the complete grammar.
[/Update]

Regards,

—MRB


将其置于易中断循环之外是语法错误.这不是语义错误,因为逻辑不是不正确的,而只是没有意义,即语法错误.

语义错误是不正确的逻辑,但流程正确.例如,我可以拥有

It is a syntax error to have it outside of a breakable loop. It is not a semantic error because the logic is not incorrect, but simply makes no sence, i.e. syntax error.

Semantic errors are incorrect logic but correct flow. For example, I can have

bool flag = true;
bool otherFlag = false;
...
while(flag = otherFlag)
{
   ...
}



这可能"是语义错误.检查flag的逻辑是正确的.将flag设置为otherFlag的附加效果可能是语义错误(也许是开发人员想要的).

但是,如果有:



This ''could'' be a semantic error. The logic of checking the flag is correct. The added effect of setting the flag to otherFlag could be the semantic error (maybe what the developer wanted though).

If however one had:

int flag = 0;
int otherFlag = 1;
...
while(flag = otherFlag)
{
   ...
}



现在,您遇到语法错误,因为它不会将while表示为整数.就像它不会从不存在的循环中引起break一样.



You now have a syntax error, for it does not make sence to while on an integer. Just as it does not make sence to break from a non-existing loop.


您需要创建支撑组*,以便循环操作将不同的上下文推入上下文堆栈(就像命名空间,类和方法的支撑组各自推送不同类型的上下文,并且内部具有不同的有效操作).循环上下文遵循与普通代码上下文相同的规则,除了在循环上下文(包括子上下文)内,break和continue在语法上是有效的.

*-包括仅是一条语句的隐式创建的组,例如
You need to make braced groups* for loop operations push a different context onto the context stack (just as the braced groups for namespaces, classes and methods each push a different type of context and have different valid operations inside them). The loop context follows the same rules as a normal code context, except that within a loop context (including child contexts) break and continue are syntactically valid.

* - including implicitly created groups that are just one statement, e.g.
foreach(var thing in list) if(thing.name == targetname) break;


这篇关于在loop/switch语句之外的break语句是否是语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆