示例#1
0
int main(int argc, char *argv[]){
  FILE *fp;
  Token token;

  fp = fopen(argv[1], "r");
  lex_initialize(fp);

  do{
    token = lex_get_token();
    if(token.kind == INT_VALUE_TOKEN){
      printf("%d 整数\n", token.u.int_value);
    }else if(token.kind == IDENTIFIER_TOKEN){
      printf("%s 識別子\n", token.u.identifier);
    }else if(token.kind == STRING_LITERAL_TOKEN){
      printf("%s 文字列リテラル\n", token.u.string);
    }else if(token.kind >= EQ_TOKEN 
      && token.kind <= SEMICOLON_TOKEN){
      printf("%s 演算子または区切り子\n", 
        st_operator_table[token.kind - EQ_TOKEN].token);
    }else if(token.kind != END_OF_FILE_TOKEN 
      && token.kind >= IF_TOKEN){
      printf("%s 予約語\n",
        st_keyword_table[token.kind - IF_TOKEN].token);
    }
  }while(token.kind != END_OF_FILE_TOKEN);

  fclose(fp);

  return 0;
}
示例#2
0
文件: lex.c 项目: mrc0mmand/ifj2015
int main(int argc, char *argv[])
{
    lex_data_t d;
    lex_token_t t;

    if(argc < 2) {
        printf("Usage: %s source.code\n", argv[0]);
        exit(1);
    }

    lex_initialize(&d, argv[1]);

    while(lex_get_token(&d, &t) == 0) {
        switch(t.type) {
        case LEX_INTEGER:
        case LEX_DOUBLE:
        case LEX_LITERAL:
        case LEX_IDENTIFIER:
        case LEX_KW_INT:
        case LEX_KW_DOUBLE:
        case LEX_KW_STRING:
        case LEX_KW_AUTO:
        case LEX_KW_CIN:
        case LEX_KW_COUT:
        case LEX_KW_FOR:
        case LEX_KW_IF:
        case LEX_KW_ELSE:
        case LEX_KW_RETURN:
            printf("%d [%s, %s]\n", d.line + 1, ENUM_TO_STR(t.type), t.val);
        break;
        case LEX_EOF:
            printf("%d [%s, ]\n", d.line + 1, ENUM_TO_STR(t.type));
            exit(0);
        break;
        default:
            printf("%d [%s, ]\n", d.line + 1, ENUM_TO_STR(t.type));
        }
    }

    lex_destroy(&d);
}
示例#3
0
文件: main.c 项目: cglinden/autocook
int
main(int argc, char **argv)
{
    int             retval;

    /*
     * Some versions of cron(8) and at(1) set SIGCHLD to SIG_IGN.
     * This is kinda dumb, because it breaks assumprions made in
     * libc (like pclose, for instance).  It also blows away most
     * of Cook's process handling.  We explicitly set the SIGCHLD
     * signal handling to SIG_DFL to make sure this signal does what
     * we expect no matter how we are invoked.
     */
#ifdef SIGCHLD
    signal(SIGCHLD, SIG_DFL);
#else
    signal(SIGCLD, SIG_DFL);
#endif

    /*
     * initialize things
     * (order is critical here)
     */
    progname_set(argv[0]);
    str_initialize();
    id_initialize();
    lex_initialize();

    /*
     * parse the COOK environment variable
     */
    arglex_init_from_env(argv[0], argtab);
    argparse(OPTION_LEVEL_ENVIRONMENT);

    /*
     * parse the command line
     */
    arglex_init(argc, argv, argtab);
    argparse(OPTION_LEVEL_COMMAND_LINE);

    option_tidy_up();

    log_open();

    /*
     * turn on progress stars if they asked for them
     */
    if (option_test(OPTION_STAR))
        star_enable();

    /*
     * If we were asked to update the fingerprints, do it here.
     * We don't actually ant to read in the cookbook.
     */
    if (option.fingerprint_update)
    {
        fp_tweak();
        quit(0);
    }

    /*
     * read in the cook book
     *
     * If there are #include-cooked directives,
     * we may need to do it more than once.
     */
    if (!option.o_book)
        fatal_intl(0, i18n("no book found"));
    for (;;)
    {
        int             status;
        size_t          j;

        builtin_initialize();

        /*
         * instanciate the command line variable assignments
         */
        for (j = 0; j < option.o_vardef.nstrings; ++j)
        {
            char            *s;
            char            *cp;
            string_ty       *name;
            string_ty       *value;
            string_list_ty  wl;
            opcode_context_ty *ocp;

            s = option.o_vardef.string[j]->str_text;
            cp = strchr(s, '=');
            assert(cp);
            if (!cp)
                continue;
            name = str_n_from_c(s, cp - s);
            value = str_from_c(cp + 1);
            str2wl(&wl, value, (char *)0, 0);
            str_free(value);
            ocp = opcode_context_new(0, 0);
            opcode_context_id_assign(ocp, name, id_variable_new(&wl), 0);
            opcode_context_delete(ocp);
            str_free(name);
            string_list_destructor(&wl);
        }

        set_command_line_goals();

        parse(option.o_book);
        status = cook_auto_required();
        if (status < 0)
            quit(1);
        if (!status)
            break;
        id_reset();
        cook_reset();
    }

    /*
     * work out what to cook.
     * If no targets have been given, use the first explicit recipe.
     */
    set_command_line_goals();
    if (!option.o_target.nstrings)
        cook_find_default(&option.o_target);
    assert(option.o_target.nstrings);

    /*
     * cook the target
     */
    if (option.pairs)
        retval = cook_pairs(&option.o_target);
    else if (option.script)
        retval = cook_script(&option.o_target);
    else if (option.web)
        retval = cook_web(&option.o_target);
    else
        retval = cook(&option.o_target);

#ifdef DEBUG
    fflush_slowly_report();
#endif

    quit(retval);
    /*NOTREACHED*/
    return 0;
}