Selasa, 21 Oktober 2014

Assignment chapter 3 Programming Language Concept


Lecturer: Tri Djoko Wahjono, Ir., M.Sc.


1. Define syntax and semantics.
    Answer:

 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?
Answer:    

 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.
Answer:    

 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.
Answer:    

 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?
Answer:     

A sentence is a sentential form that has only terminal symbols.While a  sentential form is every string of symbols in the derivation.
Problem:
  1. The two mathematical models of a language describe are generation and recognition. Describe how each can define the syntax of a programming language.                                                                     Answer:                                                                                                                                              Syntax error refers to an error in the syntax of a sequence of which is written in a particular programming language, while Semantic Error it is a logical error, it is due to wrong logical statements
  1. Write EBNF descriptions for the following statement:
Answer:
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>
  1. Rewrite the BNF of example 3.4 to give +precedence over * and force + to be right associative.
Answer:
<assign> -> <id> = expr
 <id> -> A | B | C
 <expr> -> <expr> – <term>| <term>
 <term> -> <term> / <factor>| <factor>
 <factor> -> (<expr>)| <id>
  1. Rewrite the BNF of example 3.4 to add the ++ and — unary operators of Java.
Answer:
<assign> -> <id> = <expr>
<id> -> A | B | C
<expr> += <term>| <term>
<term> *= <factor>| <factor>
<factor> -> ( <expr> )| <id>
  1. Write a BNF description of the Boolean expression of Java, including the three operators &&, || and ! and the relational expressions.
Answer:
<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