Oppimateriaalin copyright © 2007 Arto Wikla. Tämän oppimateriaalin käyttö on sallittu vain yksityishenkilöille opiskelutarkoituksissa. Materiaalin käyttö muihin tarkoituksiin, kuten kaupallisilla tai muilla kursseilla, on kielletty.

581362 Ohjelmointikielten periaatteet keväällä 2007: 1. harjoitukset 23.1.

Näihin ensimmäisiinkin harjoituksiin saa tulla jo "opintopiirinä", jos sellaisen onnistuu kavereiden kanssa muodostamaan. Muista muodostetaan opiskelijatyöryhmiä tilaisuuden aikana.

  1. [Scott 1.1] Errors in a computer program can be classified according to when they are detected and, if they are detected at compile time, what part of the compiler detects them. Using your favorite imperative language, give an example of each of the following.
    1. A lexical error, detected by the scanner
    2. A syntax error, detected by the parser
    3. A static semantic error, detected by semantic analysis
    4. A dynamic semantic error, detected by code generated by the compiler
    5. An error that the compiler can neither catch nor easily generate code to catch (this should be a violation of the language definition, not just a program bug)

  2. [Scott 1.3] The gcd program of Example 1.17 might also be written
    program gcd(input, output);
    var i, j : integer;
    begin
       read(i, j);
       while i <> j do
          if i > j then i := i mod j
          else j := j mod i;
       writeln(i)
    end.
    
    Does this program compute the same result? If not, can you fix it? Under what circumstances would you expect one or the other to be faster?

  3. [Scott 1.4] In your local implementation of C, what is the limit on the size of integers? What happens in the event of arithmetic overflow? What are the implications of size limits on the portability of programs from one machine/compiler to another? How do the answers to these questions differ for Java? For Ada? For Pascal? For Scheme? (You may need to find a manual.)

  4. [Scott 1.6] Why is it difficult to tell whether a program is correct? How do you go about finding bugs in your code? What kinds of bugs are revealed by testing? What kinds of bugs are not? (For more formal notions of program correctness, see the bibliographic notes at the end of Chapter 4.)

  5. [Scott 2.1] Write regular expressions to capture [Huom: Tässä tehtävässä saat ja voit halutessasi kirjoittaa säännöllisten lausekkeiden lisäksi myös kieliopillisia produktioita!]
    1. Strings in C. These are delimited by double quotes ("), and may not contain newline characters. They may contain double quote or backslash characters if and only if those characters are "escaped" by a preceding backslash. You may find it helpful to introduce short-hand notation to represent any character that is not a member of a small specified set.
    2. Comments in Pascal. These are delimited by (* and *), as shown in Figure 2.6, or by { and }.
    3. Floating-point constants in Ada. These are the same as in Pascal (see the definition of unsigned number in Example 2.2 [page 41]), except that (1) an underscore is permitted between digits, and (2) an alternative numeric base may be specified by surrounding the nonexponent part of the number with pound signs, preceded by a base in decimal (e.g., 16#6.a7#e+2). In this latter case, the letters a . . f (both upper and lower case) are permitted as digits. Use of these letters in an inappropriate (e.g., decimal) number is an error, but need not be caught by the scanner.
    4. Inexact constants in Scheme. Scheme allows real numbers to be explicitly inexact (imprecise). A programmer who wants to express all constants using the same number of characters can use sharp signs (#) in place of any lower-significance digits whose values are not known. A base-ten constant without exponent consists of one or more digits followed by zero of more sharp signs. An optional decimal point can be placed at the beginning, the end, or anywhere in-between. (For the record, numbers in Scheme are actually a good bit more complicatedthan this. For the purposes of this exercise, please ignore anything you may know about sign, exponent, radix, exactness and length specifiers, and complex or rational values.)
    5. Financial quantities in American notation. These have a leading dollar sign ($), an optional string of asterisks (*--used on checks to discourage fraud), a string of decimal digits, and an optional fractional part consisting of a decimal point (.) and two decimal digits. The string of digits to the left of the decimal point may consist of a single zero (0). Otherwise it must not start with a zero. If there are more than three digits to the left of the decimal point, groups of three (counting from the right) must be separated by commas (,). Example: $**2,345.67. (Feel free to use "productions" to define abbreviations, so long as the language remains regular.)


Takaisin harjoitusten pääsivulle.