void FUNCTIONDEFINITION(void){ /*functiondefinition = type ID ( idlist ) defstartsymbol statementlist defendsymbol*/ switch (l){ case KW_BOOLEAN: case KW_FLOAT: case KW_INT: case KW_VOID: TYPE(); eat(ID); eat('('); IDLIST(); eat(')'); DEFSTARTSYMBOL(); STATEMENTLIST(); DEFENDSYMBOL(); return; default: StdError(__func__); } }
void IDLIST(void){ /*idlist = ID IDLIST_TAIL | epsilon*/ switch(l){ case ID: eat(ID);IDLIST_TAIL();return; case ')': return; default: StdError(__func__); } }
void RETURNSTATEMENT(void){ /*returnstatement ::= <KW_RETURN> returnstatements_assignment*/ switch(l){ case KW_RETURN: eat(KW_RETURN); RETURNSTATEMENTS_ASSIGNMENT(); return; default: StdError(__func__); } }
void IFSTATEMENT(void){ /*ifstatement ::= <KW_IF> "(" assignment ")" block*/ switch(l){ case KW_IF: eat(KW_IF); eat('('); ASSIGNMENT(); eat(')'); BLOCK(); return; /*expand parse tree with these 5 nodes, then expand the leftmost, then the next, ...*/ default: StdError(__func__); } }
void POSITIVESIMPEXPR(void){ /*positivesimpexpr = term positivesimpexpr_tail*/ switch(l){ case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case ID: case '(': TERM(); POSITIVESIMPEXPR_TAIL(); return; default: StdError(__func__); } }
void TERM(void){ /*term ::= factor term_tail*/ switch(l){ case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case '(': case ID: FACTOR(); TERM_TAIL(); return; default: StdError(__func__); } }
void IDLIST_TAIL(void){ /*idlist_tail = , ID IDLIST_TAIL | epsilon*/ switch(l){ case ',': eat(',');eat(ID);IDLIST_TAIL();return; case ')': return; default: StdError(__func__); } }
void PRINTF(void){ /*printf ::= <KW_PRINTF> "(" assignment ")"*/ switch(l){ case KW_PRINTF: eat(KW_PRINTF); eat('('); ASSIGNMENT(); eat(')'); return; default: StdError(__func__); } }
void BLOCK(void){ /*block = defstartsymbol statementlist defendsymbol | statement*/ switch(l){ case '{': case KW_BEGIN: DEFSTARTSYMBOL(); STATEMENTLIST(); DEFENDSYMBOL(); return; case KW_IF: case KW_RETURN: case KW_PRINTF: case ID: STATEMENT(); return; default: StdError(__func__); } }
void STATEMENTLIST(void){ /*STATEMENTLIST ::= block statementlist | epsilon*/ switch(l){ case '{': case KW_BEGIN: case KW_IF: case KW_RETURN: case KW_PRINTF: case ID: BLOCK(); STATEMENTLIST(); return; case '}': case KW_END: return; default: StdError(__func__); } }
void ASSIGNMENTLIST_TAIL(void){ /*assignmentlist = , assignment assignmentlist_tail | epsilon*/ switch(l){ case ',': eat(','); ASSIGNMENT(); ASSIGNMENTLIST_TAIL(); return; case ')': return; default: StdError(__func__); } }
void DEFENDSYMBOL(void){ /*defendsymbol = } | KW_END*/ switch(l){ case '}': eat('}'); return; case KW_END: eat(KW_END);return; default: StdError(__func__); } }
void DEFSTARTSYMBOL(void){ /*defstartsymbol = { | KW_BEGIN*/ switch(l){ case '{': eat('{'); return; case KW_BEGIN: eat(KW_BEGIN);return; default: StdError(__func__); } }
void RESTOF_STATICASSIGNMENT_OR_FUNCTIONCALL(void){ /*restof_statassignment_or_functioncall ::= "=" assignment | ( assignmentlist )*/ switch(l){ case '=': eat('='); ASSIGNMENT(); return; case KW_FLOAT: eat('('); ASSIGNMENTLIST(); eat(')'); return; default: StdError(__func__); } }
void TcpCSocket::waitForConnection(int timeout) { struct pollfd pollfd; int ret; pollfd.fd = this->fd(); pollfd.events = (POLLOUT); pollfd.revents = 0; if ((ret = poll(&pollfd, 1, timeout)) == -1) throw (StdError("TcpCSocket::connect(poll)")); if (ret == 0) { errno = ETIMEDOUT; throw (StdError()); } if ((errno = this->getError())) throw (StdError("TcpCSocket::connect")); }
void EXPR_TAIL(void){ /*expr_tail = comparator simpexpr | epsilon*/ switch(l){ case EQ: case NEQ: case LEQ: case GEQ: case LSS: case GRT: COMPARATOR(); SIMPEXPR(); return; case ')': case ';': case ',' /*follow(EXPR_TAIL)*/: return; default: StdError(__func__); } }
void SIMPEXPR(void){ /*simpexpr ::= - positivesimpexpr | positivesimpexpr*/ switch(l){ case '-': eat('-'); POSITIVESIMPEXPR(); return; case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case ID: case '(': POSITIVESIMPEXPR(); return; default: StdError(__func__); } }
void EXPR(void){ /*expr ::= simpexpr expr_tail*/ switch(l){ case '-': case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case ID: case '(': SIMPEXPR(); EXPR_TAIL(); return; default: StdError(__func__); } }
void RESTOF_FUNCTIONCALL_OR_EPSILON(void){ /*restof_functioncall_or_epsilon = ( assignmentlist ) | epsilon*/ switch(l){ case '(': eat('('); ASSIGNMENTLIST(); eat(')'); return; case '*': case '/': case AND: case '+': case '-': case OR: case LSS: case GRT: case LEQ: case GEQ: case EQ: case NEQ: case ')': case ';': case ',': return; default: StdError(__func__); } }
void TERM_TAIL(void){ /*term_tail = dotoperator term | epsilon*/ switch(l){ case '*': case '/': case AND: DOTOPERATOR(); TERM(); return; case '+': case '-': case OR: case LSS: case GRT: case LEQ: case GEQ: case EQ: case NEQ: case ')': case ';': case ',': return; default: StdError(__func__); } }
void ASSIGNMENTLIST(void){ /*assignmentlist = assignment assignmentlist_tail | epsilon*/ switch(l){ case ID: case '-': case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case '(': ASSIGNMENT(); ASSIGNMENTLIST_TAIL(); return; case ')': return; default: StdError(__func__); } }
void DOTOPERATOR(void){ /*dotoperator ::= dotoperator = "*" | "/" | "&&"*/ switch(l){ case '*': eat('*'); return; case '/': eat('/'); return; case AND: eat(AND); return; default: StdError(__func__); } }
void OPERATOR(void){ /*operator ::= "+" | "-" | "||"*/ switch(l){ case '+': eat('+'); return; case '-': eat('-'); return; case OR: eat(OR); return; default: StdError(__func__); } }
void POSITIVESIMPEXPR_TAIL(void){ /*positivesimpexpr_tail = operator positivesimpexpr_tail | epsilon*/ switch(l){ case OR: case '+': case '-': OPERATOR(); TERM(); POSITIVESIMPEXPR_TAIL(); return; case LSS: case GRT: case LEQ: case GEQ: case EQ: case NEQ: case ')': case ';': case ',': return; default: StdError(__func__); } }
void RETURNSTATEMENTS_ASSIGNMENT(void){ /*returnstatements_assignment :== assignment | epsilon*/ switch(l){ case ID: case '-': case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case '(': ASSIGNMENT(); return; case ';': return; /*apply rule 2. lookahead is in follow(returnstatements_assignment) = {;}*/ default: StdError(__func__); } }
void COMPARATOR(void){ /*comparator = "==" | "!=" | "<=" | ">=" | "<" | ">"*/ switch(l){ case EQ: eat(EQ); return; case NEQ: eat(NEQ); return; case LEQ: eat(LEQ); return; case GEQ: eat(GEQ); return; case LSS: eat(LSS); return; case GRT: eat(GRT); return; default: StdError(__func__); } }
void ASSIGNMENT(void){ /*assignment = id "=" assignment | expr*/ /*Note that FIRST(id = assignment) and FIRST(EXPR) both contain ID*/ switch(l){ case '-': case CONST_INT: case CONST_FLOAT: case CONST_BOOLEAN: case '(': EXPR(); return; case ID: if (l2 == '=') {eat(ID); eat('='); ASSIGNMENT(); return;} /*expand according to rule 1*/ else /*note that EXPR can only derive into something that starts with an ID by deriving into id restof_functioncall_or_epsilon. Strings derived from the latter nonterminal either start with '(' or with something in FOLLOW(restof_functioncall_or_epsilon). The latter set does not contain =. It is '*'||'/'||AND||'+'||'-'||OR||LSS||GRT||LEQ||GEQ||EQ||NEQ||')'||';'||',').*/ {EXPR(); return;} /*rule 2*/ default: StdError(__func__); } }
void PROGRAM(void){ /*PROGRAM ::= FUNCTIONDEFINITION PROGRAM | eof*/ switch (l){ case KW_BOOLEAN: case KW_FLOAT: case KW_INT: case KW_VOID: { /*expand the parse-tree node PROGRAM with the children (FUNCTIONDEFINITION, PROGRAM)*/ FUNCTIONDEFINITION(); PROGRAM(); return; } case EOF: { eat(EOF); /*Input is in the language. Terminating.*/ exit(0); } default: StdError(__func__); } }
void STATEMENT(void){ /*statement ::= ifstatement | returnstatement ";" | printf ";" | id restof_statassignment_or_functioncall ";"*/ switch(l){ case KW_IF: IFSTATEMENT(); return; case KW_RETURN: RETURNSTATEMENT(); eat(';'); return; case KW_PRINTF: PRINTF(); eat(';'); return; case ID: eat(ID); RESTOF_STATICASSIGNMENT_OR_FUNCTIONCALL(); eat(';'); return; default: StdError(__func__); } }
void TYPE(void){ /*type ::= <KW_BOOLEAN> | <KW_FLOAT> | <KW_INT> | <KW_VOID>*/ switch(l){ case KW_BOOLEAN: eat(KW_BOOLEAN); return; case KW_FLOAT: eat(KW_FLOAT); return; case KW_INT: eat(KW_INT); return; case KW_VOID: eat(KW_VOID); return; default: StdError(__func__); } }