27 March 2000 Release 2.22 Notes for New Users of PCCTS Version 1.33MR22
16
class MyParser {
<<
public:
virtual void syn(
ANTLRAbstractToken * tok,
ANTLRChar * egroup,
SetWordType * eset,
ANTLRTokenType etok,
int k);
>>
...
}
The actual error recovery code for
ANTLR
is rather long. For an example of adding column information to the
syntax error message see Example #5.
ASTs
#77. To enable AST construction (automatic or explicit) use the
ANTLR
­gt switch
#78. Use
ANTLR
option -newAST to make AST creation a member function of the parser
This allows a user to define a parser member function to create an AST object. This is useful for factory methods
and cases where the AST are "owned" by a particular instance of a parser.
#79. Use symbolic tags (rather than numbers) to refer to tokens and ASTs in rules
prior to version 1.30:
rule! : x y
<<#0=#(#1,#2);>> ;
with version 1.30:
rule! : xx:x yy:y
<<#0=#(#xx,#yy);>> ;
The symbolic tags are implemented as pointers to ASTs. The pointers are initialized to 0 at the start of the rule and
remain defined for the entire rule. See Item #62. Rules no longer return pointers to tokens (Item #97
#80. Constructor AST(
ANTLR
TokenPtr) is automatically called for terminals when
ANTLR
­gt switch is used
This can be suppressed using the "!" operator.
#81. If you use ASTs you have to pass a root AST to the parser
ASTBase *root=NULL;
...
Parser.startRule(&root,otherArguments);
root->preorder();
root->destroy();
#82. Use ast­>destroy() to recursively descend the AST tree and free all sub-trees
#83. Don't confuse
#[...]
with
#(...)
The first creates a single AST node using an AST constructor (which is usually based on an
ANTLR
Token or an
ANTLR
TokenType). It converts lexical information to an AST.
The second creates an AST tree or list (usually more than a single node) from other ASTs by filling in the "down"
field of the first node in the list to create a root node, and the "sibling" fields of each of the remaining ASTs in the
lists. It combines existing ASTs to create a more complex structure.
#token ID "[a-z]*"
#token COLON ":"
#token Stmt_With_Label
id! : name:ID <<#0=#[Stmt_With_Label,$name->getText()];>> ; /* a1 */
Standard AST constructors
ANTLR
-newAST option AST
Automatic conversion of token
new AST(ANTLRTokenPtr)
newAST(ANTLRTokenPtr)
Manual construction:
#(X,Y,Z) => new AST(X,Y,Z)
#(X,Y,Z) => newAST(X,Y,Z)