Tuesday, March 26, 2013

KBP - Chapter 3

Review Question

1. Define syntax and semantics.

   Syntax is the form of its expressions, statements, and program units.
   Semantics is the meaning of those expressions, statements, and program unit.

2. Who are language descriptions for?

   For Language Recognizers and Language Generators.

5. What is the difference between a sentence and a sentential form?

   A sentence is the string of a language, while sentential form is each of the strings in derivation.

21. When is a grammar rule said to be left recursive?

    When a grammar rule has its LHS also appearing at the beginning of its RHS.

22. Give an example of an ambiguous grammar.

    Example :

       <assign> -> <id> = <expr>

       <id> -> A|B|C
       <expr> -> <expr> + <expr>
                | <expr * <expr>
                | (<expr>)
                | <id>

28. What is the use of the wp function? Why it is called a predicate transformer?

    Wp fuction is the least restrictive precondition that will guarantee the validity of the associated postcondition.
    It's called predicate transformer because it takes a predicate, or assertion, as a parameter and returns another predicate.

29. Give the difference between total correctness and partial correctness.

    Total correctness is the loop that can be shown, while partial correctness is the other condition that can be met, but termination is not guaranteed.

_______________________________________________________________________
Problem Set


1. Syntax error and semantic error are two types of compilation error.
    Explain the difference between the two in a program with examples.
    
    Syntax error --> struct Abc
                            {
                                int value;
                                char name[100];
                            }
                note : it should be like this :
                           struct Abc
                          {
                               int value;
                               char name[100];
                          };

    Semantic error --> for(int i=0;i<5;i--)
                                  {
                                        printf("Hey!"); printf("\n");
                                  }
                note : it should be like this :
                               for(int i=0;i<5;i++)
                               {
                                     printf("Hey!"); printf("\n");
                               }

3. Rewrite the BNF of Example 3.4 to represent operator – and operator / instead of operator + and operator *.
    <assign>-> <id> = expr
<id> -> A| B| C
<expr>-> <expr> -<term>
|<term>
<term>-> <term> / <factor>
| <factor>
<factor> -> (<expr>)
|<id>

6. Using the grammar in example 3.2, show a parse tree for each of the following statements:
   a).    =
         /   \
       a      *
             /   \
           *       a
         /   \
       b      +
             /   \
           c      a
         
   b).    =
         /   \
        b     *
             /   \
           +      c
          / \
        a    *
            /  \
          c     b

   c).    =
         /   \
        a     +
             /  \
            *    a
          /  \
        b     c

7. Using the grammar in example 3.4, show a parse tree for each of the following statements:
   a). A = (A*B)+C

<assign>-><id>=<expr>
<id>->A|B|C
<expr>-><id>+<expr>
|<term>
<term>-><term>*<factor>
|<factor>
<factor>->(<expr>)
|<id>

   b). A=B*C+A
<assign>-><id>=<expr>
<expr>-><expr>+<id>
|<term>
<term>-><term>*<factor>
|<factor>
<factor> -> (<expr>)
|<id>

   c). A = A + (B*C)
<assign>-><id>=<expr>
<expr>-><id>+<expr>
|<term>
<term>-><term>*<factor>
|<factor>
<factor>->(<expr>)
|<id>

   d). A = B*(C+(A*B))
<assign>-><id>=<expr>
<expr>-><id>*<expr>
|<term>
<term>-><term>+<factor>
|<factor>
<factor>-><id>*<id>
|<id>

13. Write a grammar for the language consisting of strings that have n copies of the letter a followed by double the number of copies of the letter b, where n >0. For example the strings abb, aabbbb, and aaabbbbbb are in the language but, a, aabb, ba, and aaabb are not.

    S-> aSb |ab 


18. What is a fully attributed parse tree?
    The tree is said to be fully attributed if all the attribute in a parse tree have been computed.

24. Compute the weakest precondition for each of the following sequences of assignment statements and their postcondition:
    a). b=a-3 --> b<0
        a-3<0 --> a<3

        a=2*b+1 --> a<3
        2*b+1<3 --> 2*b<2 --> b<1

    b). b=2*a-1 --> b>5
        2*a-1>5 --> 2*a>6 --> a>3

        a=3*(2*b+a) --> a>3
        3*(2*b+a)>3 --> 6*b+3*a>3 --> divided by 3 --> 2*b+a>1 --> b>(1-a)/2

Monday, March 11, 2013

KBP - Chapter 2

Review Questions

2. Its mixed one-dimensional and two-dimensional layout, which has puzzled many readers of the original document.
3. Plankalkul means "plan kalkulus" which means "formal system for planning".
5. The num of bits in a single word of the UNIVAC I's memory is 72 bits, and grouped as 12 six-bit bytes.
7.The speedcoding system developed by John Backus for the IBM 701.
8.The shortcode was developed by John Mauchly in 1949. Shortcode called automatic programming because it was implemented with a pure interpreter, not translated to machine code.
10. The most significant feature added to Fortran I to get Fortran II is Independent-compilation capability.
11. Logical loop statements and IF with an optional ELSE were added to Fortarn IV to get Fortran 77.
12. Fortran 90 was the first to have any sort of dynamic variables.
13. FOrtran 77 was the first to have character string handling.
16. Common LISP allows for static scoping and dynamic scoping, Scheme only uses static scooping. Scheme is relatively small while Common LISP is large and complex.
17. Scheme dialect is used to intriductory programming courses at some universities.
18. Two professional organizations together designed ALGOL 60 were ACM and GAMM.
20. Algol 58 introduced code blocks and the begin and end pairs for delimiting them, Algol 60 was the first language that implementing nested functions definitions with lexical scope.
21. BNF language was designed to describe the syntax of ALGOL 60.
22. On flow-matic language was COBOL based.
46. The primary application for Objectives-C is MacOS/iOS - iPhone.
49. A programming language for embedded consumer electronic devices was the first application for Java.


___________________________________________________
Problem Set

1. Logical data type and logical boolean expression, with this, we can create simple version of the complex compile, and link processes of earlier compilers.
6. Undefined escape sequences in literal strings. The backslash character can be used in literal strings and characters:
    - to escape various characters
    - to introduce an escape sequence representing a character
8. First, it is an interpreter type of language and focused on ease of use at the expense of system resources. Second, the running-time of a program that was written with the help of Speedcoding was usually ten to twenty times that of machine code.
7. Because that language continue to evolve from time to time.
9. It is to shorten the initialization of a variable.
12. Procedural programming is a classic programming where the program language is used to tell the computer exactly what to do, step by step. Non-procedural programming is where you tell the computer what you want, then the computer figures out how to get it. The incorporation of procedural and non-procedural features is used to overcome the lack of computer's knowledge to figure out some procedures by itself efficiently.
13. The reasons why C is more popular than Fortran are C is very broad in scope and C is very common in commercial world.
15. Yes, they are  : 

    - Fortran
    - C++
    - COBOL
    - Algol

Sunday, March 3, 2013

KBP - Chapter 1

Review Questions

6. Unix usually written in the C language, with some small snippets of assembler code for low level bootstraps.

13. To be reliable, a program must perform its intended functions and operations in a system's environment, without experiencing failure.


14. Because it's considered very important for reliability.


15. Aliasing is the process of sublimate curves and other lines become jagged, it's because of the file is not high enough to represent a smooth curve.


16. Exception handling is the process of responding to the occurance during the computation, often changing the normal flow of program execution.


17. Because readability affects reliability in both writing and maintenance phases of the life cycle.


20. The name of the category of programming languages whose structure is dictated by Von Neumann is Imperative Languages.


21. Two programming language were discovered as a a result of the research of software development in 1970s are top-down design and stepwise refinement.


25. Three methods of implementing a programming language : Compilation, Pure Interpretation, Hybrid Implementation Sysytems.


28. Byte code provides portalbility to any machine that has a byte codeinterpreter and an associated run-time system.


29. Hybrid implementation systems is the source language statements are decoded only once.

___________________________________________________
Problem Set

2. Ada Lovelace is said to be the first programmer in human history.Her notes on the engine include what is recognized as the first algorithm intended to be processed by a machine. Because of this, she is considered the world's first computer programmer.

3. Java’s thread model is low-level and error-prone, and the language’s stated objective to hide machine details is an obstacle for low-level and real-time applications where such details are intrinsic to the problem.


4. The scientific applications used relatively simple data structures, but requires large number of floating-point arithmetic computations.
   While the business languages are characterized by facilities for producing elaborate reports, precise ways of describing and storing decimal numbers and character data.


5. Artificial Intelligence is a broad area of computer applications characterized by the use of symbolic rather than numeric. Requires more flexibility than other programming domains.
   While the Web Software is an eclectic collection of languages, ranging from markup languages, such as HTML(not a programming language) and to general-purpose programming languages(Java).


15. C++ programming languages uses preprocessor directive while Java is not using preprocessor directive.
Advantage : save time when calling a lot of function.
Disadvantage : the problem is the size of the program. The pre-processor will replace all the macros in the program by its real definition prior to the compilation process of the program.