Tuesday 26 March 2013

Chapter 3



Concept of Programming Languages by Robert W. Sebesta Answer

Review Questions

1. Define syntax and semantics.

Syntax is form of a programming language’s expressions, statements, and program units.

Semantics is meaning of expressions, statements and program units.

2.Who are language descriptions for ?
Language descriptions is for programming language implementors

 4. Describe the operation of a general language recognizer.
The general operation of language recognizers is it will scan whether inputted string is inside certain language. If it is, it will accept the string.

5. What is the difference between a sentence and a sentential form ?
Sentence is strings of a language while sentential form is strings in the derivation

7. What three extensions are common to most EBNFs ?
optional part of RHS is delimited by brackets, the enclosed part can be repeated indefinitely or left out left altogether.

8. Distinguish between static and dynamic semantics.

Static semantics is only indirectly related to the meaning of programs during execution.

Dynamic semantics is not universally accepted in notation.

9. What purpose do predicates serve in an attribute grammar ?

Predicates function as boolean expression on the union of the attribute set and a set of literal attribute values

10. What is the difference between a synthesized and an inherited attribute ?
Synthesized attributes are used to pass semantic information up a parse tree while inherited attributes pass smenatic information down and accross a tree.

11. How is the order of evaluation attributes determined for the trees of a given attribute grammar?
The parse tree is evaluated based on its underlying BNF grammar, wit a possibility of having empty set of attributes values attached to each node.

12. What is the primary use of attribute grammars ?
Attribute grammars are used to describe more of the structure of a programming language than can be described with a context-free grammar.

14. Why can machine languages not be used to define statements in operational semantics ?
Because operational semantics needs an appropriate intermediate language. While machine language is a low-level language that can’t be understood easily.


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.

Example of syntax error in C programming language : while boolean_expr  -> no brackets before and after the boolean expression.

Example of semantics error in C programming language : while (counter) {}  -> but, the variable 'counter' is not defined by user before.

2. Write EBNF descriptions for the following :

a. class { }
b. ( ) ;
c. switch ( variable ) { case : }
d. union { };
e. float x ;


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>


4. Rewrite the BNF of Example 3.4 to add the += and *= operators of Java

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

<id> -> A | B | C

<expr> += <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 ) ) 
-> = 
          -> A|B|C
          -> + 
                       |   * ( )
                       |  ( )
                       |

b. B = C * (A + C * B )

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

c. A = A + (B * (C) )

-> = 
          -> A|B|C
          -> * ( )
                       |   + 
                       |  ( )
                       | 

18. What is a fully attributed parse tree ?

A parse tree is fully attributed if the attributes of all its nodes are computed.


0 comments:

Post a Comment