Writing an interpreter in C++ - c++

I'm working on a C++ project which should do following operations:
Open a .txt file which contains list of strings
(for example String1: "Hi,name_1_is,;Ondrej,age24;year,,88;") with optional values determined by empty commas ",,".
After this check each string using regular expressions for valid input
(like "Hi" shouldn't be a number or "1" must be a number and everything with ",," is optional and can be skipped or user can enter this value as well).
Then evaluate the result and save it to variable or new .txt generated file.
This result shows if whole string is correct with an "ok" message attached to it or it will attach "not ok" message right to the parameter with wrong input.
I have already finished the part with opening a .txt file, checking the whole string and saving the right strings to the new file (using Qt and Visual Studio 2010 Express).
I need to do the part where each parameter will be checked but somehow I don't know how exactly, as I should not build Parser but the whole programm must be build like Interpreter.
Actually I'm stucked at this point because I have no idea how to start to build this like an Interpreter.
All my attempts resulted always with structure similar to Parser
(that means: I used split string, then checked each token or char using regex, then built the string together again, ect.)
Could you provide me with some usefull links or tips of how to achieve that or at least where to start at all please?

Related

How to input an arbitrary number of text files in C++?

so I'm working on a coding project for a class, and I understand the basic things I want to accomplish, but one thing that nobody seems to be able to help me with is inputting an unspecified number of text files. The user is prompted to enter the text files they want to compare (overall purpose of my code), separated by spaces, thus allowing them to compare an arbitrary amount of text files (eg. 2, 3, 8, 16, etc). I know that the getline function is helpful here, as well as searching for the number of "." because files can only contain one ".", all within a for loop. After that logic I am utterly lost. Eventually, I'm going to have to open the text files and put them in sets to compare them against every other file once, and output their similarities and differences into yet another text file. Any ideas?
Here is the general process I would try to follow (if I interpreted the prompt correctly)
Get the line of text files using getline
Put that into a stringstream
Open the next file in the stream while there is still information in the stringstream (not at eof)
Store all of that information in a Vector of strings, each new file just appended on after it is read
compare strings in the vector
If you pass the text files on the commandline rather than getting them from a little dialog with the user via stdin life will be easier. Most users will type
compare *
which on Unix type systems is expanded to a list of files. ON DOS you need to match and expand the wild card yourself.
You've got an N squared problem, but the logic is easy, it's just
int mian(int argc, char **argv)
{
int i, j;
for(i=1;i<argc;i++)
for(j=i+1;j>argc;j++)
compare(argv[i], argv[j];
}

Notepad ++ Regular Expression on finding missing {/}

I have one big file filled with custom text and scripts, that are used by one software, it crashes because of one problem
The software display whole text throught
{#HEXCOLOR}TEXT TEXT TEXT{/}
for example
{#FF00FF}Hello{/}
as we can see the whole text is inside custom script that starts from {#HEXCOLOR} and ends with {/} but in some lines the "{/}" is missing, that make program crash.
for example
{#FF00FF}Hello
It is possible some how to search for missing {/} in the file via Regular Expression ?
I tried by myself but failed:
{#[^{}]}.?{/[^{}]*}
you could use this pattern
({#[^}]+}[^{\r\n]+)(?={#|$)
and replace with \1{/}
Demo

Using Sublime Text 2 Build System with SQL*Plus and Oracle

I'm trying to setup Sublime Text 2 so that I can code my Oracle packages, triggers, functions, etc. I have the build system compiling my code just fine. Now I'm working on trying to get Sublime Text to take me to the exact line/column position when an error happens. To do this you have to use the file_regex option documented here.
The file_regex option uses a Perl-style regular expression to capture
up to four fields of error information from the build program’s
output, namely: filename, line number, column number and error
message. Use groups in the pattern to capture this information. The
filename field and the line number field are required.
My problem seems to be filename, which is the first required thing. My question is at the very bottom but let me show you how everything is setup because others may find this useful.
Here is my build system (compileSql.sublime-build)...
{
"cmd": ["c:\\projectX\\compileSql.bat", "$file"],
"file_regex": "^([0-9]+)/([0-9]+)"
}
Here is compileSql.bat. All this does is it makes a new file called runFile.txt which contains the file I'm currently working on in Sublime text, except it starts the file off with set defined off;, then my code, then ends with show errors; It then logs in and compiles the package (trigger, function, whatever)...
#echo set define off; > c:\projectX\runFile.txt
type %1 >> c:\projectX\runFile.txt
#echo show errors; >> c:\projectX\runFile.txt
sqlplus -s {user}/{pwd}#{database} #c:\projectX\runFile.txt
So all I have to do is press F7 and it compiles for me...
But when I get an error, this is the result...
When I click on the error is doesn't take me to that line number. Instead it opens a file called 8. So back up to my build system, I know the problem is with this: "file_regex": "^([0-9]+)/([0-9]+)". Sublime Text 2 requires the first matched pattern to be the file. That's the part I'm not grasping, why does it need to know that? It's the file I'm working on, the one I just pressed F7 for. So something needs to come before the ([0-9]+)/([0-9]+) in the regular expression, I don't know what to put in there.

Folder with 1300 png files into html images list

I've got folder with about 1300 png icons. What I need is html file with all of them inside like:
<img src="path-to-image.png" alt="file name without .png" id="file-name-without-.png" class="icon"/>
Its easy as hell but with that number of files its pure waste of time to do it manually. Have you any ideas how to automate it?
If you need it just once, then do a "dir" or "ls" and redirect it to a file, then use an editor with macro-ability like notepad++ to record modifying a single line like you desire, then hit play macro for the remainder of the file. If it's dynamic, use PHP.
I would not use C++ to do this. I would use vi, honestly, because running regular expressions repeatedly is all that is needed for this.
But young an do this in C++. I would start with a plan text file with all the file names generated by Dir or ls on the command prompt.
Then write code that takes a line of input and turns it into a line formatted the way you want. Test this and get it working on a single line first.
The RE engine of C++ is probably overkill (and is not all that well supported in compilers), but substr and basic find and replace is all you need. Is there a string library you are familiar with? std::string would do.
To generate the file name without PNG, check the last four characters and see if they exist and are .PNG (if not report an error). Then strip them. To remove dashes, copy characters to a new string but if you are reading a dash write a space. Everything else is just string concatenation.

Incorporating text files in applications?

Is there anyway I can incorporate a pretty large text file (about 700KBs) into the program itself, so I don't have to ship the text files together in the application directory ? This is the first time I'm trying to do something like this, and I have no idea where to start from.
Help is greatly appreciated (:
Depending on the platform that you are on, you will more than likely be able to embed the file in a resource container of some kind.
If you are programming on the Windows platform, then you might want to look into resource files. You can find a basic intro here:
http://msdn.microsoft.com/en-us/library/y3sk7e6b.aspx
With more detailed information here:
http://msdn.microsoft.com/en-us/library/zabda143.aspx
Have a look at the xxd command and its -include option. You will get a buffer and a length variable in a C formatted file.
If you can figure out how to use a resource file, that would be the preferred method.
It wouldn't be hard to turn a text file into a file that can be compiled directly by your compiler. This might only work for small files - your compiler might have a limit on the size of a single string. If so, a tiny syntax change would make it an array of smaller strings that would work just fine.
You need to convert your file by adding a line at the top, enclosing each line within quotes, putting a newline character at the end of each line, escaping any quotes or backslashes in the text, and adding a semicolon at the end. You can write a program to do this, or it can easily be done in most editors.
This is my example document:
"Four score and seven years ago,"
can be found in the file c:\quotes\GettysburgAddress.txt
Convert it to:
static const char Text[] =
"This is my example document:\n"
"\"Four score and seven years ago,\"\n"
"can be found in the file c:\\quotes\\GettysburgAddress.txt\n"
;
This produces a variable Text which contains a single string with the entire contents of your file. It works because consecutive strings with nothing but whitespace between get concatenated into a single string.