I need to parse a for loop containing variables and inner for loops. Each for loop further contains multiple variables and for loops.
Every for loop needs to be stored in a separate DTO class with its member variables and inner for loops as List variables. In below example, we have a main class A containing multiple for loops and inner for loops and member variables. All these need to be separated in separate DTO classes with other DTO class references as lists. I tried parsing this code using several possible ways but failed to mark inner for loops.
Class A{
public void a(){
int a1;
int a2;
for(int b=0; b < 10; b++) {
int b1;
int b2;
for(int c=0; c<10; c++){
int c1;
int c2;
for(int d=0; d<10; d++){
int d1;
int d2;
}
}
int b3;
int b4;
}
for(int e=0; e < 10; e++) {
int e1;
int e2;
int e3;
int e4;
}
int a3;
int a4;
}
Expected :
//class for main method
Class A_DTO{
int a1;
int a2;
List<B_DTO> b;
List<E_DTO> e;
int a3;
int a4;
}
//class for inner loop
Class B_DTO{
int b1;
int b2;
List<C_DTO> c;
int b3;
int b4;
}
//class for inner loop
Class C_DTO{
int c1;
int c2;
List<D_DTO> d;
}
//class for inner loop
Class D_DTO{
int d1;
int d2;
}
//class for inner loop
Class E_DTO{
int e1;
int e2;
int e3;
int e4;
}
Related
#include<stdio.h>
#include<stdlib.h>
struct node{
char data;
struct node*next;
};
struct node* top =NULL;
void push(char x){
struct node* temp =(struct node*)malloc(sizeof(struct node));
if(temp==NULL){
printf("Stacks Overflow\n");
return;
}
else{
temp->data = x;
temp->next = top;
top = temp;
}
}
char pop(){
if(top==NULL){
printf("Stacks Underflow\n");
return -1;
}
else{
char x;
struct node* p = top;
top = top->next;
x = p->data;
free(p);
return x;
}
}
char peek(int pos){
struct node*p = top;
int i;
for(i=0;p!=NULL && i<pos-1;i++)
p = p->next;
return (p!=NULL)?p->data:-1;
}
int main(){
char n, x;
printf("Enter the Number of Character to be pushed\n");
scanf("%d", &n);
for(char i=0;i<n;i++){
printf("Enter the Character\n");
x = getchar();
push(x);
}
while(top){
printf("%d,", pop());
}
}
why this code not work with char(n, x) but work fine with int(x, n).
I tried to implement char stack using linked list.
It works fine with integer but doesnot work with character.
When I use to insert char, one input get skipped but this doesnot happen in int .. Why?
I've produced this kernel
struct csrFormat {
int M, N;
int *IRP;
int *JA;
double *AS;
};
struct vector {
int dim;
double *val;
};
double csrSIMDReduction(struct csrFormat *csr, struct vector *vec, struct vector *res, int nz){
int M = csr->M;
int nonzerosPerRow = nz/M;
int chunk_size = 10000/nonzerosPerRow;
double *val = res->val;
struct timeval start,end;
gettimeofday(&start, NULL);
//IRP is the row_pointer array
//JA is the column indices array
//AS is the values array
int i,j,tmp;
#pragma omp parallel
{
#pragma omp for private(i, j, tmp) schedule(dynamic, chunk_size)
for (i=0; i<M; i++)
{
double result = 0.0;
#pragma omp simd reduction(+ : result)
for (j = csr->IRP[i]; j < csr->IRP[i+1]; j++)
{
tmp = csr->JA[j];
result += csr->AS[j] * vec->val[tmp];
}
val[i] = result;
}
}
gettimeofday(&end, NULL);
res->dim = M;
//printf("%ld.%06ld\n", start.tv_sec, start.tv_usec);
//printf("%ld.%06ld\n", end.tv_sec, end.tv_usec);
long t = (end.tv_sec - start.tv_sec)*1000000.0 + end.tv_usec - start.tv_usec;
return (double) t;
}
but i'm having a doubt: how does the vectorization works?
I mean, from what i've understood, the inner cicle is runned running multiple iterations together, but how can a single thread do this?
EDIT: code updated.
I want to identify the Expression like int a = function(b,c), so I wrote the code as followers:
void foo(int* a, int *b) {
int x;
int m;
int z;
int *p;
if (a[0] > 1) {
b[0] = 2;
z=10;
x = function( sizeof(char));
}
m = function( sizeof(char));
bar(x,m);
}
void bar(float x, float y);
int function(int size){
return size;
}
And than I used clang -Xclang -ast-dump -fsyntax-only cfunc_with_if.c to get the AST of the code:
From the result I found the AST Node type of int a = function(b,c) is BinaryOperator. In order to verify this, I use VisitStmt(Stmt *s) to print out all stmts' type.
bool VisitStmt(Stmt *s) {
if(isa<Stmt>(s)) {
Stmt *Statement = dyn_cast<Stmt>(s);
//Statement->dump();
std::string st(Statement->getStmtClassName());
st = st + "\n";
TheRewriter.InsertText(Statement->getLocStart(), st, true, true);
}
return true;
}
But the result is so weird. There is nothing printed out about the type of int a = function(b,c). and I'm so confused about the result. Is there some error in my code or something else?
There's no output at bar(x,m); either. Are there any errors when the tool compiles the code being analyzed? As written above, the code would fail to compile at x = function( sizeof(char)); since function has not been declared. Even when compilation has failed due to errors, the libtool tools can still run at least partially, with strange results.
Edit to add: what happens if you run the tool on this code?
void bar(float x, float y);
int function(int size);
void foo(int* a, int *b) {
int x;
int m;
int z;
int *p;
if (a[0] > 1) {
b[0] = 2;
z=10;
x = function( sizeof(char));
}
m = function( sizeof(char));
bar(x,m);
}
void bar(float x, float y);
int function(int size){
return size;
}
I am seeing different behaviors when variable used to get return values using pthread_join is defined gloabal vs static scope. I have included code_snippet here.
Static variables
int main()
{
static int r1,r2;
pthread_t t1, t2;
int i1[] = {1,2};
int i2[] = {3,4};
r1 = pthread_create( &t1, NULL, myfn, (void*)i1);
r2 = pthread_create( &t2, NULL, myfn, (void*)i2);
pthread_join( t1, (void *)&r1 );
pthread_join( t2, (void *)&r2 );
printf("Thread 1 returns: %d\n",r1);
printf("Thread 2 returns: %d\n",r2);
return 0;
}
void *myfn( void *intarray )
{
pthread_t t=pthread_self();
int *g = (int *) intarray;
int i=0;
int d=1;
for (i=g[0];i<=g[1];i++)
d*=i;
fprintf(stderr, "TID=%u %d\n",t, d);
pthread_exit((void *)d);
}
Return value
TID=3425117952 12
TID=3433510656 2
Thread 1 returns: 2
Thread 2 returns: 12
Global variables
int r1,r2;
int main()
{
same as above
}
void *myfn( void *intarray )
{
same as above
}
Return value
TID=3425117952 12
TID=3433510656 2
Thread 1 returns: 0 <<<<< it returns 0
Thread 2 returns: 12
Could someone please explain why it behaves differently ?
Almost certainly it's because the size of int and void * differ on your platform, so when pthread_join() writes a void * value through the int * pointer you gave it, it overwrites adjacent memory.
The different declaration of r1 and r2 changes the layout of the variables enough to change the effect you see.
Casting an int to void * in order to return it is messy; you're better off either allocating space for a result in the main thread and passing that to the thread when it starts, or have the thread allocate the result and return a pointer to it when it finishes.
However, if you insist on the cast to void method, you can fix it by passing the address of an actual void * object to pthread_join and then casting from that to int:
int main()
{
static int r1,r2;
void *result;
pthread_t t1, t2;
int i1[] = {1,2};
int i2[] = {3,4};
r1 = pthread_create( &t1, NULL, myfn, (void*)i1);
r2 = pthread_create( &t2, NULL, myfn, (void*)i2);
pthread_join( t1, &result );
r1 = (int)result;
pthread_join( t2, &result );
r2 = (int)result;
printf("Thread 1 returns: %d\n",r1);
printf("Thread 2 returns: %d\n",r2);
return 0;
}
I get _CrtIsValidHeapPointer(pUserData) error when running the code above.
Sometimes the code works perfectly, and sometimes this message appears. So I guess the problem is related to the memory allocation. But I've gone through the code many times and the numbers make sence to me (and also when debugging).
I noticed it happens in line "free(str_temp)" at the debugging.
The relevant code is here:
int main(){
int n;
int len;
char *str;
char command[3];
printf("Enter your string:\n");
scanf("%d", &n);
str = malloc(n+1);
scanf("%s", str);
while (1){
printf(">");
scanf("%s", command);
if (compare(command, "ml")) {
int k;
scanf("%d", &k);
multiply(str, n, k);
printf("Current string is %s\n", str);
n = ln(str);
continue;
}
free(str);
return 0;
}
void multiply(char *str, int n, int k) {
char *str_temp = malloc(n+1);
int i;
int j;
int q;
for (i = 0; i < n; i++){
str_temp[i] = str[i];
}
str_temp[n] = '\0';
free(str);
*str = malloc(n*k+1);
for (i = 0; i < k; i++){
for (j = 0; j < n; j++){
str[i*n + j] = str_temp[j];
}
}
str[n*k] = '\0';
free(str_temp);
}
Try to use message defination
void multiply(char **str, int n, int k)//Use **str(double pointer) instead of *str.
And call it like
multiply(&str, n, k);