home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / compiler / 1469 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  4.9 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!usc!rutgers!faatcrl!iecc!compilers-sender
  2. From: neitzel@ips.cs.tu-bs.de (Martin Neitzel)
  3. Newsgroups: comp.compilers
  4. Subject: Re: error productions in YACC/Bison
  5. Keywords: yacc, debug
  6. Message-ID: <92-08-169@comp.compilers>
  7. Date: 27 Aug 92 15:33:31 GMT
  8. References: <92-08-145@comp.compilers>
  9. Sender: compilers-sender@iecc.cambridge.ma.us
  10. Reply-To: neitzel@ips.cs.tu-bs.de (Martin Neitzel)
  11. Organization: Inst. f. Informatik, TU Braunschweig, FRG
  12. Lines: 114
  13. Approved: compilers@iecc.cambridge.ma.us
  14.  
  15. I've found it worth while to expose all my students dealing with yacc to
  16. an illustrated version of byacc.  (Authors: byacc: Bob Corbett,
  17. illustrations: Jim Roskind.)
  18.  
  19. Mr Roskind's small changes to the byacc driver template will replace the
  20. standard "yydebug messages" indicating current state and action of the
  21. parser with a much more comprehensible graphical output.  I've appended a
  22. real life example of an Modula2 parse below.  We were pretty in the dark
  23. about the effects of interspersed error symbols until we made small
  24. experiments with this enhanced yacc.  The students can see the difference
  25. between discarding states and symbols, the reduction of the error symbol,
  26. the exact timing of events in a rule like "foo: bar {A;} error {B;} ';'",
  27. and more.
  28.  
  29. You will find Jim Roskind's enhanced driver skeleton for byacc
  30. packaged with his C++-grammar.
  31.  
  32.                         Martin Neitzel
  33.  
  34. So here is the promised example.  It's a bit lengthy (90 lines), but it
  35. should "give you the picture".  There's a rule "IdentList: error" in the
  36. grammar, see what happens during the faulty input "*+-":
  37.  
  38. input:    MODULE a; VAR foo, bar, *+- : INTEGER; BEGIN END a.
  39.  
  40. parse trace:
  41.  
  42.            .... look ahead at MODULE   `MODULE'
  43. MODULE <-- `MODULE'
  44. |            .... look ahead at IDENTIFIER   `a'
  45. |  IDENTIFIER <-- `a'
  46. |  ident
  47. |  |            .... look ahead at SEMI   `;'
  48. |  |  opt.priority
  49. |  |  |  SEMI <-- `;'
  50. |  |  |  |  ImportList
  51. |  |  |  |  |            .... look ahead at VAR   `VAR'
  52. |  |  |  |  |  DeclarationList
  53. |  |  |  |  |  |  VAR <-- `VAR'
  54. |  |  |  |  |  |  |  VarDeclList
  55. |  |  |  |  |  |  |  |            .... look ahead at IDENTIFIER   `foo'
  56. |  |  |  |  |  |  |  |  IDENTIFIER <-- `foo'
  57. |  |  |  |  |  |  |  |  ident
  58. |  |  |  |  |  |  |  |  IdentList
  59. |  |  |  |  |  |  |  |  |            .... look ahead at COMMA   `,'
  60. |  |  |  |  |  |  |  |  |  COMMA <-- `,'
  61. |  |  |  |  |  |  |  |  |  |            .... look ahead at IDENTIFIER   `bar'
  62. |  |  |  |  |  |  |  |  |  |  IDENTIFIER <-- `bar'
  63. |  |  |  |  |  |  |  |  |  |  ident
  64. |  |  |  |  |  |  |  |  +--+--+
  65. |  |  |  |  |  |  |  |  |
  66. |  |  |  |  |  |  |  |  IdentList
  67. |  |  |  |  |  |  |  |  |            .... look ahead at COMMA   `,'
  68. |  |  |  |  |  |  |  |  |  COMMA <-- `,'
  69. |  |  |  |  |  |  |  |  |  |            .... look ahead at MULT   `*'
  70.  
  71. syntax error before "*".
  72. |  |  |  |  |  |  |  |  +--+  discarding state
  73. |  |  |  |  |  |  |  |  |
  74. |  |  |  |  |  |  |  +--+  discarding state
  75. |  |  |  |  |  |  |  |
  76. |  |  |  |  |  |  |  |  error
  77. |  |  |  |  |  |  |  |  IdentList
  78. |  |  |  |  |  |  |  |  |  discarding token MULT
  79. |  |  |  |  |  |  |  |  |            .... look ahead at PLUS   `+'
  80. |  |  |  |  |  |  |  |  |  discarding token PLUS
  81. |  |  |  |  |  |  |  |  |            .... look ahead at MINUS   `-'
  82. |  |  |  |  |  |  |  |  |  discarding token MINUS
  83. |  |  |  |  |  |  |  |  |            .... look ahead at COLON   `:'
  84. |  |  |  |  |  |  |  |  |  COLON <-- `:'
  85. |  |  |  |  |  |  |  |  |  |            .... look ahead at IDENTIFIER   `INTEGER'
  86. |  |  |  |  |  |  |  |  |  |  IDENTIFIER <-- `INTEGER'
  87. |  |  |  |  |  |  |  |  |  |  ident
  88. |  |  |  |  |  |  |  |  |  |  QualIdent
  89. |  |  |  |  |  |  |  |  |  |  |            .... look ahead at SEMI   `;'
  90. |  |  |  |  |  |  |  |  |  |  QualTypeIdent
  91. |  |  |  |  |  |  |  |  |  |  type
  92. |  |  |  |  |  |  |  |  +--+--+
  93. |  |  |  |  |  |  |  |  |
  94. |  |  |  |  |  |  |  |  VariableDeclaration
  95. |  |  |  |  |  |  |  |  |  SEMI <-- `;'
  96. |  |  |  |  |  |  |  +--+--+
  97. |  |  |  |  |  |  |  |
  98. |  |  |  |  |  |  |  VarDeclList
  99. |  |  |  |  |  |  |  |            .... look ahead at START   `BEGIN'
  100. |  |  |  |  |  |  +--+
  101. |  |  |  |  |  |  |
  102. |  |  |  |  |  |  declaration
  103. |  |  |  |  |  +--+
  104. |  |  |  |  |  |
  105. |  |  |  |  |  DeclarationList
  106. |  |  |  |  |  |  START <-- `BEGIN'
  107. |  |  |  |  |  |  |            .... look ahead at END   `END'
  108. |  |  |  |  |  |  |  statement
  109. |  |  |  |  |  |  |  StatementSequence
  110. |  |  |  |  |  |  |  |  END <-- `END'
  111. |  |  |  |  |  +--+--+--+
  112. |  |  |  |  |  |
  113. |  |  |  |  |  block
  114. |  |  |  |  |  |            .... look ahead at IDENTIFIER   `a'
  115. |  |  |  |  |  |  IDENTIFIER <-- `a'
  116. |  |  |  |  |  |  ident
  117. |  |  |  |  |  |  |            .... look ahead at PERIOD   `.'
  118. |  |  |  |  |  |  |  PERIOD <-- `.'
  119. +--+--+--+--+--+--+--+
  120. |
  121. ProgramModule
  122. CompilationUnit
  123. TopLevelProductions
  124. |            .... look ahead at end-of-file   `'
  125. done.
  126. -- 
  127. Send compilers articles to compilers@iecc.cambridge.ma.us or
  128. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  129.