void print_ancestor_line(node *n){ node *lt,*rt; if( ! is_terminal(n) ){ lt=get_terminal(n,LEFT); rt=get_terminal(n,RIGHT); dprint(RES_FILE,"%-20s %-20s ",lt->name,rt->name); if( n->name == NULL ) dprint(RES_FILE , "%-20s ", "-"); else dprint(RES_FILE , "%-20s ", n->name); if(EDGE_LENS_ARE_INTEGERS == TRUE ){ dprint(RES_FILE,"%10.3f +/- %-10.3f",n->mpl,prob2normval(1-PROB_LIMIT) * sqrt(n->var)); } else{ dprint(RES_FILE,"%10.3f +/- %-10s",n->mpl,STAT_MISSING); } dprint(RES_FILE , "%12d " , n->num_of_terminals); //printf("chis: %f free:%d prob:%f\n", n->chi_sq, n->num_of_children-1, n->prob); if(EDGE_LENS_ARE_INTEGERS == FALSE){ dprint(RES_FILE,"%14s",STAT_MISSING); } else if( n->accepted == FALSE ) dprint(RES_FILE,"%8s, %s%f",REJ_WORD, "prob=",(double)n->prob); else dprint(RES_FILE,"%8s",ACC_WORD); dprint(RES_FILE,"\n"); } }
void reduction_step(reduction_list * temp_reduction_list, uint32_t red_list_node, token_node * list_itr, parsing_ctx * ctx) { uint32_t node, offset; const uint32_t *itr, *end; if (is_terminal(list_itr->token)) { itr = &ctx->gr_token_alloc[token_value(list_itr->token)]; end = itr + 1; } else { offset = rewrite_rules[list_itr->token]; itr = &(rewrite_rules[offset]); end = itr + *itr + 1; ++itr; } /* For each token in the current stack element token list. */ for (; itr != end; ++itr) { node = get_son_with(vect_reduction_tree, red_list_node, *itr); /* If current position has a son corresponding to the current token, navigate the tree. */ if (node != 0) { append_position_on_reduction_list(temp_reduction_list, node); } } }
std::string StatsNode::str(int indent) const { if (is_terminal()) { std::ostringstream out; if (terminal_items_.size() == 1) { out << "\"" << terminal_items_.front() << "\""; } else { out << "["; for (std::size_t i = 0; i < terminal_items_.size() - 1; ++i) { out << "\"" << terminal_items_[i] << "\", "; } out << "\"" << terminal_items_.back() << "\"]"; } return out.str(); } std::ostringstream out; std::string indent_str(indent*indent_size, ' '); std::string indent_deep_str((indent + 1)*indent_size, ' '); out << "{" << std::endl; for (auto it = std::begin(nodes_); it != std::end(nodes_); ++it) { out << indent_deep_str << "\"" << it->first << "\": " << it->second.str(indent + 1); if (std::distance(std::begin(nodes_), it) < nodes_.size() - 1) { out << ","; } out << std::endl; } out << indent_str << "}"; return out.str(); }
long _cdecl sys_f_datime (MetaDOSFile ushort *timeptr, short fd, short wflag) { PROC *p = curproc; FILEPTR *f; long r; TRACE (("%s(%i)", __FUNCTION__, fd)); r = GETFILEPTR (&p, &fd, &f); if (r) return r; /* some programs use Fdatime to test for TTY devices */ if (is_terminal (f)) return EACCES; if (f->fc.fs && f->fc.fs->fsflags & FS_EXT_3) { ulong t = 0; /* long r; */ if (wflag) t = unixtime (timeptr [0], timeptr [1]) + timezone; r = xdd_datime (f, (ushort *) &t, wflag); if (!r && !wflag) *(long *) timeptr = dostime (t - timezone); return r; } return xdd_datime (f, timeptr, wflag); }
void G_parser::update_dec(vector<int>& seq_t){ if (seq_t[3] == form_length / harm_rh - 1 && !ending && is_terminal(seq_t)){ for (int i=0; i<dec_bars.size(); i++) curr_cycle[dec_bars[i]].name = "dec"; } }
void build_first_sets(grammar_p g){ g->first_count = g->nonterminal_count; g->first_sets = malloc(g->first_count * sizeof(symbol_set_p)); for(size_t i = 0; i < g->first_count; i++) { g->first_sets[i] = symbol_set_new(); } while(true) { size_t appended = 0; for(size_t i = 0; i < g->length; i++) { rule_p rule = &g->rules[i]; uint32_t rule_head = rule->symbols[0]; ssize_t rule_head_index = nonterminal_index(rule_head, g); if (rule_head_index == -1) printf("nonterminal not found: %c !!!\n", nt_to_char(rule_head)); symbol_set_p rule_head_first_set = symbol_set_new(); bool epsilon_everywhere = true; for(size_t j = 1; j < rule->length; j++) { uint32_t jth_symbol = rule->symbols[j]; if ( is_terminal(jth_symbol) ) { symbol_set_add(rule_head_first_set, jth_symbol); epsilon_everywhere = false; break; } // jth_symbol is a nonterminal size_t jth_symbol_index = nonterminal_index(jth_symbol, g); symbol_set_p jth_first_set = g->first_sets[jth_symbol_index]; symbol_set_append(rule_head_first_set, jth_first_set); // Only continue with the next symbol of this rule if the jth sym allows for expsilon if ( ! symbol_set_contains(jth_first_set, T_EPSILON) ) { epsilon_everywhere = false; break; } } if (!epsilon_everywhere) rule_head_first_set = symbol_set_without(rule_head_first_set, T_EPSILON); appended += symbol_set_append(g->first_sets[rule_head_index], rule_head_first_set); symbol_set_destroy(rule_head_first_set); } // If the follow sets haven't changed we're done if (appended == 0) break; } }
void analyze (const std::vector<std::string>& ingrammer, std::vector<std::string>& outgrammer, std::set<std::string>& nonterminals, std::set<std::string>& terminals, std::set<std::string>& symbols, std::vector<std::string>& LHSList, std::vector<std::string>& RHSList) { std::string LHS = "", RHS = ""; unsigned sepIndex = 0; for (unsigned i = 0; i < ingrammer.size(); ++i) { sepIndex = ingrammer[i].find ("->"); if (sepIndex != std::string::npos) { LHS = ingrammer[i].substr (0, sepIndex); RHS = ingrammer[i].substr (sepIndex+2); LHSList.push_back (LHS); RHSList.push_back (RHS); if (is_terminal(RHS)) { if (RHS.compare("") != 0) { terminals.insert (RHS); symbols.insert (RHS); } // nonterminals.insert (LHS); } else { nonterminals.insert (LHS); symbols.insert (LHS); } } else { std::cerr << "\nError in production formatting : \" " << ingrammer[i] << ".\n"; exit(1); } LHS = "", RHS = ""; } // since we are not currently manipulating the input grammer outgrammer = ingrammer; }
void print_ancestor_age(node *n){ int i; if( ! is_terminal(n) && PRINT_ANCESTOR == TRUE){ if( n->mother == NULL ){ dprint(RES_FILE , "%-20s %-20s %-20s %10s %11s %16s %14s %11s %11s\n", "Ancestor of","Ancestor of","Name","Age","#Terminals","MPL","Rate *", R8S_MIN , R8S_MAX); } print_ancestor_age_line(n); for (i = 0; i < n->num_of_children; i++) print_ancestor_age(n->child[i]); } }
long _cdecl sys_f_write (MetaDOSFile short fd, long count, const char *buf) { PROC *p = curproc; FILEPTR *f; long r; r = GETFILEPTR (&p, &fd, &f); if (r) return r; if ((f->flags & O_RWMODE) == O_RDONLY) { DEBUG (("Fwrite: write on a read-only handle")); return EACCES; } if (is_terminal (f)) return tty_write (f, buf, count); /* Prevent broken device drivers from wiping the disk. * We return a zero rather than a negative error code * to help programs those don't handle GEMDOS errors * returned by Fwrite() */ if (count <= 0) { DEBUG (("Fwrite: invalid count: %d", count)); return 0; } /* it would be faster to do this in the device driver, but this * way the drivers are easier to write */ if (f->flags & O_APPEND) { r = xdd_lseek (f, 0L, SEEK_END); /* ignore errors from unseekable files (e.g. pipes) */ if (r == EACCES) r = 0; } else r = 0; if (r >= 0) { TRACELOW (("Fwrite: %ld bytes to handle %d", count, fd)); r = xdd_write (f, buf, count); } if (r < 0) DEBUG (("Fwrite: error %ld", r)); return r; }
void print_ancestor(node *n){ int i; if( ! is_terminal(n) && PRINT_ANCESTOR == TRUE){ if( n->mother == NULL ){ dprint(RES_FILE , "%-20s %-20s %-20s %10s %s %10s %11s %16s %s/%s\n","Ancestor of","Ancestor of","Name"," ", "MPL", " " , "#Terminals","Clock test:",ACC_WORD,REJ_WORD); } print_ancestor_line(n); for (i = 0; i < n->num_of_children; i++) print_ancestor(n->child[i]); } }
int main() { auto f = [&]() resumable { countdown_t f1(countdown(10)); yield from f1; countdown_t f2(countdown(5)); return from f2; }; while (!is_terminal(f)) printf("%d\n", f()); }
//========================================================================================================== //========================================================================================================== void Grammar::calc_follow_table() { calc_first_table(); //------------------------------------------------------------------------------------------------------ // To handle end-of-input correctly, add the EOI (= $) symbol to the follow of the start symbol. // This is instead of add another symbol to be the new start and a production: START' -> START $ // That will have the same effect but causes some overhead of needing to deal with the new symbol and // production. //------------------------------------------------------------------------------------------------------ follow_table[START].insert(EOI); //------------------------------------------------------------------------------------------------------ // Go over the productions: // For a sequence ...NA, add First(A) to Follow(N). // For a sequence ...N, add a constraint: Follow(M) ⊆ Follow(N), where M is the LHS of the production. //------------------------------------------------------------------------------------------------------ vector<pair<Symbol, Symbol>> follow_constraints; // Pair (M,N) means Follow(M) ⊆ Follow(N) for(auto& p: productions) { Symbol M = p[0]; for(int j = 1; j < p.size(); ++j) { if(is_terminal(p[j])) continue; Symbol N = p[j]; if(j < p.size() - 1) { follow_table[N].insert(get_first_set(p[j+1])); } else if(M != N) { follow_constraints.push_back({M,N}); } } } //------------------------------------------------------------------------------------------------------ // Go over the constraints and apply them, until no more changes are made //------------------------------------------------------------------------------------------------------ bool added = true; while(added) { added = false; for(auto& c: follow_constraints) { Symbol M = c.first, N = c.second; int old_size = follow_table[N].size(); follow_table[N].insert(follow_table[M]); added = (added or follow_table[N].size() > old_size); } } } // calc_follow_table()
/*creators*/ struct tree_node* create_node(int lineno, char *unit_name){ assert(strlen(unit_name) >= 2); assert(strlen(unit_name) < 20); struct tree_node *node = (struct tree_node *)malloc(sizeof(struct tree_node)); node -> unit_code = token_stoc(unit_name); node -> is_terminal = is_terminal(node -> unit_code); node -> lineno = lineno; node -> unit_value = NULL; node -> child = NULL; node -> sibling = NULL; strcpy(node -> unit_name, unit_name); return node; }
void flatten( std::vector<int> *fl ) const { assert( fl != 0 ); assert( !is_null() ); if( is_terminal() ) { fl->push_back(term_); } else { for( auto it = begin(); it != end(); ++it ) { it->flatten(fl); } } }
/////////////////////////////////////////////////////////////////////////////////////////// /// \brief Prints the nodes recursively /// \author DJ /// \date 20051021 /// \test No /// \todo Remove root-argument /// \warning Changed 2005-11-28 the root-argument is newer used /////////////////////////////////////////////////////////////////////////////////////////// void print_nodes_rec(node *root,node *n, char print_mode){ int i; if( !is_terminal(n)){ if( n->accepted == FALSE && print_mode == PM_FINAL) dprint(RES_FILE,"[%s %4.2e]",REJ_WORD, 1.0-n->prob); if(print_mode == PM_INPUT && FIXAGE_MODE == TRUE) print_fixage_data(n); dprint(RES_FILE,"%c",SYM_LEFT); for (i =0 ; i < n->num_of_children-1; i++) { print_nodes_rec(root,n->child[i],print_mode); dprint(RES_FILE,"%c",SYM_COMMA); } print_nodes_rec(root,n->child[n->num_of_children-1],print_mode); dprint(RES_FILE,"%c",SYM_RIGHT); } print_node_line(n,print_mode); }
/** delete empty terminals from tree when final data is deleted */ static void del_empty_term(struct local_zone* z, struct local_data* d, uint8_t* name, size_t len, int labs) { while(d && d->rrsets == NULL && is_terminal(d)) { /* is this empty nonterminal? delete */ /* note, no memory recycling in zone region */ (void)rbtree_delete(&z->data, d); /* go up and to the next label */ if(dname_is_root(name)) return; dname_remove_label(&name, &len); labs--; d = lz_find_node(z, name, len, labs); } }
//========================================================================================================== // This is the First(symbol) relation. Since I assume not to have any nullable symbols, the definition is: // // S is a terminal: {S} // First(S) = // S is a nonterminal: Union(First(F)) where F is the first symbol of a right-hand-side sequence of a production for S // // This is a non-efficient algorithm, since it goes over all the production again and again until nothing // new is added. A possible optimization is to build a directed graph, where there's an edge for the first // symbol of a RHS of a production to the nonterminal of the production. // Then from all the nodes that are terminals, go BFS-ly and add the terminal to all reachable nodes. //========================================================================================================== void Grammar::calc_first_table() { bool added = true; while(added) { added = false; for(auto& p: productions) { Symbol N = p[0], F = p[1]; int old_size = first_table[N].size(); if(is_terminal(F)) first_table[N].insert(F); else first_table[N].insert(first_table[F]); added = (added or first_table[N].size() > old_size); } } }
long _cdecl sys_f_seek (MetaDOSFile long place, short fd, short how) { PROC *p = curproc; FILEPTR *f; long r; TRACE (("Fseek(%ld, %d) on handle %d", place, how, fd)); r = GETFILEPTR (&p, &fd, &f); if (r) return r; if (is_terminal (f)) return 0; r = xdd_lseek (f, place, how); TRACE (("Fseek: returning %ld", r)); return r; }
long _cdecl sys_f_read (MetaDOSFile short fd, long count, char *buf) { PROC *p = curproc; FILEPTR *f; long r; r = GETFILEPTR (&p, &fd, &f); if (r) return r; if ((f->flags & O_RWMODE) == O_WRONLY) { DEBUG (("Fread: read on a write-only handle")); return EACCES; } if (is_terminal (f)) return tty_read (f, buf, count); TRACELOW (("Fread: %ld bytes from handle %d to %lx", count, fd, buf)); return xdd_read (f, buf, count); }
static void *comp_sym_follow(void *sym, void *first_set_of_following) { t_lst *first_set_of_next; t_bool sym_added; if (first_set_of_following == NULL) return (NULL); first_set_of_next = first_set_of_following; if (!is_empty_first(first_set_of_next) && !is_terminal(sym)) { if (!add_set_to_follow_set(sym, first_set_of_next, &sym_added)) return (NULL); } if (!has_symbol_in_first(sym, EMPTY_SYMBOL)) { f_lstdel(&first_set_of_next, no_destroy); if (NULL == f_lstpush(EMPTY_SYMBOL, &first_set_of_next)) return (NULL); } return (lst_add_to_filter( &first_set_of_next, get_first_set(sym), &sym_added)); }
void print_ancestor_age_line(node *n){ node *lt,*rt; double min, max; if( ! is_terminal(n) ){ lt=get_terminal(n,LEFT); rt=get_terminal(n,RIGHT); dprint(RES_FILE,"%-20s %-20s ", lt->name,rt->name); if( n->name == NULL ) dprint(RES_FILE , "%-20s ", "-"); else dprint(RES_FILE , "%-20s ", n->name); dprint(RES_FILE,"%10.3f ",n->fix->calc_age); dprint(RES_FILE,"%11d ",n->num_of_terminals); dprint(RES_FILE,"%16.3f ",n->mpl); dprint(RES_FILE,"%14f ",n->mpl / n->fix->calc_age); min = n->fix->minage; max = n->fix->maxage; if( is_fixnode(n)==TRUE && is_forced_fixnode(n) == FALSE){ min = max = n->fix->fixage; } if( is_minnode(n)==FALSE && !(is_fixnode(n) == TRUE && is_forced_fixnode(n)==FALSE) ) dprint(RES_FILE,"%11s " ,"-"); else dprint(RES_FILE,"%11.1f " ,min); if( is_maxnode(n)==FALSE && !(is_fixnode(n) == TRUE && is_forced_fixnode(n) == FALSE) ) dprint(RES_FILE,"%11s " ,"-"); else dprint(RES_FILE,"%11.1f ",max); dprint(RES_FILE,"\n"); } }
Grammar* load_grammar(FILE* file, Grammar* g) { enum States current_state = START; // Stato iniziale //enum States error = -1; Symbol s; Production* p = NULL; Errors error; error.size = 0; if(file != stdin) g->numprod = 0; // Inizializza la grammatica while ( !feof(file) ) { s = read_sym(file); switch (current_state) { case START: if (is_terminal(s) || is_nonterminal(s)) { current_state = LEFT; //p = &(g->productions[g->numprod++]); //p->left.length = 0; p = add_new_production(g); add_symbol(&p->left, s); //L'istruzione precedente corrisponde a p->left.word[p->left.length++] = s; } else if (is_prodsep(s)) { current_state = START; } else if (ispunct(s) || isgraph(s)) { current_state = LEFT; p = add_new_production(g); add_symbol(&p->left, s); error.type[error.size] = INVALID_SYMBOL; error.lines[error.size] = g->numprod; error.size++; } break; case LEFT: if (is_terminal(s) || is_nonterminal(s)) { current_state = LEFT; add_symbol(&p->left, s); } else if (is_prodsym(s)) { current_state = RIGHT; //ErrorManager(NO_NT, p); } else if (ispunct(s) || isgraph(s)) { current_state = LEFT; add_symbol(&p->left, s); error.type[error.size] = INVALID_SYMBOL; error.lines[error.size] = g->numprod; error.size++; } else if(is_prodsep(s) || s == EOF) { error.type[error.size] = NO_PRODSYM; error.lines[error.size] = g->numprod; error.size++; //ErrorManager(NO_PRODSYM_MAYBE, p, g->numprod); current_state = START; if (!CheckNonTerminal(p)) { error.type[error.size] = NO_NT; error.lines[error.size] = g->numprod; error.size++; } } else { error.type[error.size] = NO_PRODSYM; error.lines[error.size] = g->numprod; error.size++; current_state = RIGHT; } break; case RIGHT: if (is_terminal(s) || is_nonterminal(s)) { current_state = RIGHT; add_symbol(&p->right, s); } else if (is_prodsep(s) || s == EOF) { current_state = START; g->productions[g->numprod-1].left.word[g->productions[g->numprod-1].left.length] = '\0'; g->productions[g->numprod - 1].right.word[g->productions[g->numprod - 1].right.length] = '\0'; //ErrorManager(error, p,g->numprod); } else if (ispunct(s) || isgraph(s)) { current_state = RIGHT; add_symbol(&p->right, s); error.type[error.size] = INVALID_SYMBOL; error.lines[error.size] = g->numprod; error.size++; } break; } } if (error.size > 0) { DrawErrors(error, g); if (!CheckInitSymbol(g)) ErrorManager(NO_INITSYM, NULL, 0); g = NULL; } if (g) if (!CheckInitSymbol(g)) { ErrorManager(NO_INITSYM, NULL,0); g = NULL; } return g; }
Grammar* load_grammar(FILE* file, Grammar* g){ enum States {START,LEFT,RIGHT,ERROR}; /* START = Scansione di una nuova produzione [F] LEFT = Scansione della parte sinistra RIGHT = Scansione della parte destra [F] ERROR = Errore di scansione */ enum States current_state = START; // Stato iniziale Symbol s; Production* p; int contatore=0; while ( !feof(file)) { s = read_sym(file); if (feof(file)) break; switch(current_state){ case START: if (is_terminal(s) || is_nonterminal(s)||is_prodsym(s)){ current_state = LEFT; p = add_new_production(g); if (is_prodsym(s)){ current_state = RIGHT; p->error=4; add_symbol(&p->left,' '); } else add_symbol(&p->left,s); } else if (is_prodsep(s)){ current_state = START; } else { current_state = LEFT; add_symbol(&p->left,s); p->error=3; } break; case LEFT: if (is_terminal(s) || is_nonterminal(s)){ current_state = LEFT; add_symbol(&p->left,s); } else if (is_prodsym(s)){ current_state = RIGHT; } else{ if(is_prodsep(s)){ p->error=1; current_state=START; } else{ current_state = LEFT; add_symbol(&p->left,s); p->error=3; } } break; case RIGHT: if (is_terminal(s) || is_nonterminal(s)){ current_state = RIGHT; add_symbol(&p->right,s); } else if(is_prodsep(s)){ current_state = START; } else{ current_state = RIGHT; p->error=2; add_symbol(&p->right,s); } break; } } return g; }
uint32_t opp_parse(token_node * lookback_ptr, token_node * lookahead_ptr, token_node * list_begin, token_node * chunk_end, parsing_ctx * ctx) { uint32_t parse_result = 0; reduction_list *main_reduction_list, *temp_reduction_list; /* reduction_list represents where we are inside the reduction tree. */ uint32_t end_chunk_parse = __MORE_INPUT; uint32_t reduction_error = 0; uint32_t node = 0; token_node * current_list_pos = list_begin; token_node * prev_symbol_ptr = lookback_ptr; token_node * list_itr = NULL; vect_stack yields_prec_stack,parsing_stack; token_node * last_terminal = NULL; uint32_t prec=0; vect_stack stack; init_vect_stack(&stack, ctx->NODE_ALLOC_SIZE); init_vect_stack(&yields_prec_stack, ctx->PREC_ALLOC_SIZE); init_vect_stack(&parsing_stack, ctx->PREC_ALLOC_SIZE); main_reduction_list = (reduction_list *) malloc(sizeof(reduction_list)); init_reduction_list(main_reduction_list); temp_reduction_list = (reduction_list *) malloc(sizeof(reduction_list)); init_reduction_list(temp_reduction_list); /* set correctly last_terminal and current_pos */ while ( !( is_terminal(current_list_pos->token) || end_chunk_parse ) ) { end_chunk_parse = get_next_token(¤t_list_pos, &prev_symbol_ptr, chunk_end, lookahead_ptr); } last_terminal= is_terminal(lookback_ptr->token) ? lookback_ptr : current_list_pos; /* Start the parsing process. */ while (current_list_pos != NULL) { /* lookup precedence between last stack terminal token and new token. */ prec = get_precedence(current_list_pos,last_terminal); /*this was inserted by the guys: add the string terminator to the precedence table to remove this cruft */ if (lookback_ptr->token == __TERM && prec == __EQ && yields_prec_stack.top_of_stack == 0) { prec = __GT; } /* Invalid precedence value fetched from precedence table, abort parsing */ if (prec == __NOP) { parse_result = __PARSE_ERROR; break; } /* if precedence is __EQ or __LT, we should shift */ if (prec != __GT || yields_prec_stack.top_of_stack == 0) { if (end_chunk_parse == __END_OF_INPUT) { parse_result = __PARSE_IN_PROGRESS; break; } /* Push token_node on top of yields_prec_stack. */ if (prec == __LT) { vect_stack_push(&yields_prec_stack, last_terminal, ctx->PREC_REALLOC_SIZE); } /* Get next token. */ assert(is_terminal(current_list_pos->token)); vect_stack_push(&parsing_stack, last_terminal, ctx->NODE_REALLOC_SIZE); last_terminal=current_list_pos; end_chunk_parse = get_next_token(¤t_list_pos, &prev_symbol_ptr, chunk_end, lookahead_ptr); } else { /*clear reduction list */ main_reduction_list->idx_last = 0; /* node is the offset of the root of the vectorized reduction trie. */ node = vect_reduction_tree[0]; /* obtain the position of the previous yields precedence */ prev_symbol_ptr = vect_stack_pop(&yields_prec_stack); /* add pop to parsing stack to remove all the terminals up to the one which should be reduced */ while (last_terminal!= prev_symbol_ptr && last_terminal!=NULL){ last_terminal=vect_stack_pop(&parsing_stack); } /* the stack pop should always be successful, as TOS !=0 * it should_never get here*/ assert(prev_symbol_ptr !=NULL); list_itr = prev_symbol_ptr->next; /* Set the first element of the reduction list to the root. */ append_position_on_reduction_list(main_reduction_list, node); reduction_error = 0; /* starting from the previous yields precedence scan the candidate rhs and *match it against the reduction trie */ while (list_itr != NULL && list_itr != current_list_pos && list_itr != chunk_end) { /* For each available position in the reduction tree. */ uint32_t i; for (i = 0; i < main_reduction_list->idx_last; i++) { reduction_step(temp_reduction_list, get_reduction_position_at_index(main_reduction_list, i), list_itr, ctx); } swap_reduction_lists(&temp_reduction_list, &main_reduction_list); /* erase temp_reduction_list */ temp_reduction_list->idx_last = 0; list_itr = list_itr->next; /* If there is at least one available position in the reduction tree after having considered current token, go on. */ if (main_reduction_list->idx_last <= 0) { reduction_error = 1; break; } } /* Finished handle identification. */ if (!reduction_error && vect_reduction_tree[get_reduction_position_at_index(main_reduction_list, 0)] == __GRAMMAR_SIZE) { DEBUG_PRINT("Not in reducible position.\n") reduction_error = 1; } if (!reduction_error) { call_semantics(main_reduction_list, prev_symbol_ptr, &stack, ctx); } else { parse_result = __PARSE_NOT_RECOGNIZED; break; } /* if the axiom is reached and only two terminators are left reduce and exit */ if (lookback_ptr->token == __TERM && ctx->token_list->next == NULL && current_list_pos->token == __TERM && !is_terminal(ctx->token_list->token) && rewrite_to_axiom(ctx->token_list->token)) { perform_rewrite(lookback_ptr, ctx->token_list->token, __S, &stack, ctx); parse_result = __PARSE_SUCCESS; break; } } /* If next token is a nonterminal just move on. Eventually you might hit the terminator */ while ( !( is_terminal(current_list_pos->token) || end_chunk_parse ) ) { end_chunk_parse = get_next_token(¤t_list_pos, &prev_symbol_ptr, chunk_end, lookahead_ptr); } }/*end of the parsing loop */ cleanup_stack_and_lists(&yields_prec_stack, main_reduction_list, temp_reduction_list); return parse_result; }
void G_parser::find_rule(vector<int>& seq_t){ //cout << "reached 1" << endl; //cout << "seq_t: " << seq_t[2] << " & " << seq_t[3] << endl; // if (previous (in left_str) is rootpitch) { search for its terminal equivalents; } //if IV in previous context (instead of iv) then lookup corresponding chords (IV = iv*, iv6*, iv7*) OR (IV = iv*) //what about IV instead of iv later? (what if already translated as iv from IV?) --> e.g. "IV I_3" ->... // if context not found, look for equivalent rootpitch // perhaps categorise IV as iv|iv6|iv7 bool rule_found = false; bool opt; rule r; int cont_size; vector<int> leftmost_t; //find (if) timed_rule //search existing_times for (int i=0; i<existing_times.size(); i++){//replace with while() //cout << "reached 2" << endl; if (seq_t[2]==existing_times[i][0] && seq_t[3]==existing_times[i][1]){//seq_t[2,3] for beat, bar.. //cout << "reached 3" << endl; //check context (match rule with current context) for (int j=0; j<timed_rules[{seq_t[2], seq_t[3]}].size(); j++){ //cout << "reached 4" << endl; //check_context(seq_t); //i6 could map to I etc. if context not found ? cont_size = timed_rules[{seq_t[2], seq_t[3]}][j].left_str.size(); leftmost_t = timed_rules[{seq_t[2], seq_t[3]}][j].leftmost_time; opt = timed_rules[{seq_t[2], seq_t[3]}][j].is_optional; vector<string> curr_cont = get_context(leftmost_t, cont_size); if (timed_rules[{seq_t[2], seq_t[3]}][j].is_optional){//timed and optional rule r = timed_rules[{seq_t[2], seq_t[3]}][j]; rule_found = check_optional(r, seq_t); if (rule_found) break; } else {//timed, but not optional //cout << "reached 11" << endl; if (curr_cont == timed_rules[{seq_t[2], seq_t[3]}][j].left_str){ //cout << "reached 12" << endl; rule_found = true; //expand to support optional too r = timed_rules[{seq_t[2], seq_t[3]}][j]; the_rule = r; //debug cout << "the_rule: "; for (int j=0; j<the_rule.left_str.size(); j++) cout << the_rule.left_str[j] << ", "; cout << endl; rewrite(r, seq_t); cout << "case 2" << endl;//timed, but not optional break; } } } break; } } //else find general_rule if(!rule_found) {//replace with while() //search general_rules (context again) for (int i=0; i<general_rules.size(); i++){ //check if context exists before or after current chord (elem) int c_off = - general_rules[i].left_str.size();//context offset (back n forth) cont_size = general_rules[i].left_str.size(); //leftmost_t = general_rules[i].leftmost_time;//is {0, 0} opt = general_rules[i].is_optional; //need to access specific general_rule for (int z = c_off; z < 0; z++){ general_rules[i].leftmost_time[1] = seq_t[3] + (z+1) / harm_rh; vector<string> curr_cont = get_context(general_rules[i].leftmost_time, cont_size); //check context if(general_rule[i].is_optional) if (opt){ r = general_rules[i]; rule_found = check_optional(r, seq_t); if (rule_found) break; } //check context if (!general_rule[i].is_optional) else if(get_context(general_rules[i].leftmost_time, cont_size) == general_rules[i].left_str){ rule_found = true; r = general_rules[i]; the_rule = r; //debug cout << "the_rule: "; for (int j=0; j<the_rule.left_str.size(); j++) cout << the_rule.left_str[j] << ", "; cout << endl; rewrite(r, seq_t); cout << "case 5" << endl;//general and not optional break; } } if (rule_found) break; } } cout << "is_terminal?: " << curr_cycle[seq_t[3]].name << endl << endl; //stop if terminal if (!is_terminal(seq_t)) find_rule(seq_t); //else return_terminal(curr_cycle[seq_t[3]].name); else translate_to_midi();//translate / spit out (return_terminal()) }
const std::vector<node> &get_list() const { assert( !is_terminal() ); assert( !is_null() ); return *this; }
parseTree build_tree(int curr_node) { if(token_pos >= token_cnt) return NULL; int j; parseTree pnode = initParseTree(); (pnode->t).pos=-1; pnode->rule=-1; if(is_terminal(curr_node)) { if(curr_node==105) { strcpy(pnode->t.val, "epsilon"); strcpy(pnode->t.name, "EPSILON"); pnode->t.pos=105; pnode->t.lineno = -1; return pnode; } else if(token_pos >= token_cnt) { return NULL; } else if(tokenList[token_pos].pos==curr_node) { pnode->t=tokenList[token_pos]; pnode->rule=-1; //setting the rule to -1 for the terminal if(DEBUG2) printf("Terminal matches with token in input. Building token number : %d:%s\n\n",token_pos, parserTokenList[tokenList[token_pos].pos]); token_pos++; return pnode; } else { /*if(DEBUG2) {printf("Going to epsilon\n"); printf("not EPS no match error\n"); printf("Curr node is %d\n",curr_node); printf("Building token number : %d:%s\n\n",token_pos, parserTokenList[tokenList[token_pos].pos]); }*/ tok t_c = tokenList[token_pos]; printf("Error_4 : The token %s for lexeme %s does not match at line %d. Expected token here is %s\n", t_c.val, parserTokenList[t_c.pos], t_c.lineno,parserTokenList[curr_node]); token_pos++; return pnode; //error } } else { int rule; rule=T[curr_node][tokenList[token_pos].pos]; if(rule==-1) { //printf("Cant find rule ::: building token number : %d and curr_node is %d",token_pos,curr_node); //printf(" and seen token is %d\n",tokenList[token_pos].pos); printf("-1 rule :: Error in parse table\n"); return pnode; //return pnode with appropriate error value } else { // Check things you wanna make sure with asserts, not ifs //printf("Rule : %d curr_node %d \n",rule,curr_node); int i; if(DEBUG2) { PRINT_RULE(rule,i); } (pnode->t).pos=curr_node; strcpy(pnode->t.name,parserTokenList[curr_node]); (pnode->t).lineno = -1; pnode->rule=rule; /*if(G[rule][1]==105) printf("EPS being called");*/ parseTree child = build_tree(G[rule][1]); if(child != NULL) { child->parent = pnode; parseTree curr = child; pnode->child = child; strcpy((pnode->t).val, parserTokenList[G[rule][0]]); for(i=2;i<sz_grammar[rule];i++){ curr->sibling = build_tree(G[rule][i]); if(curr->sibling == NULL) { break; } (curr->sibling)->parent = pnode; curr = curr->sibling; } if(DEBUG2) printf("Building token number : %d\n",token_pos); } return pnode; } } }
//========================================================================================================== //========================================================================================================== Set<Symbol> Grammar::get_first_set(Symbol sym) { if(is_terminal(sym)) return {sym}; else return first_table[sym]; }
int main(int argc, char **argv) { if (!owns_tty()) { fprintf(stderr, "process does not own the terminal\n"); exit(2); } if (!is_terminal(stdin)) { fprintf(stderr, "stdin is not a terminal\n"); exit(2); } if (!is_terminal(stdout)) { fprintf(stderr, "stdout is not a terminal\n"); exit(2); } if (!is_terminal(stderr)) { fprintf(stderr, "stderr is not a terminal\n"); exit(2); } if (argc > 1) { errno = 0; int count = (int)strtol(argv[1], NULL, 10); if (errno != 0) { abort(); } count = (count < 0 || count > 99999) ? 0 : count; for (int i = 0; i < count; i++) { printf("line%d\n", i); } fflush(stdout); return 0; } int interrupted = 0; uv_prepare_t prepare; uv_prepare_init(uv_default_loop(), &prepare); uv_prepare_start(&prepare, prepare_cb); #ifndef WIN32 uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1); #else uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1); uv_tty_init(uv_default_loop(), &tty_out, fileno(stdout), 0); int width, height; uv_tty_get_winsize(&tty_out, &width, &height); #endif uv_tty_set_mode(&tty, UV_TTY_MODE_RAW); tty.data = &interrupted; uv_read_start(STRUCT_CAST(uv_stream_t, &tty), alloc_cb, read_cb); #ifndef WIN32 struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sig_handler; sigaction(SIGHUP, &sa, NULL); sigaction(SIGWINCH, &sa, NULL); #else uv_signal_t sigwinch_watcher; uv_signal_init(uv_default_loop(), &sigwinch_watcher); uv_signal_start(&sigwinch_watcher, sigwinch_cb, SIGWINCH); #endif uv_run(uv_default_loop(), UV_RUN_DEFAULT); #ifndef WIN32 // XXX: Without this the SIGHUP handler is skipped on some systems. sleep(100); #endif return 0; }
int get_terminal() const { assert( !is_null() ); assert( is_terminal() ); return term_; }