Mercurial > ~dholland > hg > ag > index.cgi
comparison doc/misc/html/examples/mpp/parsers.html @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> | |
2 | |
3 <HTML> | |
4 <HEAD> | |
5 <TITLE>C Parsers - Macro preprocessor and C Parser </TITLE> | |
6 </HEAD> | |
7 | |
8 | |
9 <BODY BGCOLOR="#ffffff" BACKGROUND="tilbl6h.gif" | |
10 TEXT="#000000" LINK="#0033CC" | |
11 VLINK="#CC0033" ALINK="#CC0099"> | |
12 | |
13 <P> | |
14 <IMG ALIGN="right" SRC="../../images/agrsl6c.gif" ALT="AnaGram" | |
15 WIDTH=124 HEIGHT=30 > | |
16 <BR CLEAR="all"> | |
17 Back to : | |
18 <A HREF="../../index.html">Index</A> | | |
19 <A HREF="index.html">Macro preprocessor overview</A> | |
20 <P> | |
21 | |
22 | |
23 <IMG ALIGN="bottom" SRC="../../images/rbline6j.gif" ALT="----------------------" | |
24 WIDTH=1010 HEIGHT=2 > | |
25 <P> | |
26 | |
27 <H1>C Parsers - Macro preprocessor and C Parser </H1> | |
28 | |
29 <IMG ALIGN="bottom" SRC="../../images/rbline6j.gif" ALT="----------------------" | |
30 WIDTH=1010 HEIGHT=2 > | |
31 <P> | |
32 <BR> | |
33 | |
34 Two C grammars are provided with this example: <tt>jrc.syn</tt> is a | |
35 translation of a <tt>yacc</tt> grammar for C created by James A. | |
36 Roskind. <tt>krc.syn</tt> is a grammar adapted from Kernighan and | |
37 Ritchie, Section A13. | |
38 <P> | |
39 Both grammars use semantically determined productions to | |
40 distinguish between typedef names and identifiers, however, | |
41 neither grammar has been adapted to take advantage of this | |
42 capability to resolve many of the problems associated with | |
43 typedef names. | |
44 <P> | |
45 In order to parse simple examples successfully, it was | |
46 necessary to create a rudimentary capability to recognize | |
47 typedef names. Such capabilities were incorporated into each | |
48 grammar. Note that the scope handling is inadequate for | |
49 production use. | |
50 <P> | |
51 In <tt>jrc.syn</tt>, the typedef token causes a flag to be set. When | |
52 the identifier in a declaration is encountered, the flag is | |
53 interrogated. The complication is the possible interposition | |
54 of field names within a struct or union which could be | |
55 inadvertently marked as typedef names. To avoid this, the | |
56 flag is stored on encountering a struct or union and | |
57 restored when the danger is past. | |
58 <P> | |
59 In <tt>krc.syn</tt>, the treatment of typedefs is marginally less | |
60 inelegant. Storage class attributes are accumulated in a bit | |
61 mask. As identifiers are encountered in the declaration | |
62 statement they are pushed onto a multilevel stack, <tt>id_stack</tt>. | |
63 When struct or union declarations are encountered, the level | |
64 of the id_stack is incremented. When the danger is past, the | |
65 level is decremented so that identifiers stacked in the | |
66 course of parsing the struct or union are discarded. | |
67 Eventually, when the statement is complete, all the | |
68 identifiers that have been found are popped from <tt>id_stack</tt> | |
69 and given the appropriate storage class attribute. | |
70 <P> | |
71 Both grammars are set up so that the name of the parser is | |
72 <tt>cc()</tt>. | |
73 Each | |
74 syntax file contains member function definitions for the | |
75 class <tt>c_parser</tt>. They are identical except for the | |
76 initialization of the typedef logic. The definition of the | |
77 class <tt>c_parser</tt> is found in <tt>MPP.H</tt>. | |
78 <P> | |
79 <B>Note:</B> Until AnaGram 2.40, the parsers were set up | |
80 so both would emit the files <tt>cc.cpp</tt> and | |
81 <tt>cc.h</tt>. This caused problems for AnaGram 2.40's more | |
82 automated build and test system. As of this writing, | |
83 <tt>krc.syn</tt> is used by default; to use <tt>jrc.syn</tt> | |
84 you must change the <tt>#include</tt> near the top of | |
85 <tt>mpp.h</tt> to include <tt>jrc.h</tt>. With luck, this | |
86 annoyance will be cleared up in a future release. | |
87 <P> | |
88 It should be noted that the KRC grammar has at least one | |
89 bug. It will not recognize function declarations of the | |
90 form: | |
91 <PRE> | |
92 foo(int); | |
93 </PRE> | |
94 It will, however, recognize | |
95 <PRE> | |
96 int foo(int); | |
97 </PRE> | |
98 This problem appears to be inherent in the grammar published | |
99 in K & R. | |
100 <P> | |
101 <BR> | |
102 | |
103 | |
104 <IMG ALIGN="bottom" SRC="../../images/rbline6j.gif" ALT="----------------------" | |
105 WIDTH=1010 HEIGHT=2 > | |
106 <P> | |
107 <IMG ALIGN="right" SRC="../../images/pslrb6d.gif" ALT="Parsifal Software" | |
108 WIDTH=181 HEIGHT=25> | |
109 <BR CLEAR="right"> | |
110 | |
111 <P> | |
112 Back to : | |
113 <A HREF="../../index.html">Index</A> | | |
114 <A HREF="index.html">Macro preprocessor overview</A> | |
115 | |
116 <P> | |
117 <ADDRESS><FONT SIZE="-1"> | |
118 AnaGram parser generator - examples<BR> | |
119 C Parsers - Macro preprocessor and C Parser <BR> | |
120 Copyright © 1993-1999, Parsifal Software. <BR> | |
121 All Rights Reserved.<BR> | |
122 </FONT></ADDRESS> | |
123 | |
124 </BODY> | |
125 </HTML> | |
126 |