home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / UPDT106.TXT < prev    next >
Text File  |  1993-09-07  |  17KB  |  560 lines

  1.  
  2.  
  3.  
  4.  
  5.               The Purdue Compiler Construction Tool Set
  6.  
  7.                          Version 1.06 Update
  8.  
  9.                            December 1, 1992
  10.  
  11.  
  12. 1.  Enhancements
  13.  
  14.      This section describes the update of PCCTS Version 1.00  to  Ver-
  15. sion  1.06.  The list of changes here is not complete since many "lit-
  16. tle" fixes were made.  Here, we concentrate on the enhancements to the
  17. system;  file BUGS100 contains a list of the bug fixes incorporated in
  18. the 1.06 release.  In addition, note that  the  manual  has  not  been
  19. updated  and will not be until the next major release (or until we get
  20. around to it).
  21.  
  22. 1.1.  1.06 Is Written in 1.00
  23.  
  24.      The grammar for the PCCTS meta-language (file  format)  has  been
  25. implemented in Version 1.00, making heavy use of #lexclass directives.
  26. File lexhelp.c has been eliminated due to the superiority of  1.00  to
  27. 1.00B.
  28.  
  29. 1.2.  ANTLR Compiles Under ANSI C
  30.  
  31.      Because of the rewrite of the grammar and some rewrites of  ANTLR
  32. code,  ANTLR now compiles with ANSI compilers without a wimper (except
  33. for two "unknown escape sequence" warnings).  Your mileage may vary.
  34.  
  35. 1.3.  Grammar Files Distributed
  36.  
  37.      antlr.g and dlg_p.g are now included in the  source  distribution
  38. for  1.06.  Note that the 1.00 PCCTS (both ANTLR and DLG) are required
  39. to process these grammar files.  DO NOT DESTROY YOUR OLD COPY OF  1.00
  40. PCCTS (at least save the executables).
  41.  
  42. 1.4.  Script Generates Makefiles for PCCTS Projects
  43.  
  44.      A C program called genmk.c is available in the support  directory
  45. of the PCCTS release which has the following usage:
  46.  
  47. genmk project f1.g f2.g ... fn.g
  48.  
  49. It generates a makefile that creates an executable,  project,  from  a
  50. set of grammar files.  For example, genmk t t.g generates:
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.                                                                 Page 1
  63.  
  64.                                                                  PCCTS
  65.  
  66.  
  67.  
  68. #
  69. # PCCTS makefile for: t.g
  70. #
  71. DLG_FILE = parser.dlg
  72. ERR_FILE = err.c
  73. HDR_FILE = stdpccts.h
  74. TOK_FILE = tokens.h
  75. K = 1
  76. ANTLR_H = .
  77. BIN = .
  78. ANTLR = $(BIN)/antlr
  79. DLG = $(BIN)/dlg
  80. CFLAGS = -I. -I$(ANTLR_H)
  81. AFLAGS = -fe err.c -fh stdpccts.h -fl parser.dlg -ft tokens.h -k $(K)
  82. -gk
  83. DFLAGS = -C2 -i
  84. GRM = t.g
  85. SRC = scan.c t.c err.c
  86. OBJ = scan.o t.o err.o
  87.  
  88.  
  89.  
  90. t: $(OBJ) $(SRC)
  91.         cc -o t $(CFLAGS) $(OBJ)
  92.  
  93. t.c parser.dlg : t.g
  94.         $(ANTLR) $(AFLAGS) t.g
  95.  
  96. scan.c : parser.dlg
  97.         $(DLG) $(DFLAGS) parser.dlg scan.c
  98.  
  99.  
  100. This program is handy when beginning a new PCCTS project or when first
  101. learning about PCCTS.
  102.  
  103. 1.5.  DLG Supports Case Insensitive Scanners
  104.  
  105.      DLG has two new options which provide control over the case  sen-
  106. sitivity  of  the generated scanner.  Specifically, case insensitivity
  107. implies that when a character is referenced in a  regular  expression,
  108. DLG  behaves  as  if the user had typed both upper and lower case ver-
  109. sions of that character; i.e. (a|A) where a is  some  character.   The
  110. new options are:
  111.  
  112. -ci  Make lexical analyzer case insensitive
  113.  
  114. -cs  Make lexical analyzer case sensitive (default).
  115.  
  116. 1.6.  Delayed Lookahead Fetches in Generated Parser
  117.  
  118.      Currently, PCCTS generates parsers which always have k tokens  of
  119. lookahead.   This is done by following the strategy that another token
  120. is fetched (zzCONSUME) when one is matched (zzmatch).  This can  be  a
  121.  
  122.  
  123.  
  124.                                                                 Page 2
  125.  
  126.                                                                  PCCTS
  127.  
  128.  
  129. problem for actions that need to occur after a token has been matched,
  130. but before the next token of lookahead is fetched.  This  is  somewhat
  131. overcome  in  PCCTS  1.00  by delaying the fetch if an action is found
  132. immediately after a token reference.  With the new  delayed  lookahead
  133. scheme,  the  next  token  is  not  consumed  until  the next match is
  134. required.  This means that any  action  before  the  next  match  (not
  135. necessarily  adjacent to the previous match) will be executed before a
  136. lookahead fetch occurs.  Turn on ANTLR option -gk and DLG option -i to
  137. enable  this  feature.   This  feature  appears to work with AST's and
  138. attributes with the constraints mentioned below in the  incompatibili-
  139. ties  section  (e.g.  use of LA(i) and LATEXT(i) has been restricted).
  140. It has been tested with the C (k>1 and Pascal (k==1) examples provided
  141. in the release and with several other large grammars.
  142.  
  143.      This feature is primarily useful for  developers  of  interactive
  144. tools.   Previously, it was really hard to get PCCTS to generate truly
  145. interactive tools.  It appeared as if the parser was always waiting on
  146. a  token  fetch  rather than executing an appropriate action.  E.g. in
  147. PCCTS 1.00,
  148.  
  149.  
  150. a : ( "A" "B" "C" "\n" ) <<printf("got it\n");>>
  151.   ;
  152.  
  153.  
  154. would not print got it until one token  after  the  newline  had  been
  155. typed.   PCCTS  1.06  will  generate  parsers  that  print the message
  156. immediately upon newline and will exit  without  waiting  for  another
  157. token as there are no token references following the action.
  158.  
  159.      Another way in which delayed lookahead is useful lies in transla-
  160. tors which add symbols to the symbol table which must be examined by a
  161. lexical action.  If a lookahead fetch occurs  too  fast,  the  lexical
  162. action may miss the introduction of a symbol into the symbol table.
  163.  
  164. 1.7.  Tutorials Available
  165.  
  166.      With release 1.06, we  are  distributing  both  a  beginning  and
  167. advanced  tutorial.  They have not been thoroughly "debugged" much are
  168. much better than nothing:
  169.  
  170. Beginning
  171.      This tutorial introduces the  basic  functionality  of  PCCTS  by
  172.      example.   The  user  need not be familiar with parsing theory or
  173.      other compiler tools, but any familiarity  reduces  the  learning
  174.      curve substantially.
  175.  
  176. Advanced
  177.      Constructing a translator can be viewed as an  iterative  refine-
  178.      ment  process  moving  from language recognition to intermediate-
  179.      form  transformation.   This  tutorial  presents   one   possible
  180.      sequence of refinements.  It uses as many features of PCCTS as is
  181.      reasonable without regards to optimality.  It develops a compiler
  182.      for  a  simple  string  manipulation  language  called  sc.   The
  183.  
  184.  
  185.  
  186.                                                                 Page 3
  187.  
  188.                                                                  PCCTS
  189.  
  190.  
  191.      resulting compiler generates code for a simple stack machine.
  192.  
  193. 1.8.  Error Messages for k>1
  194.  
  195.      Previous versions of PCCTS did not handle error message correctly
  196. for  k>1.  For example, with two tokens of lookahead and the following
  197. grammar:
  198.  
  199.  
  200. a : "A" "B" "D"
  201.   | "A" "C" "E"
  202.   ;
  203.  
  204.  
  205. an incorrect input of A D D would yield:
  206.  
  207.  
  208. line 1: syntax error at "A" missing A
  209.  
  210.  
  211. which is wrong (and incredibly confusing).  The  new  error  mechanism
  212. generates the following error message upon the same incorrect input:
  213.  
  214.  
  215. line 1: syntax error at "A D"; "D" not in { B C }
  216.  
  217.  
  218. which is infinitely superior.   Unfortunately,  situations  may  arise
  219. when  even  this  method will give an invalid message.  This may occur
  220. when alternatives have lookahead sequences which are  permutations  of
  221. the same tokens.
  222.  
  223.      The definition of the standard error reporting function,  zzsyn()
  224. has been modified.  The parameter list is now:
  225.  
  226.  
  227. void
  228. zzsyn(char *text,
  229.       int tok,
  230.       char *egroup,
  231.       unsigned *eset,
  232.       int etok,
  233.       int k,
  234.       char *bad_text);
  235.  
  236.  
  237. Users can ignore this as it is transparent to them; unless, of course,
  238. the standard error reporting must be modified.  In addition, zzFAIL is
  239. now a function rather than a macro.
  240.  
  241. 1.9.  Trace Facility has Exit Macro
  242.  
  243.      Previously, only an entry trace macro  was  inserted  in  parsers
  244. when  the  -gd  ANTLR option was used.  An exit macro has been defined
  245.  
  246.  
  247.  
  248.                                                                 Page 4
  249.  
  250.                                                                  PCCTS
  251.  
  252.  
  253. which resulted in zzTRACE becoming zzTRACEIN.  Also, a  default  trace
  254. macro  prints  out  "enter rule rule"  if  no default trace macros are
  255. defined.  To define your own, the macro definitions must appear in the
  256. #header action.  As before, the sole argument to the trace routines is
  257. a string representing the rule which has been entered or is  about  to
  258. be exited.
  259.  
  260. 1.10.  Resource Limitation
  261.  
  262.      Occasionally, ANTLR is unable to analyze a grammar  submitted  by
  263. the  user.   This  rare  situation  can only occur when the grammar is
  264. large and the amount of lookahead is greater than  one.   A  nonlinear
  265. analysis  algorithm  is  used  by  PCCTS to handle the general case of
  266. LL(k) parsing.  The average complexity of analysis, however,  is  near
  267. linear  due to some fancy footwork in the implementation which reduces
  268. the number of calls to the full LL(k) algorithm.
  269.  
  270.      To avoid the situation where ANTLR takes 23 hours of CPU time and
  271. then  runs  out of virtual memory, use the -rl n resource limit option
  272. where n is the maximum number of tree nodes to be used by the analysis
  273. algorithm.   An  error  message  will  be  displayed, if this limit is
  274. reached, which indicates which grammar construct  was  being  analyzed
  275. when  ANTLR hit a non-linearity.  Use this option if ANTLR seems to go
  276. off to lunch and your disk start swapping; try n=10000 to start.  Once
  277. the  offending  construct has been identified, try to remove the ambi-
  278. guity that ANTLR was trying to overcome with large lookahead analysis.
  279. Future  versions  of PCCTS may incorporate a known algorithm that does
  280. not exhibit this exponential behavior.
  281.  
  282. 1.11.  Rule Prefix Option
  283.  
  284.      An ANTLR option  has  been  added  that  prefixes  all  functions
  285. corresponding  to  rules  with  a prefix.  This can be used to provide
  286. symbol hiding in your project to isolate the parser.  It can  also  be
  287. used  to allow rule names that correspond to C keywords such as if and
  288. typedef.
  289.  
  290. 1.12.  Standard PCCTS Header
  291.  
  292.      Two new ANTLR options have been added that control  the  creation
  293. of  a  standard  PCCTS header file - stdpccts.h.  Option -gh instructs
  294. ANTLR to create a file, stdpccts.h unless -fh is used, which  contains
  295. all  header  information needed by non-PCCTS generated files that want
  296. to access PCCTS symbols.  For example,  it  indicates  the  k  of  the
  297. parser,  whether  trees are being constructed, whether lookahead is to
  298. be delayed, and indicates what  the  user  specified  in  the  #header
  299. action in the grammar file.  Previously, the user had to manually con-
  300. struct this information from the grammar file in order  to  place  the
  301. information  in  a non-PCCTS C file.  The -fh option is merely used to
  302. rename stdpccts.h.
  303.  
  304. 1.13.  Doubly-Linked AST's
  305.  
  306.      A new function is available in ast.c which will  doubly-link  any
  307.  
  308.  
  309.  
  310.                                                                 Page 5
  311.  
  312.                                                                  PCCTS
  313.  
  314.  
  315. tree  that  is  passed  in.   To use this option, the user must define
  316. zzAST_DOUBLE in the #header directive or on the command-line of the  C
  317. compile.   This  defines  left  and up fields automatically in the AST
  318. node typedef.   ANTLR  generated  parsers,  normally,  only  construct
  319. singly-linked  trees.  The fields can be filled in via code similar to
  320. the following:
  321.  
  322.  
  323. #header <<
  324. #define AST_FIELDS      user-fields;
  325. >>
  326.  
  327. <<
  328. main()
  329. {
  330.         AST *root = NULL;
  331.  
  332.         ANTLR(start(&root), stdin);
  333.         zzdouble_link(root, NULL, NULL);
  334. }
  335.  
  336.  
  337. where the function is defined as:
  338.  
  339.  
  340. zzdouble_link(AST *t, AST *left, AST *up);
  341.  
  342.  
  343. 1.14.  C++ Compatible Parsers
  344.  
  345.      PCCTS parsers may now be compiled with C++  compilers;  i.e.  the
  346. output is more ANSI C-like than before.  It has been successfully com-
  347. piled with GCC 2.2, but not with GCC 1.37.  We do not  guarantee  any-
  348. thing.   To  be safe, use the -ga option so that PCCTS generates ANSI-
  349. style prototypes for functions generated  from  rules.   As  a  simple
  350. example, consider:
  351.  
  352.  
  353. #header <<
  354. #include "charbuf.h"
  355. >>
  356.  
  357. #token "[       0" <<zzskip();>>
  358.  
  359. <<
  360. main()
  361. {
  362.     ANTLR(a(), stdin);
  363. }
  364. >>
  365.  
  366. a   :   "A" "B"
  367.     ;
  368.  
  369.  
  370.  
  371.  
  372.                                                                 Page 6
  373.  
  374.                                                                  PCCTS
  375.  
  376.  
  377. which does not do much, but does compile with G++ 2.2  except  that  a
  378. few  warnings  are generated concerning exit() and strncpy() not being
  379. declared before use.  This is trivial to fix, of course - include  the
  380. C++ string include file and define exit().
  381.  
  382. 1.15.  Predicated Parsing - EXTREMELY ALPHA
  383.  
  384.      Normally, parsing is  a  function  of  syntax  alone.   Semantics
  385. (meaning/value  of  the input) are not taken into account when parsing
  386. decisions are made.  For example, the following grammar is LL(k) ambi-
  387. guous:
  388.  
  389.  
  390. a   :   ( expr | decl )+
  391.     ;
  392.  
  393. /* recognize 'a+b+c+...' */
  394. expr:   WORD ("+" WORD)*
  395.     ;
  396.  
  397. /* recognize 'int i' or 'user_type j' etc... */
  398. decl:   (   WORD
  399.         |   "int"
  400.         )
  401.         WORD
  402.     ;
  403.  
  404.  
  405. because, upon WORD, rule a does not know whether an expr or a decl  is
  406. coming.   However, adding predicates (actions suffixed with a '?') can
  407. disambiguate the decision.  E.g.
  408.  
  409.  
  410. a   :   ( expr | decl )+
  411.     ;
  412.  
  413. /* recognize 'a+b+c+...' */
  414. expr:   <<isVAR(LATEXT(1))>>? WORD ("+" WORD)*
  415.     ;
  416.  
  417. /* recognize 'int i' or 'user_type j' etc... */
  418. decl:   (   <<isTYPE(LATEXT(1))>>? WORD
  419.         |   "int"
  420.         )
  421.         WORD
  422.     ;
  423.  
  424.  
  425. where isVAR and isTYPE  are  user-defined  macros  or  functions  that
  426. return  true or false after looking up the symbol in the symbol table.
  427. ANTLR attempts to find predicates when a syntactic  ambiguity  occurs.
  428. Otherwise,  the  predicates are simply tested to ensure semantic vali-
  429. dity.  ANTLR still prints out the ambiguity message, for  the  moment,
  430. but generates code in rule a that looks (kind of) like this:
  431.  
  432.  
  433.  
  434.                                                                 Page 7
  435.  
  436.                                                                  PCCTS
  437.  
  438.  
  439.  
  440. a()
  441. {
  442.         ...
  443.     while ( ... ) {
  444.         if ( (isVAR(LATEXT(1)) && LA(1)==WORD) && LA(1)==WORD ) {
  445.             expr();
  446.         }
  447.         else if ( (isTYPE(LATEXT(1)) && LA(1)==WORD) &&
  448.                   (LA(1)==WORD || LA(1)=="int") ) {
  449.             decl();
  450.         }
  451.     }
  452.         ...
  453. }
  454.  
  455.  
  456. ANTLR has "hoisted" the predicates from the other rules for use in the
  457. prediction  decision  for rule a.  Currently, at most one predicate is
  458. hoisted.  In addition, there may be nothing between a decision  and  a
  459. predicate  for  it to be hoisted; i.e. no token or rule references and
  460. certainly no actions.  Predicates should be side-effect free, as well.
  461. The  context under which predicates are currently evaluated may not be
  462. computed correctly.  IT DOES NOT WORK WITH LARGE GRAMMARS and has only
  463. been tested on small test cases.
  464.  
  465.      Why are we even mentioning them  if  they  do  not  really  work?
  466. Because  we  want users to play with them to find better ways of doing
  467. things and to find new uses for them.  We really want user feedback on
  468. these critters before a full implementation is developed.  Please send
  469. mail to parrt@ecn.purdue.edu, hankd@ecn.purdue.edu or  use  the  email
  470. server mechanism at pccts@ecn.purdue.edu.
  471.  
  472. 2.  Acknowledgements
  473.  
  474.      We acknowledge Dan Lawrence of MDBS for the new  error  reporting
  475. facility  concering greater than one token of lookahead; Dana Hoggatt,
  476. also of MDBS, suggested the rule prefix idea  (-gp  option)  and  beta
  477. tested 1.06.  ### Beta testers: Peter Dahl of U of MN, ...
  478.  
  479. 3.  Machine Compatibility
  480.  
  481.      PCCTS Version 1.06 has been successfully tested on the  following
  482. platforms:
  483.  
  484. Sun 3/60
  485.  
  486. Sun SPARC I, II
  487.  
  488. DEC 5000
  489.  
  490. Linux on 386PC
  491.  
  492. IBM RS6000
  493.  
  494.  
  495.  
  496.                                                                 Page 8
  497.  
  498.                                                                  PCCTS
  499.  
  500.  
  501. 4.  Incompatibilities
  502.  
  503.      Calls to zzTRACE are no longer generated by the -gd option.  Now,
  504. zzTRACEIN,  zzTRACEOUT  are  called  at the beginning and end of func-
  505. tions, respectively.
  506.  
  507.      Attribute variables in strings in actions  need  to  be  escaped,
  508. \$i, whereas, before, $i was ok.
  509.  
  510.      The user should no longer set the next token of lookahead or  the
  511. text  of  the  next  token  in  the  lexical  analyzer using LA(1) and
  512. LATEXT(1).  This is incompatible with the -gk option; hence,  NLA  and
  513. NLATEXT should be used instead where the N means "next".
  514.  
  515. 5.  Future Work:
  516.  
  517.      Predicates are currently under development with an in-house  ver-
  518. sion  of PCCTS.  We expect a release in the Spring that allows parsing
  519. to be a function of syntax as well as semantics.
  520.  
  521.      Attribute names  are  expected  to  be  enhanced.   For  example,
  522. instead of $i, $token_name could be used:
  523.  
  524.  
  525. a : WORD TYPE <<printf("%s %s\n", $WORD, $TYPE);>>
  526.   ;
  527.  
  528.  
  529.      We anticipate a version that supports object-oriented programming
  530. and  generates C++ instead of ANSI C.  For the moment, PCCTS is compa-
  531. tible with C++, but future versions will support C++.
  532.  
  533.      Future versions, both C and C++, will be able to refer  to  PCCTS
  534. symbols  by  pccts.symbol  instead  of  zzsymbol.   E.g. zzskip() will
  535. become pccts.skip().
  536.  
  537.      DLG will soon use lookahead of its own to allow  the  recognition
  538. of  more  complicated expressions; specifically, those which have left
  539. substrings in common with other regular expressions.
  540.  
  541.      We expect future versions of PCCTS to dump grammar  analysis  and
  542. parser construction statistics such as how many rules required 1 token
  543. of lookahead, how many ambiguities etc...
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.                                                                 Page 9
  559.  
  560.