Minggu, 12 Oktober 2014

Chapter 3: Describing Syntax and Semantics

This time I will post the answer of first 5 numbers of Review Questions and first 5 numbers of Problem Set.

Review Questions:
1. Define syntax and semantics.
   Syntax is more to the structure, form of the programming language; while semantics are more into the meaning of the code.

2. Who are language descriptions for?
   Language descriptions are for initial evaluators, implementors, and users.

3. Describe the operation of a general language generator.
   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. It is often possible to determine whether the syntax of a particular statement is correct by comparing it with the structure of the generator.

4. Describe the operation of a general language recognizer.
   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. These recognition devices are like filters separating correct sentences from those that are incorrectly. A recognizer is used in the syntax analysis part of the compiler. In this role, the recognizer need not test all possible strings of characters from some set to determine whether each is in the language. The syntax analyzer just determines whether the given programs are syntactically correct.
   
5. What is the difference between a sentence and a sentential form?
   A sentence is a sentenential form that has only terminal symbols. A sentence form is every strings of the symbols in the derivation.

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.
   Syntax error refers to an error in the program that caused by misused syntax. Semantics error is caused by wrong logical statements.

2. Write EBNF descriptions for the following:
a. A Java class definition header statement
   <class_head> ® {<modifier>} class <id> [extends class_name]
    [implements <interface_name> {, <interface_name>}]
    <modifier> ® public | abstract | final
b. A Java method call statement
   <for> -> for '(' [[<type>] <id> = <expr> {,[<type>] <id> = <expr>}] ; [<expr>] ; [<expr>{, <expr>}] ')' '{' <stmt_list> '}
c. A C switch statement   
   <stmt> -> switch (<int expr>){case <int const> : {<stmt>;} 
    { case<int const> : {<stmt> :}}
    [default : {<stmt>;}]    }
d. A C union definition
   <union_defn> -> union <var_list> <union_identifier>;
    <var_list> -> <list_of_data-type specifier> <var>    <list_of_data-type specifier> -> int | float | long | char | double    <union_identifier> -> <var>
e. C float literals
   <float-literal> -> <real> <suffix>
    | <real> <exponent> <suffix>    | <integer> <exponent> <suffix>

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>
 
4. Rewrite the BNF of Example 3.4 to add the ++ and -- unary operators of Java.
   <assign> -> <id> = <expr>
   <id> = A|B|C
   <expr> -> <expr> + <term> | <term>++ | <term>--
   <term> -> <term> * <factor> | <factor>
   <factor> -> (<expr>) | <id>

5. Write a BNF description of the Boolean expressions of Java, including the three operators &&, ||, and ! and the relational expressions.
   <Boolean_expr> -> <Boolean_expression> || <Boolean_term> | <Boolean_term>   <Boolean_term> -> <Boolean_term> && <Boolean_factor> | <Boolean_factor>   <Boolean_factor> -> id | !<Boolean_factor> | (<Boolean_expr>) | <relation_expr>   <relation_expr> -> id == id | id != id | id < id | id <= id | id >= id | id > id

Tidak ada komentar:

Posting Komentar