Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

parser.C

Go to the documentation of this file.
00001 // $Id: parser.C,v 1.5 2001/08/19 11:03:45 dvermeir Exp $
00002 
00003 #include "parser.h"
00004 
00005 Parser::Parser(istream& is): is_(is) {
00006 }
00007  
00008 ref<Fsm>
00009 Parser::parse() {
00010 // Will return 0 if there is a syntax error.
00011 
00012 // Parse initial state: just read the string.
00013 
00014 Fsm::State      initial_state;
00015 if (!(is_ >> initial_state))
00016   return ref<Fsm>();
00017 
00018 ref<Fsm>        fsm = Fsm::create(initial_state);
00019 
00020 // Parse set of final states and add them to fsm.
00021 
00022 Fsm::STATES     final_states;
00023 if (parse_states(final_states))
00024   for (Fsm::STATES::const_iterator i= final_states.begin();
00025        i != final_states.end(); ++i) 
00026     fsm->add_final_state(*i);
00027 else 
00028   return ref<Fsm>();
00029   
00030 // Parse transitions until eof.
00031 
00032 while (is_) {
00033   Fsm::STATES   from;
00034   Fsm::STATES   to;
00035   Fsm::SYMBOLS  syms;
00036 
00037   if (parse_transition(from,syms,to)) {
00038     // Insert all single transitions from the sets from, syms, to.
00039     for (Fsm::STATES::const_iterator i=from.begin(); i!=from.end(); ++i)
00040       for (Fsm::SYMBOLS::const_iterator j=syms.begin(); j!=syms.end(); ++j)
00041         for (Fsm::STATES::const_iterator k=to.begin(); k!=to.end(); ++k)
00042           fsm->add_transition(*i,*j,*k);
00043     }
00044   else 
00045     return ref<Fsm>();
00046   }
00047 return fsm;
00048 }
00049 
00050 bool
00051 Parser::parse_states(Fsm::STATES& states) {
00052 string s;
00053 if (is_ >> s) {
00054   if (s == "{") {
00055     while ( (is_ >> s) && ( s != "}" ) && ( s != "{" ) )
00056       states.insert(s);
00057     if (s != "}" ) // syntax error: eof while reading states..
00058       return false;
00059     else
00060       return true;
00061     }
00062   else 
00063     if ( s != "}" ) {
00064       states.insert(s);
00065       return true;
00066       }
00067     else
00068       return false;
00069   }
00070 return true;
00071 }
00072 
00073 bool
00074 Parser::parse_syms(Fsm::SYMBOLS& syms) {
00075 // Almost identical fo parse_states.
00076 string s;
00077 if (is_ >> s) {
00078   if (s == "{") {
00079     while ( (is_ >> s) && ( s != "}" ) && ( s != "{" ) )
00080       syms.insert(s);
00081     if (s != "}" ) // syntax error: eof while reading syms..
00082       return false;
00083     else
00084       return true;
00085     }
00086   else 
00087     if ( s != "{") {
00088       syms.insert(s);
00089       return true;
00090       }
00091     else
00092       return false;
00093   }
00094 return true;
00095 }
00096 
00097 bool
00098 Parser::parse_transition(Fsm::STATES& from, Fsm::SYMBOLS& syms, Fsm::STATES& to) {
00099 return parse_states(from) && parse_syms(syms) && parse_states(to);
00100 }
00101 

FSM [ August, 2001]