Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted
Updated 26-Oct-11 5:36am
v2
Comments
Richard MacCutchan 26-Oct-11 11:23am    
bc i m
Please don't use this childish txtspk it makes it very difficult to understand your questions.
Manfred Rudolf Bihy 26-Oct-11 12:13pm    
Corrected! :)
Sweety Khan 26-Oct-11 12:15pm    
thanks :)
Sweety Khan 26-Oct-11 12:11pm    
ok sorry for that

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;
 
Share this answer
 
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"


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
 
Share this answer
 
v4
Comments
Nish Nishant 26-Oct-11 11:36am    
Excellent answer. 5!
Manfred Rudolf Bihy 26-Oct-11 12:10pm    
Thanks Nish!
Sweety Khan 26-Oct-11 12:19pm    
i got it. thanks million times :)
Manfred Rudolf Bihy 26-Oct-11 13:54pm    
You're welcome!
Espen Harlinn 26-Oct-11 15:01pm    
Well, 5'ed again :)
antlr is one of my favourite tools ...
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

C#
bool flag = true;
bool otherFlag = false;
...
while(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:
C#
int flag = 0;
int otherFlag = 1;
...
while(flag = otherFlag)
{
   ...
}


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.
 
Share this answer
 
v3
Comments
Manfred Rudolf Bihy 26-Oct-11 11:28am    
You missed the point. If the grammar doesn't allow it then it's a syntax error. If the grammar would allow for it, but it just doesn't make any sense it's a semantic error.

Since the C# grammar allows for break to appear anywhere inside a block, it can only be a semantic error.

Cheers!
[no name] 26-Oct-11 11:49am    
Your definition is one way to look at it (breaking down the grammer file).

However, a semantic error by most definitions is a run time error also known as a logic error. You can not at run time break out of a non existing loop. That is why the compiler generates a syntax error. It may not just use the grammer file to determine syntax errors, but it is a syntax error non the less.
Manfred Rudolf Bihy 26-Oct-11 12:23pm    
No, sorry it is a compiler error! A syntax error would be if you spelled "break" as "bread". If the grammar cannot continue because it enounters something that cannot be recognized then it is a syntax error. You can even envision a C# language specification where not even a compile time eror would be thrown:
What if a break statement in a function/method would break out of a loop if the method was called from within a loop/switch construct. OK I admit that this is a quite horrific scenario, but if you define it that way it still is valid. It is impossible to tell by statical analysis if a break is valid as this could only be discovered at runtime.
I do recognize that there is some confusion about what exactly is a syntactic error. Most compilers even throw an syntax error even if it is formally a grammatic error.
[no name] 26-Oct-11 12:58pm    
OK, your scenerio does create a case where it would be a run-time error so I stand corrected.

Have a 5 :)
Manfred Rudolf Bihy 26-Oct-11 13:11pm    
Thanks! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900