Пример #1
0
//==========================================================================================================
// Create grammar from definitions in input file.
// Production are of the form:
// N -> A B C ...
//==========================================================================================================
void Grammar::read_grammar_file(string grammar_file) {
    ifstream file = ifstream(grammar_file);
    
    if(!file.is_open())
        throw string("File " + grammar_file + " not found");
        
    //------------------------------------------------------------------------------------------------------
    // For each production line extract the symbols and add a production
    //------------------------------------------------------------------------------------------------------
    for(string line; getline(file, line);) {
        if(trim(line, "//").empty()) continue; // Ignore comments and empty lines
        
        vector<Symbol> symbols;
        string action_name;
        
        // If an action is specified, a number sign will serve to separate between the production and it
        int split_pos = line.find_first_of('#');        
        extract_symbols(line.substr(0, split_pos), symbols);
                
        if(split_pos != string::npos)
            extract_action(line.substr(split_pos + 1), action_name);
        
        productions.push_back(Production(symbols, action_name));
    }
    
    //------------------------------------------------------------------------------------------------------
    // First production nonterminal considered the start symbol. If it is not START, add a production:
    // START -> FIRST-NONTERMINAL
    //------------------------------------------------------------------------------------------------------
    if(productions[0][0] == START) {
        if(productions[0].size() != 2)
            throw string("Expected production for START to have just one symbol on right hand side");
        if(not is_nonterminal(productions[0][1]))
            throw string("Expected production for START to have a nonterminal on the right hand side");
    }
    else {
        productions.push_front(Production({START, productions[0][0]}, ""));
    }

} // read_grammar_file()
Пример #2
0
int		nm(const char * filename)
{
  void		*data;
  t_elf		elf;
  t_sym_list	*list;
  int		msize;

  if ((msize = map_file(&data, filename)) == -1)
    return (1);
  if (init_elf_data(&elf, data) == -1)
    return (1);
  list = extract_symbols(&elf);
  list_sort(list, symcmp);
  list_iter(list, print_symbol);
  delete_list(list);
  if (elf.ehdr->e_ident[EI_CLASS] == ELFCLASS32)
    {
      free(elf.ehdr);
      free(elf.shtab);
    }
  (void)munmap(data, msize);
  return (0);
}