473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error handling - how to get rid of multiple flags?

In the following code fragment, I only want to print an error
message when an invalid symbol follows a valid symbol. My
'error_found' and 'error_this_case' handles that, but it seems a
little convoluted. Is there a clearer way?

Symbol get_sym(void);
Symbol sym;

void stmt_seq(void) {
int error_found = 0;

for (;;) {
int error_this_case = 0;
switch (sym) {
case ident_sym : ident_stmt(); break;
case while_sym : while_stmt(); break;
case if_sym: if_stmt(); break;

case endwhile_sym:
case else_sym:
case eof_sym:
return;

default:
if (!error_found)
error("expecting stmt");
error_this_case = 1;
get_sym();
}
if (error_this_case)
error_found = 1;
else
error_found = 0;
}
}
Nov 14 '05 #1
3 1057
"Ed Davis" <ed*******@yahoo.com> wrote in message
news:42**************************@posting.google.c om...
In the following code fragment, I only want to print an error
message when an invalid symbol follows a valid symbol. My
'error_found' and 'error_this_case' handles that, but it seems a
little convoluted. Is there a clearer way?


Read your own specification. If I get it right, you only need to know
that the previous symbol was valid. One flag. Simple.

This is not a C question BTW, it is about algorithms. Hence OT here.

Peter
Nov 14 '05 #2

"Ed Davis" <ed*******@yahoo.com> wrote in message

In the following code fragment, I only want to print an error
message when an invalid symbol follows a valid symbol.

What you want to do is write this function.

void handlerr(char *message)
{
static int flag = 0;
if(!flag)
printf("%s\n", message);
flag = 1;
}

and pass all your errors through it.
Nov 14 '05 #3
Malcolm wrote:

"Ed Davis" <ed*******@yahoo.com> wrote in message

In the following code fragment, I only want to print an error
message when an invalid symbol follows a valid symbol.

What you want to do is write this function.

void handlerr(char *message)
{
static int flag = 0;
if(!flag)
printf("%s\n", message);
flag = 1;
}

and pass all your errors through it.


You might want to make that parameter const, so that the caller feels a
little more confident about passing (the address of the first character in)
a string literal to the function.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
10
by: k.jayachandran | last post by:
I have a very curious and unique problem here. I'm creating a parser using bison and flex. i did all the development work in a linux environment. the project includes the source files output from...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
4
by: Jerry West | last post by:
I am new to VB .NET moving from VB6. I wrote the following code and as a result I received an error concerning it from the IDE. I don't understand why I get the message or why that what I am doing...
0
by: dhyder | last post by:
I'm working on an admin page for a SQL Server 05 db. The page is in ASP.NET 2.0/C#. The db has multiple tables with foreign keys/constraints. I have multiple SqlDataSources and GridViews, which...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.