Describing Syntax and Semantics

Written on 17.36 by Unknown

We meet yet again through my blog and this time I'm gonna talk about my third assignment from Mr. Tri Djoko Wahjono, Ir, M.Sc. It is about "Describing Syntax and Semantics", the third chapter from Sebesta's Programming Language Concepts book, so i answered 5 questions from the review questions and 5 questions from the problem sets. So here is my answer for them : 


Review Questions :

1. Define syntax and semantics. 
  • Syntax is form of expressions, statements and program units in a programming language. while semantics is the meaning of the expressions, statements and program units in a programming language.

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

3. Describe the operation of a general language generator.
  • A language generator is a device that can be used to generate the sentences of a language. We can think of the generator as having a button that produces a sentence of the language every time it is pushed. Because the particular sentence that is produced by a generator when its button is pushed is unpredictable, a generator seems to be a device of limited usefulness as a language descriptor. For example, to determine the correct syntax of a particular statement using a compiler, the programmer can only submit a speculated version and note whether the compiler accepts it. On the other hand, 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. 
  • Suppose we have a language L that uses an alphabet of characters. To define L formally using the recognition method, we would need to construct a mechanism R, called a recognition device, capable of reading strings of characters from the alphabet . R would indicate whether a given input string was or was not in L. In effect, R would either accept or reject the given string. Such devices are like filters, separating legal sentences from those that are incorrectly formed. If R, when fed any string of characters over , accepts it only if it is in L, then R is a description of L. Because most useful languages are, for all practical purposes, infinite, this might seem like a lengthy and ineffective process. Recognition devices, however, are not used to enumerate all of the sentences of a language they have a different purpose. The syntax analysis part of a compiler is a recognizer for the language the compiler translates. In this role, the recognizer need not test all possible strings of characters from some set to determine whether each is in the language. Rather, it need only determine whether given programs are in the language. In effect then, the syntax analyzer determines whether the given programs are syntactically correct.

5. What is the difference between a sentence and a sentential form? 
  • A sentence is a sentential form that consist only terminal or lexemes while sentential form is a string that consist of derivations.


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. 
  • The recognizer doesn't need to test all possible strings of characters from some set to determine whether each is in the language. Rather, it need only determine whether given programs are in the language. In effect then, the syntax analyzer determines whether the given programs are syntactically correct.
  • To determine the correct syntax of a particular statement using a compiler, the programmer can only submit a speculated version and note whether the compiler accepts it. On the other hand, it is often possible to determine whether the syntax of a particular statement is correct by comparing it with the structure of the generator.

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 <class_head> ® {<modifier>} class <id> [extendsclass_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> += <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

This is my answer for the third chapter review questions and problem sets.

If you enjoyed this post Subscribe to our feed

No Comment

Posting Komentar