Example #1
0
//Function for accepting tokens in parser. Prints type/data
int accept(struct token* src,int curr){
	if(src->tok_type!=curr)
		return 0;
	else{
		char* temp;
		if(curr==STRING){
			temp=(char*)src->data;
			printf("ACCEPTED:%s,%s\n",put_token(curr),temp);
		}
		printf("ACCEPTED:%s\n",put_token(curr));
		return 1;
	}
}
Example #2
0
static void
parse(pairmatcher_t *pm, VALUE tokenizer, VALUE reporter)
{
  VALUE token_info;
  while ((token_info = get_token(tokenizer)) != Qnil) {
    VALUE token_type, token_text, token_lineno, token_byteno;
    VALUE token;
    Check_Type(token_info, T_ARRAY);
    if (RARRAY(token_info)->len != 8) {
      rb_raise(rb_eArgError, "unexpected token");
    }
    token_type = RARRAY(token_info)->ptr[0];
    token_text = RARRAY(token_info)->ptr[1];
    token_lineno = RARRAY(token_info)->ptr[2];
    token_byteno = RARRAY(token_info)->ptr[4];
    token = rb_funcall(Fragment, id_new, 4, token_type, token_text, token_lineno, token_byteno);
    if (intertoken_p(pm, token_type)) {
      rb_funcall(reporter, id_call, 1, token);
    }
    else {
      put_token(pm, token, reporter);
    }
  }
  finish(pm, reporter);
}
Example #3
0
/**
*\brief Fonction where_play, utilisée pour savoir où l'IA doit jouer.
*\details Cette fonction permet à l'IA (modulo sa profondeur) de savoir dans quelle colonne jouer, en se basant sur les
fonctions alphabeta et pond.
*\return a de type \a int, c'est le numéro de colonne (entre 1 et 7 donc) dans lequel l'IA doit jouer.
*/
int where_play(grid g,int depth,token t) {
	int i;
	int BestMove=1000 ;
	int tmp=1000 ;
	/* On initialise buf, mais Valgrind râle quand même, cf ligne 105 */
	char buf[7] = { 0 } ;
	int j=0 ;
	int z ;
	for(i=1;i<8;i++) {
		if(g->heights[i-1] != LINE_NB) {
			/* On pose un jeton */
			put_token(g,i,t) ;
			/* Si on est RED ... */
			if(t==RED) { 
				/* ... On lance alphabeta en indiquant que le prochain joueur à joueur joue les YELLOW */
				tmp=alphabeta(g,-1000,1000,depth,YELLOW) ; 
			}
			/* Si on est YELLOW ... */
			if(t==YELLOW) { 
				/* ... On lance alphabeta en indiquant que le prochain joueur à joueur joue les RED */
				tmp=alphabeta(g,-1000,1000,depth,RED) ; 
			}
			/* En cas d'égalité, on stocke à la suite dans un tableau, en vue de décider du choix aléatoirement : 
			permet de gérer le cas où quel que soit la colonne dans laquelle on joue " le cas est le même " */
			if(tmp==BestMove) { 
				buf[j]=i ;
				j = j+1 ;
			}
			/* Si une valeur renvoyée est < BestMove, alors on remet les indices à 0, et on écrase le tableau, pour
			favoriser les pondérations " faibles " */
			if(tmp<BestMove) { 
				j=0 ;
				for(z=0;z<7;z++) {
					buf[z] = 0 ;
				} 
				BestMove = tmp ;
				buf[j]=i ;
				j = j + 1 ;
			}
			/* On enlève le jeton : parcours destructif de l'arbre */
			erase_token(g,i) ;
		}
	}
	int k=0 ;
	int l=0 ;
	/* On compte le nombre de valeurs non nulles dans le tableau. */
	while(k<7&&buf[k]!=0) { 
		l = l + 1 ;
		k = k + 1 ;
	}
	/* On balance une fonction aléatoire, avec une seed horaire */
  	srand (time (NULL)); 
  	/* On module modulo le nombre de valeurs non nulle qu'il faut considérer dans buf[]
  	-> permet de choisir aléatoirement en cas d'égalité !*/
   	int a = rand()%l; 
	return(buf[a]) ; 
}
Example #4
0
void lex_stream_t::implementation_t::parse_token(char c)
{
    stream_lex_token_t  result;
    
    if (!(  is_number(c, result)
        ||  is_identifier_or_keyword(c, result)
        ||  is_comment(c, result)
        ||  is_string(c,result)
        ||  is_compound(c, result)
        ||  is_simple(c, result)))
        { throw_parser_exception("Syntax Error"); }

    put_token(adobe::move(result));
}
Example #5
0
void infix_to_prefix(char *expr,char *prefix_expr,int length) //prefix_expr儲存轉換後的結果 expr為infix expression 
{
     int i,count = 0; //count:目前prefix_expr的index從0開始 
     char ctemp;
     precedence token;
     element temp;
     
     while(length != -1) //每讀過1個字元length減1,讀完整個expr就跳出(由右往左讀) 
     {
                  token = get_token(expr,&length); //透過get_token讀取目前的字元類別 
                  if(token == space) continue;     //token為空白就跳到下一次讀取 
                  if(token == operand) prefix_expr[count++] = expr[length+1]; //token是數字或未知數就輸出到prefix_expr中 
                  else //token如果是運算子,stack為空(top=-1)或讀到token的優先度>=stack最上面token的優先度,push到stack 
                  {
                      temp.data = token;  //token存到暫存的element中 
                      if(token == leftparenthesis) //如果token為'(',代表')'已經在stack中 
                      {                            //透過put_token輸出stack中的token直到碰到')' 
                               while(stack[top].data != rightparenthesis) put_token(prefix_expr,pop(),&count);
                               pop();              // 移除stack中的')' 
                      }
                      else if(top == -1 || icp[token] >= isp[stack[top].data]) push(temp);
                      else  //讀到token的優先度<stack最上面的token 
                      {     //透過put_token輸出stack中的token直到token的優先度>=stack[top]才push 
                          while(top != -1 && icp[token] < isp[stack[top].data]) put_token(prefix_expr,pop(),&count);
                          push(temp);
                      }
                  }
     }
     while(top != -1) put_token(prefix_expr,pop(),&count);  //字串讀完將剩下的運算子輸出 
     for(i = 0 ; i < (count-(count%2))/2 ; i++) //因為結果是由後往前放(右往左讀),所以最後反轉prefix_expr 
     {
           ctemp = prefix_expr[i];
           prefix_expr[i] = prefix_expr[count-1-i];
           prefix_expr[count-1-i] = ctemp;
     }
}
Example #6
0
/**
*\brief Fonction alphabeta, qui est l'implémentation de l'algorithme alpha/beta.
*\details Cette fonction permet de générer l'arbre (détruit au fur et à mesure) de l'ensemble des coups et des
grilles possibles. Note pour l'utilisation pratique : Il faut alpha < beta, typiquement alpha = -INFINI et beta = INFINI.
*\return best de type \a int : c'est la valeur de la pondération affectée à une grille.
*/
int alphabeta(grid g, int alpha, int beta, int depth, token t) { 
	if(winner(g)==1||depth==1) {								
		return(pond(g)) ;
	}
	else {
		int best ;
		int column ;
		best = -INFINI ;
		for(column=1; column<=COLUMN_NB; column++) {
			if(g->heights[column-1]!=LINE_NB) {
				int val ;
				/* On joue le coup */
				put_token(g, column, t) ; 
				if(t==RED) {
					/* On alterne les coups */
					val = - alphabeta(g, -beta, -alpha, depth-1, YELLOW) ; 
				}
				if(t==YELLOW) {
					/* On alterne les coups */
					val = - alphabeta(g, -beta, -alpha, depth-1, RED) ; 
				}
				/* On annule le coup -> Parcours destructif de l'arbre */
				erase_token(g, column) ; 
				/* Coupures Alpha et Beta */
				if(val > best) { 
					best = val ;
					if(best > alpha) {
						alpha = best ;
						if(alpha >= beta) {
							return(best) ;
						}
					}
				}
			}
		}
		return(best) ;
	}
}
Example #7
0
NMErr
NMGetConfig(
    NMConfigRef	inConfig,
    char		*outConfigStr,
    NMSInt16	*ioConfigStrLen)
{
    DEBUG_ENTRY_EXIT("NMGetConfig");

    NMIPConfigPriv *theConfig = (NMIPConfigPriv *) inConfig;
    char			hostName[kMaxHostNameLen + 1];
    NMBoolean		putToken;
    NMUInt32		port;
    NMUInt32		tokenLen;
    NMErr			status = kNMNoError;

    op_vassert_return((inConfig != NULL),"Config ref is NULL!",kNMParameterErr);
    op_vassert_return((outConfigStr != NULL),"outConfigStr is NULL!",kNMParameterErr);
    op_vassert_return((theConfig->type == kModuleID),"Config ref doesn't belong to this module!",kNMInvalidConfigErr);
    op_vassert_return((theConfig->version <= kVersion),"Config ref belongs to a later version of this module!",kNMNewerVersionErr);
    op_vassert_return((ioConfigStrLen != NULL),"ioConfigStrLen is NULL!",kNMParameterErr);

    //Try_
    {

        *outConfigStr = '\0';

        //	Write the type
        tokenLen = sizeof (NMType);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigModuleType, LONG_DATA, &theConfig->type, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the version
        tokenLen = sizeof (NMUInt32);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigModuleVersion, LONG_DATA, &theConfig->version, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the game id
        tokenLen = sizeof (NMUInt32);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigGameID, LONG_DATA, &theConfig->gameID, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the name
        tokenLen = strlen(theConfig->name);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigGameName, STRING_DATA, &theConfig->name, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the connection mode
        tokenLen = sizeof (NMUInt32);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigEndpointMode, LONG_DATA, &theConfig->connectionMode, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the NetSprocket mode
        tokenLen = sizeof (NMBoolean);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigNetSprocketMode, BOOLEAN_DATA, &theConfig->netSprocketMode, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the custom data, if any
        tokenLen = theConfig->customEnumDataLen;

        if (tokenLen)
        {
            putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigCustomData, BINARY_DATA, &theConfig->customEnumData, tokenLen);

            op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);
        }

        //	Convert out host addr into a string
        OTInetHostToString(theConfig->address.fHost, hostName);

        //	Write the name
        tokenLen = strlen(hostName);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kIPConfigAddress, STRING_DATA, hostName, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        //	Write the port
        port = theConfig->address.fPort;
        tokenLen = sizeof (NMUInt32);
        putToken = put_token(outConfigStr, *ioConfigStrLen, kIPConfigPort, LONG_DATA, &port, tokenLen);

        op_vassert_return((putToken),"put_token returned false.  We need a bigger config string!",kNMInvalidConfigErr);

        *ioConfigStrLen = strlen(outConfigStr);

        return kNMNoError;
    }
    //Catch_(code)
error:
    if (status)
    {
        NMErr code = status;
        return code;
    }
    return status;
}