lex:converting hex to dec - lex

I am trying to build a simple lexical analysis with flex and bison, but I have a problem with converting hex to dec in my lexer.l.
Here are my codes.
hex (0)([x]|[X])([0-9][A-Fa-f])+
{hex}{count++;printf("%d\t(hex,%s)\n",count,yytext);}

ohhh!I solved the problem! I just need to add two functions like c program and change the type of my output.
{hex} {count++;printf("%d\t(hex,%d)\n",count,hextodec(yytext));}
{oct} {count++;printf("%d\t(oct,%d)\n",count,octtodec(atoi(yytext)));}
int octtodec(int oct){
int dec=0,pos=0;
while (oct){
int a=oct%10;
dec += a * pow(8,pos);
pos++;
oct /= 10;
}
return dec;}
int hextodec(char *hex){
int dec=0,pos=0;
int len;
len=strlen(hex);
for (int i =2;i<len;i++){
if(hex[i]>='a'&&hex[i]<='f'){
dec=dec+(hex[i]-'a'+10)*pow(16,len-i-1);
}
else if(hex[i]>='A'&&hex[i]<='F'){
dec=dec+(hex[i]-'A'+10)*pow(16,len-i-1);
}
else{
dec+=(hex[i]-'0')*pow(16,len-i-1);
}
}
return dec;}

Related

Bad address error when comparing Strings within BPF

I have an example program I am running here to see if the substring matches the string and then print them out. So far, I am having trouble running the program due to a bad address. I am wondering if there is a way to fix this problem? I have attached the entire code but my problem is mostly related to isSubstring.
#include <uapi/linux/bpf.h>
#define ARRAYSIZE 64
struct data_t {
char buf[ARRAYSIZE];
};
BPF_ARRAY(lookupTable, struct data_t, ARRAYSIZE);
//char name[20];
//find substring in a string
static bool isSubstring(struct data_t stringVal)
{
char substring[] = "New York";
int M = sizeof(substring);
int N = sizeof(stringVal.buf) - 1;
/* A loop to slide pat[] one by one */
for (int i = 0; i <= N - M; i++) {
int j;
/* For current index i, check for
pattern match */
for (j = 0; j < M; j++)
if (stringVal.buf[i + j] != substring[j])
break;
if (j == M)
return true;
}
return false;
}
int Test(void *ctx)
{
#pragma clang loop unroll(full)
for (int i = 0; i < ARRAYSIZE; i++) {
int k = i;
struct data_t *line = lookupTable.lookup(&k);
if (line) {
// bpf_trace_printk("%s\n", key->buf);
if (isSubstring(*line)) {
bpf_trace_printk("%s\n", line->buf);
}
}
}
return 0;
}
My python code here:
import ctypes
from bcc import BPF
b = BPF(src_file="hello.c")
lookupTable = b["lookupTable"]
#add hello.csv to the lookupTable array
f = open("hello.csv","r")
contents = f.readlines()
for i in range(0,len(contents)):
string = contents[i].encode('utf-8')
print(len(string))
lookupTable[ctypes.c_int(i)] = ctypes.create_string_buffer(string, len(string))
f.close()
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="Test")
b.trace_print()
Edit: Forgot to add the error: It's really long and can be found here: https://pastebin.com/a7E9L230
I think the most interesting part of the error is near the bottom where it mentions:
The sequence of 8193 jumps is too complex.
And a little bit farther down mentions: Bad Address.
The verifier checks all branches in your program. Each time it sees a jump instruction, it pushes the new branch to its “stack of branches to check”. This stack has a limit (BPF_COMPLEXITY_LIMIT_JMP_SEQ, currently 8192) that you are hitting, as the verifier tells you. “Bad Address” is just the translation of kernel's errno value which is set to -EFAULT in that case.
Not sure how to fix it though, you could try:
With smaller strings, or
On a 5.3+ kernel (which supports bounded loops): without unrolling the loop with clang (I don't know if it would help).

CS50 pset 3: Tideman sort_pairs function

I need some assistance in understanding the logic behind this function. This is my current sort_pairs function in Tideman:
// Sort pairs in decreasing order by the strength of victory
void sort_pairs(void)
{
qsort(pairs, pair_count, sizeof(pair), compare);
return;
}
// Function for sort_pairs
int compare(const void *a, const void *b)
{
const pair *p1 = (const pair *) a;
const pair *p2 = (const pair *) b;
if (p1->winner < p2->winner)
{
return -1;
}
else if (p1->winner > p2->winner)
{
return 1;
}
else
{
return 0;
}
}
This does not clear check50 and I looked online to find how to approach this problem. It seems that most functions compare the values from the preferences array instead (eg preferences[pairs[i].winner][pairs[i].loser]) . My previous functions vote, record_preferences, and add_pairs all clear check50. I have not advanced beyond sort_pairs yet.
Why can't I compare the strength of victory directly from the pairs array instead since I already have the data stored there?
You don't need to make this so complex, you can use your own sorting here. Let's try a simple insertion sort-
void sort_pairs()
{
pair temp;
for (int i = 1, j; i < pair_count; i++)
{
temp = pairs[i];
j = i - 1;
for (; j >= 0 && preferences[pairs[j].winner][pairs[j].loser] < preferences[temp.winner][temp.loser]; j--)
{
pairs[j + 1] = pairs[j];
}
pairs[j + 1] = temp;
}
}
The pair struct looks like-
typedef struct
{
int winner;
int loser;
}
pair;
Explanation:-
We go through each pair of elements inside the pairs array - starting at 1 since I'm going to compare with the previous element (j = i - 1)
Now we check all the previous elements from the current element and compare them with the key - preferences[pairs[INDEX].winner][pairs[INDEX].loser]
This is the key you should be sorting by. preferences[WINNER_ID][LOSER_ID] means the amount of people that prefer WINNER_ID over LOSER_ID.
And that's pretty much it!, it's simply a insertion sort but the key is the important part.

convert array<uchar>^ to std::vector<char> data;

I'm working with managed and unmanaged code and I need to convert
convert array<uchar>^ image to std::vector<char> data.
I've started doing this:
array<uchar> ^image = gcnew array<uchar>(tam);
reader2->GetBytes(0, 0, image, 0, tam);
vector<uchar> data;
for (int idxImage = 0; idxImage < tam; idxImage++)
{
data.push_back(image[idxImage]);
}
It looks like it is working but it is very slow. Any idea about how can be done faster?
A first optimization you could do is to reserve the needed space in the vector, so it doesn't have to resize itself as you insert data into it.
This is as easy as:
data.reserve(tam);
This will improve performance, but not by that much. You can do better, you could use memcpy, which, as cppreference says:
std::memcpy is the fastest library routine for memory-to-memory copy.
So let's use that.
First, you need to resize (not reserve) the vector, so it knows the number of used bytes. Then, you can get a pointer to the raw data it holds with the data() function.
As for the array, it's a managed object, which means you need to pin it so the GC won't move it around. In C++/CLI this is done with pin_ptr.
Together, here's the final code:
data.resize(tam);
pin_ptr<uchar> pinned = &image[0];
std::memcpy(data.data(), pinned, tam);
I've tested it, and it's much faster. Here's a full test program:
#include "stdafx.h"
#include <vector>
typedef unsigned char uchar;
void test1(array<uchar>^ image)
{
std::vector<uchar> data;
int tam = image->Length;
auto sw = System::Diagnostics::Stopwatch::StartNew();
for (int idxImage = 0; idxImage < tam; idxImage++)
{
data.push_back(image[idxImage]);
}
sw->Stop();
System::Console::WriteLine("OP: {0} ms", sw->ElapsedMilliseconds);
}
void test2(array<uchar>^ image)
{
std::vector<uchar> data;
int tam = image->Length;
auto sw = System::Diagnostics::Stopwatch::StartNew();
data.reserve(tam);
for (int idxImage = 0; idxImage < tam; idxImage++)
{
data.push_back(image[idxImage]);
}
sw->Stop();
System::Console::WriteLine("reserve: {0} ms", sw->ElapsedMilliseconds);
}
void test3(array<uchar>^ image)
{
std::vector<uchar> data;
int tam = image->Length;
auto sw = System::Diagnostics::Stopwatch::StartNew();
data.resize(tam);
pin_ptr<uchar> pinned = &image[0];
std::memcpy(data.data(), pinned, tam);
sw->Stop();
System::Console::WriteLine("memcpy: {0} ms", sw->ElapsedMilliseconds);
}
int main(array<System::String ^> ^args)
{
size_t tam = 20 * 1024 * 1024;
array<uchar>^ image = gcnew array<uchar>(tam);
(gcnew System::Random)->NextBytes(image);
test1(image);
test2(image);
test3(image);
return 0;
}
My results are:
OP: 123 ms
reserve: 95 ms
memcpy: 8 ms

QCustomPlot using timestamp to set Date along one axis

I am having trouble setting the date properly. Basically I have timestamp, open, close, high, low, volume stored line by line in a text file (downloaded using Yahoo API). My program then reads each line and converts it to a QStringList. It then puts each item in the list into the appropriate QVector (dates[], open[], close[], high[], low[], volume[]) converting each item to a double. Here is where the problem is. It appears that the precision is lost during the conversion. The dates always show as periods back in 1970 when the actually timestamp is in fact a date from a few days ago.
#include "dialog.h"
#include "ui_dialog.h"
#include<QFile>
#include<QTextStream>
#include<string>
#include<iostream>
#include<ctime>
using namespace std;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QStringList lines;
QString line;
QVector<double> dates;
QVector<double> high;
QVector<double> low;
QVector<double> open;
QVector<double> close;
QVector<double> volume;
QFile file ("YHOO.cvs");
if(file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
while (!in.atEnd())
{
line = in.readLine();
lines = line.split(",");
dates.append(lines[0].toDouble());
close.append(lines[1].toDouble());
high.append(lines[2].toDouble());
low.append(lines[3].toDouble());
open.append(lines[4].toDouble());
volume.append(lines[5].toInt());
}
file.close();
}
else{
QMessageBox::information(0,"info",file.errorString());
}
ui->plot->addGraph();
ui->plot->graph(0)->setData(dates, high);
ui->plot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
ui->plot->xAxis->setDateTimeFormat("MM/dd/yyyy");
QPen pen;
pen.setColor(QColor(200,200,200));
ui->plot->graph(0)->setPen(pen);
ui->plot->graph(0)->setLineStyle(QCPGraph::lsLine);
ui->plot->graph(0)->setBrush(QBrush(QColor(160,50,150)));
ui->plot->xAxis->setRange(dates[0], dates[dates.length()-1]);
ui->plot->yAxis->setRange(*std::min_element(high.begin(), high.end()),*std::max_element(high.begin(),high.end()));
}
Dialog::~Dialog()
{
delete ui;
}
YHOO.cvs
20140227,30.1000,30.1600,28.4100,29.7000,2351300
20140228,28.3000,32.0000,27.0000,29.2000,3781000
20140303,28.1900,28.9100,26.8900,27.3000,1664900
20140304,30.0400,30.3800,28.6300,28.8500,2341700
20140305,28.5500,29.5000,28.4900,29.2400,7314100
20140306,27.1700,29.0100,27.1500,28.7600,3007300
20140307,27.2000,28.3200,26.7100,27.8400,2961800
20140310,28.2400,28.5000,27.3500,27.7200,1622100
20140311,27.5300,28.7400,27.1800,28.4400,1745200
20140312,28.5400,28.7400,27.3500,27.4700,2206300
I figured it out. Turns out the first column of data is a date and not a timestamp. I came up with this function to convert the QString to a double value timestamp.
double timeStamp(QString qs){
char tempDate[10];
memcpy(tempDate, qs.toStdString().c_str(), 10);
char date[] = " ";
date[0] = tempDate[0];
date[1] = tempDate[1];
date[2] = tempDate[2];
date[3] = tempDate[3];
date[4] = '\0';
date[5] = tempDate[4];
date[6] = tempDate[5];
date[7] = '\0';
date[8] = tempDate[6];
date[9] = tempDate[7];
struct tm tmdate = {0};
tmdate.tm_year = atoi(&date[0]) - 1900;
tmdate.tm_mon = atoi(&date[5]) - 1;
tmdate.tm_mday = atoi(&date[8]);
time_t t = mktime( &tmdate );
double actual_time_sec = difftime(t,0);
return actual_time_sec;
}

does boost python support a function returning a vector, by ref or value?

I am new to python, I have looked at boost python, and it looks very
impressive. However going through the introduction I can not find
any examples where, vector of objects are returned as python list/tuples.
i.e Take this example, I want to expose class X, Cont and all its functions.
critical bit being return a vector of X's or strings to python
class X {};
class Cont {
.....
// how can this be exposed using boost python
const std::vector<X>& const_ref_x_vec() const { return x_vec_;}
std::vector<X> value_x_vec() const { return x_vec;}
const std::vector<std::string>& const_ref_str_vec() const { return str_vec_;}
std::vector<std::string> value_str_vec() const { return str_vec_; }
...
private:
std::vector<X> x_vec_;
std::vector<std::string> str_vec_;
};
My own fruitless attempt at trying to expose the functions like
const_ref_x_vec(), value_x_vec(),etc just leads to compile errors.
from googling around I have not seen any example that support returning vectors
by value or reference. Is this even possible with boost python?
are there any workarounds ? should I be using SWIG for this case ?
Any help appreciated.
Avtar
Autopulated's reason was essentially correct, but the code was more complicated then necessary.
The vector_indexing_suite can do all that work for you:
class_< std::vector<X> >("VectorOfX")
.def(vector_indexing_suite< std::vector<X> >() )
;
There is a map_indexing_suite as well.
Because you can't expose template types to python you have to explicitly expose each sort of vector that you want to use, for example this is from my code:
Generic template to wrap things:
namespace bp = boost::python;
inline void IndexError(){
PyErr_SetString(PyExc_IndexError, "Index out of range");
bp::throw_error_already_set();
}
template<class T>
struct vec_item{
typedef typename T::value_type V;
static V& get(T& x, int i){
static V nothing;
if(i < 0) i += x.size();
if(i >= 0 && i < int(x.size())) return x[i];
IndexError();
return nothing;
}
static void set(T& x, int i, V const& v){
if(i < 0) i += x.size();
if(i >= 0 && i < int(x.size())) x[i] = v;
else IndexError();
}
static void del(T& x, int i){
if(i < 0) i += x.size();
if(i >= 0 && i < int(x.size())) x.erase(x.begin() + i);
else IndexError();
}
static void add(T& x, V const& v){
x.push_back(v);
}
};
Then, for each container:
// STL Vectors:
// LineVec
bp::class_< std::vector< Line > >("LineVec")
.def("__len__", &std::vector< Line >::size)
.def("clear", &std::vector< Line >::clear)
.def("append", &vec_item< std::vector< Line > >::add,
bp::with_custodian_and_ward<1, 2>()) // let container keep value
.def("__getitem__", &vec_item< std::vector< Line > >::get,
bp::return_value_policy<bp::copy_non_const_reference>())
.def("__setitem__", &vec_item< std::vector< Line > >::set,
bp::with_custodian_and_ward<1,2>()) // to let container keep value
.def("__delitem__", &vec_item< std::vector< Line > >::del)
.def("__iter__", bp::iterator< std::vector< Line > >())
;
// ...
A similar approach is possible for std::map.
I used lots of help from wiki.python.org when writing this.