Mercurial > ~dholland > hg > ag > index.cgi
comparison doc/misc/html/examples/hw.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 <HTML> | |
3 <HEAD> | |
4 <TITLE>Hello World!</TITLE> | |
5 </HEAD> | |
6 | |
7 | |
8 <BODY BGCOLOR="#ffffff" BACKGROUND="tilbl6h.gif" | |
9 TEXT="#000000" LINK="#0033CC" | |
10 VLINK="#CC0033" ALINK="#CC0099"> | |
11 | |
12 <P> | |
13 <IMG ALIGN="right" SRC="../images/agrsl6c.gif" ALT="AnaGram" | |
14 WIDTH=124 HEIGHT=30 > | |
15 <BR CLEAR="all"> | |
16 Back to <A HREF="../index.html">Index</A> | |
17 <P> | |
18 <IMG ALIGN="bottom" SRC="../images/rbline6j.gif" ALT="----------------------" | |
19 WIDTH=1010 HEIGHT=2 > | |
20 <P> | |
21 | |
22 | |
23 <H1>Hello, World!</H1> | |
24 | |
25 <IMG ALIGN="bottom" SRC="../images/rbline6j.gif" ALT="----------------------" | |
26 WIDTH=1010 HEIGHT=2 > | |
27 | |
28 <P> | |
29 <H2>Introduction</H2> | |
30 <P> | |
31 Hello, world! has become the traditional first program to | |
32 try in a new programming language. This directory provides | |
33 two instances of "Hello, world!" The first, <tt>hw1</tt>, uses C. The | |
34 second, <tt>hw2</tt>, uses C++. | |
35 <P> | |
36 | |
37 <H2>Hello, world! using C - HW1.SYN</H2> | |
38 <tt>hw1</tt> accepts a single newline character as input and then | |
39 prints the "Hello, world!" message on stdout. | |
40 <P> | |
41 Every grammar must have a single token, the grammar token, | |
42 to which the entire grammar finally condenses. The default | |
43 name for this token is "grammar", which is used here. | |
44 <P> | |
45 AnaGram productions use an arrow ( <CODE>-></CODE> ) to separate the | |
46 token or tokens being described, on the left, from their | |
47 descriptions, on the right. <tt>hw1</tt> consists of a single | |
48 production, defining the token "grammar" to consist of a | |
49 single newline character. | |
50 <P> | |
51 The right hand side of a production is called a "grammar | |
52 rule" and in the general case consists of a series of rule | |
53 elements separated by commas. Following the rule on the same | |
54 line, introduced by an equal sign, there may be a "reduction | |
55 procedure", C code to be executed when the rule is | |
56 identified in the input stream. Here there is one rule | |
57 element and the reduction procedure consists of a single C | |
58 expression, followed by a semicolon. | |
59 <P> | |
60 Note that AnaGram permits comments in accordance with the | |
61 rules for C comments. AnaGram comments, like C comments, do | |
62 not ordinarily nest. However, you may set the "nest | |
63 comments" configuration switch to allow nesting. | |
64 <P> | |
65 When AnaGram builds a parser, it will provide defaults for | |
66 all important aspects of the parser which you do not | |
67 specify. In <tt>hw1</tt>, there is no specification of input, so the | |
68 parser will be set up to read characters one at a time from | |
69 stdin. | |
70 <P> | |
71 If a syntax file contains no embedded C, AnaGram will | |
72 automatically provide a main program to call the parser. | |
73 Thus this simple grammar suffices to describe a complete | |
74 program. | |
75 <P> | |
76 | |
77 <H2>Testing HW1</H2> | |
78 To test <tt>hw1</tt>, run AnaGram and build a parser using | |
79 <tt>hw1.syn</tt>. | |
80 The name of the output source file will be <tt>hw1.c</tt>. | |
81 Compile and link the | |
82 parser with your C compiler. Then run it from the command line | |
83 and press Enter. | |
84 The "Hello, world!" message should appear on your screen. | |
85 <P> | |
86 | |
87 <H2>Hello, world! using C++ - HW2.SYN</H2> | |
88 <tt>hw2</tt> is a step up in complexity. <tt>hw2</tt> | |
89 creates a C++ program | |
90 instead of C, and uses stream I/O to write the famous | |
91 message. | |
92 <P> | |
93 The first complexity is the requirement to tell AnaGram to | |
94 output a file with a C++ name rather than a C name. | |
95 <!-- | |
96 Of | |
97 course, this is a frill, since you could probably have | |
98 compiled it anyway. | |
99 --> | |
100 The <b>parser file name</b> configuration | |
101 parameter specifies that the name of the parser file should | |
102 be the same as the name of the syntax file, but with the | |
103 extension <tt>.cpp</tt>. | |
104 <P> | |
105 The second complexity is the need to provide an #include | |
106 statement for <tt>iostream.h</tt> to get a definition of | |
107 <tt>cout</tt>. <tt>hw1</tt> | |
108 did not need to include <tt>stdio.h</tt>, since the parser | |
109 always has | |
110 an <CODE>#include</CODE> statement for <tt>stdio.h</tt> to | |
111 support the default | |
112 input procedures. To provide an <CODE>#include</CODE> | |
113 statement, we need a | |
114 block of embedded C. | |
115 <P> | |
116 Since we have a block of embedded C, AnaGram will not create | |
117 a main program automatically. So we add a main program which | |
118 does nothing but call the parser. The parser name is set (by | |
119 default) to <tt>hw2</tt>. | |
120 <P> | |
121 Note also that in addition to C style comments, AnaGram also | |
122 supports C++ style comments. | |
123 <P> | |
124 | |
125 <H2>Testing HW2</H2> | |
126 To test <tt>hw2</tt>, build a parser using <tt>hw2.syn</tt>, | |
127 just as you did | |
128 for <tt>hw1</tt>. This time the name of the parser file will be | |
129 <tt>hw2.cpp</tt>. Compile and link the parser with your C++ compiler. | |
130 Then run it from the command line and press Enter. The | |
131 "Hello, world!" message | |
132 should appear on your screen. | |
133 </P> | |
134 | |
135 <BR> | |
136 <IMG ALIGN="bottom" SRC="../images/rbline6j.gif" ALT="----------------------" | |
137 WIDTH=1010 HEIGHT=2 > | |
138 <P> | |
139 <IMG ALIGN="right" SRC="../images/pslrb6d.gif" ALT="Parsifal Software" | |
140 WIDTH=181 HEIGHT=25> | |
141 <BR CLEAR="right"> | |
142 <P> | |
143 Back to <A HREF="../index.html">Index</A> | |
144 <P> | |
145 <ADDRESS><FONT SIZE="-1"> | |
146 AnaGram parser generator - examples<BR> | |
147 Hello World!<BR> | |
148 Copyright © 1993-1999, Parsifal Software. <BR> | |
149 All Rights Reserved.<BR> | |
150 </FONT></ADDRESS> | |
151 | |
152 </BODY> | |
153 </HTML> | |
154 | |
155 |