how to get bison to bail out for all errors - parsing

I am using Flex/Bison for a script parser which needs to break out of the parser and return a nonzero status from yyparse() for ALL ERRORS. Every bit of documentation for Bison I can find is about recovery -- how can I write a rule set which bails (i.e., with YYABORT) rather than trying to recover?
Thanks for your collective wisdom.

If you refer to semantic errors, i.e., checks that you are doing in the semantic part of the syntax rules, then you can just invoke the exit() function to immediately exit the parser's executable. The parameter to exit() is the error code to return to the shell environment.
You may also implement yyerror() that would invoke exit() if you wish (to exit on syntax errors). This is documented here.

If there is no applicable error rule, bison will not attempt to recover and will immediately return when a syntax error is detected. So unless you explicitly attempt error recovery, the bison parser will act as you wish.
If you do attempt error recovery, you can still invoke YYABORT in an action to cause yyparse to return.
If your parser is not behaving in this way, please post more details.
If you want to force an error from the scanner, just return a token value which is not used in any production. That is guaranteed to create an error in the parser because the token cannot be shifted.

Related

How can I suppress Extraction rule error in load test and make the test marked as pass?

In my web test, I have used a loop to redirect it to the previous request if it fails to get the expected response which includes a context parameter param01.
So, when I run a load test sometimes the loop 1 fails because it cannot find the Context parameter param01 and throws an extraction rule error, but loop2 becomes success because it found the context parameter and extraction rule is passed.
Now, I want to suppress the Extraction rule error which occurs in the failed loop (loop 1) and pass the test. Please help me on this.
Many extraction rules have a Required property, of boolean type. Set it to False so that the rule (and hence the test) will not fail when the expected data is not available.
Be aware that the extraction rule for Extract regular expression has a Required property but it appears to be handled wrongly in some Visual Studio versions . See this fault report. See also this discussion of the issue plus a work-around that calls the real extraction rule then writes true to the Success field.

Bisonc++ - Verbose reporting to return conflicting rules?

I have a massive problem with bisonc++'s compiler: The error messages are garbage. It's really hard for me to find the problem, when all it says is Line 63: 2 Reduce/Reduce conflict(s) (line 63 is the last line of the code). Is there a way, a switch or anything to make bisonc++ also return the rules that cause the conflict? Any verbose error reporting? Thank you!
According to the bisonc++ man pages, you can request a detailed output that gets written into a .output file with the -V switch.
--verbose (-V)
Writes a file containing verbose descriptions of the parser states and what is done for each type of look-ahead token in
that state. This file also describes all conflicts detected in the
grammar, both those resolved by operator precedence and those that
remain unresolved. It is not created by default, but if requested the
information is written on .output, where is the
grammar specification file passed to bisonc++

Flex input buffer reset after error

I'm using flex & bison to parse a custom language and I'm in the situation described here: http://www.gnu.org/software/bison/manual/html_node/How-Can-I-Reset-the-Parser.html.
To be more precise
I invoke yyparse several times, and on correct input it works
properly; but when a parse error is found, all the other calls fail
too. How can I reset the error flag of yyparse?
My parser and scanner run inside a separate thread, but there is only one thread working with the input file. In my understanding I don't need to write a reentrant scanner since there is only one thread working with the input file. In that page the problem is clearly explained but the solution is not clear to me.
It says:
Therefore, whenever you change yyin, you must tell the Lex-generated
scanner to discard its current buffer and switch to the new one. This
depends upon your implementation of Lex; see its documentation for
more. For Flex, it suffices to call ‘YY_FLUSH_BUFFER’ after each
change to yyin. If your Flex-generated scanner needs to read from
several input streams to handle features like include files, you might
consider using Flex functions like ‘yy_switch_to_buffer’ that
manipulate multiple input buffers
My parser thread calls yyparse in order to build my AST. What is not clear to me is when and where I have to call yy_flush_buffer to fix the problem. In my understanding the scanner code (generated by Flex) is called by the parser code (generated by Bison). The Bison generated code is generated by the grammar. As a result the parser code is not under my direct control. This means I cannot include the call to yy_flush_buffer into the parser code since it would be overwritten every time I generate the parser code by the grammar. It means that I should put the yy_flush_buffer in the grammr file somewhere. But where?
I fixed the problem by doing:
...
FILE *f = fopen(_filename, "r");
yyrestart(f);
yyparse();
...
I leave the question since it could be useful for other people.

Exceptions in LEX&YACC

I'm developing a lex/yacc c compiler.
In order to handle failures and parse errors, I want to deploy an exception system handler.
Actually only a "parse error" message is handled whatever the problem is.for example:
typedef struct , struct_name{...} this input will produce a parsing error because of the extra comma.
My purpose is to throw a contextual exception,giving us the possibility to focus exactly where the problem is.such as for this example :
"Invalid structure declaration "
I really need help to solve this problème.
This will go into your parser. As it runs, it gets tokens from the lexer. If the next token does not "fit" the current rule, then you have a problem. Luckily, there already exists a section for dealing with these situations! See bison error recovery for the gnu version of yacc and how to deal with this. It'll go through the concepts, and variables to deal with just the situation you have here.

What's the point of NSAssert, actually?

I have to ask this, because: The only thing I recognize is, that if the assertion fails, the app crashes. Is that the reason why to use NSAssert? Or what else is the benefit of it? And is it right to put an NSAssert just above any assumption I make in code, like a function that should never receive a -1 as param but may a -0.9 or -1.1?
Assert is to make sure a value is what its supposed to be. If an assertion fails that means something went wrong and so the app quits. One reason to use assert would be if you have some function that will not behave or will create very bad side effects if one of the parameters passed to it is not exactly some value (or a range of values) you can put an assert to make sure that value is what you expect it to be, and if it's not then something is really wrong, and so the app quits. Assert can be very useful for debugging/unit testing, and also when you provide frameworks to stop the users from doing "evil" things.
I can't really speak to NSAssert, but I imagine that it works similarly to C's assert().
assert() is used to enforce a semantic contract in your code. What does that mean, you ask?
Well, it's like you said: if you have a function that should never receive a -1, you can have assert() enforce that:
void gimme_positive_ints(int i) {
assert(i > 0);
}
And now you'll see something like this in the error log (or STDERR):
Assertion i > 0 failed: file example.c, line 2
So not only does it safe-guard against potentially bad inputs but it logs them in a useful, standard way.
Oh, and at least in C assert() was a macro, so you could redefine assert() as a no-op in your release code. I don't know if that's the case with NSAssert (or even assert() any more), but it was pretty useful to compile out those checks.
NSAssert gives you more than just crashing the app. It tells you the class, method, and the line where the assertion occurred. All the assertions can also be easily deactivated using NS_BLOCK_ASSERTIONS. Thus making it more suitable for debugging. On the other hand, throwing an NSException only crashes the app. It also does not tell about the location of the exception, nor can it be disabled so simply. See the difference in the images below.
The app crashes because an assertion also raises an exception, as the NSAssert documentation states:
When invoked, an assertion handler prints an error message that
includes the method and class names (or the function name). It then
raises an NSInternalInconsistencyException exception.
NSAssert:
NSException:
Apart from what everyone said above, the default behaviour of NSAssert() (unlike C’s assert()) is to throw an exception, which you can catch and handle. For instance, Xcode does this.
Just to clarify, as somebody mentioned but not fully explained, the reason for having and using asserts instead of just creating custom code (doing ifs and raising an exception for bad data, for instance) is that asserts SHOULD be disabled for production applications.
While developing and debugging, asserts are enabled for you to catch errors. The program will halt when an assert is evaluated as false.
But, when compiling for production, the compiler omits the assertion code and actually MAKE YOUR PROGRAM RUN FASTER. By then, hopefully, you have fixed all the bugs.
In case your program still has bugs while in production (when assertions are disabled and the program "skips over" the assertions), your program will probably end up crashing at some other point.
From NSAssert's help: "Assertions are disabled if the preprocessor macro NS_BLOCK_ASSERTIONS is defined."
So, just put the macro in your distribution target [only].
NSAssert (and its stdlib equivalent assert) are to detect programming errors during development. You should never have an assertion that fails in a production (released) application. So you might assert that you never pass a negative number to a method that requires a positive argument. If the assertion ever fails during testing, you have a bug. If, however, the value that's passed is entered by the user, you need to do proper validation of the input rather than relying on the assertion in production (you can set a #define for release builds that disables NSAssert*.
Assertions are commonly used to enforce the intended use of a particular method or piece of logic. Let's say you were writing a method which calculates the sum of two greater than zero integers. In order to make sure the method was always used as intended you would probably put an assert which tests that condition.
Short answer: They enforce that your code is used only as intended.
It's worthwhile to point out that aside from run time checking, assert programming is a important facility used when you design your code by contract.
More info on the subject of assertion and design by contract can be found below:
Assertion (software development)
Design by contract
Programming With Assertions
Design by Contract, by Example [Paperback]
To fully answer his question, the point of any type of assert is to aid debugging. It is more valuable to catch errors at their source, then to catch them in the debugger when they cause crashes.
For example, you may pass a value to a function expects values in a certain range. The function may store the value for later use, and on later use the application crashes. The call stack seen in this scenario would not show the source of the bad value. It's better to catch the bad value as it comes in to find out who's passing the bad value and why.
NSAssert make app crash when it match with the condition. If not match with the condition the next statements will execute. Look for the EX below:
I just create an app to test what is the task of NSAssert is:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self testingFunction:2];
}
-(void)testingFunction: (int)anNum{
// if anNum < 2 -> the app will crash
// and the NSLog statement will not execute
// that mean you cannot see the string: "This statement will execute when anNum < 2"
// into the log console window of Xcode
NSAssert(anNum >= 2, #"number you enter less than 2");
// If anNum >= 2 -> the app will not crash and the below
// statement will execute
NSLog(#"This statement will execute when anNum < 2");
}
into my code the app will not crash.And the test case is:
anNum >= 2 -> The app will not crash and you can see the log string:"This statement will execute when anNum < 2" into the outPut log console window
anNum < 2 -> The app will crash and you can not see the log string:"This statement will execute when anNum < 2"

Resources