C++ parsing as custom language Interpreter - c++

I need to parse input text file as custom language that i should interpret it's commands (line by line) and execute it, that's the input i should expect:
#Some variables
myInt = 2
myFloat = 2.5
myString = “Hello”
#Lists
myList = (myInt, myFloat, myInt + myFloat)
myOtherList = (myFloat + myFloat, myInt+ myInt)
subList = myList[:1]
completeList = myList + myOtherList + subList
#This should have no effect (it is ok if it is being calculated)
2+4
#Now some printing
print(myString)
print(“World”)
print(completeList)
print(completeList[3])
#Some syntax errors
b = “hello, this string is not enclosed right
c = myString + completeList
d = myInt + SOME_VARIABLE_I_HAVENT_DEFINED_YET
#First string to appear makes everything a string
print(1 + 2 + 15.5 + 2.2 + “Hi” + 3 + 4 + 6)
print(1 + 2 + 15.5 + 2.2 + 3 + 4 + 6 + “hi”)
print((1,2))
So I already have a first checking function, now I know when it's print/assign/comment/bad syntax command or whatever. I now should parse what inside the print function and the assign commands, I should ignore white spaces, they also might not be as delimiters to count on.
Please guide me a bit, what string functions i should use and how in order to to make it work, I mean how you can cut to tokens and also identify the mathematical signs? I'm guessing it should use some stack to follow the parentheses of the list type and quotation signs, no? Any general and more detailed information will be appreciated, thanks(:
p.s.
That's the output for this code:
Hello
World
(2, 2.5, 4.5, 5.0, 4, 2, 2.5)
5.0
InvalidSyntax : b = “hello, this string is not enclosed right
InvalidSyntax : c = myString + completeList
UndefinedVariableName : SOME_VARIABLE_I_HAVENT_DEFINED_YET
20.7Hi346
33.7hi
(1,2)
I already have all the overloading operators for what I need, I only need to parse it right and send it to my already built functions.

So you haven't had a chance to read the Dragon Book...
How do you think about embedding Lua or Python interpreter into your product, instead of inventing your own language? They are more common and full-fledged programming languages. Moreover Google will help you find lots of tutorials on how to embed them, such as:
http://docs.python.org/extending/embedding.html
http://www.ibm.com/developerworks/linux/library/l-embed-lua/
The disadvantage of inventing your own language is that: even after you successfully parsed your own language, you need to define semantics for it. Parsing only deals with the syntax, which is a different thing from the semantics. I don't know your situation but both of them usually require too long time to learn for just a single software project.
As for Boost Spirit: I don't recommend to use it which was written by people who just wanted to show their smartness by writing it (but in the end showed their ignorance about what is practical software design.)

A wonderful C++ library exists for that : SPIRIT

Related

Set up model in CPLEX using Python API

In fact, I'm trying to implement the very simple model formulation:
min sum_i(y_i*f_i) + sum_i(sum_j(x_ij*c_ij))
s.t. sum_i(x_ij) = 1 for all j
x_ij <= y_i for all i,j
x_ij, y_i are binary
But I simply cannot figure out how the Python API works. They suggest creating variables like this:
model.variables.add(obj = fixedcost,
lb = [0] * num_facilities,
ub = [1] * num_facilities,
types = ["B"] * num_facilities)
# Create one binary variable for each facility/client pair. The variables
# model whether a client is served by a facility.
for c in range(num_clients):
model.variables.add(obj = cost[c],
lb = [0] * num_facilities,
ub = [1] * num_facilities,
types = ["B"] * num_facilities)
# Create corresponding indices for later use
supply = []
for c in range(num_clients):
supply.append([])
for f in range(num_facilities):
supply[c].append((c+1)*(num_facilities)+f)
# Constraint no. 1:
for c in range(num_clients):
assignment_constraint = cplex.SparsePair(ind = [supply[c][f] for f in \
range(num_facilities)],
val = [1] * num_facilities)
model.linear_constraints.add(lin_expr = [assignment_constraint],
senses = ["L"],
rhs = [1])
For this constraints I have no idea how the variables from above are refered to since it only mentions an auxiliary list of lists. Can anyone explain to me how that should work? The problem is easy, I also know how to do it in C++ but the Python API is a closed book to me.
The problem is the uncapacitated facility location problem and I want to adapt the example file facility.py
EDIT: One idea for constraint no.2 is to create one-dimensional vectors and use vector addition to create the final constraint. But that tells me that this is an unsupported operand for SparsePairs
for f in range(num_facilities):
index = [f]
value = [-1.0]
for c in range(num_clients):
open_constraint = cplex.SparsePair(ind = index, val = value) + cplex.SparsePair(ind = [supply[c][f]], val = [1.0])
model.linear_constraints.add(lin_expr=[open_constraint],
senses = ["L"],
rhs = [0])
The Python API is closer to the C Callable Library in nature than the C++/Concert API. Variables are indexed from 0 to model.variables.get_num() - 1 and can be referred to by index, for example, when creating constraints. They can also be referred to by name (the add method has an optional names argument). See the documentation for the VariablesInterface here (this is for version 12.5.1, which I believe you are using, given your previous post).
It may help to start looking at the most simple examples like lpex1.py (and read the comments). Finally, I highly recommend playing with the Python API from the interactive Python prompt (aka the REPL). You can read the help there and type things in to see what they do.
You also might want to take a look at the docplex package. This is a modeling layer built on top of the CPLEX Python API (or which can solve on the cloud if you don't have a local installation of CPLEX installed).

Python 2.7 - Split comma separated text file into smaller text files

I was (unsuccessfully) trying to figure out how to create a list of compound letters using loops. I am a beginner programmer, have been learning python for a few months. Fortunately, I later found a solution to this problem - Genearte a list of strings compound of letters from other list in Python - see the first answer.
So I took that code and added a little to it for my needs. I randomized the list, turned the list into a comma separated file. This is the code:
from string import ascii_lowercase as al
from itertools import product
import random
list = ["".join(p) for i in xrange(1,6) for p in product(al, repeat = i)]
random.shuffle(list)
joined = ",".join(list)
f = open("double_letter_generator_output.txt", 'w')
print >> f, joined
f.close()
What I need to do now is split that massive file "double_letter_generator_output.txt" into smaller files. Each file needs to consist of 200 'words'. So it will need to split into many files. The files of course do not exist yet and will need to be created by the program also. How can I do that?
Here's how I would do it, but I'm not sure why you're splitting this into smaller files. I would normally do it all at once, but I'm assuming the file is too big to be stored in working memory, so I'm traversing one character at a time.
Let bigfile.txt contain
1,2,3,4,5,6,7,8,9,10,11,12,13,14
MAX_NUM_ELEMS = 2 #you'll want this to be 200
nameCounter = 1
numElemsCounter = 0
with open('bigfile.txt', 'r') as bigfile:
outputFile = open('output' + str(nameCounter) + '.txt', 'a')
for letter in bigfile.read():
if letter == ',':
numElemsCounter += 1
if numElemsCounter == MAX_NUM_ELEMS:
numElemsCounter = 0
outputFile.close()
nameCounter += 1
outputFile = open('output' + str(nameCounter) + '.txt', 'a')
else:
outputFile.write(letter);
outputFile.close()
now output1.txt is 1,2, output2.txt is 3,4, output3.txt is 5,6, etc.
$ cat output7.txt
13,14
This is a little sloppy, you should write a nice function to do it and format it the way you like!
FYI, if you want to write to a bunch of different files, there's no reason to write to one big file first. Write to the little files right off the bat.
This way, the last file might have fewer than MAX_NUM_ELEMS elements.

wxString: Is there any C/C++ code that implements string formatting using Python3-like placeholders? [duplicate]

This question already has answers here:
How to construct a std::string with embedded values, i.e. "string interpolation"?
(8 answers)
Closed 2 years ago.
I am rewriting our application using wxWidgets. One of the goals is to replace our old approach to the localized strings. Another possible goal is to embed the Python interpreter to the application -- but only later. Anyway, it would be nice any C/C++ code or library capable of Python-like string formatting that uses the curly braces.
If you do not know Python, here is the doc for its format function. You can find the reference to the Format Specification Mini-Language inside, and the Format examples. Some extract from the doc... (The >>> is a prompt of the interactive Python mode, the line below shows the result of the call. Python uses single or double quotes as a string delimiter):
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'
I would also like to use the Python formatting with the named placeholders (instead of numeric indices). In Python, the str.format_map(mapping) method of strings is used where the mapping is of the map<string, some_type> like type. Say, my_dictionary contains mapping like:
"name" --> "Guido"
"country" --> "Netherlands"
Having a template string like below, I would like to get the result...
wxString template("{name} was born in {country}.");
wxString result(format_map(s, my_dictionary));
// the result should contain...
// "Guido was born in Netherlands."
Do you know any avaliable C or C++ code capable of that, or do I have to write my own?
Thanks for your time and experience,
Petr
Yes. The fmt library uses format string syntax based on Python's str.format. It supports most of the formatting options of str.format including named arguments. Here's an example:
std::string s = fmt::format("{0}{1}{0}", "abra", "cad");
// s == "abracadabra"
Disclaimer: I'm the author of this library.

Scala string template

Is there default(in SDK) scala support for string templating? Example: "$firstName $lastName"(named not numbered parameters) or even constructs like for/if. If there is no such default engine, what is the best scala library to accomplish this.
If you want a templating engine, I suggest you have a look at scalate. If you just need string interpolation, "%s %s".format(firstName, lastName) is your friend.
Complementing Kim's answer, note that Java's Formatter accepts positional parameters. For example:
"%2$s %1$s".format(firstName, lastName)
Also, there's the Enhanced Strings plugin, which allows one to embed arbitrary expressions on Strings. For example:
#EnhanceStrings // enhance strings in this scope
trait Example1 {
val x = 5
val str = "Inner string arithmetics: #{{ x * x + 12 }}"
}
See also this question for more answers, as this is really a close duplicate.
In Scala 2.10 and up, you can use string interpolation
val name = "James"
println(s"Hello, $name") // Hello, James
val height = 1.9d
println(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall
This compiler plug-in has provided string interpolation for a while:
http://jrudolph.github.com/scala-enhanced-strings/Overview.scala.html
More recently, the feature seems to be making it into the scala trunk: https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/run/stringInterpolation.scala -- which generates some interesting possiblities: https://gist.github.com/a69d8ffbfe9f42e65fbf (not sure if these were possible with the plug-in; I doubt it).

Creating simple calculator with bison & flex in C++ (not C)

I would like to create simple C++ calculator using bison and flex. Please note I'm new to the creating parsers. I already found few examples in bison/flex but they were all written in C.
My goal is to create C++ code, where classes would contain nodes of values, operations, funcs - to create AST (evaluation would be done just after creating whole AST - starting from the root and going forward).
For example:
my_var = sqrt(9 ** 2 - 32) + 4 - 20 / 5
my_var * 3
Would be parsed as:
=
/ \
my_var +
/ \
sqrt -
| / \
- 4 /
/ \ / \
** 32 20 5
/ \
9 2
and the second AST would look like:
*
/ \
my_var 3
Then following pseudocode reflects AST:
ast_root = create_node('=', new_variable("my_var"), exp)
where exp is:
exp = create_node(OPERATOR, val1, val2)
but NOT like this:
$$ = $1 OPERATOR $3
because this way I directly get value of operation instead of creation the Node.
I believe the Node should contain type (of operation), val1 (Node), val2 (Node). In some cases val2 would be NULL, like above mentioned sqrt which takes in the end one argument. Right?
It will be nice if you can propose me C++ skeleton (without evaluation) for above described problem (including *.y file creating AST) to help me understand the way of creating/holding Nodes in AST. Code can be snipped, just to let me get the idea.
I'll also be grateful if you point me to an existing (possibly simple) example if you know any.
Thank you all for your time and assistance!
http://www.progtools.org/compilers/tutorials/cxx_and_bison/cxx_and_bison.html is a mini-tutorial which should create something like what you want.