template<class I,class T,class input_iterator_type>input_iterator_type turingMachine<I,T>::parse(const input_iterator_type &iterator) throw(typename turingMachine<I,T>::MACHINE_NOT_ACTIVE)
{
			if(is_active())
			{
				pair<bool,input_iterator_type> current=read_character(iterator);
				while(!current.first)
						current=read_character(current.second);
				return current.second;
			}
			else
				throw MACHINE_NOT_ACTIVE();
}
Exemplo n.º 2
0
// STRING_LITERAL_QUOTE and STRING_LITERAL_SINGLE_QUOTE
// Initial quote is already eaten by caller
static Ref
read_STRING_LITERAL(SerdReader* reader, SerdNodeFlags* flags, uint8_t q)
{
	Ref ref = push_node(reader, SERD_LITERAL, "", 0);
	while (true) {
		const uint8_t c = peek_byte(reader);
		uint32_t      code;
		switch (c) {
		case '\n': case '\r':
			r_err(reader, SERD_ERR_BAD_SYNTAX, "line end in short string\n");
			return pop_node(reader, ref);
		case '\\':
			eat_byte_safe(reader, c);
			if (!read_ECHAR(reader, ref, flags) &&
			    !read_UCHAR(reader, ref, &code)) {
				r_err(reader, SERD_ERR_BAD_SYNTAX,
				      "invalid escape `\\%c'\n", peek_byte(reader));
				return pop_node(reader, ref);
			}
			break;
		default:
			if (c == q) {
				eat_byte_check(reader, q);
				return ref;
			} else {
				read_character(reader, ref, flags, eat_byte_safe(reader, c));
			}
		}
	}
	eat_byte_check(reader, q);
	return ref;
}
Exemplo n.º 3
0
/* Wait for any of the characters in the given character set to be read from
 * stdin.  If charset is NULL wait for any character.  Returns 0 when the
 * timeout occurs. */
char wait_for_character(const char *charset, struct timespec *timeout)
{
    struct termios term;
    tcflag_t lflag;
    char c;

    /* switch off line buffering */
    (void) tcgetattr(STDIN_FILENO, &term);
    lflag = term.c_lflag;
    term.c_lflag &= ~ICANON;
    (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);

    for (;;) {
        c = read_character(timeout);

        if (c == 0 || charset == NULL)
            break;
        else if (strchr(charset, c) != NULL)
            break;
    }

    /* restore line buffering */
    term.c_lflag = lflag;
    (void) tcsetattr(STDIN_FILENO, TCSANOW, &term);

    return c;
}
Exemplo n.º 4
0
// STRING_LITERAL_LONG_QUOTE and STRING_LITERAL_LONG_SINGLE_QUOTE
// Initial triple quotes are already eaten by caller
static Ref
read_STRING_LITERAL_LONG(SerdReader* reader, SerdNodeFlags* flags, uint8_t q)
{
	Ref ref = push_node(reader, SERD_LITERAL, "", 0);
	while (true) {
		const uint8_t c = peek_byte(reader);
		uint32_t      code;
		switch (c) {
		case '\\':
			eat_byte_safe(reader, c);
			if (!read_ECHAR(reader, ref, flags) &&
			    !read_UCHAR(reader, ref, &code)) {
				r_err(reader, SERD_ERR_BAD_SYNTAX,
				      "invalid escape `\\%c'\n", peek_byte(reader));
				return pop_node(reader, ref);
			}
			break;
		default:
			if (c == q) {
				eat_byte_safe(reader, q);
				const uint8_t q2 = eat_byte_safe(reader, peek_byte(reader));
				const uint8_t q3 = peek_byte(reader);
				if (q2 == q && q3 == q) {  // End of string
					eat_byte_safe(reader, q3);
					return ref;
				} else {
					*flags |= SERD_HAS_QUOTE;
					push_byte(reader, ref, c);
					read_character(reader, ref, flags, q2);
				}
			} else {
				read_character(reader, ref, flags, eat_byte_safe(reader, c));
			}
		}
	}
	return ref;
}
Exemplo n.º 5
0
    GrammarUnknown(const std::string& __signature,
		   const std::string& __parameter,
		   const std::string& __character)
      : base_type(1),
	signature(&signature_type::create(__signature)),
	__grammar_oov(new GrammarMutable(1)),
	backoff(),
	ngram(),
	unigram(),
	logprob_unk(0),
	feature_character("rule-character")
    {
      base_type::read(__parameter);
      read_character(__character);
    }
Exemplo n.º 6
0
Arquivo: read.c Projeto: troter/thesis
/* <simple datum> -> <boolean> | <number> | <character> | <string> | <symbol>
 */
static SCM read_simple_datum(FILE *file, int previous)
{
    int c;
    c = previous == 0 ? skip_comment_and_space(file) : previous;

    if (SCM_INITIAL_CHARACTERS_P(c)) { /* <symbol> */
        return read_symbol(file, c);
    }
    if (isdigit(c)) { /* <number> */
        return read_number(file, c);
    }
    if (SCM_PECULIAR_IDENTIFIER_P(c)) { /* <number> or <symbol> */
        return read_number_or_peculiar(file, c);
    }

    switch (c) {
    case EOF:
        return SCM_EOF;
    case '#': /* <boolean> or <character> or <number prefix> */
        c = fgetc(file);
        switch (c) {
        case EOF:
            scheme_error("syntax error");
            /* <boolean> */
        case 't':    return SCM_TRUE;
        case 'f':    return SCM_FALSE;

            /* <character> */
        case '\\':
            return read_character(file);

            /* <number prefix> */
        default:
            scheme_error("unsupport number prefix");
        }
    case '"': /* <string> */
        return read_string(file);
    default:
        scheme_error("unsupport character");
    }
}
// returns a token on the heap, or a null pointer
Token *scan_string(char **buf) {
  char *starting_buf = *buf;
  static int in_multiline_comment = false;
  char c = read_character(buf);

  if (in_multiline_comment) {
    if (c == '*') {
      if (read_character(buf) == '/') {
        in_multiline_comment = false;
        return token_new(COMMENT, "/* ... */", 9); 
      } 
      unread_character(buf);
    }
    if (c != '\0') {
      return scan_string(buf); 
    }
  }

  switch(c) {
    case ':':
      if (read_character(buf) == '=') {
        return token_new(ASSIGN, ":=", 2);
      } 
      unread_character(buf);
    case '+':
      return token_new(PLUS, "+", 1);
    case '-':
      return token_new(MINUS, "-", 1);
    case '*':
      return token_new(TIMES, "*", 1);
    case '/':
      c = read_character(buf);
      if (c == '/') {
        int len = 2;
        while ((c = read_character(buf)) && c != '\n' && c != EOF) {
          len++; 
        }
        unread_character(buf);
        return token_new(COMMENT, *buf - len, len);
      } else if (c == '*' && !in_multiline_comment) {
        in_multiline_comment = true;
        return scan_string(buf);
      }
      unread_character(buf); 
      return token_new(DIV, "/", 1);
    case '(':
      return token_new(LPAREN, "(", 1);
    case ')':
      return token_new(RPAREN, ")", 1);
    case ' ':
    case '\t':
    case '\n':
      return scan_string(buf);
    case '\0':
    case EOF:
      return NULL;
  }
  // try and parse an identifer or keyword
  if (isalpha(c)) {
    // check if keyword 
    char *keywords[] = {"read", "write"};
    int keywords_len = 2;
    for (int i = 0; i < keywords_len; i++) {
      if (strncmp(*buf - 1, keywords[i], strlen(keywords[i])) == 0) {
        *buf += strlen(keywords[i]) - 1; 
        return token_new(KEYWORD, keywords[i], strlen(keywords[i]));
      }
    }

    // parse as identifer
    int len = 1;
    while ((c = read_character(buf)) && isalnum(c)) {
      len++; 
    }
    // unread the character that broke the while loop
    unread_character(buf);
    return token_new(IDENTIFIER, *buf - len, len);
  }

  if (isdigit(c) || c == '.') {
    // parse as digit
    int len = 1;
    int dot_found = (c == '.');
    while ((c = read_character(buf))) {
      if (c == '.') {
        if (dot_found) {
          break;
        } 
        dot_found = true;
      } else if (!isdigit(c)) {
        break;
      } 
      len++;
    }  
    // unread the character that broke the while loop
    unread_character(buf);
    return token_new(NUMBER, *buf - len, len);
  }

  char debug_str[2] = {c, '\0'};
  return token_new(INVALID, debug_str, 1);
}
Exemplo n.º 8
0
int main(int argc, char* argv[]) {
  clear_screen();
  print_string("\nExamen de ETC de ensamblador\n");
  while (true) {
    print_string("\n\n 1. Lectura de enteros por teclado\n 2. Cálculo del Máximo Común divisor (MCD)\n 3. Lectura de fracciones por teclado\n 4. Simplificación de fracciones\n 5. Suma de dos fracciones\n 6. Suma de un array de fracciones\n 7. Salir\n\nElige una opción: ");
    char opc = read_character();
    print_string("\n\n");
    if (opc == '1') {
      int n = preguntar_entero("Introduce un entero: ");
      print_string("\nEl entero leído ha sido: ");
      print_integer(n);
      print_string("\n");
    } else if (opc == '2') {
      int a = preguntar_entero("Introduce un número (a): ");
      int b = preguntar_entero("Introduce otro número (b): ");
      print_string("\nEl MCD de a y b es: ");
      print_integer(mcd(a, b));
      print_string("\n");
    } else if (opc == '3') {
      Fraccion f;
      preguntar_fraccion(&f);
      print_string("\nLa fraccion leída ha sido: ");
      print_fraccion(&f);
      print_string("\n");
    } else if (opc == '4') {
      Fraccion f;
      preguntar_fraccion(&f);
      print_string("\nLa fraccion leída ha sido: ");
      print_fraccion(&f);
      simplificar_fraccion(&f);
      print_string("\nLa fraccion simplificada es: ");
      print_fraccion(&f);
      print_string("\n");
    } else if (opc == '5') {
      Fraccion fa;
      print_string("\nLeyendo la primera fracción:\n");
      preguntar_fraccion(&fa);
      print_string("La primera fraccion leída ha sido: ");
      print_fraccion(&fa);
      Fraccion fb;
      print_string("\nLeyendo la segunda fracción:\n");
      preguntar_fraccion(&fb);
      print_string("La segunda fraccion leída ha sido: ");
      print_fraccion(&fb);
      Fraccion fc;
      sumar_fracciones(&fc, &fa, &fb);
      print_string("\nLa suma es: ");
      print_fraccion(&fc);
      simplificar_fraccion(&fc);
      print_string("\nLa suma simplificada es: ");
      print_fraccion(&fc);
      print_string("\n");
    } else if (opc == '6') {
      print_string("\n");
      for (int i = 0; i < NUM_FRACCIONES - 1; ++i) {
        print_fraccion(&fracciones[i]);
        print_string(" + ");
      }
      print_fraccion(&fracciones[NUM_FRACCIONES - 1]);
      print_string(" = ");
      Fraccion fr;
      sumar_fracciones_array(&fr, fracciones, NUM_FRACCIONES);
      print_fraccion(&fr);
      print_string("\n");
    } else if (opc == '7') {
      print_string("¡Adiós!\n");
      mips_exit(0);
    } else {
      print_string("Opción incorrecta. Pulse cualquier tecla para seguir.\n");
      read_character();
    }
  }
}
Exemplo n.º 9
0
scheme_object* read(vm* context, FILE* in)
{
	int c;
	eat_whitespace(in);
	c = getc(in);
	
	if(isdigit(c) || (c == '-' && (isdigit(peek(in)))))
	{
		short sign = 1;//positive
		long num = 0;
		//read a number
		if(c == '-')
		{
			sign = -1;
		}
		else
		{
			ungetc(c, in);
		}
		while(isdigit(c = getc(in)))
		{
			num = num *10 + (c -'0');
		}
		num *= sign;
		if(is_delimiter(c))
		{
			ungetc(c, in);
			return make_number(context, num);
		}
		
		//its not followed by a delimiter
		fprintf(stderr, "Number not followed by a delimiter!\n");
		exit(1);
	}
	else if(c == '#')
	{//character or boolean
		c = getc(in);

		switch(c)
		{
			case 't':
				return (scheme_object*) true;
			case 'f':
				return (scheme_object*) false;
			case '\\':
				return read_character(context, in);
			default:
				fprintf(stderr, "unknown boolean or character literal\n");
				exit(1);
		}
	}
	else if(c =='"')
	{
		return read_string(context, in);
	}
	else if(c == '(')
	{
		return read_pair(context, in);
	}
	else if(is_initial(c) || ((c == '+' || c == '-') && is_delimiter(peek(in))))
	{
		ungetc(c, in);
		return read_symbol(context, in);
	}
	else if(c == '\'')
	{
		return cons(context, (scheme_object*) quote_symbol, cons(context, read(context, in), the_empty_list));
	}
	else if(c == EOF)
	{
		return NULL;
	}
	else
	{
		fprintf(stderr, "Unexpected input: '%c'\n", c);
		exit(1);
	}
	fprintf(stderr, "read: illegal state\n");
	exit(1);
}
Exemplo n.º 10
0
Arquivo: io.c Projeto: cmatei/yalfs
object lisp_read(FILE *in)
{
	int c;

	while ((c = fgetc(in)) != EOF) {
		/* atmosphere */
		if (isspace(c)) {
			continue;
		}
		else if (c == ';') {
			while (c != EOF && c != '\n')
				c = fgetc(in);
			continue;
		}
		/* characters, booleans or numbers with radix */
		else if (c == '#') {
			c = fgetc(in);

			switch (c) {
			/* number prefixes */
			case 'b':
			case 'B':
			case 'o':
			case 'O':
			case 'x':
			case 'X':
			case 'd':
			case 'D':
			case 'e':
			case 'i':
				ungetc(c, in);
				return read_number(in);

			/* booleans */
			case 't':
			case 'T':
				return the_truth;
			case 'f':
			case 'F':
				return the_falsity;

			/* characters */
			case '\\':
				return read_character(in);

			/* vectors */
			case '(':
				ungetc(c, in);
				return read_vector(in);

			/* commented form, read and discard */
			case ';':
				lisp_read(in);
				continue;

			case '<':
				error("Object cannot be read back -- read", nil);


			default:
				error("Unexpected character -- read", nil);
			}
		}
		/* number */
		else if (isdigit(c) ||
			 ((c == '-') && isdigit(peek_char(in))) ||
			 ((c == '+') && isdigit(peek_char(in)))) {
			ungetc(c, in);
			return read_number(in);
		}
		/* string */
		else if (c == '"') {
			return read_string(in);
		}
		/* symbol */
		else if (is_initial(c)) {
			ungetc(c, in);
			return read_identifier(in);
		}
		/* peculiar identifiers */
		else if (((c == '+') || c == '-') && is_delimiter(peek_char(in))) {
			return make_symbol_c((c == '+' ? "+" : "-"));
		}
		/* stuff starting with dot, FIXME for floats */
		else if (c == '.') {
			if (is_delimiter(peek_char(in)))
				error("Illegal use of . -- read", nil);

			c = fgetc(in);
			if (c != '.' || peek_char(in) != '.')
				error("Symbol has bad name -- read", nil);

			c = fgetc(in);
			if (!is_delimiter(peek_char(in)))
				error("Symbol has bad name -- read", nil);

			return _ellipsis;
		}
		/* pair */
		else if (c == '(') {
			return read_pair(in);
		}
		/* quote */
		else if (c == '\'') {
			return cons(_quote, cons(lisp_read(in), nil));
		}
		/* quasiquote */
		else if (c == '`') {
			return cons(_quasiquote, cons(lisp_read(in), nil));
		}
		/* unquote & unquote-splicing */
		else if (c == ',') {
			if (peek_char(in) == '@') {
				c = fgetc(in);
				return cons(_unquote_splicing, cons(lisp_read(in), nil));
			} else
				return cons(_unquote, cons(lisp_read(in), nil));
		}
		else
			error("Unexpected character -- read", nil);
	}

	return end_of_file;
}