Exemplo n.º 1
0
void stemfile(struct stemmer * z, FILE * f)
{  while(TRUE)
{  int ch = getc(f);
	if (ch == EOF) return;
	if (LETTER(ch))
	{  int i = 0;
		while(TRUE)
		{  if (i == i_max)
		{  i_max += INC;
			s = realloc(s, i_max + 1);
		}
            ch = tolower(ch); /* forces lower case */
			
            s[i] = ch; i++;
            ch = getc(f);
            if (!LETTER(ch)) { ungetc(ch,f); break; }
		}
		s[stem(z, s, i - 1) + 1] = 0;
		/* the previous line calls the stemmer and uses its result to
            zero-terminate the string in s */
		printf("%s",s);
	}
	else putchar(ch);
}
}
Exemplo n.º 2
0
static void
findstats(Pos p, Ori o)
{
	/* Recalculate cross assert and score total at 'p'
	 */
	Pos	left, right;
	Word lword, rword;
	Node n;
	Edge e;
	int	s;

	lword.n = rword.n = 0;
	if(EDGE(p))
		return;

	/* find word to the left */
	s = 0;
	for(left=PREV(p,o); HASLETTER(left); left = PREV(left,o))
		;
	left = NEXT(left,o);
	while (HASLETTER(left)) {
		lword.c[lword.n++] = LETTER(left);
		s += SCORE(left);
		left = NEXT(left,o);
	}
	/* find word to the right */
	for(right=NEXT(p,o); HASLETTER(right); right = NEXT(right,o)) {
		rword.c[rword.n++] = LETTER(right);
		s += SCORE(right);
	}
	if(DBG) {
		wordprint(&lword);
		print("X");
		wordprint(&rword);
		print(" [%d] ", s);
	}

	SIDE(p,o) = s;
	ISANCHOR(p) = true;

	/* calculate cross asserts */
	CROSS(p,o) = 0;
	n = traverse(root, &lword, 0);
	assert(n>=0);
	if(n>0)
		do {
			e = dict[n++];
			if ( (rword.n && isword(NODE(e), &rword)) || 
				 (!rword.n && TERM(e)) ) {
				CROSS(p,o) |= 1 << LET(e);
				DPRINT("%c, ", LET(e)+'a');
			}
		} while (!(LAST(e)));
	DPRINT("\n");
}
Exemplo n.º 3
0
void build_tokens(int * alphabet, char * word) {
	memset(alphabet, 0, 26*sizeof(int));
	for(;*word; word++) {
		if(!isalpha(*word)) error("Non-alphabetic character encountered");
		alphabet[ LETTER(*word) ] += 1;
	}	
}	
Exemplo n.º 4
0
int load_data(int WORDS, struct stemmer **stem_list, FILE *f) {
  static int a_max = WORDS;
  int a_size = 0;
  while (a_size < WORDS) {
    int ch = getc(f);
    if (ch == EOF) return a_size;
    char *s = (char *)sirius_malloc(i_max + 1);
    if (LETTER(ch)) {
      int i = 0;
      while (TRUE) {
        if (i == i_max) {
          i_max += INC;
          void *_realloc = NULL;
          if ((_realloc = realloc(s, i_max + 1)) == NULL) {
            fprintf(stderr, "realloc() failed!\n");
            return -ENOMEM; 
          }
          s = (char*)_realloc;
        }
        ch = tolower(ch); /* forces lower case */

        s[i] = ch;
        i++;
        ch = getc(f);
        if (!LETTER(ch)) {
          ungetc(ch, f);
          break;
        }
      }
      struct stemmer *z = create_stemmer();
      z->b = s;
      z->k = i - 1;
      stem_list[a_size] = z;
      if (a_size == a_max) {
        a_max += A_INC;
        void *_realloc = NULL;
        if ((_realloc = realloc(stem_list, a_max)) == NULL) {
            fprintf(stderr, "realloc() failed!\n");
            return -ENOMEM; 
        }
        stem_list = (struct stemmer **)_realloc;
      }
      a_size += 1;
    }
  }
  return a_size;
}
Exemplo n.º 5
0
int match_word(char * word, int * tokens) {
	int i, copy[26];
	memcpy(copy, tokens, 26 * sizeof(int));
	for(i = 0; word[i] && isalpha(word[i]); i++) {
		if(!(copy[LETTER(word[i])]--)) return 0; /* the magic line */
	}
	return (i > 2) && !(word[i+1]);
}
Exemplo n.º 6
0
unsigned int Alphabet::getIndex(Letter letter) const {
	if (letter >= kFirstBaseLetter && letter <= kLastBaseLetter) {
		return static_cast<unsigned int>(letter) - static_cast<unsigned int>(LETTER('a'));
	} else {
		size_t index = diacritics_.find_first_of(letter);
		assert(index != std::wstring::npos);
		return kBaseLetterCount + index;
	}
}
Exemplo n.º 7
0
/*
 * Return a pointer to the first occurence of the given keyword in the string
 * or NULL if not found.  Unlike 'strstr()', which verifies that the match is
 * against an identifier-token.
 */
char *
strkey(char *src, const char *key)
{
    if (src != 0 && key != 0) {
	char *s = src, *d;
	size_t len = strlen(key);

	while (*s) {
	    if (!LETTER(*s)) {
		s++;
	    } else {
		for (d = s; LETTER(*d); d++) ;
		if ((d - s) == (int) len && !strncmp(s, key, len))
		    return s;
		s = d;
	    }
	}
    }
    return NULL;
}
Exemplo n.º 8
0
/*
 * Delete a specified keyword from a string if it appears there
 */
void
strcut(char *src, const char *key)
{
    char *s, *d;

    if ((s = strkey(src, key)) != 0) {
	d = s + strlen(key);
	while (*d != '\0' && !LETTER(*d))
	    d++;
	while ((*s++ = *d++) != '\0') ;
    }
}
Exemplo n.º 9
0
static void stemfile(FILE * f)
{  while(TRUE)
   {  int ch = getc(f);
      if (ch == EOF) return;
      if (LETTER(ch))
      {  int i = 0;
         while(TRUE)
         {  if (i == i_max) increase_s();

            ch = tolower(ch); /* forces lower case */

            s[i] = ch; i++;
            ch = getc(f);
            if (!LETTER(ch)) { ungetc(ch,f); break; }
         }
         s[stem(s,0,i-1)+1] = 0;
         /* the previous line calls the stemmer and uses its result to
            zero-terminate the string in s */
         printf("%s",s);
      }
      else putchar(ch);
   }
}
Exemplo n.º 10
0
void
play(Play *play)
{
/* put the pieces on the board,
 * adjust the board data structures.
 */
	int		j;
	Pos		p;
	char	c;

	firstmove = false;

	/* describe the play */
	if(1) {
		print("(%d,%d,%c) %d ",
			play->pos.x, play->pos.y, play->o ? 'V' : 'H', play->score);
		wordprint(&play->word);
		print("\n");
		if(DBG)
			score(&play->word, play->pos, play->o);
	}

	assert(isword(root, &play->word));

	/* place the letters */
	p = play->pos;
	for (j= play->word.n -1; j>= 0; j--) {
		if(!HASLETTER(p)){
			/* place the letter on the board */
			LETTER(p) = play->word.c[j];
			HASLETTER(p)=true;
			SPECIAL(p) = Not;	/* not needed */
			ISANCHOR(p) = false;
			SCORE(p) = play->word.blank[j] ? 0 : points[play->word.c[j]];
			adjust(p,ORTHO(play->o));
		}
		p = PREV(p,play->o);
	}
	p = NEXT(p,play->o);
	adjust(p, play->o);	 /* squares to the sides */
}
Exemplo n.º 11
0
void stemfile(FILE * f)
{  while(TRUE)
   {  int ch = getc(f);
      if (ch == EOF) return;
      if (LETTER(ch))
      {  int i = 0;
         while(TRUE)
         {  if (i == i_max) increase_s();

            if UC(ch) ch = FORCELC(ch);
            /* forces lower case. Remove this line to make the program work
               exactly like the Muscat stemtext command. */

            s[i] = ch; i++;
            ch = getc(f);
            if (!LETTER(ch)) { ungetc(ch,f); break; }
         }
         s[stem(s,0,i-1)+1] = 0;
         /* the pevious line calls the stemmer and uses its result to
            zero-terminate the string in s */
         printf("%s",s);
      }
      else putchar(ch);
   }
Exemplo n.º 12
0
/* FIXME: little-endian byte ordering is assumed */
int
reuse_writehld(char *str, size_t size, long double const *pval,
               int prec, int flags)
{
#if R_LONG_DOUBLE_IS_DOUBLE - 0 == 1
  return reuse_writehd(str, size, (double const *) pval, prec, flags);
#else
#if CONF_PRINTF_LG_WORKS - 0 == 1 && CONF_PRINTF_A_WORKS - 0 == 1
  char format[32];
  int i = 0;

  format[i++] = '%';
  if ((flags & REUSE_FORMAT_PLUS)) format[i++] = '+';
  else if ((flags & REUSE_FORMAT_SPC)) format[i++] = ' ';
  if ((flags & REUSE_FORMAT_ALT)) format[i++] = '#';
  if (prec >= 0) {
    format[i++] = '.';
    format[i++] = '*';
  }
  format[i++] = 'L';
  if ((flags & REUSE_FORMAT_UP)) {
    format[i++] = 'A';
  } else {
    format[i++] = 'a';
  }
  format[i] = 0;
  if (prec >= 0) {
    return os_snprintf(str, size, format, prec, *pval);
  } else {
    return os_snprintf(str, size, format, *pval);
  }
#else
  size_t ol = 0;
  const unsigned long *pw = (const unsigned long *) pval;
  unsigned long ww[3];
  unsigned long long m;
  int d;
  int ed;
  char expbuf[32], *pexp;
  unsigned char const *digit_conv = _reuse_dig2charlower;

  if ((flags & REUSE_FORMAT_UP)) digit_conv = _reuse_dig2charupper;

  ww[0] = pw[0];
  ww[1] = pw[1];
  ww[2] = pw[2];
  ww[2] &= 0xFFFF;

  if (!ww[0] && ww[1] == 0xc0000000 && ww[2] == 0xFFFF) {
    PUT_TO_STRING(str, size, ol, LETTER('n'));
    PUT_TO_STRING(str, size, ol, LETTER('a'));
    PUT_TO_STRING(str, size, ol, LETTER('n'));
    return reuse_strnput0(str, size, ol);
  }

  if ((ww[2] & 0x8000)) {
    PUT_TO_STRING(str, size, ol, '-');
  } else if ((flags & REUSE_FORMAT_PLUS)) {
    PUT_TO_STRING(str, size, ol, '+');
  } else if ((flags & REUSE_FORMAT_SPC)) {
    PUT_TO_STRING(str, size, ol, ' ');
  }
  ww[2] &= 0x7fff;

  if (!ww[0] && !ww[1] && !ww[2]) {
    PUT_TO_STRING(str, size, ol, '0');
    PUT_TO_STRING(str, size, ol, LETTER('x'));
    PUT_TO_STRING(str, size, ol, '0');
    if (prec >= 1) {
      PUT_TO_STRING(str, size, ol, '.');
      for (; prec; prec--) {
        PUT_TO_STRING(str, size, ol, '0');
      }
    } else if ((flags & REUSE_FORMAT_ALT)) {
      PUT_TO_STRING(str, size, ol, '.');
    }
    PUT_TO_STRING(str, size, ol, LETTER('p'));
    PUT_TO_STRING(str, size, ol, '+');
    PUT_TO_STRING(str, size, ol, '0');
    return reuse_strnput0(str, size, ol);
  }
  if (!ww[0] && ww[1] == 0x80000000 && ww[2] == 0x7FFF) {
    PUT_TO_STRING(str, size, ol, LETTER('i'));
    PUT_TO_STRING(str, size, ol, LETTER('n'));
    PUT_TO_STRING(str, size, ol, LETTER('f'));
    return reuse_strnput0(str, size, ol);
  }

  if (prec < 0) prec = 15;
  PUT_TO_STRING(str, size, ol, '0');
  PUT_TO_STRING(str, size, ol, LETTER('x'));
  m = ((unsigned long long) ww[1] << 32) | ww[0];
  if (prec < 15) {
    rullong_t old_m = m;
    m += R_U64(0x0800000000000000) >> prec * 4;
    if (m < old_m) {
      PUT_TO_STRING(str, size, ol, '1');
    }
  }
Exemplo n.º 13
0
extern int EiC_lexan(void)
{
    int t=0, loop; char c=0, EiC_nextchar();

#ifdef ILOOKAHEAD

    token = &EiC_TokenArray[EiC_TokenP];

    if(EiC_TokenR > 0) {
	EiC_TokenR--;
	EiC_TokenI++;
	EiC_TokenP=(EiC_TokenP+1)%MAX_TOKENS;
	return token->Tok;
    }


#else

    if (STOKEN != NOTOKEN) {
	STOKEN = NOTOKEN;
	return token->Tok;
    }

#endif
    
    loop  = 1;
    state = 0;
    while (loop) {
	switch (state) {
	  case 0: lex_lastpos = lex_curpos; c = EiC_nextchar();
	    state = (WHITE(c) ? 0 :
		    (c == '\n' ? lex_lineno++, 0 :
		    (c == '<' ? t = LT, 1 :
		    (c == '>' ? t = GT, 2 :
		    (c == '+' ? t = '+', 3 :
		    (c == '-' ? t = '-', 4 :
		    (c == '|' ? t = BOR, 5 :
		    (c == '&' ? t = AND, 6 :
		    (c == '\''? 7 :
		    (c == '"' ? 8 :
		    (c == '.' ? 9 :  
		    (c == '/' ? t = '/', c = EiC_nextchar(), 50 :
		    (c == '%' ? t = '%', c = EiC_nextchar(), 50 :
		    (c == '*' ? t = '*', c = EiC_nextchar(), 50 :
		    (c == '=' ? t = ASS, c = EiC_nextchar(), 50 :
		    (c == '!' ? t = NOT, c = EiC_nextchar(), 50 :
		    (c == '^' ? t = XOR, c = EiC_nextchar(), 50 :
			//(c == '~' ? t = NOTB, c = EiC_nextchar(), 50 : //maks: ~
		     fail(RELOP, c))))))))))))))))));
	    break;
	  case 1: /* get <,  <= and << */
	    if ((c = EiC_nextchar()) == '<') t = LSHT;
	    else state = 50;
	    break;
	  case 2: /* get >, >= and >> */
	    if ((c = EiC_nextchar()) == '>') t = RSHT;
	    else state = 50;
	    break;
	  case 3: c = EiC_nextchar();                         /* get +, += or ++ */
	    if (c == '+') t = INC, state = 60;
	    else state = 50;
	    break;
	  case 4: c = EiC_nextchar();                            /* get -, -= -- */
	    state = 60;
	    if (c == '-') t = DEC;
	    else if (c == '>') t = RARROW;
	    else state = 50;
	    break;
	  case 5: c = EiC_nextchar();                         /* get |, |= or || */
	    if (c == '|') t = LOR, state = 60;
	    else state = 50;
	    break;
	  case 6: c = EiC_nextchar();                         /* get &, &= or && */
	    if (c == '&') t = LAND, state = 60;
	    else state = 50;
	    break;
	  case 7:token->Val.ival = charliteral(EiC_nextchar()); /* char_constants */
	    t = TOKEN_CHAR;
	    if (EiC_nextchar() != '\'')
		EiC_error("Missing single quote '");
	    state = 60;
	    break;
	  case 8: EiC_stringliteral();                        /* string literals */
	    token->Tok = STR;
	    /*return STR;*/ loop = 0; break;
	  case 9: c = EiC_nextchar();
	    t = '.';
	    if(DIGIT(c)) 
		state = 22;
	    else
		state = 60;
	    retract(c);
	    break;
	  case 10: c = EiC_nextchar();              /* identifiers and  keywords */
	    state = (LETTER(c) ? 11 :
		    (c == '_' ? 11 : fail(ID, c)));
	    break;
	  case 11: c = EiC_nextchar();
	    state = (LETTER(c) ? 11 :
		    (DIGIT(c) ? 11 :
		    (c == '_' ? 11 : 12)));
	    break;
	  case 12: retract(c); success(ID); /*return (token->Tok);*/ loop = 0; break;

	  case 20: c = EiC_nextchar();                     /* integers and reals */
	    state = (c == '0' ? 30 :
		    (DIGIT(c) ? 21 : fail(TOKEN_INT, c)));
	    break;
	  case 21: c = EiC_nextchar();
	    state = (DIGIT(c) ? 21 :
		    (c == '.' ? 22 :
		    (c == 'e' ? 23 :
		    (c == 'E' ? 23 : 25))));
	    break;
	  case 22: c = EiC_nextchar();
	    state = (DIGIT(c) ? 22 :
		    (c == 'e' ? 23 :
		    (c == 'E' ? 23 : 26)));
	    break;
	  case 23: c = EiC_nextchar();
	    state = (c == '+' ? 24 :
		    (c == '-' ? 24 :
		    (DIGIT(c) ? 24 : fail(TOKEN_FLOAT, c) /* ??? */ )));
	    break;
	  case 24: c = EiC_nextchar();
	    state = (DIGIT(c) ? 24 : 26);
	    break;
	  case 25: checkExt(c); success(TOKEN_INT); /*return (token->Tok);*/ loop = 0; break;
	  case 26: checkExt(c); success(TOKEN_FLOAT); /*return (token->Tok);*/ loop = 0; break;
	  case 27: checkExt(c); success(HEX);   /*return (token->Tok);*/ loop = 0; break;
	  case 28: checkExt(c); success(OCTAL); /*return (token->Tok);*/ loop = 0; break;
	  case 30:			  /* check for octal and hex numbers */
	    if ((c = EiC_nextchar()) == 'x' || c == 'X') {
		while (gethex((c = EiC_nextchar())) > -1);
		state = 27;
		break;
	    }
	    if (c != '.' && c != 'e' && c != 'E') {
		while (getoct(c) > -1)
		    c = EiC_nextchar();
		state = 28;
		break;
	    }
	    retract(c); state = 21; break;
	  case 50:                                      /* mix with equal's  */
	    if (c == '=')
		switch (t) {
		  case '+': t = ADDEQ;  break;		/* += */
		  case '-': t = SUBEQ;  break;		/* -= */
		  case '/': t = DIVEQ;  break;		/* /= */
		  case '*': t = MULEQ;  break;		/* *= */
		  case '%': t = MODEQ;  break;		/* %= */
		  case ASS: t = EQ;     break;		/* == */
		  case GT:  t = GE;     break;		/* >= */
		  case LT:  t = LE;     break;		/* <= */
		  case NOT: t = NE;     break;		/* != */
		  case RSHT:t = RSHTEQ; break;		/* >>= */
		  case LSHT:t = LSHTEQ; break;		/* <<= */
		  case AND: t = ANDEQ;  break;		/* &= */
		  case BOR: t = BOREQ;  break;		/* |= */
		  case XOR: t = XOREQ;  break;		/* ^= */
		  //case NOTB: t = NOTBEQ;  break;		/* maks ~= */
		  default: retract(c);
	    }
		else if(c == '/' && t == '/') //maks
		{
			//C++ comment
			//Only for colorize
			//Comments are removed by preprocessor before parser

			do 
			{
				c = EiC_nextchar();
				
			} while(c && c != '\n');

			retract(c);

			success(MISC); 
			token->Tok = TOKEN_COMMENT;
			loop = 0;
			break;
		}
		else retract(c);
	    state = 60;
	    break;
	  case 60: success(MISC); token->Tok = t; /*return (token->Tok);*/ loop = 0; break;
	  case 100: token->Tok = EiC_nextchar(); /*return (token->Tok);*/ loop = 0; break;
	}
    }

#ifdef ILOOKAHEAD

    if(EiC_TokenI<MAX_TOKENS)
	EiC_TokenI++;

    EiC_TokenP = (EiC_TokenP +1)%MAX_TOKENS;

#endif

    return token->Tok;


}
Exemplo n.º 14
0
int32 SCRIPT_ParseBuffer(int32 scripthandle, char *data, int32 length)
{
	char *fence = data + length;
	char *dp, *sp, ch=0, lastch=0;
	char *currentsection = "";
	char *currententry = NULL;
	char *currentvalue = NULL;
	enum {
		ParsingIdle,
		ParsingSectionBegin,
		ParsingSectionName,
		ParsingEntry,
		ParsingValueBegin,
		ParsingValue
	};
	enum {
		ExpectingSection = 1,
		ExpectingEntry = 2,
		ExpectingAssignment = 4,
		ExpectingValue = 8,
		ExpectingComment = 16
	};
	int state;
	int expect;
	int linenum=1;
	int rv = 0;
#define SETRV(v) if (v>rv||rv==0) rv=v

	if (!data) return 1;
	if (length < 0) return 1;
	
	dp = sp = data;
	state = ParsingIdle;
	expect = ExpectingSection | ExpectingEntry;

#define EATLINE(p) while (length > 0 && *p != '\n' && *p != '\r') { p++; length--; }
#define LETTER() { lastch = ch; ch = *(sp++); length--; }

	while (length > 0) {
		switch (state) {
			case ParsingIdle:
				LETTER();
				switch (ch) {
					// whitespace
					case ' ':
					case '\t': continue;
					case '\n': if (lastch == '\r') continue; linenum++; continue;
					case '\r': linenum++; continue;

					case ';':
					/*case '#':*/
						  EATLINE(sp);
						  continue;

					case '[': if (!(expect & ExpectingSection)) {
							  // Unexpected section start
							  printf("Unexpected start of section on line %d.\n", linenum);
							  SETRV(-1);
							  EATLINE(sp);
							  continue;
						  } else {
							  state = ParsingSectionBegin;
							  continue;
						  }

					default:  if (isalpha(ch)) {
							  if (!(expect & ExpectingEntry)) {
								  // Unexpected name start
								  printf("Unexpected entry label on line %d.\n", linenum);
								  SETRV(-1);
								  EATLINE(sp);
								  continue;
							  } else {
								  currententry = dp = sp-1;
								  state = ParsingEntry;
								  continue;
							  }
						  } else {
							  // Unexpected character
							  printf("Illegal character (ASCII %d) on line %d.\n", ch, linenum);
							  SETRV(-1);
							  EATLINE(sp);
							  continue;
						  }
				}

			case ParsingSectionBegin:
				currentsection = dp = sp;
				state = ParsingSectionName;
			case ParsingSectionName:
				LETTER();
				switch (ch) {
					case '\n':
					case '\r':	// Unexpected newline
						printf("Unexpected newline on line %d.\n", linenum);
						SETRV(-1);
						state = ParsingIdle;
						linenum++;
						continue;

					case ']':
						*(dp) = 0;	// Add new section
						expect = ExpectingSection | ExpectingEntry;
						state = ParsingIdle;
						EATLINE(sp);
						continue;

					default:
						dp++;
						continue;
				}

			case ParsingEntry:
				LETTER();
				switch (ch) {
					case ';':
					/*case '#':*/
						// unexpected comment
						EATLINE(sp);
						printf("Unexpected comment on line %d.\n", linenum);
						SETRV(-1);
					case '\n':
					case '\r':
						// Unexpected newline
						printf("Unexpected newline on line %d.\n", linenum);
						SETRV(-1);
						expect = ExpectingSection | ExpectingEntry;
						state = ParsingIdle;
						linenum++;
						continue;

					case '=':
						// Entry name finished, now for the value
						while (*dp == ' ' || *dp == '\t') dp--;
						*(++dp) = 0;
						state = ParsingValueBegin;
						continue;

					default:
						dp++;
						continue;
				}

			case ParsingValueBegin:
				currentvalue = dp = sp;
				state = ParsingValue;
			case ParsingValue:
				LETTER();
				switch (ch) {
					case '\n':
					case '\r':
						// value complete, add it using parsed name
						while (*dp == ' ' && *dp == '\t') dp--;
						*(dp) = 0;
						while (*currentvalue == ' ' || *currentvalue == '\t') currentvalue++;
						state = ParsingIdle;
						linenum++;

						SCRIPT_AddSection(scripthandle,currentsection);
						SCRIPT_AddEntry(scripthandle,currentsection,currententry,currentvalue);
						continue;

					default:
						dp++;
						continue;
				}

			default: length=0;
				 continue;
		}
	}

	if (sp > fence) printf("Stepped outside the fence!\n");

	return rv;
}
Exemplo n.º 15
0
void getToken()
{
    char tmp;
    token.clear();                         //清空token
    while( BLANK(ch) ){                 //跳过空白
        getch();
    }

    if(last_is_character && left_right_single_quotation){  //上一个是左单引号
        last_is_character = false;
        symbol = "CHARACTER";
        while(ch != '\''){              //获取字符内容(理论上单引号内应该就一个字符)
            token += ch;
            getch();
        }
    }
    else if(last_is_string && left_right_double_quotation){ //上一个是左双引号
        last_is_string = false;
        symbol = "STRING";
        while(ch != '"'){
            token += ch;
            getch();
        }
    }
    else if( LETTER(ch) ){                  //以下是跟引号无关的正常判断:是字母,读出字符串
        while(NUM_LETTER(ch)){
            token += ch;
            tmp = ch;
            getch();
        }

        //查找是否在保留字中
        map<string, string>::iterator it = revWord.find(token);
        if(it != revWord.end()){        //找到,是保留字
            symbol = it->second;
            printToken(symbol, token);
        }
        else{                           //不是保留字,是标识符
            symbol = "IDENTIFIER";
            printToken(symbol, token);
        }
    }
    else if( NUM(ch) ){
        while(NUM(ch)){
            token += ch;
            tmp = ch;
            getch();
        }

        symbol = "NUMBER";
        printToken(symbol, token);
    }
    else if(ch == ':'){
        token += ch;
        tmp = ch;
        getch();
        if(ch == '='){
            symbol = "ASSIGNSY";            //:=
            token += ch;
            printToken(symbol, ":=");
            getch();                    //else语句中不能再放getch()了,下一个字符已经被读过了
        }
        else{
            symbol = charSymb[tmp];         //:
            // cout << ++counter << " " << symbol << " " << tmp << endl;
        }
    }
    else if(ch == '<'){
        tmp = ch;
        token += ch;
        getch();
        if(ch == '='){
            symbol = "LEQ";                 //<=
            token += ch;
            printToken(symbol, "<=");
            getch();                                //!!!!!!!!!!!
        }
        else if(ch == '>'){
            symbol = "NEQ";                 //<>
            token += ch;
            printToken(symbol, "<>");
            getch();                                //!!!!!!!!!!!
        }
        else{
            symbol = charSymb[tmp];         //<
            // cout << ++counter << " " << symbol << " " << tmp << endl;
        }
    }
    else if(ch == '>'){
        tmp = ch;
        token += ch;
        getch();
        if(ch == '='){
            symbol = "GEQ";                 //>=
            token += ch;
            printToken(symbol, ">=");
            getch();                                //!!!!!!!!!!!
        }
        else{
            symbol = charSymb[tmp];         //>
            // cout << ++counter << " " << symbol << " " << tmp << endl;
        }
    }
    else{                                   //其余单字操作符
        symbol = charSymb[ch];
        token += ch;
        // cout << ++counter << " " << symbol << " " << ch << endl;
        if(ch == '\''){                     //单引号和双引号还要再处理一些东西
            last_is_character = true;
            left_right_single_quotation = !left_right_single_quotation;
        }
        else if(ch == '"'){
            last_is_string = true;
            left_right_double_quotation = !left_right_double_quotation;
        }
        getch();           //!!!!太重要了
    }


}
Exemplo n.º 16
0
#include "Alphabet.h"

#include <assert.h>
#include <iostream>

static const wchar_t kFirstBaseLetter = LETTER('a');
static const wchar_t kLastBaseLetter = LETTER('z');
static const unsigned int kBaseLetterCount = kLastBaseLetter - kFirstBaseLetter + 1;

Alphabet::Alphabet(std::wstring diacritics)
		: diacritics_(diacritics + LETTER('#')) {
}

unsigned int Alphabet::getLetterCount() const {
	return kLastBaseLetter - kFirstBaseLetter + 1 + diacritics_.length();
}

unsigned int Alphabet::getIndex(Letter letter) const {
	if (letter >= kFirstBaseLetter && letter <= kLastBaseLetter) {
		return static_cast<unsigned int>(letter) - static_cast<unsigned int>(LETTER('a'));
	} else {
		size_t index = diacritics_.find_first_of(letter);
		assert(index != std::wstring::npos);
		return kBaseLetterCount + index;
	}
}

wchar_t Alphabet::getLetter(unsigned int index) const {
	if (index < kBaseLetterCount) {
		return kFirstBaseLetter + index;
	} else {
Exemplo n.º 17
0
char*
valid(Play *play)
{
	/* Return NULL if play is valid, or a reason if the play is invalid.
	 */
	Pos	p;
	Pos	p2;
	int	j;
	char	c;
	static char	buf[LLEN];
	int	n;
	Bool	newletter;	/* uses at least one new letter */
	Bool	crosscentre;	/* crosses the centre square */
	Bool	hasanchor;	/* crosses at least one anchor */

	if(DBG) {
		print("assert (%d,%d,%c)", play->pos.x,
			play->pos.y, play->o == LR ? 'H' : 'V');
		wordprint(&play->word);
	}

	p = play->pos;
	p2 = NEXT(play->pos,play->o);
	if (HASLETTER(p2)){
		return "abuts another word\n";
	}
	if(!isword(root, &play->word)){
		return "not a word";
	}

	newletter = crosscentre = hasanchor = false;

	/* For each letter of the word. */
	for(j= play->word.n - 1; j>=0; j--) {
		if (p.x < 0 || p.y < 0 || p.x > BLEN || p.y > BLEN)
			return "off the edge";

		c = play->word.c[j];

		if (ISANCHOR(p)){
			hasanchor = true;
		}
		if (HASLETTER(p)) {
			if (LETTER(p) != c){
				sprintf(buf,"wanted %c, got %c at (%d,%d)",
					c+'a', LETTER(p)+'a', p.x, p.y);
				return buf;
			}
		} else {
			newletter = true;
			if(!firstmove){
				if(!(CROSS(p,ORTHO(play->o)) & 1 << c)) {
					sprintf(buf,"invalid cross word at (%d,%d)",p.x,p.y);
					return buf;
				}
			}
		}
		if (p.x == 8 && p.y ==8){
			crosscentre = true;
		}
		p = PREV(p,play->o);
	}

	if (firstmove){
		DPRINT("FIRSTMOVE\n");
		if (!crosscentre)
			return ("first move doesn't touch centre square");
	}
	if (!(hasanchor|| firstmove)){
		return ("not attached to another word");
	}	
	if(HASLETTER(p))
		return "abutting another word";

	if (! newletter)
		return "adds no letters";
	return (char*)0;
}
Exemplo n.º 18
0
char *
gstrptime(char *s, char *fmt, struct tm *tm)
{
    FPRINTF((stderr, "gstrptime(\"%s\", \"%s\")\n", s, fmt));

    /* linux does not appear to like years before 1902
     * NT complains if its before 1970
     * initialise fields to midnight, 1st Jan, 1970 (for relative times)
     */
    tm->tm_sec = tm->tm_min = tm->tm_hour = 0;
    tm->tm_mday = 1;
    tm->tm_mon = 0;
    tm->tm_year = 70;
    /* oops - it goes wrong without this */
    tm->tm_isdst = 0;

    for (; *fmt && *s; ++fmt) {
        if (*fmt != '%') {
            if (*s != *fmt)
                return s;
            ++s;
            continue;
        }
        assert(*fmt == '%');

        switch (*++fmt) {
        case 0:
            /* uh oh - % is last character in format */
            return s;
        case '%':
            /* literal % */
            if (*s++ != '%')
                return s - 1;
            continue;

#define NOTHING	/* nothing */
#define LETTER(L, width, field, extra)		\
	case L:					\
	    s=read_int(s,width,&tm->field);	\
	    extra;				\
	    continue;

            LETTER('d', 2, tm_mday, NOTHING);
            LETTER('m', 2, tm_mon, NOTHING);
            LETTER('y', 2, tm_year, NOTHING);
            LETTER('Y', 4, tm_year, tm->tm_year -= 1900);
            LETTER('H', 2, tm_hour, NOTHING);
            LETTER('M', 2, tm_min, NOTHING);
            LETTER('S', 2, tm_sec, NOTHING);
#undef NOTHING
#undef LETTER

        default:
            int_error(DATAFILE, "incorrect time format character");
        }
    }

    FPRINTF((stderr, "Before mktime : %d/%d/%d:%d:%d:%d\n", tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec));
    /* mktime range-checks the time */

    if (mktime(tm) == -1) {
        FPRINTF((stderr, "mktime() was not happy\n"));
        int_error(DATAFILE, "Invalid date/time [mktime() did not like it]");
    }
    FPRINTF((stderr, "After mktime : %d/%d/%d:%d:%d:%d\n", tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec));

    return s;
}
Exemplo n.º 19
0
Alphabet::Alphabet(std::wstring diacritics)
		: diacritics_(diacritics + LETTER('#')) {
}
Exemplo n.º 20
0
    /* '3'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* '4'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* '5'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* '6'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* '7'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* '8'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* '9'  */ bit_alnum | bit_print | bit_digit | bit_graph | bit_xdigit,
    /* ':'  */ bit_print | bit_punct | bit_graph,
    /* ';'  */ bit_print | bit_punct | bit_graph,
    /* '<'  */ bit_print | bit_punct | bit_graph,
    /* '='  */ bit_print | bit_punct | bit_graph,
    /* '>'  */ bit_print | bit_punct | bit_graph,
    /* '?'  */ bit_print | bit_punct | bit_graph,
    /* '@'  */ bit_print | bit_punct | bit_graph,

    /* 'A'  */ LETTER (bit_upper | bit_xdigit),
    /* 'B'  */ LETTER (bit_upper | bit_xdigit),
    /* 'C'  */ LETTER (bit_upper | bit_xdigit),
    /* 'D'  */ LETTER (bit_upper | bit_xdigit),
    /* 'E'  */ LETTER (bit_upper | bit_xdigit),
    /* 'F'  */ LETTER (bit_upper | bit_xdigit),
    /* 'G'  */ LETTER (bit_upper),
    /* 'H'  */ LETTER (bit_upper),
    /* 'I'  */ LETTER (bit_upper),
    /* 'J'  */ LETTER (bit_upper),
    /* 'K'  */ LETTER (bit_upper),
    /* 'L'  */ LETTER (bit_upper),
    /* 'M'  */ LETTER (bit_upper),
    /* 'N'  */ LETTER (bit_upper),
    /* 'O'  */ LETTER (bit_upper),
    /* 'P'  */ LETTER (bit_upper),
Exemplo n.º 21
0
static
void
big_file(int size)
{
	int i, j, fileid;

	printf("[BIGFILE] test starting :\n");
	printf("\tCreating a file of size: %d\n", size);

	fileid = open(BIGFILE_NAME, O_WRONLY|O_CREAT|O_TRUNC, 0664);
	if (fileid < 0) {
		err(1, "[BIGFILE]: %s: open for write", BIGFILE_NAME);
	}

	for(i = 0; i < BUFFER_SIZE; i++) {
		fbuffer[i] = LETTER(i);
	}

	printf("\tWriting to file.\n");
	for (i = 0; i < size; i += BUFFER_SIZE) {
		write(fileid, fbuffer, BUFFER_SIZE);

		if (!(i % (10 * BUFFER_SIZE))) {
			printf("\rBW : %d", i);
		}
	}

	printf("\n\tReading from file.\n");
	close(fileid);

	fileid = open(BIGFILE_NAME, O_RDONLY);
	if (fileid < 0) {
		err(1, "[BIGFILE]: %s: open for read", BIGFILE_NAME);
	}

	for (i = 0; i < size; i += BUFFER_SIZE) {
		j = read(fileid, fbuffer, BUFFER_SIZE);
		if (j<0) {
			err(1, "[BIGFILE]: read");
		}
		if (j != BUFFER_SIZE) {
			errx(1, "[BIGFILE]: read: only %d bytes", j);
		}
	}

	if (!(i % (10 * BUFFER_SIZE))) {
		printf("\rBR : %d", i);
	}

	/* Check to see that the data is consistent : */
	for (j = 0; j < BUFFER_SIZE; j++) {
		if (fbuffer[j] != LETTER(j)) {
			errx(1, "[BIGFILE] : Failed read check : "
			     "inconsistent data read: %d", i+j);
		}
	}


	close(fileid);
	if (remove(BIGFILE_NAME)) {
		err(1, "[BIGFILE]: %s: remove", BIGFILE_NAME);
	}

	printf("\n[BIGFILE] : Success!\n");
}