Concept of Programming Language Chapter: 3

REVIEW QUESTIONS

2.  Who are language descriptions for?
     Language descriptions are for programmers and Programming language implementors.

5.  What is the difference between a sentence and a sentential form?
     a. A sentence contains only terminal symbols but a sentential form can contain some non-terminal symbols
     b. Sentential forms are a subset of sentences but the converse is not true
     c. Sentential forms have no handles but a sentence does.

7.  What three extentions are common to most EBNFs?
     a. The first of these denotes an optional part of an RHS, which is delimited by brackets.
     b. The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether.
     c. The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether.

8.  Distinguish between static and dynamic semantics.
     The static semantics of a language is only indirectly related to the meaning of programs during execution; rather, it has to do with the legal forms of programs
(syntax rather than semantics). Many static semantic rules of a language state its type constraints. Static semantics is so named because the analysis required to
check these specifications can be done at compile time.
     Dynamic semantics is the meaning of the expression, statements, and program units of a programming language.  

12. What is the primary use of attribute grammars?
      An attribute grammar is a device used to describe more of the structure of a programming language than can be described with a context-free grammar. It is also
used to specify how attribute values are computed.

14. Why can machine language not be used to define statements in operational semantics?
      Machine language can not be used to define statements in operational semantics because of some problems. First, the individual steps in the execution of machine
language and the resulting changes to the state of the machine are too small and too numerous. Second, the storage of a real computer is too large and complex.

15. Describe the two levels of uses of operational semantics.
      At the highest level, the interest is in the final result of the execution of a complete program. This is sometimes called natural operational semantics.
      At the lowest level, operational semantics can be used to determine the precise meaning of a program through an examination of the complete sequence of state
changes that occur when the program is executed. This use is sometimes called structural operational semantics.

21. When a grammar rule said to be left recursive?
      A grammar rule is 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.
      A = B + C * A

29. Give the difference between total correctness and partial correctness.
      If loop termination can be shown, the axiomatic description of the loop is called total correctness. If the other conditions can be met but termination is not
guaranteed, it is called partial correctnes.

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.
     A syntax error refers to an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language.
     Example of syntax error:
     In java this is error
     System.out.println(Hello World);
     The correct syntax is System.out.println(“Hello World”);
     Semantic Error is a logical error. It is due to wrong logical statements. Semantics is the interpretations of and meanings derived from the sentence transmission
and understanding of the message. Semantics errors are Logical, while Syntax errors are code errors.
     Example of Semantic Error:
     int average(int a, int b)
     {
       return a + b / 2; /* should be (a + b) / 2 */
     }
     It compiles and runs but does not give the right answer.

3.  Rewrite the BNF of example 3.4 to represent operator – and operator / instead of operator + and operator *.
     <expr> -> <expr> * <term> | <term>

     <term> = <factor> + <term> | <factor>

     <factor> -> (<expr>) | <id>

9.  Modify the grammar of Example 3.4 to add a unary minus operator that has higher precedence than either + or *.

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

12. Consider the following grammar:

      <S> -> a <S> c <B> | <A> | b

      <A> -> c<A> | c

      <B> -> d | <A>   

   Which of the following sentences are in the language generated by this grammar?     

   a) abbccd
   b) acccbda
   c) accbcbccc
   d) acddaccd
   e) acccdc

  No one of the following statements are generated.

18. What is a fully attributed parse tree?
      A parse tree of an attribute grammar is the parse tree based on its underlying BNF grammar, with a possibly empty set of attribute values attached to each node. If
all the attribute values in a parse tree have been computed, the tree is said to be fully attributed.

17. Convert the following EBNF to BNF:

      S -> A { b A}

      A -> a [ b ] A

     Answer:   

     S -> A | A B

     B -> b A | b A B

     A -> a A | a b A

19. Write an attribute grammar whose BNF basis is that of Example 3.6 in Section 3.4.5 but whose language rules are as follows: Data types cannot be mixed in expressions, but assignment statements need not have the same types on both sides of the assignment operator.

    Replace the second semantic rule with:

    <var>[2].env <- <expr>.env

    <var>[3].env <- <expr>.env

    <expr>.actual_type <- <var>[2].actual_type

    predicate: <var>[2].actual_type == <var>[3].actual_type

23. Compute the weakest precondition for each of the following assignment statements and postconditions:
      (a) a = 2 * (b – 1) – 1 {a > 0}
           2 * (b – 1) – 1 > 0
           2 * b – 2 – 1 > 0
           2 * b > 3
           b > 3 / 2
      (b) b = (c + 10) / 3 {b > 6}
           (c + 10) / 3 > 6
            c + 10 > 18
            c > 8
      (c) a = a + 2 * b – 1 {a > 1}
           a + 2 * b – 1 > 1
           2 * b > 2 – a
           b > 1 – a / 2
      (d) x = 2 * y + x – 1 {x > 11}
           2 * y + x – 1 > 11
           2 * y + x > 12

24. Compute the weakest precondition for each of the following sequences of assignment statements and their postconditions:
      (a) a = 2 * b + 1
           b = a – 3
           {b < 0}
           a – 3 < 0 -> a < 3
           2 * b + 1 < 3 -> 2 * b < 2 -> b < 1

          The weakest precondition is {b < 1}

    (b) a = 3 * (2 * b + a);

         b = 2 * a – 1
         {b > 5}
         2 * a – 1 > 5 
         2 * a > 6
         a > 3
         3 * (2 * b + a) > 3
         3 * (2 * b + a) > 3
         (2 * b + a) > 1
         (2 * b) > 1 – a
         b > (1 – a)/2
         The weakest precondition is {b >(1 – a)/2}

25. Compute the weakest precondition for each of the following selection constructs and their postconditions:

     (a) if (a = = b)
              b = 2 * a + 1
          else
              b = 2 * a;
          {b > 1}
              2 * a + 1 > 1 -> 2 * a > 0 -> a > 0
              2 * a > 1 -> a > 1/2
          The weakest precondition is {a > 1/2}

     (b) if (x < y)
              x = x + 1
          else
              x = 3 * x
         {x < 0}
              x + 1 < 0
                    x < -1

             3 * x < 0
                  x < 0
            The weakest precondition is {x < 0}
    (c) if (x  > y)
             y = 2 * x + 1
         else
             y = 3 * x – 1;
         {y > 3}
            2 * x + 1 > 3
                  2 * x > 2
                        x > 1

             3 * x – 1 > 3
                  3 * x > 4
                       x > 4/3
         The weakest precondition is {x > 4/3}

Concept of Programming Language Chapter: 2

REVIEW QUESTIONS

1. In what year was Plankalkül designed? In what year was that design published?
Plankalkül was designed by Konrad Zuse between 1943 and 1945.That designed was published in 1972.
3. What does Plankalkul mean?
Plankalkul means program calculus.
5. What is the number of bits in a single word of the UNIVAC I’s memory? How are the bits grouped?
The words of the UNIVAC I’s memory had 72 bits, grouped as 12 six-bit bytes.
7. Who developed the Speedcoding system for the IBM 701?
The Speedcoding system developed by John Backus for the IBM 701
8. Who developed Short Code? Why is Short Code called automatic programming?
Short Code was developed by John Mauchly in 1949 for the BINAC computer. Short code is called automatic programming because Short Code was not translated to machine code; rather, it was implemented with a pure interpreter. It clearly simplified the programming process, but at the expense of execution time. Short Code interpretation was approximately 50 times slower than machine code.
10. What was the most significant feature added to Fortran I to get Fortran II?
It fixed many of the bugs in the Fortran I compilation system and added some significant features to the language, the most important being the independent compilation of subroutines.
13. Which version of Fortran was the first to have character string handling?
Fortran 77 was the first to have character string handling.
24. What data structure that appeared in COBOL originated with Plankalkul?
Data structure that appeared in COBOL and originated with Plankalkul is hierarchical data structures(records).
28. Why was BASIC an important language in the early 1980s?
Because, it was easy for beginners to learn, especially those who were not science oriented, and its smaller dialects can be implemented on computers with very small memories.
33. What language introduced the case statement?
ALGOL-W introduced the case statement.
37. What are two kinds of statements that populate a Prolog database?
Two kinds of statements that populate a Prolog database are facts and rules.
38. What is the primary application area for which Ada was designed?
Ada is widely used in both commercial and defense avionics, air traffic control, and rail transportation, as well as in other areas.
52. What array structure is included in C# but not in C, C++, or Java?
Rectangular arrays are included in C#, but not in C, C++, or Java.
54. For what application area is JavaScript most widely used?
JavaScript is most widely used in Web browsers.
60. How does Java provide storage deallocation?
Java uses implicit storage deallocation for its objects, often called garbage
collection. This frees the programmer from needing to delete objects explicitly when they are no longer needed. Programs written in languages that do not have garbage collection often suffer from what is sometimes called memory leakage, which means that storage is allocated but never deallocated. This can obviously lead to eventual depletion of all available storage.
65. What are the inputs to an XSLT processor?
The inputs to an XSLT processor are an XML data document and an XSLT document (which is also in the form of an XML document).
66. What is the output of an XSLT processor?
The output of an XSLT processor could be in HTML or plain text.

PROBLEM SET

3. Write a short history of the Fortran 0, Fortran I, Fortran II, and Fortran IV systems.
Fortran 0 is the first version of Fortran. Fortran 0 was modified during the implementation period, which began in January 1955 and continued until the release of the compiler in April 1957. The implemented language, which we call Fortran I, is described in the first Fortran Programmer’s Reference Manual, published in October 1956 (IBM, 1956).
Fortran I included input/output formatting, variable names of up to six characters, user-defined subroutines, although they could not be separately compiled, the If selection statement, and the Do loop statement. There were no data-typing statements in the Fortran I language. Variables whose names began with I, J, K, L, M, and N were implicitly integer type, and all others were implicitly floating-point.
Fortran II compiler was distributed in the spring of 1958. It fixed many of the bugs in the Fortran I compilation system and added some significant features to the language, the most important being the independent compilation of subroutines.
Fortran IV became one of the most widely used programming languages of its time.It evolved over the period 1960 to 1962 and was standardized as Fortran 66 (ANSI, 1966), although that name was rarely used. Fortran IV was an improvement over Fortran II in many ways. Among its most important additions were explicit type declarations for variables, a logical If construct, and the capability of passing subprograms as parameters to other subprograms.

6. Make an educated guess as to the most common syntax error in C programs.
The most common syntax error in C programs are missing the (;) semicolon at the end of statement, missing “&” when using scanf, send the wrong paramater when using function, missing specific library when using specific function, missing () or missing {}, etc.

10. Outline the major developments in ALGOL 60.
• The concept of block structure was introduced. This allowed the programmer to localize parts of programs by introducing new data environments, or scopes.
• Two different means of passing parameters to subprograms were allowed: pass by value and pass by name.
• Procedures were allowed to be recursive. The ALGOL 58 description was unclear on this issue. Note that although this recursion was new for the imperative languages, LISP had already provided recursive functions in 1959.
• Stack-dynamic arrays were allowed. A stack-dynamic array is one for which the subscript range or ranges are specified by variables, so that the size of the array is set at the time storage is allocated to the array, which happens when the declaration is reached during execution.
13. What is the primary reason why C became more widely used than Fortran?
Reasons why C became more widely use than Fortran:
• Efficient compilers are universally available for all the main architechtures in use, and a good public compiler also exists (gcc). C compilers often come free with machines, while Fortran 90 compilers must be purchased, and often expensive.
• C is very broad in scope, and is more powerful of Fortran 90 in some areas, such as pointers, and manipulations of strings of characters.
• Acquired coding experience can be directly used outside the scientific world : C is very common in commercial world.

14. What are the arguments both for and against the idea of a typeless language?
Arguments for the idea:
Flexibility, Brevity of syntax. It places stricter controls on what objects can receive and send, so making it easier to enforce design strategies throughout the application. When there are errors in types, there can be picked up during precompilation or in the IDE.
Arguments against the idea:
Without type checking, there is no means to verify the integrity of the data without executing the application. The syntax of typed languages can be viewed as overly long or verbose. Typed languages aren’t as flexible as untyped languages, as data structures need to be cast to the correct type before another object can receive them. It is also said that by using typed languages, the compiler spends less time dynamically typing objects and so executes faster. However, this point is a grey area and that the main area of argument is how these language differences play out when designing applications where more then a few people are working on the code.

15. Are there any nonprocedural programming language other than prolog?
Yes, there are Fortran, C++, COBOL, Algol.
16. What is your opinion of the argument that languages that are too complex are too dangerous to use, and we should therefore keep all languages small and simple?
Languages are too complex are too dangerous to use because of its meaning itself. Ambiguous languages can cause trouble and misunderstanding among people.    So, we must keep it small and simple to avoid ambiguation and misunderstanding.
25. Give a brief general description of the Java servlet.
A servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web servers, so they can be thought of as Java Applets that run on servers instead of in web browsers. These kinds of servlets are the Java counterpart to non-Java dynamic Web content technologies such as PHP and ASP.NET.

Concept of Programming Language Chapter: 1

REVIEW QUESTIONS

3.   What programming language has dominated scientific computing over the past 50 years?

Programming language that has dominated scientific computing over the past 50 years is FORTRAN. Fortran is the early high-level programming language. For some scientific applications, efficiency is the primary concern, and at 1950s and 1960s there are no subsequent language that is significantly better than Fortran. That’s why Fortran is still used.

4.   What programming language has dominated business applications over the past 50 years?

Programming language that has dominated business applications over the past 50 years is COBOL. The first successful high-level language for business was COBOL (ISO/IEC, 2002), the initial version of which appeared in 1960.

5.   What programming language has dominated artificial intelligence over the past 50 years?

Artificial intelligence (AI) is a broad area of computer applications characterized by the use of symbolic rather than numeric computations. Symbolic computation means that symbols, consisting of names rather than numbers, are manipulated.

Programming language that has dominated artificial intelligence over the past 50 years is the functional language LISP (McCarthy et al., 1965), which appeared in 1959. Most All applications developed prior to 1990 were written in LISP or one of its close relatives.

9.   What is one example of a lack of orthogonality in the design of C?

One example of a lack of orthogonality in the design of C: in a programming language that supports pointers, it should be possible to define a pointer to point to any specific type defined in the language. However, if pointers are not allowed to point to arrays, many potentially useful user-defined data structures cannot be defined.

13.   What does it mean for a program to be reliable?

A program is said to be reliable if it performs to its specifications under all conditions. Reliability including Type Checking, Exception Handling, Aliasing, Readability and Writability.

15.   What is aliasing?

Aliasing is having two or more distinct names that can be used to access the same memory cell. It is now widely accepted that aliasing is a dangerous feature in a programming language. Most programming languages allow some kind of aliasing—for example, two pointers set to point to the same variable, which is possible in most languages.

16.   What is exception handling?

Exception handling is the ability of a program to intercept run-time errors (as well as other unusual conditions detectable by the program), take corrective measures, and then continue.

27.   What role does the symbol table play in a compiler?

The symbol table serves as a database for the compilation process. The primary contents of the symbol table are the type and attribute information of each user-defined name in the program.

28.   What is the utility of byte code?

Byte code, provides portability to any machine that has a byte code interpreter and an associated run-time system.

29.   What is a hybrid implementation system?

Some language implementation systems are a compromise between compilers and pure interpreters; they translate high-level language programs to an intermediate language designed to allow easy interpretation. This method is faster than pure interpretation because the source language statements are decoded only once. Such implementations are called hybrid implementation systems.

PROBLEM SET

1.   Do you believe that solving a problem in a particular algorithmic step requires programming language skills? Support your opinion.

I don’t think so. Because, the first step to create a program is make the algorithm and after that the programmer choose what kind of language they will use to execute their program. So, programming language skills are used when a problem in a particular algorithmic step has been solved.

2.   Who is said to be the first programmer in human history? Use the internet help for help.

Augusta Ada King, Countess of Lovelace, now commonly known as Ada Lovelace, was an English mathematician and writer chiefly known for her work on Charles Babbage’s early mechanical general-purpose computer, the Analytical Engine. 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 often considered the world’s first computer programmer.

3.   What are the disadvantages of multiple programming languages?

The disadvantages of multiple programming languages are the different syntax and the efficiency of the programming language. When the programmer is facing a task to create a program, it is not impossible when the programmer mistaking syntax design of a programming language to another programming language. And sometimes there is program when more efficient to be written in specific programming language.

4.   In what way do the languages for scientific application differ from the languages for business applications? Support your view.

The scientific applications used relatively simple data structures, but required large numbers of floating-point arithmetic computations. Business languages are characterized by facilities for producing elaborate reports, precise ways of describing and storing decimal numbers and character data, and the ability to specify decimal arithmetic operations.

5.   In what way do the languages for artificial intelligence differ from the language for web software? Support your review.

Artificial intelligence (AI) is a broad area of computer applications characterized by the use of symbolic rather than numeric computations. Symbolic computation means that symbols, consisting of names rather than numbers, are manipulated. Web software is supported by an eclectic collection of languages, ranging from markup languages, such as HTML, which is not a programming language, to general-purpose programming languages, such as Java. Because of the pervasive need for dynamic Web content, some computation capability is often included in the technology of content presentation.

9.   Explain how orthogonality in a programming language is closely related to simplicity?

Orthogonality is closely related to simplicity: The more orthogonal the design of a language, the fewer exceptions the language rules require. Fewer exceptions mean a higher degree of regularity in the design, which makes the language easier to learn, read, and understand. Anyone who has learned a significant part of the English language can testify to the difficulty of learning its many rule exceptions (for example, i before e except after c).