int getOccurence(char* funcName){ //printf("getOccurence\n"); if(symStack == NULL){ return 0; } void* backup = stackNew(sizeof(funcVars*)); int occ = 0; while(!(stackIsEmpty(symStack))){ funcVars* pk = (funcVars*) malloc (sizeof(funcVars)); stackPeek(symStack, &pk); if(strcmp(pk->funcName,funcName) == 0){ occ = pk->occurence; break; } else{ stackPop(symStack, (&pk)); stackPush(backup, (&pk)); } } while(!(stackIsEmpty(backup))){ funcVars* b = (funcVars*) malloc (sizeof(funcVars)); stackPop(backup, (&b)); stackPush(symStack, (&b)); } return occ; }
// Returns 0 to indicate error // Verify that the next stack element exists and is of the given type // If e==ANY_TYPE, the type check is ommited static int checkPeek(Elm e) { if (stackIsEmpty(stack)){ fprintf(stderr,"Illegal document structure, expected %s\n", elmNames[e]); XML_StopParser(parser, XML_FALSE); return 0; // error } return e==ANY_TYPE ? 1 : checkElementType(stackPeek(stack), e); }
/////////////////////////////////////////////////////////////////////////////// /// Verify that the next stack element exists and is of the given type. /// If e==ANY_TYPE, the type check is ommited. /// ///\param e The element. ///\return 0 if there is no error occurred. Otherwise, return 1 to indicate an error. /////////////////////////////////////////////////////////////////////////////// static int checkPeek(Elm e) { if (stackIsEmpty(stack)){ printf("Illegal document structure, expected %s\n", elmNames[e]); XML_StopParser(parser, XML_FALSE); return 1; // error } return e==ANY_TYPE ? 0 : checkElementType(stackPeek(stack), e); //stackPeek() retrieves item at top of stack }
void test_peek_stack_empty() { stack *stack = stackCreate(); int *position1 = stackPeek(stack); CU_ASSERT_EQUAL(stack -> size, 0); CU_ASSERT_PTR_NULL(position1); }
char* infixToPostfix(char const* str) { generic_stack* infixStack = stackAllocate(1); generic_stack* output = stackAllocate(1); char peek; for (; *str; ++str) { stackPeek(infixStack, &peek); if (*str == '(') { peek = '('; stackPush(infixStack, &peek); } else if (*str == ')') { while (peek != '(') { stackPop(infixStack, &peek); stackPush(output, &peek); stackPeek(infixStack, &peek); } //Discard the '(' stackPop(infixStack, &peek); } else { while (!stackEmpty(infixStack) && precidence(peek) >= precidence(*str)) { stackPop(infixStack, &peek); stackPush(output, &peek); //Update the peeked stackPeek(infixStack, &peek); } stackPush(infixStack, str); } } while (!stackEmpty(infixStack)) { stackPop(infixStack, &peek); stackPush(output, &peek); } peek = '\0'; stackPush(output, &peek); char* result = malloc(stackSize(output)); memcpy(result, output->data, output->current); stackFree(infixStack); stackFree(output); return result; }
int getExecutionFlag(){ if(didFuncEntryExecute == NULL || stackIsEmpty(didFuncEntryExecute)){ return 1; } else{ int* execFlag = (int *)malloc(sizeof(int)); stackPeek(didFuncEntryExecute,&execFlag); return *execFlag; } }
void test_peek_stack_filled() { stack *stack = stackCreate(); int value1 = 3; int value2 = 2; int value3 = 1; stack = stackPush(stack, &value1); stack = stackPush(stack, &value2); stack = stackPush(stack, &value3); int *position3 = stackPeek(stack); CU_ASSERT_EQUAL(stack -> size, 3); CU_ASSERT_PTR_EQUAL(position3, &value3); }
int queuePeek(queue_t * queue,DATA_TYPE *data){ if( StackIsEmpty((queue->s1)) && StackIsEmpty((queue->s2)) ) return 0; DATA_TYPE tempdata; if(StackIsEmpty((queue->s2)) ){ while(stackPop((queue->s1),&tempdata)) stackPush((queue->s2),tempdata); } stackPeek((queue->s2),data); return 1; }
int descendantsTest(){ stackPushState(newState(0,0)); Node* currentNode; int i=0; while(!isStackEmpty()){ currentNode = stackPeek(); if(currentNode->isBranched) { stackDeleteTop(); } else { currentNode->isBranched = 1; if(i<3){ stackPushState(newState(i*3+1,1)); stackPushState(newState(i*3+2,i)); stackPushState(newState(i*3+3,i)); i++; } } stackPrintOut(); } return 0; }
int main(int argc, char *argv[]) { stItem_t *stackItem_A; stItem_t *stackItem_B; stItem_t *stackItem_1; stItem_t *stackItem_2; mdsDirectory_t dir_1; mdsDirectory_t dir_2; mdsDirectory_t dir_3; mdsDirectory_t dir_4; mdsDirectory_t dir_5; stackInitAll(); strcpy(dir_1.szFilename, "/home/ann/contentExample/E200581216"); strcpy(dir_2.szFilename, "/home/root"); strcpy(dir_3.szFilename, "/home/ann"); strcpy(dir_4.szFilename, "/home/ann/projects"); strcpy(dir_5.szFilename, "/test"); // create a stack item stackItem_A = g_new0(stItem_t, 1); stackItem_A->dirArray = g_array_sized_new(FALSE, TRUE, ERMDS_MAX_FILENAME_SIZE, INITIAL_DIR_COUNT_ARRAY_SIZE); CL_STACKPRINTF("stackItem_A->dirArray - 0x%x", (unsigned int) stackItem_A->dirArray); g_array_append_val(stackItem_A->dirArray, dir_1); g_array_append_val(stackItem_A->dirArray, dir_2); g_array_append_val(stackItem_A->dirArray, dir_3); stackItem_A->sort.ascending = 1; stackItem_A->sort.ft = mdsFieldFile; stackPush(st_ContentTypeBooks, stackItem_A); // create a stack item stackItem_B = g_new0(stItem_t, 1); stackItem_B->dirArray = g_array_sized_new(FALSE, TRUE, ERMDS_MAX_FILENAME_SIZE, INITIAL_DIR_COUNT_ARRAY_SIZE); CL_STACKPRINTF("stackItem_B->dirArray - 0x%x", (unsigned int) stackItem_B->dirArray); g_array_append_val(stackItem_B->dirArray, dir_4); g_array_append_val(stackItem_B->dirArray, dir_5); stackItem_B->sort.ascending = 0; stackItem_B->sort.ft = mdsFieldFile; stackPush(st_ContentTypeBooks, stackItem_B); CL_STACKPRINTF("height %d", stackHeight(st_ContentTypeBooks));; stackItem_2 = stackPeek(st_ContentTypeBooks); stackItemDisplay(stackItem_2); stackPop(st_ContentTypeBooks); stackItem_1 = stackPeek(st_ContentTypeBooks); stackItemDisplay(stackItem_1); stackPop(st_ContentTypeBooks); g_free(stackItem_A); g_free(stackItem_B); stackDestroyAll(); return 0; }
// Pop the children from the stack and // check for correct type and sequence of children static void XMLCALL endElement(void *context, const char *elm) { Elm el; el = checkElement(elm); assert((el >= elm_fmiModelDescription) && (el <=elm_Enumeration)); switch(el) { case elm_fmiModelDescription: { ModelDescription* md; ListElement** ud = NULL; // NULL or list of BaseUnits Type** td = NULL; // NULL or list of Types Element* de = NULL; // NULL or DefaultExperiment ListElement** va = NULL; // NULL or list of Tools ScalarVariable** mv = NULL; // NULL or list of ScalarVariable ListElement* child; child = checkPop(ANY_TYPE); if (child->type == elm_ModelVariables){ mv = (ScalarVariable**)child->list; free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_VendorAnnotations){ va = (ListElement**)child->list; free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_DefaultExperiment){ de = (Element*)child; child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_TypeDefinitions){ td = (Type**)child->list; free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_UnitDefinitions){ ud = (ListElement**)child->list; free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (!checkElementType(child, elm_fmiModelDescription)) return; md = (ModelDescription*)child; md->modelVariables = mv; md->vendorAnnotations = va; md->defaultExperiment = de; md->typeDefinitions = td; md->unitDefinitions = ud; stackPush(stack, md); break; } case elm_Type: { Type* tp; Element* ts = checkPop(ANY_TYPE); if (!ts) return; if (!checkPeek(elm_Type)) return; tp = (Type*)stackPeek(stack); switch (ts->type) { case elm_RealType: case elm_IntegerType: case elm_BooleanType: case elm_EnumerationType: break; default: logFatalTypeError("RealType or similar", ts->type); return; } tp->typeSpec = ts; break; } case elm_ScalarVariable: { ScalarVariable* sv; Element** list = NULL; Element* child = checkPop(ANY_TYPE); if (!child) return; if (child->type==elm_DirectDependency){ list = ((ListElement*)child)->list; free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (!checkPeek(elm_ScalarVariable)) return; sv = (ScalarVariable*)stackPeek(stack); switch (child->type) { case elm_Real: case elm_Integer: case elm_Boolean: case elm_Enumeration: break; default: logFatalTypeError("Real or similar", child->type); return; } sv->directDependencies = list; sv->typeSpec = child; break; } case elm_ModelVariables: popList(elm_ScalarVariable); break; case elm_VendorAnnotations: popList(elm_Tool);break; case elm_Tool: popList(elm_Annotation); break; case elm_TypeDefinitions: popList(elm_Type); break; case elm_EnumerationType: popList(elm_Item); break; case elm_UnitDefinitions: popList(elm_BaseUnit); break; case elm_BaseUnit: popList(elm_DisplayUnitDefinition); break; case elm_DirectDependency: popList(elm_Name); break; case elm_Name: { // Exception: the name value is represented as element content. // All other values of the XML file are represented using attributes. Element* name = checkPop(elm_Name); if (!name) return; name->n = 2; name->attributes = malloc(2*sizeof(char*)); name->attributes[0] = attNames[att_input]; name->attributes[1] = data; data = NULL; skipData = 1; // stop recording element content stackPush(stack, name); break; } default: // must be a leaf Element assert(getAstNodeType(el)==astElement); break; } // All children of el removed from the stack. // The top element must be of type el now. checkPeek(el); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Pop the children from the stack and check for correct type and sequence of children /// This is the end handler of parser. The parser ends here. /// ///\param context ///\param elm ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// static void XMLCALL endElement(void *context, const char *elm) { Elm el; el = checkElement(elm); printfDebug(" **** ending element %s.\n", elmNames[el]); switch(el) { case elm_fmiModelDescription: { ModelDescription* md; ListElement** im = NULL; // NULL or list of CoSimulation_StandAlone under Implementation ListElement** ud = NULL; // NULL or list of BaseUnits under UnitDefinitions Type** td = NULL; // NULL or list of Types under TypeDefinitions Element* de = NULL; // NULL or DefaultExperiment ListElement** va = NULL; // NULL or list of Tools ScalarVariable** mv = NULL; // NULL or list of ScalarVariables under ModelVariables ListElement* child; printDebug("$$$$$ entered endElement 'elm_fmiModelDescription'\n"); child = checkPop(ANY_TYPE); // get a child from stack while (child && child->type != elm_fmiModelDescription) // add while-loop in case the elements in the random order { if (child->type == elm_VendorAnnotations){ va = (ListElement**)child->list; // VendorAnnotations (ListElement) contains Tool (ListElement)) free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_DefaultExperiment){ de = (Element*)child; // DefaultExperiment (Element) with only attributes child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_TypeDefinitions){ td = (Type**)child->list; // TypeDefinitions (ListElement) contains Type (Type) free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_UnitDefinitions){ ud = (ListElement**)child->list; // UnitDefinitions (ListElement) contains BaseUnit (ListElement) free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (child->type == elm_Implementation){ // Implementation is listElement, it can be treated the same as UnitDefinitions im = (ListElement**)child->list; // Implementation (Implementation) contains CoSimulation_StandAlone (ListElement) free(child); // Zuo: child = checkPop(ANY_TYPE); // Zuo: if (!child) return; // Zuo: } // Zuo: if (child->type == elm_ModelVariables){ mv = (ScalarVariable**)child->list; // ModelVariables (ListElement) contains ScalarVariable (ScalarVariable) free(child); child = checkPop(ANY_TYPE); if (!child) return; } } printDebug("$$$$$ checking element type \n"); if (checkElementType(child, elm_fmiModelDescription)) return; printDebug("$$$$$ checked element type \n"); md = (ModelDescription*)child; // the following build the link of elements for ModelDescriptions md->modelVariables = mv; md->implementation = im; // added M. Wetter md->vendorAnnotations = va; md->defaultExperiment = de; md->typeDefinitions = td; md->unitDefinitions = ud; printDebug("$$$$$ calling stackPush \n"); stackPush(stack, md); printDebug("$$$$$ called stackPush \n"); break; } case elm_Type: { Type* tp; Element* ts = checkPop(ANY_TYPE); if (!ts) return; // If there is no sub-element, return if (checkPeek(elm_Type)) return; // If the sub-element is not Type, return tp = (Type*)stackPeek(stack); // Get the element switch (ts->type) { case elm_RealType: case elm_IntegerType: case elm_BooleanType: case elm_EnumerationType: case elm_StringType: break; default: // Element type does not match logFatalTypeError("RealType or similar", ts->type); return; } tp->typeSpec = ts; // parent (tp) links to sub-element (ts) break; } case elm_ScalarVariable: { ScalarVariable* sv; Element** list = NULL; Element* child = checkPop(ANY_TYPE); if (!child) return; if (child->type==elm_DirectDependency){ list = ((ListElement*)child)->list; // DirectDependency is ListElement free(child); child = checkPop(ANY_TYPE); if (!child) return; } if (checkPeek(elm_ScalarVariable)) return; // If next element is not ScalarVariable, return sv = (ScalarVariable*)stackPeek(stack); switch (child->type) { case elm_Real: case elm_Integer: case elm_Boolean: case elm_Enumeration: case elm_String: break; default: logFatalTypeError("Real or similar", child->type); return; } sv->directDependencies = list; sv->typeSpec = child; break; } case elm_Implementation: popList(elm_CoSimulation_StandAlone); break; // Needs to be modified if CoSimulation_Tool is added case elm_CoSimulation_StandAlone: popList(elm_Capabilities); break; // CoSimulation_StandAlone only has Capabilities case elm_ModelVariables: popList(elm_ScalarVariable); break; case elm_VendorAnnotations: popList(elm_Tool);break; case elm_Tool: popList(elm_Annotation); break; case elm_TypeDefinitions: popList(elm_Type); break; case elm_EnumerationType: popList(elm_Item); break; case elm_UnitDefinitions: popList(elm_BaseUnit); break; case elm_BaseUnit: popList(elm_DisplayUnitDefinition); break; case elm_DirectDependency: popList(elm_Name); break; case elm_Name: { // Exception: the name value is represented as element content. // All other values of the XML file are represented using attributes. Element* name = checkPop(elm_Name); if (!name) return; name->n = 2; name->attributes = malloc(2*sizeof(char*)); name->attributes[0] = attNames[att_input]; name->attributes[1] = data; data = NULL; skipData = 1; // stop recording element content stackPush(stack, name); break; } case -1: return; // illegal element error default: // must be a leaf Element printDebug("+++++++ got a leaf element\n"); assert(getAstNodeType(el)==astElement); break; } // All children of el removed from the stack. // The top element must be of type el now. checkPeek(el); }
static void pythonbriefLexer_insertImaginaryIndentDedentTokens( ppythonbriefLexer ctx, pANTLR3_TOKEN_SOURCE toksource ) { pANTLR3_COMMON_TOKEN t = pythonbriefLexer_nextTokenLowerLevelImpl( ctx, toksource ); vectorAdd( ctx->tokens, t, NULL ); // if not a NEWLINE, doesn't signal indent/dedent work; just enqueue if ( t->type != NEWLINE ) return; // Ignore newlines on hidden channel if ( t->channel == HIDDEN ) return; ANTLR3_INT32 newlineno = t->line + 1; // grab first token of next line (skip COMMENT tokens) for ( ; ; ) { t = pythonbriefLexer_nextTokenLowerLevelImpl( ctx, toksource ); if ( t->type == COMMENT ) // Pass comments to output stream (= skip processing here) { vectorAdd( ctx->tokens, t, NULL ); continue; } //Ignore LEADING_WS on HIDDEN channel - these are emited by empty line with some whitespaces on it if ( (t->type == LEADING_WS) && (t->channel == HIDDEN)) continue; break; } // compute cpos as the char pos of next non-WS token in line STACK_INT cpos; switch ( t->type ) { case EOF: cpos = -1; // pretend EOF always happens at left edge break; case LEADING_WS: cpos = t->getText( t )->len; break; default: cpos = t->charPosition; break; } STACK_INT lastIndent = (STACK_INT) stackPeek( ctx->identStack ); ANTLR3_INT32 lineno = t->line; if ( lineno <= 0 ) lineno = newlineno; if ( cpos > lastIndent ) { stackPush( ctx->identStack, (void *)cpos, NULL ); vectorAdd( ctx->tokens, pythonbriefLexer_createDedentIdentToken( ctx, INDENT, lineno ), NULL ); } else if (cpos < lastIndent) { ANTLR3_INT32 prevIndex = pythonbriefLexer_findPreviousIndent( ctx, cpos ); // generate DEDENTs for each indent level we backed up over while ( ctx->identStack->vector->count > (prevIndex + 1) ) { vectorAdd( ctx->tokens, pythonbriefLexer_createDedentIdentToken( ctx, DEDENT, lineno ), NULL ); stackPop( ctx->identStack ); } } // Filter out LEADING_WS tokens if ( t->type != LEADING_WS ) vectorAdd( ctx->tokens, t, NULL ); }