Tuesday, March 26, 2013

Concepts of Programming Languages 10th Edition : Chapter 3



Created By : Robert W. Sebesta
Lecturer : Mr. Tri Djoko Wahjono, Ir. M.Sc
Answered by : Shirley Halim Ng
NIM : 1601233805
Class : 02PCT


Review Questions

1. Define syntax and semantic.
=> The syntax of a programming language is the form of its expressions, statements, and program units. Semantics is the meaning of those expressions, statements, and program units.

5. What is the difference between a sentence and a sentential form?
=> Sentence is the strings of a language and a sentential form is the start symbol S of a grammar or any string in (V union T)* that can be derived from S. 

7. What three extensions are common to most EBNFs?
=> The three extensions are common to most EBNFs are brackets, braces, and multiple-choice options.

8. Distinguish between static and dynamic semantics.
=> The static semantics of a language is only indirectly related to the meaning of programs (syntax rather than semantics) and the dynamic semantics of a language is the meaning of the expression, statements, and program units of a programming language.

10. What is the difference between a synthesized and an inherited attribute?
=> Synthesizes attributes are used to pass semantic information up a parse tree, while inherited attributes pass semantic information down and across a tree.

15. Describe the two levels of uses of operational semantics.
=> The two levels of uses of operational semantics are natural operational semantics which is the highest level that interest in the final result of the execution of a complete program and the lowest level is structural operational semantics which 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.

17. What is stored in the state of a program for denotational semantics?
=> The state of a program for denotational semantics is the value of all its current variable.

18. Which semantics approach is most widely known?
=> The Denotational semantics is the most widely known semantics approach.

22. Give an example of an ambiguous grammar.
=> <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 function is used to treat the process of producing a weakest precondition. It is called a predicate transformer because it takes a predicate, or assertion, as a parameter and returns another predicate.

Problem Set

1. Syntax error and semantics error are two types of compilation error. Explain the difference between the two in a program with examples.
=> Syntax Error: error due to missing colon, semicolon, parenthesis, etc. Syntax is the way in which we construct sentences by following principles and rules. 

Example: In C++, it would be a syntax error to say 

int x = "five";

This will not compile because it does not follow the syntax of the language and does not make any sense to the compiler. 


Semantic Error: it 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: A semantic error would compile, but be incorrect logically:

const int pi = 12345;

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 >
< term > -> (< expr >)
 | < id >


4. Rewrite the BNF of Example 3.4 to add the += and *= operators of Java.
=><assign>   -><id>< expr >
< id > -> A|B|C

< expr >-> < expr >+=< term >
 |< term >
< term > -> < term > *= < factor >
                  |< factor >
< term > -> (< 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
b. A = B * C + A
c. A = A + (B * C)
d. A = B * (C + (A *







10. Describe, in English, the language defined by the following grammar:
<S>-> <X> <Y>
<X>-> x < X >  | x
<Y>->y < Y > | y

=> <S>-> <X> <Y>   = S includes X and Y

<X>-> x < X >  | x                  = x is the element of X
<Y>->y < Y > | y                    = y is the element of Y

No comments:

Post a Comment