|
parser.ccGo to the documentation of this file.00001 // 00002 // $Id: parser.cc,v 1.6 2003/11/18 11:27:31 cholm Exp $ 00003 // 00004 // parser.cc 00005 // Copyright (C) 2002 Christian Holm Christensen <cholm@nbi.dk> 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public License 00009 // as published by the Free Software Foundation; either version 2 of 00010 // the License, or (at your option) any later version. 00011 // 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 // Lesser General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU Lesser General Public 00018 // License along with this library; if not, write to the Free 00019 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00020 // MA 02111-1307 USA 00021 // 00022 // 00023 /** @file doc/parser.cc 00024 @author Christian Holm 00025 @date Fri Jan 03 05:00:58 2003 00026 @brief Parser documentation */ 00027 /** @page parser_doc The Parser Class 00028 00029 @section parser_setup Setting up the Parser Class 00030 00031 To make a C++ class for the parser, you should have a grammar file 00032 that looks like 00033 @code 00034 %{ 00035 ... // Normal declaration stuff goes here 00036 #define YLMM_PARSER_CLASS parser 00037 #define YLMM_LEX_STATIC 00038 #include <ylmm/yaccmm.hh> 00039 %} 00040 ...// The rest is as usual 00041 @endcode 00042 00043 The file @link yaccmm.hh ylmm/yaccmm.hh @endlink redefines the 00044 usual @b Bison macros to forward calls to the user provided class 00045 (or it's base class ylmm::basic_parser). The macro 00046 @c YYPRINT is defined to forward calls to 00047 ylmm::basic_parser::print, @c YYFPRINTF to 00048 ylmm::basic_parser::message, @c YYFPRINTF to 00049 ylmm::basic_parser::trace, yyerror to 00050 ylmm::basic_parser::error. @c YYLTYPE is defined to be 00051 ylmm::location, and @c YYLLOC_DEFAULT to call 00052 ylmm::location::last. To customize the behaviour, overload the 00053 corresponding member functions in you derived classess. 00054 00055 You are free to define macros @c YYPARSE_PARAM, @c YYLEX_PARAM, 00056 @c YYERROR_VERBOSE, @c YYLSP_NEEDED, and so on. 00057 00058 The file @link yaccmm.hh ylmm/yaccmm.hh @endlink 00059 defines the interface to the generated C function via a C++ class, 00060 where @c YLMM_PARSER_CLASS is the name of the user parser class. 00061 The developer can either specify this as a specific instantation 00062 of ylmm::basic_parser, or it can be a sub-class of specific 00063 instantation of ylmm::basic_parser. 00064 00065 The macro @link yaccmm.hh YLMM_LEX_STATIC @endlink @e must be 00066 defined if the @b Yacc input file isn't a pure parser. If it's 00067 defined, the static function @c int @c yylex() will be defined. 00068 If your grammar uses location information, then you need to define 00069 @link yaccmm.hh YLMM_LEX_STATIC_LOCATION @endlink instead of 00070 @c YLMM_LEX_STATIC. Location information comes about in a 00071 @b Bison grammar via the use of @c @@$ or @c @@N (where @c N is a 00072 number) in the grammar rules or an explicit definition of the 00073 preprocessor constant @c YYLSP_NEEDED in the declaration part. 00074 00075 If the sematic token type isn't defined before inclussion of 00076 ylmm/yacmm.hh, then it is defined to be 00077 ylmm::basic_parser::token_type of the specific instantation. 00078 00079 If the grammar defines a @e Pure (that is @e reentrant) parser 00080 (via the @b Bison directive @c %pure_parser), then 00081 @c YLMM_LEX_STATIC must not be defined. 00082 00083 A derived class must define the member function @c scan. Hence, a 00084 minimal derived class looks like. 00085 @code 00086 #ifndef YLMM_basic_parser 00087 #include <ylmm/basic_parser.hh> 00088 #endif 00089 00090 class parser : public ylmm::basic_parser<YYSTYPE> { 00091 public: 00092 int scan(); 00093 } 00094 @endcode 00095 The @c scan member function should be defined in the regular way 00096 of @c yylex - that is, it should read a token from the input and 00097 return it's sematic token number (see also the @b Bison 00098 documentation). 00099 00100 If the application uses the ylmm::basic_parser template directly, 00101 the ylmm::basic_parser::scan member function must be defined for 00102 the particular instantitation, as the default behaviour is to end 00103 parsing immediately. 00104 00105 See also the example 00106 @link simple_parser.yy simple_parser.yy @endlink 00107 for and example of simple usage, and 00108 @link toycalc_parser.yy toycalc_parser.yy @endlink for a more 00109 complex usage. 00110 00111 @section parser_file Example Grammar 00112 00113 The grammar is defined in the usual way. Note, that a pointer to 00114 the parser is passed as the parameter @c _parser to the @b Bison 00115 generated C function. Hence, you can use that pointer in actions 00116 of the grammar. 00117 00118 Below follows a simple example of a parser of integers. This is 00119 reproduced from the example 00120 @link simple_parser.yy simple_parser.yy @endlink. See also the 00121 example @link toycalc_parser.yy toycalc_parser.yy @endlink for a 00122 more complex example. 00123 00124 First, we setup the usual stuff (see @ref parser_setup). 00125 @dontinclude simple_parser.yy 00126 @skip %{ 00127 @until %} 00128 00129 Next we define all the tokens and precedence rules. 00130 @until %% 00131 00132 And then finally we make the production rules, where we use the 00133 pointer to the parser object (via the @c _parser static variable), 00134 to make the actual productions 00135 @until %% 00136 00137 @subsection parser_class The Parser Class 00138 00139 The user defined parser class can then be setup to make a parse 00140 tree, using its member functions. 00141 00142 Below follows a simple example of a parser of integers. This is 00143 reproduced from the example 00144 @link simple_parser.hh simple_parser.hh @endlink. See also 00145 @link toycalc_parser.hh toycalc_parser.hh @endlink for a more 00146 complex example. 00147 00148 Here, we use an object of a ylmm::basic_scanner derived class to do 00149 the lexical scanning. That class will be explored in 00150 @ref scanner_file below. 00151 @dontinclude simple_parser.hh 00152 @skip class 00153 @until scan( 00154 00155 We define the needed member functions (see @ref parser_setup), as 00156 well as a couple a utility function. The @c error member 00157 functions uses the data member ylmm::basic_parser::_err_stream for 00158 output. The point is, that the user can set these so that all 00159 error messages are treated the same when using other libraries, 00160 etc. 00161 @until if 00162 00163 And finally, we define the member functions to deal with the 00164 semantic tokens to generated by the production rules in the parser 00165 file. Notice that we again use the data member 00166 ylmm::basic_parser::_msg_stream for output so that we can insure 00167 coherient output from the client application. 00168 @until }; 00169 00170 */ 00171 #error This file is not for compilation 00172 // 00173 // EOF 00174 // Christian Holm (home page) Last update Fri Jul 8 12:58:03 2005 Created by DoxyGen 1.4.3-20050530 |