Productions as functions

Top  Previous  Next

Introduction > Productions as functions


A production may be considered as a specification for creating a routine that parses a part of the input text. By creating code, this specification will result in a real routine. The routine can return a valueand will constitute its own scope for parameters and other local components like variables and constants. These again, can be passed to other productions, which are called like functions inside the body of the first production. The called productions parse sub sections of the part of text, which is parsed by the calling production.




In the following example the die Outer production calls the Inner production, which returns the string, which the Inner production has recognized.


Outer = 


{{string s = }}


{{out << "found a and " << s;}}



Inner =

( "b" | "c" )

{{ return xState.str(); }}



Input: "a b"

output: found: a and b


Input: "a c"

output: found: a and c



The created code is (in essence):


typedef std::string::const_iterator cts; )



// token ordered by symbol numbers

// ----------------------------------------------------

// Name               SymNo     regular expression

// ----------------------------------------------------


// a                      (  6 )         "a"

// b                      (  7 )         "b"

// c                      (  8 )         "c"



void COuterParser::Outer(cts xtBegin, cts xtEnd, plugin_type xpPlugin /*=NULL*/) 


  sps xState(xtBegin, xtEnd);

  // ... falls xpPlugin == NULL neues lokale Plugin, sonst xpPlugin in xState einsetzen

  if( m_apOuterScanner->GetNext(xState, false))


    Outer(xState, NULL, false);





void COuterParser::Outer(sps& xState, ...)


  m_apT0_a_of_OuterScanner->GetNext(xState, false);


  string s = 

  Inner(xState, ... );

  out << "found: a and " << s;




std::string COuterParser::Inner(sps& xState, ...)



  switch ( Alt0_of_Inner( xState.Sym() ) )


    case 7:  // "b"

    m_apT1_b_of_InnerScanner->GetNext(xState, true);


    case 8:  // "c"

    m_apT2_c_of_InnerScanner->GetNext(xState, true);


    default :

    throw tetra::CTT_ErrorUnexpected( ...);



  return xState.str(); 




The scanners are holding the information about the permitted token and the variable xState holds the information about the last recognized and the expected token.


Remark: the parenthesis around "b" | "c" are necessary. Otherwise the first alternative would return an undefined value.


This page belongs to the TextTransformer Documentation

Home  Content  German