home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson3.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  8.5 KB  |  249 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic and other WWW browsers.>
  4.  
  5. <TUTOR NAME="Lovelace">
  6. <LESSON NUMBER=3>
  7. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  8. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  9.  
  10. <COMMENT $Id: lesson3.les,v 1.6 1995/05/17 21:25:18 wheeler Exp $ >
  11.  
  12. <COMMENT A lesson is divided into 1 or more "sections".>
  13.  
  14. <SECTION NAME="Ada Lexical Elements">
  15. Now that we've seen a little about the ``big picture'' of Ada structure --
  16. essentially Ada from the ``top down'' -- let's look at Ada from the
  17. other direction -- ``bottom up.''
  18. <P>
  19. Ada compilation units are, at the lowest level, composed of a sequence
  20. of <EM>lexical elements</EM>.
  21. Lexical elements include identifiers (such as the name of a procedure),
  22. reserved words, and various punctuation marks.
  23. Reserved words include ``procedure'', ``package'', ``end'', ``if'', and
  24. so on.
  25. <P>
  26. Ada is a free-form language like Pascal, C, and C++;
  27. line breaks and spaces can go essentially wherever you like between
  28. lexical elements.
  29. If identifiers or keywords are next to each other,
  30. they must be explicitly separated by
  31. one or more spaces and/or end-of-line markers.
  32. <P>
  33. Ada is also case-insensitive;
  34. except for the contents of strings (which are inside quotes),
  35. upper and lower case keywords and identifiers are equivalent.
  36. <P>
  37. Ada as a language permits a great deal of freedom, but some consistency
  38. of capitalization and indentation are helpful for reading later.
  39. The style used in this tutorial is the style suggested in the
  40. <A HREF="http://lglwww.epfl.ch/Ada/Resources/References.html#style">Software
  41. Productivity Consortia's (SPC)
  42. <I>Ada Quality and Style: A Guide to Professional Programmers</I></A>
  43. (which is the recommended style guide by the Ada Joint Program Office).
  44. In this style keywords are in lower case, identifiers
  45. have initial capitals, and there is at most one statement per line.
  46. If an identifier has more than one word in it, each word should have
  47. an initial capital letter and the words should have underscores
  48. (``_'') between them.
  49. <QUESTION Type=Multiple-Choice>
  50. From a compiler's point of view, are procedures 1 and 2 identical or not?
  51. <P>
  52. Procedure 1:
  53. <P>
  54. <PRE>
  55.  with Text_IO;
  56.  procedure Hello is
  57.  begin
  58.   Text_IO.Put("This is a test!");
  59.  end Hello;
  60. </PRE>
  61. <P>
  62. Procedure 2:
  63. <P>
  64. <PRE>
  65.  WITH TEXT_IO; PROCEDURE hello IS           BeGiN
  66.  TEXT_IO.put("This is a test!"); END hello;
  67. </PRE>
  68. <CHOICES>
  69. <CHOICE ANS=1>They are identical to the compiler.
  70. <CHOICE ANS=2>They are different to the compiler.
  71. </CHOICES>
  72. <ANSWER ANS=1>
  73. <RESPONSES>
  74. <WHEN ANS=1>
  75. Right. Outside of a string, capitalization and indentation don't matter.
  76. <WHEN ANS=2>
  77. No, sorry.
  78. Remember, outside of a string, capitalization and indentation don't matter.
  79. </RESPONSES>
  80. <SECTION NAME="Identifiers">
  81. Ada requires names for procedures, packages, and many other constructs.
  82. These names are called <EM>identifiers</EM>.
  83. Sample identifiers include ``Hello'', ``Launch_Torpedo'', and ``X12''.
  84. Identifiers must begin with a letter, though after that
  85. initial letter they may also contain digits and underscores.
  86. As noted in the last section, upper and lower case are considered equivalent.
  87. <P>
  88. Here is the required syntax for an identifier
  89. <A HREF="bnf.htm">in BNF format</A>:
  90. <P>
  91. <EM>
  92. identifier ::= letter { [ "_" ] letter_or_digit }
  93. <BR>
  94. letter_or_digit ::= letter | digit
  95. </EM>
  96. <P>
  97. All characters of an identifier are significant, and Ada compilers must
  98. support lines and identifier lengths of at least 200 (!) characters.
  99. Hopefully you won't use that many, of course, but the idea is to be very flexible.
  100. <P>
  101. One implication of this syntax is that
  102. underscores must not be adjacent to each other.
  103. This was intentional, because on some
  104. printers two adjacent underscores look the same as one underscore.
  105. Underscores also can't begin or end an identifier.
  106. <P>
  107. The Ada language permits the single letters "L" and "O" to be identifiers,
  108. but I recommend against it - a lower case "L" is nearly indistinguishable
  109. from a one, and an upper case "O" is nearly indistinguishable from a zero
  110. on some systems.
  111. <QUESTION Type=Multiple-Choice>
  112. Here are some lists of identifiers:
  113. <OL>
  114. <LI> Hello, 2Run, Really_Quit
  115. <LI> Refresh_Screen, X22
  116. </OL>
  117. Which of the preceding lists have only legal identifiers
  118. (ignoring the commas, which are there to separate the identifiers)?
  119. <CHOICES>
  120. <CHOICE ANS=1>List 1
  121. <CHOICE ANS=2>List 2
  122. </CHOICES>
  123. <ANSWER ANS=2>
  124. <RESPONSES>
  125. <WHEN ANS=1>
  126. No, sorry. Identifiers must start with a letter, and the `2' in
  127. `2Run' isn't considered a letter.
  128. <WHEN ANS=2>
  129. Right!
  130. </RESPONSES>
  131.  
  132. <SECTION NAME="Numeric Literals">
  133. A written-out number is called a ``numeric literal''.
  134. There are two kinds of numeric literals: real literals and integer literals.
  135. A real literal includes a point (``.''), while an integer literal does not.
  136. Sample integer literals include ``2'', ``400'', and ``-7''.
  137. Sample real literals include ``2.'', ``400.0'', and ``-3.14159''.
  138. <P>
  139. Traditional exponent operators (such as 1.0E9) are permitted in
  140. numeric literals.
  141. Exponents are even allowed for integer literals, though for integer
  142. literals the exponent must not be negative.
  143. <P>
  144. To make long numbers easier to read, underscores are permitted inside
  145. a numeric literal. For example, "1_000_000" is legal.
  146. This is similar to the way commas are used in the United States and periods
  147. are used in Europe.
  148. Underscores aren't allowed to be consecutive,
  149. numbers may not end in an underscore, and underscores don't
  150. change the value of a number.
  151. <P>
  152. A useful Ada capability is its ability to write out literals in
  153. other bases from 2 to 16 (C has this capability to a lessor extent as well).
  154. These are called, reasonably enough, <EM>based literals</EM>.
  155. To create a based literal, write out the desired base,
  156. a "#" sign, the number in the requested base, and another "#" sign.
  157. For example, "2#1001_1000#" is a base 2 number equal to 128+16+8 = 152.
  158. <P>
  159. For completeness, here's the <A HREF="bnf.htm">BNF</A> of numeric literals:
  160. <P>
  161. <EM>
  162. numeric_literal ::= decimal_literal | based_literal
  163. <BR>
  164. decimal_literal ::= numeral [ . numeral ] [ exponent ]
  165. <BR>
  166. numeral ::= digit { digit | "_" }
  167. <BR>
  168. exponent ::= "E" [ "+" | "-" ] numeral
  169. <BR>
  170. based_literal ::= base "#" based_numeral "#" [ exponent ]
  171. <BR>
  172. base ::= numeral
  173. <BR>
  174. based_numeral ::= extended_digit { extended_digit | "_" }
  175. <BR>
  176. extended_digit ::= digit | "A" | "B" | "C" | "D" | "E" | "F"
  177. </EM>
  178. <QUESTION Type=Multiple-Choice>
  179. Here are two lists of numbers:
  180. <OL>
  181. <LI> 5.5, 200_000.12, 2#1000_0100#, 8#123#
  182. <LI> 60, 12, 0x50
  183. </OL>
  184. Which of those lists has only legal numeric literals?
  185. <CHOICES>
  186. <CHOICE ANS=1>List number 1.
  187. <CHOICE ANS=2>List number 2.
  188. </CHOICES>
  189. <ANSWER ANS=1>
  190. <RESPONSES>
  191. <WHEN ANS=1>
  192. Right.
  193. <WHEN ANS=2>
  194. No, sorry, 'x' isn't allowed in numeric literals.
  195. In the programming languages C and C++,
  196. hexadecimal "50" is written as 0x50.
  197. The way to write the same literal in Ada is 16#50#.
  198. </RESPONSES>
  199.  
  200. <SECTION NAME="Character and String Literals">
  201. Sometimes a literal of a single character is needed.
  202. A single character is represented using by enclosing it in single
  203. quotes ('). For example, 'a' represents the lower case letter A.
  204. This is true even if it's a single quote character, so <TT>'''</TT>
  205. represents a single quote character.
  206. <P>
  207. Strings are enclosed in double quote characters (").
  208. To include a double quote character in a string, type it twice ("")
  209. inside the larger string.
  210. Thus "Hello" is a string, as is "She said, ""How are you?""".
  211. An empty string is simply written as "".
  212. <P>
  213. We'll find out later how to represent control characters, but
  214. for now we'll note that C-like escape characters do <EM>not</EM> work.
  215. It turns out that they're not as necessary in Ada as they are in C.
  216. <QUESTION Type=Multiple-Choice>
  217. Given the following items:
  218. <OL>
  219. <LI>
  220. "Hello"
  221. <LI>
  222. '''
  223. <LI>
  224. "Please press ""RETURN"""
  225. <LI>
  226. ""
  227. <LI>
  228. "wokka""
  229. </OL>
  230. <P>
  231. Which of the following is true?
  232. <CHOICES>
  233. <CHOICE ANS=1>Items 1, 3, and 4 are string literals, item 2 is a character literal, and item 5 is neither.
  234. <CHOICE ANS=2>Items 1 and 4 are string literals, item 2 is a character literal, and items 3 and 5 are neither.
  235. <CHOICE ANS=3>They are all legal string or character literals.
  236. </CHOICES>
  237. <ANSWER ANS=1>
  238. <RESPONSES>
  239. <WHEN ANS=1>
  240. Good job!
  241. <WHEN ANS=2>
  242. Nope. Inside a string, two double quotes in a row are used to denote
  243. a double quote character.
  244. <WHEN ANS=3>
  245. No, sorry.
  246. In particular, go back and look at item 5 -
  247. you'll notice that it has an uneven number of double quotation marks.
  248. </RESPONSES>
  249.