Monday, April 8, 2013

Answers of Concepts of Programming Languages 10th - Chapter 3


Martin - 1601213690

02 PBT


Review Questions

1. Define syntax and semantics.
    * Syntax is the grammatical rules and structural patterns governing the ordered use of appropriate words and symbols for issuing commands, writing code, etc., in a particular software application or programming language.
    * Semantics provides the rules for interpreting the syntax which do not provide the meaning directly but constrains the possible interpretations of what is declared.

2. Who are language descriptions for?
    * Language descriptions are written for the potential users, who are the initial evaluates of the language.

3. Describe the operation of a general language generator.
    * A general language generator is a device that can be used to generate the sentences of the language. It generates unpredictable sentences which makes a generator seems to be a device of limited usefulness as language descriptor.

4. Describe the operation of a general language recognizer.
    * A general language recognizer is a recognition device capable of reading strings of characters from the alphabet. It would analyze the given string and it would either accept or reject the string based from the language given.

5. What is the difference between a sentence and a sentential form?
    * A sentence is a sentential form that has only terminal symbols.
    * A sentential form is every string of symbols in the derivation.

6. Define a left-recursive grammar rule.
    * Left recursion is a particular form of recursion that cannot be directly handled by the simple LL(1) parsing algorithm.

7. What three extensions are common to most EBNFs?
    * Optional parts are placed in brackets ([])
    * Put alternatives in parentheses and separate them with vertical bars (+|-)
    * Put repetitions ( 0 or more ) in braces ({})

8. Distinguish between static and dynamic semantics.

    * Static semantics is more on the legal forms of programs ( syntax rather semantics ) and is only indirectly related to the meaning of the programs during execution. Static semantics is so named because the analysis required to check these specifications can be done at compile time. In many cases, the semantic rules of language state its type constraints.
    * Dynamic semantics is describing the meaning of the programs. Programmers need to know precisely what statements of a language do. Compile writers determine the semantics of a language for which they are writing compilers from English descriptions.


10. What is the difference between a synthesized and an inherited attribute?
    * The synthesized attributes are the result of the attribute evaluation rules, and may also use the values of the inherited attributes.
    * The inherited attributes are passed down from parent nodes.

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 is possible with a context-free grammar. The primary purpose of an attribute grammar is it allows certain language rules to be described, such as type of compatibility.



Problem Set

1. The two mathematical models of language description are generation and recognition. Describe how each can define the syntax of a programming language.

3. Rewrite the BNF of Example 3.4 to give + precedence over * and force + to be right associative.
    * <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 and a leftmost derivation 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 and a leftmost derivation 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 string that have n copies of letter a followed by the same number of copies of the letter b, where n>0. For example, the strings ab, aaaabbbb, and aaaaaaaabbbbbbbb are in the language but a, abb, ba, and aaabb are not.

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.

No comments:

Post a Comment