int main() { void *pParser = ParseAlloc (malloc); /* First input: 15 / 5 */ Parse (pParser, INTEGER, 15); Parse (pParser, DIVIDE, 0); Parse (pParser, INTEGER, 5); Parse (pParser, 0, 0); /* Second input: 50 + 125 */ Parse (pParser, INTEGER, 50); Parse (pParser, PLUS, 0); Parse (pParser, INTEGER, 125); Parse (pParser, 0, 0); /* Third input: 50 * 125 + 125 */ Parse (pParser, INTEGER, 50); Parse (pParser, TIMES, 0); Parse (pParser, INTEGER, 125); Parse (pParser, PLUS, 0); Parse (pParser, INTEGER, 125); Parse (pParser, 0, 0); ParseFree(pParser, free); return 0; }
int main() { void* parser= (void*)ParseAlloc(malloc); // TODO: write a lexer // s0 : a - s1 "Ok" Parse(parser, ID, "s0"); Parse(parser, COLON); Parse(parser, ID, "a"); Parse(parser, MINUS); Parse(parser, ID, "s1"); Parse(parser, STRING, "Ok"); Parse(parser, NEWLINE); // s1 : b + s0 Parse(parser, ID, "s1"); Parse(parser, COLON); Parse(parser, ID, "b"); Parse(parser, PLUS); Parse(parser, ID, "s0"); Parse(parser, NEWLINE); // a=3 b=4 Parse(parser, ID, "a"); Parse(parser, EQUALS); Parse(parser, NUMBER, (void*)3); Parse(parser, ID, "b"); Parse(parser, EQUALS); Parse(parser, NUMBER, (void*)4); // EOF Parse(parser, 0); ParseFree(parser, free); return 0; }
int main() { void* pParser = ParseAlloc (malloc); // 15 / 5 = 3 Parse (pParser, INTEGER, 15); Parse (pParser, DIV, 0); Parse (pParser, INTEGER, 5); Parse (pParser, 0, 0); // 50 + 125 = 175 Parse (pParser, INTEGER, 50); Parse (pParser, ADD, 0); Parse (pParser, INTEGER, 125); Parse (pParser, 0, 0); // 50 * 125 + 125 = 6375 Parse (pParser, INTEGER, 50); Parse (pParser, MUL, 0); Parse (pParser, INTEGER, 125); Parse (pParser, ADD, 0); Parse (pParser, INTEGER, 125); Parse (pParser, 0, 0); ParseFree(pParser, free ); }
struct verilog_module *verilog_parse_fd(FILE *fd) { struct scanner *s; int tok; void *p; char *stoken; struct verilog_module *m; //ParseTrace(stdout, "parser: "); s = scanner_new(fd); m = verilog_new_module(); p = ParseAlloc(malloc); assert(p != NULL); tok = scanner_scan(s); while(tok != TOK_EOF) { stoken = scanner_get_token(s); Parse(p, tok, stoken, m); tok = scanner_scan(s); } Parse(p, TOK_EOF, NULL, m); ParseFree(p, free); scanner_free(s); return m; }
struct preset *generate_ast(char *preset_code) { struct scanner *s; int tok; char *identifier; void *p; struct preset *ast; s = new_scanner(preset_code); ast = NULL; p = ParseAlloc(malloc); tok = scan(s); while(tok != TOK_EOF) { identifier = malloc(IDENTIFIER_SIZE); get_token(s, identifier, IDENTIFIER_SIZE); Parse(p, tok, identifier, &ast); if(tok == TOK_ERROR) { printf("Scan error\n"); break; } tok = scan(s); } Parse(p, TOK_EOF, NULL, &ast); ParseFree(p, free); delete_scanner(s); if(ast == NULL) { printf("Parse error\n"); return NULL; } return ast; }
int steady_parse_file(const char *filename) { FILE *file = fopen(filename, "r"); if(!file) { fprintf(stderr, "could not open file for reading\n"); return 0; } fseek(file, 0, SEEK_END); size_t length = ftell(file); rewind(file); char *source = malloc(length); fread(source, 1, length, file); parser = ParseAlloc(malloc); steady_lex(source, length, parse); ParseFree(parser, free); free(source); fclose(file); return 1; }
int parse_css(FILE *fh) { // Set up the scanner yyscan_t scanner; yylex_init(&scanner); yyset_in(fh, scanner); // Set up the parser void *css_parser = ParseAlloc(malloc); int lexCode; do { lexCode = yylex(scanner); printf("Got %i\n", lexCode); Parse(css_parser, lexCode, yyget_text(scanner)); } while (lexCode > 0); // Finish the job. Parse(css_parser, 0, NULL); if (-1 == lexCode) { fprintf(stderr, "The scanner encountered an error.\n"); return 0; } // Cleanup the scanner and parser yylex_destroy(scanner); ParseFree(css_parser, free); return 1; }
int parse_kegogi_file(const char *path, Command commands[], int max_num_commands, ParamDict **defaults) { FILE *script; TokenList *tokens = NULL; bstring buffer = NULL; void *parser = NULL; CommandList commandList = { .size = max_num_commands, .count = 0, .defaults = NULL, .commands = commands }; script = fopen(path, "r"); check(script, "Failed to open file: %s", path); buffer = bread((bNread)fread, script); check_mem(buffer); fclose(script); script = NULL; tokens = get_kegogi_tokens(buffer); check_mem(tokens); bdestroy(buffer); buffer = NULL; parser = ParseAlloc(malloc); check_mem(parser); int i; for(i = 0; i < tokens->count; i++) { Parse(parser, tokens->tokens[i].type, &tokens->tokens[i], &commandList); } Parse(parser, TKNEWLINE, 0, &commandList); Parse(parser, 0, 0, &commandList); TokenList_destroy(tokens); tokens = NULL; *defaults = commandList.defaults; return commandList.count; error: TokenList_destroy(tokens); bdestroy(buffer); if(script) fclose(script); if(parser) ParseFree(parser, free); return -1; }
int main(int argc, char *argv[]) { FILE *inputFile, *outputFile; void *parser; int tokenID; perplex_t scanner; token_t *tokenData; app_data_t appData; if (argc != 3) { fprintf(stderr, "Usage: %s input output", argv[0]); exit(1); } inputFile = fopen(argv[1], "r"); outputFile = fopen(argv[2], "w"); scanner = perplexFileScanner(inputFile); perplexSetExtra(scanner, &appData); appData.outfile = outputFile; appData.doc_comment = 0; appData.tag_text = 0; appData.example_text = 0; appData.return_text = 0; bu_vls_init(&appData.description); bu_vls_init(&appData.tags); parser = ParseAlloc(parse_alloc); BU_GET(tokenData, token_t); bu_vls_init(&tokenData->value); appData.tokenData = tokenData; while ((tokenID = yylex(scanner)) != YYEOF) { Parse(parser, tokenID, tokenData, &appData); BU_GET(tokenData, token_t); bu_vls_init(&tokenData->value); appData.tokenData = tokenData; } Parse(parser, 0, tokenData, &appData); ParseFree(parser, free); perplexFree(scanner); fclose(inputFile); fclose(outputFile); return 0; }
bool ArithmeticOperate::calculate_tokens(token_seq_t& tokens, double& val) { void* parser = ParseAlloc(malloc); result_t result; for (size_t i = 0; i < tokens.size(); ++i) { Parse(parser, tokens[i].first, tokens[i].second, &result); } Parse(parser, 0, 0.0, &result); ParseFree(parser, free); if (result.error_type != result_t::ACCEPTED) { return false; } val = result.value; return true; }
int main() { FILE *in = stdin; char *line = NULL; size_t line_len = 0; struct lexer lexer; lex_init(&lexer); void *pParser; pParser = ParseAlloc(malloc); ssize_t nread = 0; while (-1 != (nread = getline(&line, &line_len, in))) { lex_setup_next_line(&lexer, line, nread, feof(in)); struct token token; while (lex(&lexer, &token)) Parse(pParser, token.type, token, print); } Parse(pParser, 0, (struct token){.type = 0, .location = lexer.location}, print);
int main(int argc, char **argv) { // emit_module(); if ((argc < 2) || access(argv[1], F_OK) == -1) { printf("usage: %s <filename>\n", argv[0]); exit(-1); } printf("mksc compiler 0.01 ashley towns <*****@*****.**>\n"); char *contents = read_file(argv[1]); token_t *tokens = lex(contents); //print_tokens(tokens); annotate_source(argv[1], tokens); void *parser = ParseAlloc((void *) malloc); // ParseTrace(stdout, "* > "); mks_token_iterate(tokens, ^(token_t *token) { Parse(parser, token->type, token, NULL); });
list* parse_expr(char *expr, int verbose) { scanner_state state; int stat; list *argList = list_create(); void *pParser = (void*)ParseAlloc(malloc); state.start = expr; if (verbose) printf("parse_expr: [%s]\n", expr); void* tokenStack = malloc(TOKEN_STACK_SIZE); memset(tokenStack, 0, TOKEN_STACK_SIZE); scanner_token *token = (scanner_token*)tokenStack; while (0 <= (stat = scan(&state, token))) { state.end = state.start; if (token->tokType == TOKEN_WS || token->tokType == TOKEN_CONNECTOR) continue; if (verbose) printf("token: [%i][%s][%i][%x]\n", token->tokType, token->data, token->opt, token); Parse(pParser, token->tokType, token, argList); token += sizeof(scanner_token); if (token >= tokenStack+TOKEN_STACK_SIZE) token = tokenStack; token->opt = 0; token->data = 0; } Parse(pParser,0,0,argList); ParseFree(pParser, free); free(tokenStack); return argList; }
ParseTree parse(const std::deque<Token>& tokens) { //Set up lemon void* lemon = ParseAlloc(malloc); // ParseTrace(stderr, (char*)""); //Output ParseTree parsetree; for (Token token : tokens) { // printf(" Token %d at %d\n", token.type, token.location); parsetree.source_location = token.location; Parse(lemon, token.type, token, &parsetree); } //End lemon Token eoftoken; Parse(lemon, 0, eoftoken, &parsetree); ParseFree(lemon, free); return parsetree; }
gaiaGeomCollPtr gaiaParseKml (const unsigned char *dirty_buffer) { void *pParser = ParseAlloc (malloc); /* Linked-list of token values */ kmlFlexToken *tokens = malloc (sizeof (kmlFlexToken)); /* Pointer to the head of the list */ kmlFlexToken *head = tokens; int yv; gaiaGeomCollPtr geom = NULL; yyscan_t scanner; struct kml_data str_data; /* initializing the helper structs */ str_data.kml_line = 1; str_data.kml_col = 1; str_data.kml_parse_error = 0; str_data.kml_first_dyn_block = NULL; str_data.kml_last_dyn_block = NULL; str_data.result = NULL; /* initializing the scanner state */ Kmllex_init_extra (&str_data, &scanner); str_data.KmlLval.pval = NULL; tokens->value = NULL; tokens->Next = NULL; Kml_scan_string ((char *) dirty_buffer, scanner); /* / Keep tokenizing until we reach the end / yylex() will return the next matching Token for us. */ while ((yv = yylex (scanner)) != 0) { if (yv == -1) { str_data.kml_parse_error = 1; break; } tokens->Next = malloc (sizeof (kmlFlexToken)); tokens->Next->Next = NULL; kml_xferString (&(tokens->Next->value), str_data.KmlLval.pval); /* Pass the token to the wkt parser created from lemon */ Parse (pParser, yv, &(tokens->Next->value), &str_data); tokens = tokens->Next; } /* This denotes the end of a line as well as the end of the parser */ Parse (pParser, KML_NEWLINE, 0, &str_data); ParseFree (pParser, free); Kmllex_destroy (scanner); /* Assigning the token as the end to avoid seg faults while cleaning */ tokens->Next = NULL; kml_cleanup (head); kml_freeString (&(str_data.KmlLval.pval)); if (str_data.kml_parse_error) { if (str_data.result) { /* if a Geometry-result has been produced, the stack is already cleaned */ kml_freeTree (str_data.result); kmlCleanMapDynAlloc (&str_data, 0); } else { /* otherwise we are required to clean the stack */ kmlCleanMapDynAlloc (&str_data, 1); } return NULL; } if (str_data.result == NULL) { kmlCleanMapDynAlloc (&str_data, 0); return NULL; } /* attempting to build a geometry from KML */ geom = kml_build_geometry (&str_data, str_data.result); geom->Srid = 4326; kml_freeTree (str_data.result); kmlCleanMapDynAlloc (&str_data, 0); return geom; }
gaiaGeomCollPtr gaiaParseGeoJSON (const unsigned char *dirty_buffer) { void *pParser = ParseAlloc (malloc); /* Linked-list of token values */ geoJsonFlexToken *tokens = malloc (sizeof (geoJsonFlexToken)); /* Pointer to the head of the list */ geoJsonFlexToken *head = tokens; int yv; yyscan_t scanner; struct geoJson_data str_data; /* initializing the helper structs */ str_data.geoJson_line = 1; str_data.geoJson_col = 1; str_data.geoJson_parse_error = 0; str_data.geoJson_first_dyn_block = NULL; str_data.geoJson_last_dyn_block = NULL; str_data.result = NULL; /* initializing the scanner state */ GeoJsonlex_init_extra (&str_data, &scanner); tokens->Next = NULL; GeoJson_scan_string ((char *) dirty_buffer, scanner); /* / Keep tokenizing until we reach the end / yylex() will return the next matching Token for us. */ while ((yv = yylex (scanner)) != 0) { if (yv == -1) { str_data.geoJson_parse_error = 1; break; } tokens->Next = malloc (sizeof (geoJsonFlexToken)); tokens->Next->Next = NULL; tokens->Next->value = str_data.GeoJsonLval.dval; /* Pass the token to the wkt parser created from lemon */ Parse (pParser, yv, &(tokens->Next->value), &str_data); tokens = tokens->Next; } /* This denotes the end of a line as well as the end of the parser */ Parse (pParser, GEOJSON_NEWLINE, 0, &str_data); ParseFree (pParser, free); GeoJsonlex_destroy (scanner); /* Assigning the token as the end to avoid seg faults while cleaning */ tokens->Next = NULL; geoJSON_cleanup (head); if (str_data.geoJson_parse_error) { if (str_data.result) { /* if a Geometry-result has been produced, the stack is already cleaned */ gaiaFreeGeomColl (str_data.result); geoJsonCleanMapDynAlloc (&str_data, 0); } else { /* otherwise we are required to clean the stack */ geoJsonCleanMapDynAlloc (&str_data, 1); } return NULL; } geoJsonCleanMapDynAlloc (&str_data, 0); if (str_data.result == NULL) return NULL; if (!geoJsonCheckValidity (str_data.result)) { gaiaFreeGeomColl (str_data.result); return NULL; } gaiaMbrGeometry (str_data.result); return str_data.result; }
int main(int argc, char *argv[]) { /* don't complain about unused variable */ (void)argc; if (argc > 1 && !strcmp(argv[1], "--write")){ /* opening the testing file */ fp = fopen("bytecode.nc", "wb"); /* writing version numbers */ BYTE major = NVM_VERSION_MAJOR; BYTE minor = NVM_VERSION_MINOR; BYTE patch = NVM_VERSION_PATCH; fwrite(&major, sizeof major, 1, fp); fwrite(&minor, sizeof minor, 1, fp); fwrite(&patch, sizeof patch, 1, fp); /* end */ void *parser = ParseAlloc(malloc); /* input: * * a = 7; * b = a - 3; */ TokenType token; token.s = "a"; Parse(parser, STRING, token); token.i = 0; Parse(parser, EQ, token); token.i = 7; Parse(parser, NUMBER, token); token.i = 0; Parse(parser, SEMICOLON, token); token.s = "b"; Parse(parser, STRING, token); token.i = 0; Parse(parser, EQ, token); token.s = "a"; Parse(parser, STRING, token); token.i = 0; Parse(parser, MINUS, token); token.i = 3; Parse(parser, NUMBER, token); token.i = 0; Parse(parser, SEMICOLON, token); /* finish parsing */ token.i = 0; Parse(parser, 0, token); fclose(fp); ParseFree(parser, free); } /* init the VM */ nvm_t *vm = nvm_init("bytecode.nc", malloc, free); if (!vm){ fprintf(stderr, "error with initializing the VM :C\n"); return 1; } /* validate the bytecode first */ if (nvm_validate(vm) != 0){ fprintf(stderr, "nvm: bytecode validation failed\n"); exit(1); } /* starts off the reading from file and executing the ops process */ nvm_blastoff(vm); /* print what's on the stack */ nvm_print_stack(vm); /* clean after yourself */ nvm_destroy(vm); return 0; }
*/ #define YY_BUFFER_EOF_PENDING 2 }; typedef struct yy_buffer_state *YY_BUFFER_STATE; extern "C" { int yylex( void ); YY_BUFFER_STATE yy_scan_string( const char * ); void yy_delete_buffer( YY_BUFFER_STATE ); } int n; int yv; char buf[BUFS+1]; void* pParser = ParseAlloc (malloc); struct panopticon::object t0,t1; struct panopticon::object mToken; namespace panopticon { std::unordered_map<std::string,Variable> string_hash_map; std::unordered_map<Variable,std::string> reverse_variable_name_lookup; Variable string_num = 11; #define MAX_DEPTH 72 unsigned int indent_stack[MAX_DEPTH]; unsigned int level = 0; int nesting = 0;
tst_t *Parse_config_string(bstring content) { Token *temp = NULL; void *parser = ParseAlloc(malloc); check_mem(parser); ParserState state = {.settings = NULL, .error = 0, .line_number = 1}; char *p = bdata(content); char *pe = bdataofs(content, blength(content) - 1); char *eof = pe; int cs = -1; int act = -1; char *ts = NULL; char *te = NULL; #line 114 "src/lexer.c" { cs = m2sh_lexer_start; ts = 0; te = 0; act = 0; } #line 135 "src/lexer.rl" #line 124 "src/lexer.c" { if ( p == pe ) goto _test_eof; switch ( cs ) { tr1: #line 77 "src/lexer.rl" {te = p+1;{ TKSTR(QSTRING) }} goto st6; tr4: #line 91 "src/lexer.rl" {te = p+1;} goto st6; tr7: #line 89 "src/lexer.rl" {te = p+1;} goto st6; tr9: #line 88 "src/lexer.rl" {te = p+1;{ state.line_number++; }} goto st6; tr10: #line 83 "src/lexer.rl" {te = p+1;{ TK(LPAREN) }} goto st6; tr11: #line 84 "src/lexer.rl" {te = p+1;{ TK(RPAREN) }} goto st6; tr12: #line 85 "src/lexer.rl" {te = p+1;{ TK(COMMA) }} goto st6; tr14: #line 86 "src/lexer.rl" {te = p+1;{ TK(COLON) }} goto st6; tr15: #line 78 "src/lexer.rl" {te = p+1;{ TK(EQ) }} goto st6; tr17: #line 81 "src/lexer.rl" {te = p+1;{ TK(LBRACE) }} goto st6; tr18: #line 82 "src/lexer.rl" {te = p+1;{ TK(RBRACE) }} goto st6; tr20: #line 79 "src/lexer.rl" {te = p+1;{ TK(LBRACKET) }} goto st6; tr21: #line 80 "src/lexer.rl" {te = p+1;{ TK(RBRACKET) }} goto st6; tr22: #line 93 "src/lexer.rl" {te = p;p--;{ TK(NUMBER) }} goto st6; tr23: #line 1 "NONE" { switch( act ) { case 15: {{p = ((te))-1;} TK(CLASS) } break; case 16: {{p = ((te))-1;} TK(IDENT) } break; } } goto st6; tr25: #line 95 "src/lexer.rl" {te = p;p--;{ TK(IDENT) }} goto st6; st6: #line 1 "NONE" {ts = 0;} if ( ++p == pe ) goto _test_eof6; case 6: #line 1 "NONE" {ts = p;} #line 210 "src/lexer.c" switch( (*p) ) { case 10: goto tr9; case 32: goto tr7; case 34: goto st1; case 35: goto st3; case 39: goto st4; case 40: goto tr10; case 41: goto tr11; case 44: goto tr12; case 58: goto tr14; case 61: goto tr15; case 91: goto tr17; case 93: goto tr18; case 95: goto st9; case 123: goto tr20; case 125: goto tr21; } if ( (*p) < 48 ) { if ( 9 <= (*p) && (*p) <= 13 ) goto tr7; } else if ( (*p) > 57 ) { if ( (*p) > 90 ) { if ( 97 <= (*p) && (*p) <= 122 ) goto st9; } else if ( (*p) >= 65 ) goto tr16; } else goto st7; goto st0; st0: cs = 0; goto _out; st1: if ( ++p == pe ) goto _test_eof1; case 1: switch( (*p) ) { case 34: goto tr1; case 92: goto st2; } goto st1; st2: if ( ++p == pe ) goto _test_eof2; case 2: goto st1; st3: if ( ++p == pe ) goto _test_eof3; case 3: if ( (*p) == 10 ) goto tr4; goto st3; st4: if ( ++p == pe ) goto _test_eof4; case 4: switch( (*p) ) { case 39: goto tr1; case 92: goto st5; } goto st4; st5: if ( ++p == pe ) goto _test_eof5; case 5: goto st4; st7: if ( ++p == pe ) goto _test_eof7; case 7: if ( 48 <= (*p) && (*p) <= 57 ) goto st7; goto tr22; tr16: #line 1 "NONE" {te = p+1;} #line 95 "src/lexer.rl" {act = 16;} goto st8; tr24: #line 1 "NONE" {te = p+1;} #line 94 "src/lexer.rl" {act = 15;} goto st8; st8: if ( ++p == pe ) goto _test_eof8; case 8: #line 301 "src/lexer.c" if ( (*p) == 95 ) goto st9; if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st9; } else if ( (*p) > 90 ) { if ( 97 <= (*p) && (*p) <= 122 ) goto tr24; } else goto tr24; goto tr23; st9: if ( ++p == pe ) goto _test_eof9; case 9: if ( (*p) == 95 ) goto st9; if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st9; } else if ( (*p) > 90 ) { if ( 97 <= (*p) && (*p) <= 122 ) goto st9; } else goto st9; goto tr25; } _test_eof6: cs = 6; goto _test_eof; _test_eof1: cs = 1; goto _test_eof; _test_eof2: cs = 2; goto _test_eof; _test_eof3: cs = 3; goto _test_eof; _test_eof4: cs = 4; goto _test_eof; _test_eof5: cs = 5; goto _test_eof; _test_eof7: cs = 7; goto _test_eof; _test_eof8: cs = 8; goto _test_eof; _test_eof9: cs = 9; goto _test_eof; _test_eof: {} if ( p == eof ) { switch ( cs ) { case 7: goto tr22; case 8: goto tr23; case 9: goto tr25; } } _out: {} } #line 136 "src/lexer.rl" if(state.error) { Parse_print_error("SYNTAX ERROR", content, (int)(ts - bdata(content)), ++state.line_number); } else if( cs == #line 359 "src/lexer.c" 0 #line 141 "src/lexer.rl" ) { Parse_print_error("INVALID CHARACTER", content, (int)(ts - bdata(content)), ++state.line_number); } else if( cs >= #line 366 "src/lexer.c" 6 #line 144 "src/lexer.rl" ) { Parse(parser, TKEOF, NULL, &state); } else { log_err("INCOMPLETE CONFIG FILE. There needs to be more to this."); } Parse(parser, 0, 0, &state); ParseFree(parser, free); return state.settings; error: if(state.error) { Parse_print_error("SYNTAX ERROR", content, (int)(ts - bdata(content)), ++state.line_number); } ParseFree(parser, free); return NULL; } tst_t *Parse_config_file(const char *path) { FILE *script; bstring buffer = NULL; tst_t *settings = NULL; script = fopen(path, "r"); check(script, "Failed to open file: %s", path); buffer = bread((bNread)fread, script); check_mem(buffer); fclose(script); script = NULL; // make sure there's a \n at the end bconchar(buffer, '\n'); settings = Parse_config_string(buffer); check(settings != NULL, "Failed to parse file: %s", path); bdestroy(buffer); buffer = NULL; return settings; error: bdestroy(buffer); if(script) fclose(script); return NULL; }
Parser::Parser(QObject *parent) : QObject(parent) { parser = ParseAlloc(std::malloc); isError_ = false; }