| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
|
| 3 |
|
| 4 |
<HTML> |
| 5 |
<HEAD> |
| 6 |
<TITLE>C++ Notes</TITLE> |
| 7 |
<STYLE type="text/css"> |
| 8 |
p.c7 {font-family: Arial; font-size: 80%} |
| 9 |
p.c6 {font-weight: bold} |
| 10 |
tt.c5 {font-style: italic} |
| 11 |
span.c4 {font-family: Courier New} |
| 12 |
small.c3 {font-family: Courier New} |
| 13 |
small.c2 {color: #000000; font-family: Arial} |
| 14 |
a.c1 {color: #000000; font-family: Arial} |
| 15 |
</STYLE> |
| 16 |
<STYLE type="text/css"> |
| 17 |
td.c1 {font-weight: bold} |
| 18 |
</STYLE> |
| 19 |
</HEAD> |
| 20 |
|
| 21 |
<BODY bgcolor="#FFFFFF" text="#000000"> |
| 22 |
<H1><A name="_notes">C++ Notes</A></H1> |
| 23 |
<HR> |
| 24 |
|
| 25 |
<P>The C++ runtime and generated grammars look very much the |
| 26 |
same as the java ones. There are some subtle differences |
| 27 |
though, but more on this later.</P> |
| 28 |
|
| 29 |
<H2><A name="_buildruntime">Building the runtime</A></H2> |
| 30 |
|
| 31 |
<P>The following is a bit unix centric. For Windows some |
| 32 |
contributed project files can be found in lib/cpp/contrib. These |
| 33 |
may be slightly outdated. |
| 34 |
<P>The runtime files are located in the lib/cpp subdirectory |
| 35 |
of the ANTLR distribution. Building it is in general done via the |
| 36 |
toplevel configure script and the Makefile generated by the configure |
| 37 |
script. Before configuring please read INSTALL.txt in the toplevel directory. The file lib/cpp/README may contain some extra information on specific target machines.</P> |
| 38 |
<PRE> |
| 39 |
./configure --prefix=/usr/local |
| 40 |
make |
| 41 |
</PRE> |
| 42 |
|
| 43 |
<P>Installing ANTLR and the runtime is then done by typing</P> |
| 44 |
<PRE> |
| 45 |
make install |
| 46 |
</PRE> |
| 47 |
This installs the runtime library libantlr.a in |
| 48 |
/usr/local/lib and the header files in |
| 49 |
/usr/local/include/antlr. Two convenience scripts antlr and |
| 50 |
antlr-config are also installed into /usr/local/bin. The first |
| 51 |
script takes care of invoking antlr and the other can be used to |
| 52 |
query the right options for your compiler to build files with |
| 53 |
antlr. |
| 54 |
|
| 55 |
<H2><A name="_usingruntime">Using the runtime</A></H2> |
| 56 |
Generally you will compile the ANTLR generated files with |
| 57 |
something similar to: |
| 58 |
<PRE> |
| 59 |
c++ -c MyParser.cpp -I/usr/local/include |
| 60 |
</PRE> |
| 61 |
Linking is done with something similar to: |
| 62 |
<PRE> |
| 63 |
c++ -o MyExec <your .o files> -L/usr/local/lib -lantlr |
| 64 |
</PRE> |
| 65 |
|
| 66 |
<H2><A name="_generatingcpp">Getting ANTLR to generate |
| 67 |
C++</A></H2> |
| 68 |
|
| 69 |
<P>To get ANTLR to generate C++ code you have to add</P> |
| 70 |
<PRE> |
| 71 |
language="Cpp"; |
| 72 |
</PRE> |
| 73 |
to the global options section. After that things are pretty |
| 74 |
much the same as in java mode except that a all token and AST |
| 75 |
classes are wrapped by a reference counting class (this to |
| 76 |
make live easier (in some ways and much harder in others)). |
| 77 |
The reference counting class uses |
| 78 |
<PRE> |
| 79 |
operator-> |
| 80 |
</PRE> |
| 81 |
to reference the object it is wrapping. As a result of this |
| 82 |
you use -> in C++ mode in stead of the '.' of java. See |
| 83 |
the examples in examples/cpp for some illustrations. |
| 84 |
|
| 85 |
<H2><A name="_changingasttype">AST types</A></H2> |
| 86 |
|
| 87 |
<P>New as of ANTLR 2.7.2 is that if you supply the |
| 88 |
<PRE>buildAST=true</PRE> option to a parser then you <b>have |
| 89 |
to</b> set and initialize an ASTFactory for the parser and |
| 90 |
treewalkers that use the resulting AST. |
| 91 |
<PRE> |
| 92 |
ASTFactory my_factory; // generates CommonAST per default.. |
| 93 |
MyParser parser( some-lexer ); |
| 94 |
// Do setup from the AST factory repeat this for all parsers using the AST |
| 95 |
parser.initializeASTFactory( my_factory ); |
| 96 |
parser.setASTFactory( &my_factory ); |
| 97 |
</PRE> |
| 98 |
<P>In C++ mode it is also possible to override the AST type used |
| 99 |
by the code generated by ANTLR. To do this you have to do the |
| 100 |
following: |
| 101 |
|
| 102 |
<UL> |
| 103 |
<LI> |
| 104 |
Define a custom AST class like the following: |
| 105 |
<PRE> |
| 106 |
#ifndef __MY_AST_H__ |
| 107 |
#define __MY_AST_H__ |
| 108 |
|
| 109 |
#include <antlr/CommonAST.hpp> |
| 110 |
|
| 111 |
class MyAST; |
| 112 |
|
| 113 |
typedef ANTLR_USE_NAMESPACE(antlr)ASTRefCount<MyAST> RefMyAST; |
| 114 |
|
| 115 |
/** Custom AST class that adds line numbers to the AST nodes. |
| 116 |
* easily extended with columns. Filenames will take more work since |
| 117 |
* you'll need a custom token class as well (one that contains the |
| 118 |
* filename) |
| 119 |
*/ |
| 120 |
class MyAST : public ANTLR_USE_NAMESPACE(antlr)CommonAST { |
| 121 |
public: |
| 122 |
// copy constructor |
| 123 |
MyAST( const MyAST& other ) |
| 124 |
: CommonAST(other) |
| 125 |
, line(other.line) |
| 126 |
{ |
| 127 |
} |
| 128 |
// Default constructor |
| 129 |
MyAST( void ) : CommonAST(), line(0) {} |
| 130 |
virtual ~MyAST( void ) {} |
| 131 |
// get the line number of the node (or try to derive it from the child node |
| 132 |
virtual int getLine( void ) const |
| 133 |
{ |
| 134 |
// most of the time the line number is not set if the node is a |
| 135 |
// imaginary one. Usually this means it has a child. Refer to the |
| 136 |
// child line number. Of course this could be extended a bit. |
| 137 |
// based on an example by Peter Morling. |
| 138 |
if ( line != 0 ) |
| 139 |
return line; |
| 140 |
if( getFirstChild() ) |
| 141 |
return ( RefMyAST(getFirstChild())->getLine() ); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
virtual void setLine( int l ) |
| 145 |
{ |
| 146 |
line = l; |
| 147 |
} |
| 148 |
/** the initialize methods are called by the tree building constructs |
| 149 |
* depending on which version is called the line number is filled in. |
| 150 |
* e.g. a bit depending on how the node is constructed it will have the |
| 151 |
* line number filled in or not (imaginary nodes!). |
| 152 |
*/ |
| 153 |
virtual void initialize(int t, const ANTLR_USE_NAMESPACE(std)string& txt) |
| 154 |
{ |
| 155 |
CommonAST::initialize(t,txt); |
| 156 |
line = 0; |
| 157 |
} |
| 158 |
virtual void initialize( ANTLR_USE_NAMESPACE(antlr)RefToken t ) |
| 159 |
{ |
| 160 |
CommonAST::initialize(t); |
| 161 |
line = t->getLine(); |
| 162 |
} |
| 163 |
virtual void initialize( RefMyAST ast ) |
| 164 |
{ |
| 165 |
CommonAST::initialize(ANTLR_USE_NAMESPACE(antlr)RefAST(ast)); |
| 166 |
line = ast->getLine(); |
| 167 |
} |
| 168 |
// for convenience will also work without |
| 169 |
void addChild( RefMyAST c ) |
| 170 |
{ |
| 171 |
BaseAST::addChild( ANTLR_USE_NAMESPACE(antlr)RefAST(c) ); |
| 172 |
} |
| 173 |
// for convenience will also work without |
| 174 |
void setNextSibling( RefMyAST c ) |
| 175 |
{ |
| 176 |
BaseAST::setNextSibling( ANTLR_USE_NAMESPACE(antlr)RefAST(c) ); |
| 177 |
} |
| 178 |
// provide a clone of the node (no sibling/child pointers are copied) |
| 179 |
virtual ANTLR_USE_NAMESPACE(antlr)RefAST clone( void ) |
| 180 |
{ |
| 181 |
return ANTLR_USE_NAMESPACE(antlr)RefAST(new MyAST(*this)); |
| 182 |
} |
| 183 |
static ANTLR_USE_NAMESPACE(antlr)RefAST factory( void ) |
| 184 |
{ |
| 185 |
return ANTLR_USE_NAMESPACE(antlr)RefAST(RefMyAST(new MyAST())); |
| 186 |
} |
| 187 |
private: |
| 188 |
int line; |
| 189 |
}; |
| 190 |
#endif |
| 191 |
</PRE> |
| 192 |
</LI> |
| 193 |
|
| 194 |
<LI> |
| 195 |
Tell ANTLR's C++ codegenerator to use your RefMyAST by |
| 196 |
including the following in the options section of your grammars: |
| 197 |
<PRE> |
| 198 |
ASTLabelType = "RefMyAST"; |
| 199 |
</PRE> |
| 200 |
After that you only need to tell the parser before every |
| 201 |
invocation of a new instance that it should use the AST |
| 202 |
factory defined in your class. This is done like this: |
| 203 |
<PRE> |
| 204 |
// make factory with default type of MyAST |
| 205 |
ASTFactory my_factory( "MyAST", MyAST::factory ); |
| 206 |
My_Parser parser(lexer); |
| 207 |
// make sure the factory knows about all AST types in the parser.. |
| 208 |
parser.initializeASTFactory(my_factory); |
| 209 |
// and tell the parser about the factory.. |
| 210 |
parser.setASTFactory( &my_factory ); |
| 211 |
</PRE> |
| 212 |
<P>After these steps you can access methods/attributes of (Ref)MyAST |
| 213 |
directly (without typecasting) in parser/treewalker productions. |
| 214 |
<P>Forgetting to do a setASTFactory results in a nice SIGSEGV or you OS's |
| 215 |
equivalent. The default constructor of ASTFactory initializes itself to |
| 216 |
generate CommonAST objects. |
| 217 |
<P>If you use a 'chain' of parsers/treewalkers then you have to make sure |
| 218 |
they all share the same AST factory. Also if you add new definitions of |
| 219 |
ASTnodes/tokens in downstream parsers/treewalkers you have to apply the |
| 220 |
respective initializeASTFactory methods to this factory. |
| 221 |
<P>This all is demonstrated in the examples/cpp/treewalk example. |
| 222 |
</LI> |
| 223 |
</UL> |
| 224 |
|
| 225 |
<H2><A name="_heteroast">Using Heterogeneous AST |
| 226 |
types</A></H2> |
| 227 |
|
| 228 |
<P>This should now (as of 2.7.2) work in C++ mode. With probably some |
| 229 |
caveats. |
| 230 |
<P>The heteroAST example show how to set things up. A short excerpt: |
| 231 |
<PRE> |
| 232 |
ASTFactory ast_factory; |
| 233 |
|
| 234 |
parser.initializeASTFactory(ast_factory); |
| 235 |
parser.setASTFactory(&ast_factory); |
| 236 |
</PRE> |
| 237 |
<P>A small excerpt from the generated initializeASTFactory method: |
| 238 |
<PRE> |
| 239 |
void CalcParser::initializeASTFactory( antlr::ASTFactory& factory ) |
| 240 |
{ |
| 241 |
factory.registerFactory(4, "PLUSNode", PLUSNode::factory); |
| 242 |
factory.registerFactory(5, "MULTNode", MULTNode::factory); |
| 243 |
factory.registerFactory(6, "INTNode", INTNode::factory); |
| 244 |
factory.setMaxNodeType(11); |
| 245 |
} |
| 246 |
</PRE> |
| 247 |
<P>After these steps ANTLR should be able to decide what factory to use at |
| 248 |
what time. |
| 249 |
|
| 250 |
<H2><A name="_extras">Extra functionality in C++ |
| 251 |
mode.</A></H2> |
| 252 |
In C++ mode ANTLR supports some extra functionality to make |
| 253 |
life a little easier. |
| 254 |
|
| 255 |
<H3>Inserting Code</H3> |
| 256 |
In C++ mode some extra control is supplied over the places |
| 257 |
where code can be placed in the gerenated files. These are |
| 258 |
extensions on the <B>header</B> directive. The syntax is: |
| 259 |
<PRE> |
| 260 |
header "<identifier>" { <CODE> } |
| 261 |
</CODE> |
| 262 |
</PRE> |
| 263 |
|
| 264 |
<TABLE summary="Overview of header id's"> |
| 265 |
<TR> |
| 266 |
<TD class="c1">identifier</TD> |
| 267 |
|
| 268 |
<TD class="c1">where</TD> |
| 269 |
</TR> |
| 270 |
|
| 271 |
<TR> |
| 272 |
<TD><CODE>pre_include_hpp</CODE></TD> |
| 273 |
|
| 274 |
<TD>Code is inserted before ANTLR generated includes in |
| 275 |
the header file.</TD> |
| 276 |
</TR> |
| 277 |
|
| 278 |
<TR> |
| 279 |
<TD><CODE>post_include_hpp</CODE></TD> |
| 280 |
|
| 281 |
<TD>Code is inserted after ANTLR generated includes in |
| 282 |
the header file, but outside any generated namespace |
| 283 |
specifications.</TD> |
| 284 |
</TR> |
| 285 |
|
| 286 |
<TR> |
| 287 |
<TD><CODE>pre_include_cpp</CODE></TD> |
| 288 |
|
| 289 |
<TD>Code is inserted before ANTLR generated includes in |
| 290 |
the cpp file.</TD> |
| 291 |
</TR> |
| 292 |
|
| 293 |
<TR> |
| 294 |
<TD><CODE>post_include_cpp</CODE></TD> |
| 295 |
|
| 296 |
<TD>Code is inserted after ANTLR generated includes in |
| 297 |
the cpp file, but outside any generated namespace |
| 298 |
specifications.</TD> |
| 299 |
</TR> |
| 300 |
</TABLE> |
| 301 |
|
| 302 |
<H3>Pacifying the preprocessor</H3> |
| 303 |
|
| 304 |
<P>Sometimes various tree building constructs with '#' |
| 305 |
in them clash with the C/C++ preprocessor. ANTLR's |
| 306 |
preprocessor for actions is slightly extended in C++ mode to |
| 307 |
alleviate these pains.</P> |
| 308 |
<P><B>NOTE:</B> At some point I plan to replace the '#' by |
| 309 |
something different that gives less trouble in C++. |
| 310 |
|
| 311 |
<P>The following preprocessor constructs are not |
| 312 |
touched. (And as a result you cannot use these as labels for |
| 313 |
AST nodes.</P> |
| 314 |
|
| 315 |
<UL> |
| 316 |
<LI><CODE>if</CODE></LI> |
| 317 |
|
| 318 |
<LI><CODE>define</CODE></LI> |
| 319 |
|
| 320 |
<LI><CODE>ifdef</CODE></LI> |
| 321 |
|
| 322 |
<LI><CODE>ifndef</CODE></LI> |
| 323 |
|
| 324 |
<LI><CODE>else</CODE></LI> |
| 325 |
|
| 326 |
<LI><CODE>elif</CODE></LI> |
| 327 |
|
| 328 |
<LI><CODE>endif</CODE></LI> |
| 329 |
|
| 330 |
<LI><CODE>warning</CODE></LI> |
| 331 |
|
| 332 |
<LI><CODE>error</CODE></LI> |
| 333 |
|
| 334 |
<LI><CODE>ident</CODE></LI> |
| 335 |
|
| 336 |
<LI><CODE>pragma</CODE></LI> |
| 337 |
|
| 338 |
<LI><CODE>include</CODE></LI> |
| 339 |
</UL> |
| 340 |
|
| 341 |
<P>As another extra it's possible to escape '#'-signs |
| 342 |
with a backslash e.g. "\#". As the action lexer sees these |
| 343 |
they get translated to simple '#' characters.</P> |
| 344 |
|
| 345 |
<H2><A name="_template">A template grammar file for |
| 346 |
C++</A></H2> |
| 347 |
<PRE> |
| 348 |
<CODE>header "pre_include_hpp" { |
| 349 |
// gets inserted before antlr generated includes in the header file |
| 350 |
} |
| 351 |
header "post_include_hpp" { |
| 352 |
// gets inserted after antlr generated includes in the header file |
| 353 |
// outside any generated namespace specifications |
| 354 |
} |
| 355 |
|
| 356 |
header "pre_include_cpp" { |
| 357 |
// gets inserted before the antlr generated includes in the cpp file |
| 358 |
} |
| 359 |
|
| 360 |
header "post_include_cpp" { |
| 361 |
// gets inserted after the antlr generated includes in the cpp file |
| 362 |
} |
| 363 |
|
| 364 |
header { |
| 365 |
// gets inserted after generated namespace specifications in the header |
| 366 |
// file. But outside the generated class. |
| 367 |
} |
| 368 |
|
| 369 |
options { |
| 370 |
language="Cpp"; |
| 371 |
namespace="something"; // encapsulate code in this namespace |
| 372 |
// namespaceStd="std"; // cosmetic option to get rid of long defines |
| 373 |
// in generated code |
| 374 |
// namespaceAntlr="antlr"; // cosmetic option to get rid of long defines |
| 375 |
// in generated code |
| 376 |
genHashLines = true; // generated #line's or turn it off. |
| 377 |
} |
| 378 |
|
| 379 |
{ |
| 380 |
// global stuff in the cpp file |
| 381 |
... |
| 382 |
} |
| 383 |
class MyParser extends Parser; |
| 384 |
options { |
| 385 |
exportVocab=My; |
| 386 |
} |
| 387 |
{ |
| 388 |
// additional methods and members |
| 389 |
... |
| 390 |
} |
| 391 |
... rules ... |
| 392 |
|
| 393 |
{ |
| 394 |
// global stuff in the cpp file |
| 395 |
... |
| 396 |
} |
| 397 |
class MyLexer extends Lexer; |
| 398 |
options { |
| 399 |
exportVocab=My; |
| 400 |
} |
| 401 |
{ |
| 402 |
// additional methods and members |
| 403 |
... |
| 404 |
} |
| 405 |
... rules ... |
| 406 |
|
| 407 |
{ |
| 408 |
// global stuff in the cpp file |
| 409 |
... |
| 410 |
} |
| 411 |
class MyTreeParser extends TreeParser; |
| 412 |
options { |
| 413 |
exportVocab=My; |
| 414 |
} |
| 415 |
{ |
| 416 |
// additional methods and members |
| 417 |
... |
| 418 |
} |
| 419 |
... rules ... |
| 420 |
</CODE> |
| 421 |
</PRE> |
| 422 |
|
| 423 |
</BODY> |
| 424 |
</HTML> |