Sub parser

Top  Previous  Next

Scripts > Class elements and c++ instructions > Calling a production > Sub parser


A production can be called directly from the interpreter code. It then is not part of the real grammar of the parser in which this interpreter code is embedded. The called production is rather a start rule for a separate parser and a new input text is passed to it explicitly. The string parameter makes the difference to the look-ahead call of a production.


For example a sub parser could extract some parameters from a part of text, which was recognized by a look-ahead parser, before the main parser continues:


IF( Production() ) // look-ahead parser

{{ Parameters( xState.la_str() ); // sub parser }}

Production // main parser



Other applications of sub parsers are the treatment of include-files or external data could be fed into the program by a sub parser.

If e.g. the production with the name Include shall be called, then this could be done as follows.




str buf;

str sIncludeFile = xState.SourceRoot() + "\\" + xState.str( 1 );


if( load_file(buf, sIncludeFile ) )



  throw CTT_Error( sIncludeFile + " could not be opened");



Possible parameters of the Include production are appended to the string for the new source text.



To determine the number of lines to be parsed already before the beginning of parsing, the sub-parser CountLines can be used:


CountLines(int& xi) ::=



{{ xi++; }}



CountLines would have to be called in an action which is executed before the first token of the main parser was recognized:



int iLines = 0;

CountLines(xState.str(-2), iLines);






This page belongs to the TextTransformer Documentation

Home  Content  German