int main() { int exitCode = 0; char* host_n = (char *)malloc(sizeof(char)); char* user_n = (char *)malloc(sizeof(char)); char** args; int writeToFile = false; if(getHostName(host_n) == ERROR) goto quit; if(getUserName(user_n) == ERROR) goto quit; signal(SIGINT, sigHandler); // Change the default SIGINT handler while(1) { printf("%d %s@%s$ ",exitCode, user_n, host_n); // Print Prompt args = ReadTokens(stdin, &writeToFile); // get user command and tokenize it to args, check if there is a redirect to file. if(args != NULL) // if user did enter a command do the following { exitCode = runExec(args, writeToFile); // fork and run the process, return child exit code FreeTokens(args); //in case of error while trying to create process - this method will free the memory already if(exitCode == ERROR) // in case we need to exit. goto quit; } writeToFile = false; // set the "boolean" back to default } quit: free(host_n); free(user_n); return 0; }
TEST_F(ParserTest, Test_001) { TokenType::vector tokens; ReadTokens("./Resources/Example0001.titan", tokens); GTEST_ASSERT_LT(0, tokens.size()); AST_Program::shared_ptr node; { Parser_program program; TokenTypeContext ctx(tokens); program.read(ctx, node); } }
int FilterRuleTable::Main(int argc, char *argv[]) { // Process command-line options. Options options; ProcessOptions(argc, argv, options); // Open input file. Moses::InputFileStream testStream(options.testSetFile); // Read the first test sentence and determine if it is a parse tree or a // string. std::string line; if (!std::getline(testStream, line)) { // TODO Error? return 0; } if (line.find_first_of('<') == std::string::npos) { // Test sentences are strings. std::vector<std::vector<std::string> > sentences; do { sentences.resize(sentences.size()+1); ReadTokens(line, sentences.back()); } while (std::getline(testStream, line)); StringBasedFilter filter(sentences); filter.Filter(std::cin, std::cout); } else { // Test sentences are XML parse trees. XmlTreeParser parser; std::vector<boost::shared_ptr<StringTree> > sentences; int lineNum = 1; do { if (line.size() == 0) { std::cerr << "skipping blank test sentence at line " << lineNum << std::endl; continue; } sentences.push_back(boost::shared_ptr<StringTree>(parser.Parse(line))); ++lineNum; } while (std::getline(testStream, line)); TreeBasedFilter filter(sentences); filter.Filter(std::cin, std::cout); } return 0; }
JsonNodeT *JsonParse(const char *json) { JsonNodeT *node = NULL; int num = 0; LOG("Lexing JSON."); if (CountTokens(json, &num)) { ParserT parser; /* read tokens into an array */ TokenT *tokens = ReadTokens(json, num); /* now... parse! */ ParserInit(&parser, tokens, num); LOG("Parsing JSON."); if (!ParseValue(&parser, &node)) { #ifdef DEBUG_LEXER LOG("Parse error: %s at token ", parser.errmsg); TokenPrint(&parser.tokens[parser.pos]); #else LOG("Parse error: %s at position %d.", parser.errmsg, parser.tokens[parser.pos].pos); #endif MemUnref(node); node = NULL; } else { LOG("Parsing finished."); } MemUnref(tokens); } return node; }
int ExtractGHKM::Main(int argc, char *argv[]) { // Process command-line options. Options options; ProcessOptions(argc, argv, options); // Open input files. InputFileStream targetStream(options.targetFile); InputFileStream sourceStream(options.sourceFile); InputFileStream alignmentStream(options.alignmentFile); // Open output files. OutputFileStream fwdExtractStream; OutputFileStream invExtractStream; std::ofstream glueGrammarStream; std::ofstream unknownWordStream; std::string fwdFileName = options.extractFile; std::string invFileName = options.extractFile + std::string(".inv"); if (options.gzOutput) { fwdFileName += ".gz"; invFileName += ".gz"; } OpenOutputFileOrDie(fwdFileName, fwdExtractStream); OpenOutputFileOrDie(invFileName, invExtractStream); if (!options.glueGrammarFile.empty()) { OpenOutputFileOrDie(options.glueGrammarFile, glueGrammarStream); } if (!options.unknownWordFile.empty()) { OpenOutputFileOrDie(options.unknownWordFile, unknownWordStream); } // Target label sets for producing glue grammar. std::set<std::string> labelSet; std::map<std::string, int> topLabelSet; // Word count statistics for producing unknown word labels. std::map<std::string, int> wordCount; std::map<std::string, std::string> wordLabel; std::string targetLine; std::string sourceLine; std::string alignmentLine; XmlTreeParser xmlTreeParser(labelSet, topLabelSet); ScfgRuleWriter writer(fwdExtractStream, invExtractStream, options); size_t lineNum = options.sentenceOffset; while (true) { std::getline(targetStream, targetLine); std::getline(sourceStream, sourceLine); std::getline(alignmentStream, alignmentLine); if (targetStream.eof() && sourceStream.eof() && alignmentStream.eof()) { break; } if (targetStream.eof() || sourceStream.eof() || alignmentStream.eof()) { Error("Files must contain same number of lines"); } ++lineNum; // Parse target tree. if (targetLine.size() == 0) { std::cerr << "skipping line " << lineNum << " with empty target tree\n"; continue; } std::auto_ptr<ParseTree> t; try { t = xmlTreeParser.Parse(targetLine); assert(t.get()); } catch (const Exception &e) { std::ostringstream s; s << "Failed to parse XML tree at line " << lineNum; if (!e.GetMsg().empty()) { s << ": " << e.GetMsg(); } Error(s.str()); } // Read source tokens. std::vector<std::string> sourceTokens(ReadTokens(sourceLine)); // Read word alignments. Alignment alignment; try { alignment = ReadAlignment(alignmentLine); } catch (const Exception &e) { std::ostringstream s; s << "Failed to read alignment at line " << lineNum << ": "; s << e.GetMsg(); Error(s.str()); } if (alignment.size() == 0) { std::cerr << "skipping line " << lineNum << " without alignment points\n"; continue; } // Record word counts. if (!options.unknownWordFile.empty()) { CollectWordLabelCounts(*t, options, wordCount, wordLabel); } // Form an alignment graph from the target tree, source words, and // alignment. AlignmentGraph graph(t.get(), sourceTokens, alignment); // Extract minimal rules, adding each rule to its root node's rule set. graph.ExtractMinimalRules(options); // Extract composed rules. if (!options.minimal) { graph.ExtractComposedRules(options); } // Write the rules, subject to scope pruning. const std::vector<Node *> &targetNodes = graph.GetTargetNodes(); for (std::vector<Node *>::const_iterator p = targetNodes.begin(); p != targetNodes.end(); ++p) { const std::vector<const Subgraph *> &rules = (*p)->GetRules(); for (std::vector<const Subgraph *>::const_iterator q = rules.begin(); q != rules.end(); ++q) { ScfgRule r(**q); // TODO Can scope pruning be done earlier? if (r.Scope() <= options.maxScope) { writer.Write(r); } } } } if (!options.glueGrammarFile.empty()) { WriteGlueGrammar(labelSet, topLabelSet, glueGrammarStream); } if (!options.unknownWordFile.empty()) { WriteUnknownWordLabel(wordCount, wordLabel, options, unknownWordStream); } return 0; }