Hi all,
I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?
In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }
Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?
Cheers,
Paulo Matos 3 9325 po******@gmail. com wrote: I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits [0-9]+ and keep them in an int, however if the number is bigger than int, I should output error. What's the best way to check this?
In the flex side I have: {numeral} { yylval.num = strtoul(yytext, NULL, 0); return NUMERAL; }
"flex" is off-topic, so without knowing what 'yylval' is or how its member
'num' is declared, it's hard to say. However, since you're using the C
function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
long'. If it isn't, you need to supply more information.
Is there a straightforward way to check if the number in yytext is bigger than INT_MAX?
It is implementation-specific, most likely, but check 'yylval.num' against
(unsigned long)INT_MAX
instead. Whether it buys you anything or not, is for you to decide. You
could also try to convert 'yytext' to an implementation-specific integral
type that is known to accommodate a larger range than 'int' and check its
value against (that_type)INT_ MAX. I here used 'unsigned long' as that
type, and your system may have something better.
V
Victor Bazarov wrote: po******@gmail. com wrote: I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits [0-9]+ and keep them in an int, however if the number is bigger than int, I should output error. What's the best way to check this?
In the flex side I have: {numeral} { yylval.num = strtoul(yytext, NULL, 0); return NUMERAL; }
"flex" is off-topic, so without knowing what 'yylval' is or how its member 'num' is declared, it's hard to say. However, since you're using the C function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned long'. If it isn't, you need to supply more information.
Is there a straightforward way to check if the number in yytext is bigger than INT_MAX?
It is implementation-specific, most likely, but check 'yylval.num' against
(unsigned long)INT_MAX
You might consider the "more C++y" static_cast<uns igned
long>(std::nume ric_limits<int> ::max()) po******@gmail. com wrote: Hi all,
I'm doing parsing with flex and bison and I read numbers which are just a sequence of digits [0-9]+ and keep them in an int, however if the number is bigger than int, I should output error. What's the best way to check this?
Easy: Strip the leading '0' from the string, get INT_MAX as a string
without leading zeroes. Compare lengths. If AsString(INT_MA X)
is longer it will fit. If AsString(INT_MA X) is shorter, it won't. If
the
lengths are equal, you can do a string compare and it will fit if
AsString(INT_MA X) < digit_sequence_ without_leading _zeroes.
HTH,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: don |
last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I
need a routine for figuring out if a number inputted by the user is a prime
number or not...... all I'm asking for is Not the exact code ( well maybe a
little) but the logic or algorithm to tell whether or not a number is
prime....
|
by: Jim Cook |
last post by:
We have a macro which takes various index constants as an argument and
offsets into an array. The macro can be an Lvalue or Rvalue. The index
is not zero based. I would like a compile time error displayed if the
index is out of bounds. Is there a way to do this well?
I read through the FAQ that I could find mentioned, and did not see this...
|
by: vikky |
last post by:
hi all,
Out of sheer curosity, I decided to initialize an integer with a number
bigger than INT_MAX, however I still am not able to justify its output.
Here is the program :
#include<stdio.h>
int main(void)
{
int t=0xFFFFFFFFFFFFFFFFDABC;
|
by: Marlene Stebbins |
last post by:
I am entering numbers into my program from the command line. I want to
check whether they are > INT_MAX. Sounds simple, but I've discovered that
if(x <= INT_MAX)
{
/* use x in some calculation */
}
else
{
/* exit with error message */
|
by: angus |
last post by:
hello,
i'm wondering how i can check if an picturebox has any content(as in got an
image).
thanks in advanced.
angus
| |
by: jacob navia |
last post by:
Many compilers check printf for errors, lcc-win32 too. But there are
other functions that would be worth to check, specially memset.
Memset is used mainly to clear a memory zone, receiving a pointer to
the start, the value (most of the time zero) and the size of the
memory array to clear.
Problems appear when the size given is not the...
|
by: LeAnne |
last post by:
If the user types some text into a rich textbox, i need to know how many
lines there are in the control.
Note, I'm not interested in counting the number of the carriage returns or
line feeds.
Suppose, the rich text is the height of a textbox (can view only one line at
a time), then if the user keeps typing and word wrap = TRUE and multi...
|
by: Patient Guy |
last post by:
Does anyone have any coding rules they follow when doing argument checking?
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw exceptions?
|
by: halcyon943 |
last post by:
have 4 folders that I watch and need to move files from to another
location. Three constraints:
-Finish time. Make sure the program stops transferring files at a
specific time
-Number of files transferred. Can only move a certain amount of files
per time period.
-Folders have a priority. The files have to be moved based on the
folder...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |