How to approach reporting errors in an interpreter? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
For learning purposes I'm writing an interpreter of arithmetic equations in C++. When the user enters incorrect input, I'd want something like this to appear: (> is user input)
> 4+2*6+#-1
Lexer error: incorrect character '#' at position 7
What is the best way to approach that? Should I use exceptions, and if yes then should I create my own ones or just use something like std::runtime_error?

The trick is to maintain source location data all the way, through all the compilation passes. This way you can report
syntax errors, which are the most trivial, you have all the source location data handy at this stage, along with (depending on a parsing technique you're using) hints of what you'd rather expect at this point instead. A lot of such reporting can be inferred automatically with very little hinting.
semantic errors during compilation: for these you need to keep source locations for your IR nodes. E.g., for a binary arithmetic operation you'll have the location of the operator itself and sub-nodes will be able to show you locations of left and right side. There's no limit to how much information you can carry along here. For example, for any fully resolved identifier you may want to attach a location where it was declared, along with all the locations where it was referenced so far.
runtime errors: these are tricky, you don't have your rich IR at this stage, but you can emit debugging data in, for example, DWARF format, and unroll it back to your source locations when needed.
Runtime errors should be handled by your system debugger, so, no exceptions here. Semantic errors and syntax errors are, in most cases, recoverable, and you may want to display more than one, so, again, no reason to ever use exceptions.

Related

Reduce the size of Flash memory embedded cpp [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
After a lot of research i could not find any solution to my question (if i did i woudln't be here ...)
I'm looking for solutions that permits me to reduce the flash memory used by my program.
I'm programming an embedded c++ programm and when i Flash my electronic card in release mode everything is fine cause it doesn't overflow the space of the flash memory, but that is not the case when i do it in Debug mode... I want to know if it is possible to find functions (my goal is to do it without reducing the code) that could reduce Flash memory.I already thought about defragmentation but I don't find how to do it in embedded even though i don't even know if i can ... I also tried the -Os cmd from gcc but without any big success
So I'm taking any advices or support and i'll be there at any question about my issue ;)
Thanks !
Look at your map file. Is there something there you don't
expect? Functions you aren't expecting (like floating point, or
exception handling, etc.) or something unreasonably large?
Turn on optimization except for the file you're interested in.
Make sure you've actually got optimizations turned on (look at the build log and ensure that you've got -Os being passed to each compile step)
Consider link time optimizations, but don't expect miracles
Welcome to embedded programming. 90% of the job is figuring out how to stuff the never ending requirements into the memory available. Rinse and repeat.

Is my company doing this right, sharing data between exes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
First off, my company is into power grid, not IT, so software is kinda a secondary role here.
I work on a power system simulation software, very old code base in C++ with MFC, like 15 years old. What we do is take large amounts of data, ~100,000 floating point values then format and write to a text file (most of the code actually uses the C FILE structure to do this). Then, it's read by a separate engine exe which computes the electrical algorithm (Electrical algorithms are mostly numeric solutions of system of diffn equations) and then writes back huge amount of data to another text file, which we read and update the UI.
My question is, is this how it should be done? It there a way to skip writing into the text file and directly pass the data to the exes?
exes are called using CreateProcess() MFC function.
EDIT::
Sorry, site won't let me comment.
#Vlad Feinstein Well, yes, it's like a Ladder. A thing called load flow solves power flow through the lines, which in turn will be used to find stability of the systems, which in turn for overvoltage ect. It's huge, the UI is million+ lines of code, engine exes another million maybe.
Doesn't MFC already implement IPC using Dynamic Data Exchange? I can pass strings to another process's PreTranslateMessage() func. A scaled up version of that?
There is no such a thing as "should be done as ..." there are multiple methods to do IPC and while the method you describe might not be the fastest, it is a viable solution nevertheless. If the performance doesn't bother you in this particular case you should not bother with modifying it. It is exactly the case where the phrase "if it ain't broke, don't fix it" applies.
Probably, you would not want to make any new IPC in the application that way, though.

Preferred method of error handling method [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a file based hash table thing that can run into various filesystem or user errors. Initially I created all the data functions as bools like
bool add(int key, int value)
bool get(int key, int &value)
and so on where the input/output would go through the parameters and the success/fail would come as the function result.
Then I had some mmf wrapper classes I needed to fool-proof for the same project and I realized that they can fail at the constructor in which case returning a bool is not really an option so I added a bunch of
if (!somethingthatindicatesfail) throw std::exception("description here");
to them.
So now i have something that throws exceptions inside something that returns a bool and then there is system error codes that I also need to include in the error log.
Its a mess.. I'm going to rewrite all of the fail scenario logic but before I do, what is your preferred error handling/conveying method?
The end result I'm imagining is a module that doesn't crash but logs the errors, prevents further damage to the data and advises the user to shut it down.
As you already pointed out, you can't use return codes from constructors. So, if you want a single method that works for all the code, your only real choice is exception handling.
Note, however, that in some cases, it's preferable to just abort instead. In particular, exceptions will attempt to unwind the stack, but if the situation is dire enough, it's possible that could cause further damage, and aborting (existing without unwinding the stack) is a better option.
For that case, it can make sense to have (for example) a separate watch-dog that logs the problem and re-starts the program when/if it crashes. Being a separate process, it can continue and execute reasonably even if the program itself is bollixed up to the point that its only reasonable choice is to abort.

Best way to describe C++ program using charts [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm currently writing a developper documentation of a program I just wrote and which has to be continued by somebody else.
I'm documenting my code using Doxygen, but I'd like to show a more global view, using for instance flow charts filled with the name of the functions and the parameters they send to each other. But I also have to represent events, and it seems to me that flow charts are not the best for that, or a least I don't really see how to do it right and clear.
So Am I right? What would you need to catch on with a development of an already started application? Is there a better alternative than flow charts and UML?
From a general familiarisation perspective, I like seeing clear and detailed requirements specifications. This is lacking in the majority of cases in my experience.
From a design perspective, I like seeing state diagrams for state transitions, UML diagrams for general code structure and relationships and communication diagrams for information about data flows.
The more documentation you can give, the better it'll be for the next guy. But bear in mind that documentation can go out-of-date very quickly, so your (good) code comments and doxygen output are likely (but not necessarily so) to be more up-to-date than anything else you write.
As someone who picked up a 35,000 line Java project with 3 comments 2 of which were "this is gross" and "yeesh" I can say that I want to know what the abstract base classes are, what the flow of the program is, and any namespaces or global variables defined.
What's really helpful to me is a paragraph or two that says something along the lines of "main creates a driver of some base class which then reads in the xml file using some file reader and sets up an output object of type DefaultOutput before it executes the driver.run() function which ostreams the std:out to a file called output.txt and std::err to log.txt.
That paragraph would save me hours of picking through your code to figure out what gets used all the time.
It's also really helpful to have a "this depends on this so don't change it or you'll ruin literally everything" for any finicky code that might be present.

How do you find a particular piece of functionality in a large codebase? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I was fascinated by the "press Tab to search site" feature in chromium, so naturally I wanted to see how exactly it was implemented in code.
A little background for anybody who aren't familiar with this. After navigating to some site, say wikipedia, and doing a search, chromium remembers the name of the query variable and will let you press tab and search the site directly from the address bar. Neat!
Problem is the codebase for chromium is huge and I've had no luck in finding the method/function that handles this.
How do you approach a large codebase when you are looking for the implementation of a particular piece of functionality? Any tricks for narrowing it down? Preferably it should not require building the software with debug symbols and following the flow through the program.
There is no one size fits all approach to this sort of problem. But for this one I would try these:
If there are any unique messages associated with the operation, grep all the source files for that string. A common pitfall of this technique is that messages might be assembled from pieces within the application, so it is often helpful to grep for a unique short phrase—or even a single word—to identify the source of the message. Once the text is found, then finding what references it often requires more text searches.
Trace execution from an easy-to-find point, like the command processing and dispatch loop. I'd look for a Tab key case and follow where it leads.
Look at source code directory and filenames for hints. Software is often constructed rationally, with good engineers dividing and conquering in a sensible way.
A test coverage tool is a good way to do this. They tell you what part of an application
is exercised by a test.
Instrument the application to collect test coverage. Execute the functionality you care about. Record what is executed. Execute something similar, but not the same as the functionality you want. Record this. Take the set difference over the coverage. The diff selects code involved in the functionality of interest, excluding code which is common to similar functionality.
Ask the Chromium team. They don't give points or bronze pixels but they're definitely the authority and right people to ask this sort of questions.