Main Page | Class List | File List | Class Members | File Members

translator.cpp

Go to the documentation of this file.
00001 
00006 //
00007 // Date:      14 November   2005   
00008 //
00009 // Copyright (c) Sergey Satskiy 2003 - 2005
00010 //               <sergesatsky@yahoo.com>
00011 //
00012 // Permission to copy, use, modify, sell and distribute this software 
00013 // is granted provided this copyright notice appears in all copies. 
00014 // This software is provided "as is" without express or implied
00015 // warranty, and with no claim as to its suitability for any purpose.
00016 //
00017 
00018 
00019 #include <iostream>
00020 #include <string>
00021 #include <vector>
00022 using namespace std;
00023 
00024 #include "FormulaGrammar.h"
00025 
00026 
00027 
00028     // The translator takes no arguments or
00029     // a help key
00030 int  main( int  argc, char **  argv )
00031 {
00032 
00033         // Check the command line arguments first
00034     if ( argc != 1 )
00035     {
00036         if ( (argc == 2 && ( string( argv[ 1 ] ) == "-h" ))     ||
00037              (argc == 2 && ( string( argv[ 1 ] ) == "--help" ))    )
00038         {
00039             cout << "Usage: " << argv[ 0 ] << " [-h | --help]" << endl
00040                  << "Expects a formula on the standard input." << endl;
00041             return 0;
00042         }
00043 
00044             // Unexpected arguments
00045         cerr << "Unexpected arguments. Type " << argv[ 0 ]
00046              << " --help for usage." << endl;
00047         return 1;
00048     }
00049 
00050     
00051     string                      InputFormula;           // Found start state
00052     vector< string >            Code;                   // List of transitions
00053     parse_info< const char * >  Info;                   // Line parsing result object
00054     SFormulaGrammar             FormulaGrammar( Code ); // Grammar rules
00055 
00056 
00057         // Input the formula to be translated
00058     cin >> InputFormula;
00059     if ( cin.fail() )
00060     {
00061         cerr << "Wrong input stream" << endl;
00062         return 2;
00063     }
00064 
00065 
00066     try
00067     {
00068             // Parse the input formula using the grammar and standard space parser
00069         Info = parse( InputFormula.c_str(), FormulaGrammar, space_p );
00070         if ( !Info.full )
00071         {
00072             cerr << "Parsing error. Stopped at: '" << Info.stop << "'" << endl;
00073             return 3;
00074         }
00075 
00076             // Print the resulting vector of code on the standard output
00077         for ( vector< string >::const_iterator  k( Code.begin() );
00078               k != Code.end(); ++k )
00079         {
00080             cout << *k << endl;
00081         }
00082     }
00083     catch ( exception &  Exception )
00084     {
00085         cerr << Exception.what() << endl;
00086         return 4;
00087     }
00088     catch ( ... )
00089     {
00090         cerr << "Unknown exception." << endl;
00091         return 4;
00092     }
00093 
00094     return 0;
00095 }
00096 

Generated on Sun Nov 20 21:09:13 2005 for boost:spirit and virtual machines by  doxygen 1.4.4