int main(int argc, char ** argv) { arena_init(); init_lexer(); execute_program(parse_program()); return 0; }
void push_two_tokens_to_stack(void) { char * code = "int main()\ {\ return 2;\ }"; lexer l; token_base * token1; token_base * token2; // Init stuff init_lexer(&l, code); token1 = (token_base *) malloc(sizeof(token_int_value)); token2 = (token_base *) malloc(sizeof(token_int_value)); // Check the stuff push_back(&l, token1); TEST_ASSERT_EQUAL(l.stack->token, token1); push_back(&l, token2); TEST_ASSERT_EQUAL(l.stack->token, token2); // Destroy stuff destroy_lexer(&l); free(token1); free(token2); }
static void compile_file(char *file) { strcpy(lexfile, file); // "-" means reading from stdin if (strcmp(file, "-")) yyin = fopen(lexfile, "r"); else yyin = stdin; if (yyin == NULL) { strcpy(rdef_err_file, lexfile); rdef_err = RDEF_FILE_NOT_FOUND; return; } init_lexer(); init_parser(); if (setjmp(abort_jmp) == 0) { yyparse(); } // About error handling: If the bison-generated parser encounters // a syntax error, it calls yyerror(), aborts parsing, and returns // from yyparse(). For other kinds of errors (semantics, problem // writing to BResources, etc), we bail out with a longjmp(). From // then on, we can tell success or failure by looking at rdef_err. clean_up_lexer(); clean_up_parser(); clean_up_mem(); }
void init_parser(parser_info *pinfo) { /* parser_info *pinfo = (parser_info *) malloc(sizeof(parser_info)); */ pinfo->linfo = (lexer_info *) malloc(sizeof(lexer_info)); init_lexer(pinfo->linfo); pinfo->cur_lex = NULL; pinfo->save_str = 0; }
/** Read configuration file. * @return Zero on failure, non-zero on success. */ int read_configuration_file(void) { conf_error = 0; feature_unmark(); /* unmark all features for resetting later */ clear_nameservers(); /* clear previous list of DNS servers */ if (!init_lexer()) return 0; yyparse(); deinit_lexer(); feature_mark(); /* reset unmarked features */ conf_already_read = 1; return 1; }
bool compile(char *source, Chunk *chunk) { setup_types(); setup_parse_rules(); init_lexer(source); compiling_chunk = chunk; parser.had_error = false; parser.panic_mode = false; advance(); no_block_statement(); consume(TOKEN_EOF, "Expect end of expression."); end_compiler(); return !parser.had_error; }
int main(int argc, const char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s source_file_name reserved_word_file_name\n", argv[0]); exit(1); } /* argv[1] is filename, argv[2] is reserved_word_file */ init_lexer(argv[1]); init_reserved_words(argv[2]); init_output(argv[1]); DEBUG_PRINT(("PARSING\n")); return parse(); }
void read_token_else(void) { char * code = "else"; lexer l; token_base * token; // Init stuff init_lexer(&l, code); // Check the stuff token = next(&l); TEST_ASSERT_EQUAL(T_ELSE, token->type); // Destroy stuff destroy_lexer(&l); }
void read_token_semicolon(void) { char * code = ";"; lexer l; token_base * token; // Init stuff init_lexer(&l, code); // Check the stuff token = next(&l); TEST_ASSERT_EQUAL(T_SEMICOLON, token->type); // Destroy stuff destroy_lexer(&l); }
void read_token_return(void) { char * code = "return"; lexer l; token_base * token; // Init stuff init_lexer(&l, code); // Check the stuff token = next(&l); TEST_ASSERT_EQUAL(T_RETURN, token->type); // Destroy stuff destroy_lexer(&l); }
void read_token_function(void) { char * code = "main"; lexer l; token_base * token; // Init stuff init_lexer(&l, code); // Check the stuff token = next(&l); TEST_ASSERT_EQUAL(T_FUNCTION, token->type); // Destroy stuff destroy_lexer(&l); }
void read_token_int_value(void) { char * code = "1234"; lexer l; token_base * token; // Init stuff init_lexer(&l, code); // Check the stuff token = next(&l); TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); // Destroy stuff destroy_lexer(&l); }
void read_token_not(void) { char * code = "!"; lexer l; token_base * token; // Init stuff init_lexer(&l, code); // Check the stuff token = next(&l); TEST_ASSERT_EQUAL(T_BOOLEAN_OP, token->type); // Destroy stuff destroy_lexer(&l); }
int main() { lexer_info linfo; init_lexer(&linfo); do { lexeme *lex = get_lex(&linfo); print_lex(stdout, lex); if (lex->type == LEX_EOFILE) return 0; destroy_lex(lex); if (linfo.state == ST_ERROR) { fprintf(stderr, "(>_<)\n"); continue; } } while (1); }
void simple_file_only_main(void) { char * code = "int main()\ {\ return 2;\ }"; lexer l; token_base * token; // Initialize lexer init_lexer(&l, code); /* * Check that the tokens generated are the right ones */ token = next(&l); // int TEST_ASSERT_EQUAL(T_INT_TYPE, token->type); token = next(&l); // main TEST_ASSERT_EQUAL(T_FUNCTION, token->type); token = next(&l); // ( TEST_ASSERT_EQUAL(T_OPAR, token->type); token = next(&l); // ) TEST_ASSERT_EQUAL(T_CPAR, token->type); token = next(&l); // { TEST_ASSERT_EQUAL(T_OBRA, token->type); token = next(&l); // return TEST_ASSERT_EQUAL(T_RETURN, token->type); token = next(&l); // 2 TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); token = next(&l); // ; TEST_ASSERT_EQUAL(T_SEMICOLON, token->type); token = next(&l); // } TEST_ASSERT_EQUAL(T_CBRA, token->type); token = next(&l); // END_OF_FILE TEST_ASSERT_EQUAL(T_END_OF_FILE, token->type); // Destroy lexer destroy_lexer(&l); }
void push_token_and_call_next(void) { char * code = "int main()\ {\ return 2;\ }"; lexer l; token_base * token; token_base * received; // Init stuff init_lexer(&l, code); token = (token_base *) malloc(sizeof(token_int_value)); // Check the stuff push_back(&l, token); received = next(&l); TEST_ASSERT_EQUAL(token, received); // Destroy stuff destroy_lexer(&l); free(token); }
Lexer* Lexer_init_with_bytes(const char* bytes, int len) { Lexer* lexer = init_lexer(); Stream_init_with_buffer(&lexer->stream, bytes, len); return lexer; }
Lexer* Lexer_init_with_file(FILE *file) { Lexer* lexer = init_lexer(); Stream_init_with_file(&lexer->stream, file); return lexer; }
void if_with_else(void) { char * code = "int main()\ {\ if (1 == 1)\ {\ return 2;\ }\ else\ {\ return 4;\ }\ return 3;\ }"; lexer l; token_base * token; // Initialize lexer init_lexer(&l, code); /* * Check that the tokens generated are the right ones */ token = next(&l); // int TEST_ASSERT_EQUAL(T_INT_TYPE, token->type); token = next(&l); // main TEST_ASSERT_EQUAL(T_FUNCTION, token->type); token = next(&l); // ( TEST_ASSERT_EQUAL(T_OPAR, token->type); token = next(&l); // ) TEST_ASSERT_EQUAL(T_CPAR, token->type); token = next(&l); // { TEST_ASSERT_EQUAL(T_OBRA, token->type); token = next(&l); // if TEST_ASSERT_EQUAL(T_IF, token->type); token = next(&l); // ( TEST_ASSERT_EQUAL(T_OPAR, token->type); token = next(&l); // 1 TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); token = next(&l); // == TEST_ASSERT_EQUAL(T_BOOLEAN_OP, token->type); token = next(&l); // 1 TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); token = next(&l); // ) TEST_ASSERT_EQUAL(T_CPAR, token->type); token = next(&l); // { TEST_ASSERT_EQUAL(T_OBRA, token->type); token = next(&l); // return TEST_ASSERT_EQUAL(T_RETURN, token->type); token = next(&l); // 2 TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); token = next(&l); // ; TEST_ASSERT_EQUAL(T_SEMICOLON, token->type); token = next(&l); // } TEST_ASSERT_EQUAL(T_CBRA, token->type); token = next(&l); // else TEST_ASSERT_EQUAL(T_ELSE, token->type); token = next(&l); // { TEST_ASSERT_EQUAL(T_OBRA, token->type); token = next(&l); // return TEST_ASSERT_EQUAL(T_RETURN, token->type); token = next(&l); // 4 TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); token = next(&l); // ; TEST_ASSERT_EQUAL(T_SEMICOLON, token->type); token = next(&l); // } TEST_ASSERT_EQUAL(T_CBRA, token->type); token = next(&l); // return TEST_ASSERT_EQUAL(T_RETURN, token->type); token = next(&l); // 3 TEST_ASSERT_EQUAL(T_INT_VALUE, token->type); token = next(&l); // ; TEST_ASSERT_EQUAL(T_SEMICOLON, token->type); token = next(&l); // } TEST_ASSERT_EQUAL(T_CBRA, token->type); token = next(&l); // END_OF_FILE TEST_ASSERT_EQUAL(T_END_OF_FILE, token->type); // Destroy lexer destroy_lexer(&l); }
static bool parse_fstab_mnt_options(const char *mnt_options, unsigned long *flags, char **unknown_opts) { struct lexer_data lexer; enum token_result tr; char *dup_options = NULL; assert(unknown_opts); assert(flags); *unknown_opts = NULL; *flags = 0; /* fstab(5) implies that options can't be empty */ if (mnt_options[0] == '\0') { log_message( "Could not parse fstab: missing mount options field\n"); goto err; } /* As lexer destroys argument, send it a copy */ dup_options = strdup(mnt_options); if (dup_options == NULL) { log_message("Could not parse fstab: %m\n"); goto err; } init_lexer(&lexer, dup_options, strlen(dup_options) + 1); while (true) { char *opt = NULL; tr = next_token(&lexer, &opt, ',', true, false); if (tr == TOKEN_END) { /* Ok, no more tokens */ break; } else if (tr == TOKEN_OK) { if (!add_option_flag(opt, flags)) { /* This option is unknown. Let's add to the ones * sent to fs */ if (!add_unknown_option(unknown_opts, opt)) { log_message( "Could not parse fstab: %m\n"); goto err; } } } else if (tr == TOKEN_UNFINISHED_QUOTE) { log_message( "Could not parse fstab: unfinished quote at '%s'\n", opt); goto err; } else { /* TOKEN_BLANK, just ignore it */ } } free(dup_options); return true; err: free(*unknown_opts); free(dup_options); *unknown_opts = NULL; return false; }
void init_soar_agent(agent* thisAgent) { /* JC ADDED: initialize the rhs function linked list */ thisAgent->rhs_functions = NIL; /* --- initialize everything --- */ init_symbol_tables(thisAgent); create_predefined_symbols(thisAgent); init_production_utilities(thisAgent); init_built_in_rhs_functions (thisAgent); init_rete (thisAgent); init_lexer (thisAgent); init_firer (thisAgent); init_decider (thisAgent); init_soar_io (thisAgent); init_chunker (thisAgent); init_tracing (thisAgent); init_explain(thisAgent); /* AGR 564 */ select_init(thisAgent); predict_init(thisAgent); init_memory_pool( thisAgent, &( thisAgent->gds_pool ), sizeof( goal_dependency_set ), "gds" ); init_memory_pool( thisAgent, &( thisAgent->rl_info_pool ), sizeof( rl_data ), "rl_id_data" ); init_memory_pool( thisAgent, &( thisAgent->rl_et_pool ), sizeof( rl_et_map ), "rl_et" ); init_memory_pool( thisAgent, &( thisAgent->rl_rule_pool ), sizeof( rl_rule_list ), "rl_rules" ); init_memory_pool( thisAgent, &( thisAgent->wma_decay_element_pool ), sizeof( wma_decay_element ), "wma_decay" ); init_memory_pool( thisAgent, &( thisAgent->wma_decay_set_pool ), sizeof( wma_decay_set ), "wma_decay_set" ); init_memory_pool( thisAgent, &( thisAgent->wma_wme_oset_pool ), sizeof( wma_pooled_wme_set ), "wma_oset" ); init_memory_pool( thisAgent, &( thisAgent->wma_slot_refs_pool ), sizeof( wma_sym_reference_map ), "wma_slot_ref" ); init_memory_pool( thisAgent, &( thisAgent->epmem_wmes_pool ), sizeof( epmem_wme_stack ), "epmem_wmes" ); init_memory_pool( thisAgent, &( thisAgent->epmem_info_pool ), sizeof( epmem_data ), "epmem_id_data" ); init_memory_pool( thisAgent, &( thisAgent->smem_wmes_pool ), sizeof( smem_wme_stack ), "smem_wmes" ); init_memory_pool( thisAgent, &( thisAgent->smem_info_pool ), sizeof( smem_data ), "smem_id_data" ); init_memory_pool( thisAgent, &( thisAgent->epmem_literal_pool ), sizeof( epmem_literal), "epmem_literals" ); init_memory_pool( thisAgent, &( thisAgent->epmem_pedge_pool ), sizeof( epmem_pedge ), "epmem_pedges" ); init_memory_pool( thisAgent, &( thisAgent->epmem_uedge_pool ), sizeof( epmem_uedge ), "epmem_uedges" ); init_memory_pool( thisAgent, &( thisAgent->epmem_interval_pool ), sizeof( epmem_interval ), "epmem_intervals" ); thisAgent->epmem_params->exclusions->set_value( "epmem" ); thisAgent->epmem_params->exclusions->set_value( "smem" ); thisAgent->smem_params->base_incremental_threshes->set_string( "10" ); #ifdef REAL_TIME_BEHAVIOR /* RMJ */ init_real_time(thisAgent); #endif /* --- add default object trace formats --- */ add_trace_format (thisAgent, FALSE, FOR_ANYTHING_TF, NIL, "%id %ifdef[(%v[name])]"); add_trace_format (thisAgent, FALSE, FOR_STATES_TF, NIL, "%id %ifdef[(%v[attribute] %v[impasse])]"); { Symbol *evaluate_object_sym; evaluate_object_sym = make_sym_constant (thisAgent, "evaluate-object"); add_trace_format (thisAgent, FALSE, FOR_OPERATORS_TF, evaluate_object_sym, "%id (evaluate-object %o[object])"); symbol_remove_ref (thisAgent, evaluate_object_sym); } /* --- add default stack trace formats --- */ add_trace_format (thisAgent, TRUE, FOR_STATES_TF, NIL, "%right[6,%dc]: %rsd[ ]==>S: %cs"); add_trace_format (thisAgent, TRUE, FOR_OPERATORS_TF, NIL, "%right[6,%dc]: %rsd[ ] O: %co"); reset_statistics (thisAgent); /* RDF: For gSKI */ init_agent_memory(thisAgent); /* END */ }