27 March 2000 Release 2.22 Notes for New Users of PCCTS Version 1.33MR22
43
(C Mode) LA/LATEXT and NLA/NLATEXT
#183. Do not use LA(
i
) or LATEXT(
i
) in the action routines of #token
To refer to the token code (in a #token action) of the token just recognized use NLA. NLA is an lvalue (can appear
on the left hand side of an assignment statement). To refer to the text just recognized use zzlextext (the entire text)
or NLATEXT. One can also use zzbegexpr/zzendexpr which refer to the last regular expression matched. The char
array pointed to by zzlextext may be larger than the string pointed to by zzbegexpr and zzendexpr because it
includes substrings accumulated through the use of zzmore().
#184. Care must be taken in using LA(
i
) and LATEXT(
i
) in interactive mode (
ANTLR
switch ­gk)
In interactive mode
ANTLR
doesn't guarantee that it will fetch lookahead tokens until absolutely necessary. It is
somewhat safer to refer to lookahead information in semantic predicates, but care is still required.
In this table the entries "prev" and "next" means that the item refers to the token which precedes (or follows) the
action which generated the output. For semantic predicate entries think of the following rule:
rule : <<semantic-predicate>>? Next NextNext ;
For rule-action entries think of the following rule:
rule : Prev <<action>> Next NextNext;
-----------------------------------------------------------------------
k=1 k=1 k=3 k=3 k=3
standard infinite standard interactive infinite
-----------------------------------------------------------------------
for a semantic predicate
------------------------
LA(0) Next Next -- -- --
LA(1) Next Next Next Next Next
zzlextext Next Next Next -- Next
ZZINF_LA(0) Next Next
ZZINF_LA(1) NextNext NextNext
-----------------
for a rule action
-----------------
LA(0) Prev Prev -- Prev --
LA(1) Prev Prev Prev Next Prev
zzlextext Prev Prev Prev -- Prev
ZZINF_LA(0) Prev Prev
ZZINF_LA(1) Next Next
-----------------------------------------------------------------------
(C Mode) Execution-Time Routines
#185. Calls to zzskip() and zzmore() should appear only in #token actions (or in subroutines they call)
#186. Use
ANTLR
s or
ANTLR
f in line-oriented languages to control the prefetching of characters and tokens
Write your own input routine and then use
ANTLR
s (input supplied by string) or
ANTLR
f (input supplied by function)
rather than plain
ANTLR
which is used in most of the examples.
#187. Saving and restoring parser state in order to parse other objects (input files)
Suppose one wants to parse files that "include" other files. The code in
ANTLR
(antlr.g) for handling #tokdefs
statements demonstrates how this may be done:
grammar: ...
| "#tokdefs" QuotedTerm
<<{
zzantlr_state st; /* defined in antlr.h */
struct zzdlg_state dst; /* defined in dlgdef.h */
FILE *f;
UserTokenDefsFile = mystrdup(LATEXT(1));
zzsave_antlr_state(&st);
zzsave_dlg_state(&dst);