27 March 2000 Release 2.22 Notes for New Users of PCCTS Version 1.33MR22
44
f = fopen(StripQuotes(LATEXT(1)),"r");
if ( f==NULL ) {
warn(eMsg1("cannot open token defs file '%s'",
LATEXT(1)+1));}
else {
ANTLRm( enum_file(), f, PARSE_ENUM_FILE);
UserDefdTokens = 1;
}
zzrestore_antlr_state(&st);
zzrestore_dlg_state(&dst);
}>>
The code uses zzsave_antlr_state() and zzsave_dlg_state() to save the state of the current parse. The
ANTLR
m macro
specifies a starting rule for
ANTLR
of "enum_file" and starts
DLG
in the
PARSE_ENUM_FILE
state rather than the
default state (which is the current state - whatever it might be). Because enum_file() is called without any
arguments it appears that enum_file() does not use ASTs nor pass back any attributes. Contributed by TJP.
(C Mode) Attributes
#188. Use symbolic tags (rather than numbers) to refer to attributes and ASTs in rules
prior to version 1.30:
rule : X Y
<<printf("%s %s",$1,$2);>> ;
sinc version 1.30:
rule : xx:X yy:Y
<<printf("%s %s",$xx,$yy);>> ;
#189. Rules no longer have attributes:
rule : r1:rule1 <<...$r1...;>>
won't work
Actually this still works if one restricts oneself to C mode and uses
numeric
labels like $1 and $2. However
numeric labels are a deprecated feature, can't be used in C++ mode, and can't be used in the same source file as
symbolic labels, so it's best to avoid them.
#190. Attributes are built automatically only for terminals
To construct attributes under any other circumstances one must use
$[
TokenCode
,...]
or zzcr_attr().
#191. How to access the text or token part of an attribute
The way to access the text, token, (or whatever) part of an attribute depends on the way the attribute is stored. If one
uses the
PCCTS
supplied routine "pccts/h/charbuf.h" then:
id : "[a-z]+" <<printf("Token is %s\n",$1.text);>> ;
If one uses the
PCCTS
supplied routine "pccts/h/charptr.c" and "pccts/h/charptr.h" then:
id : "[a-z]+" <<printf("Token is %s\n",$1);>> ;
If one uses the
PCCTS
supplied routine "pccts/h/int.h" (which stores numbers only) then:
number : "[0-9]+" <<printf ("Token is %d\n",$1);>> ;
(Note the use of "%d" rather than "%s" in the printf() format).
#192. The $0 and $$ constructs are no longer supported - use inheritance instead (Item #113)
#193. If you use attributes then define a zzd_attr() to release resources (memory) when an attribute is destroyed
#194. Don't pass automatically constructed attributes to an outer rule or sibling rule - they'll be out of scope
The
PCCTS
generated variables which contain automatically generated attributes go out of scope at the end of the
rule or sub-rule that contains them. Of course you can copy the attribute to a variable that won't go out of scope. If
the attribute contains a pointer which is copied (e.g. pccts/h/charptr.c) then extra caution is required because of the
actions of zzd_attr(). See Item #195.
#195. A charptr.c attribute must be copied before being passed to a calling rule
The pccts/h/charptr.c routines use a pointer to a string. The string itself will go out of scope when the rule or sub-
rule is exited. Why ? The string is copied to the heap when
ANTLR
calls the routine zzcr_attr() supplied by charptr.c
- however
ANTLR
also calls the charptr.c supplied routine zzd_attr() (which frees the allocated string) as soon as
the rule or sub-rule exits. The result is that in order to pass charptr.c strings to outer rules via inheritance it is
necessary to make an independent copy of the string (using strdup for example) or else by zeroing the original
pointer to prevent its deallocation.