Tuesday, April 16, 2013

Concepts of Programming Languages Chapter 7


Concepts of Programming Languages Chapter 7

Name            : Erland
NIM              : 1601218035
Lecturer       : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment  : Concept of programming languages Chapter 7

Review Questions


       1.  Define operator precedence and operator associativity.
             Answer :
-Operator precedence is a rule used to clarify which procedures should    be performed first in a given mathematical expression.-Operator associativity is a property that determines how operators of    the same precedence are grouped in the absence of parentheses

       2. What is ternary operator?
            Answer : An operator that have 3 operands.

       3. What is prefix operator?
             Answer : An operator which signifies function of one argument which 
                             argument immediately follows an occurrence of the operator

       4. what operator usually has right associativity?
             Answer : Increments(++) and decrements(–) operators in C languages

       5. What is nonassociative operator?
             Answer : Non-associative operators are operators that have no defined
                             behavior when used in sequence in an expression

        8. Define functional side effect.
             Answer:Functional side effect occurs when the function changes either one 
                           of its parameters or a global variable.

        9.  What is a coercion?
               Answer : Coercion is defined as an implicit type conversion that initiated 
                               by the compiler.

        10. What is overloaded operator?
             Answer :overloaded operator is a specific case of polymorphism, where 
                            different operators have different implementations depending on 
                            their arguments.


         12. Define narrowing and widening conversions.
               Answer : Narrowing conversion convert a value to a type that cannot 
                               store even aporiximations of all the value of the original type.
                               Widening conversion converts a value to a type that can include
                               at least approximations of all of the value of the original type.


Problem Sets

1. When might you want the compiler to ignore type differences in an expression?
     Answer:Suppose Type1 is a subrange of Integer. It may be useful for the difference 
                   between Type1 and Integer to be ignored by the compiler in an expression.
                            
5.  Should C’s assigning operations (for example, +=) be included in other languages 
     (that do not already have them) ? Why or why not ?
     Answer:No. C’s assigning operations should not be included in other languages. 
                   Because this assigning operations would be the different part of the code
                   if it is implemented on other code that might cause confusion of the 
                  programmer or even it might disrupt the semantics of a language’s syntax.

7. Describe a situation in which the add operator in a programming language 
    would not be commutative.
    Answer :
                   If the add operator for a language is also used to concatenate strings, 
                  it’s quite apparent that it would not be commutative.
     For example:
     “efg” + “hij” = “efghij”     “lkm” + “nop” = “lkmnop”
                 These two strings are obviously not equal, so the addition operator is not 
                commutative.

18.  Should an optimizing compiler for C or C++ be allowed to change the order of  
       subexpressions in a Boolean expression? Why or why not?
       Answer : No. Because of short-circuit evaluation, the order of subexpressions 
                       around an && is important.

21. Why does Java specify that operands in expressions are all evaluated in 
       left-to-right order?
       Answer: Most groups use left-to-right associativity, which means that in an 
                      expression with operators in the same precedence group, the operators 
                      are  applied in left-to-right order.

Concepts of Programming Languages Chapter 6



Concepts of Programming Languages Chapter 6

Name            : Erland
NIM              : 1601218035
Lecturer       : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment  : Concept of programming languages Chapter 6

Review Questions


       1.  What is a descriptor?
             Answer : A descriptor is the collection of the attributes of a variable.

       2. What are the advantages and disadvantages of decimal data type?
            Answer :The advantages is able to precisely store decimal value, at least
                            those within a restricted range, which cannot be done with 
                            floating-point. The disadvantages of decimal data types are 
                            that the range of value is restricted because no exponents are 
                            allowed, and their representation in use is restricted.

       3. What are the design issues for character string types?
             Answer : - Should strings be simply a special kind of character array 
                                or a primitive type?
                             - Should strings have static or dynamic length?

       4. Describe the three string length options.
             Answer : 
- Static length string: the length is static and set when created
- Limited dynamic length string: the length can vary up to the declared    and fixed maximum set by the variable’s definition.
- Dynamic length string: The length varies without maximum. Stored in    dynamic storage.

       5. Define ordinal, enumeration, and subrange types.
             Answer : An ordinal type is one i which the range of possible values 
                             can be easily associated with the set of positive integer.
                             Enumeration type is one in which all the possible values, 
                             which are named constants, are provided, or enumerated,
                             in the definition.   
                             A subrange type is a contiguous subsequence of an ordinal type.                      

        6. What are the advantages of user-defined enumeration types?
             Answer:Enumeration types can provide advantages in  both readibility 
                           and reliability.

        7.  In what ways are the user-defined enumeration types of C# more reliable 
             than those of C++?
               Answer : C++ enumeration constants to be assigned to variables of any 
                               numeric type. in C# enumeration types are defined as new types
                               with datatype declarations.

        8. What are the design issue for arrays?
             Answer : - What types are legal for subscripts?
                             - Are subscripting expressions in element reference range checked?
                             - When are subscript range bound?
                             - When does array allocation take place?
                             - Are ragged or rectangular multidimensioned arrays allowed?
                             - Can arrays be initialized when they have their storage allocated?
                             - What kinds of slices are allowed, if any?

         12. What languages support negative subscripts?
              Answer: Ruby and Lua


          17. Define row major order and collumn major order.
              Answer: 
Row major order is the mapping of multidimensional arrays such that 
the elements of the array that have their first subscript the lower bound 
value of that subscript are stored first, followed by the elements of second 
value of the first subscritpt and so on.
Column major order, on the other hand, is the mappng of array such that
 the elements of an array that have as their last subscript the lower bound 
value of that subscript are stored first, followed by the elements of the second 
value of the last subscript, and so on



Problem Sets



1. What are the arguments for and againts four signed integer sizes in java?
     Answer: bytes(1 byte), short(2 bytes), integer(4 bytes), long(8 bytes).
                    depending on the domain of the variable required, data types are used.

2. How are negative integers stored in memory?
    Answer: In the number, the leftmost bit is considered as a “sign bit”, 
                   which signifies whether the integers are negative or not. If it’s 
                   negative, then that bit must store 1, and 0 otherwise.

                            
7. Compare the pointer and reference type variable in C++
    Answer: A C++ reference type variable is a constant pointer that is always
                   implicity deferenced.


8. What are the differences between the reference type variable of C++ and 
     those of Java?
    Answer:In their quest for increased safety over C++, the designers of Java 
                   removed C++-style pointers altogether. Unlike C++ reference variables, 
                  Java reference variables can be assigned to refer to different class 
                  instances; they are not constants. All Java class instances are referenced 
                  by reference variables. That is, in fact, the only use of reference variables 
                  in Java.
21. In what way is dynamic type checking better than static type checking?
       Answer :  Static checking reduces programmer flexibility.

Concepts of Programming Languages Chapter 5


Concepts of Programming Languages Chapter 5

Name            : Erland
NIM              : 1601218035
Lecturer       : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment  : Concept of programming languages Chapter 5

Review Questions


       1.  what are the design issues for names?
             Answer : - Are names case sensitive ? 
                             Are the special words of the language reserved 
                                words or keywords ?

       2. What is the potential danger of case-sensitive name?
            Answer : Because case-sensitive violates the design principle that language
                            constructs that look similar should have similar meanings.

       3. In what way are reserved words better than keywords?
             Answer : As a language design choice, reserved words are better than
                             keywords because the ability to redefine keywords can be confusing.

       4. what is alias?
             Answer : Alias is more than variable with the same name that
                             can be used to access the same memory location.

       5. Which category of C++ reference variables is always aliases?
             Answer : One common way in C and C++ is with their union types.

        6. What is the l-value of a variable? What is the r-value?
             Answer:The address of a variable is sometimes called l-value, and 
                            a variable's value is sometimes called r-value.

        7.  Define binding and binding time.
               Answer : - Binding is an association between an attribute and an entity,
                                  such as between a variable and its type or value, or between
                                  an operation and a symbol.
                               - The time at which a binding takes place is called binding time.

        9. Define static binding and dynamic binding?
             Answer : Static binding is done at compile time when a function is
                             called in order to match it with the definition, while 
                             Dynamic binding is at run time where we can specify 
                              that the compiler matches a function call with the correct 
                              function definition at run time.

Problem Sets


1. Decide which of the following identifier names is valid in c language, support 
    your decision.
     Answer: - _student    : valid, because in C name can be started with an underline.
                    - int                : invalid, because int is a reserved data type for integer.
                    - Student       : valid, because in C uppercase is allowed.
                    - 123Student : invalid, because in C names can't be started with number.
                    - Student123 : valid, as long as the names not started with number it 
                                              is allowed.
                            
2. What is the l-value? write a statement in C language which gives the compile 
     error "l-value required".
     Answer: l-value is the address of a variable.
                     ex: printf("%d\n",*num++);

4.  Why is the type declaration of a variable necessary? What is the value range 
     of the int type variable in Java?
      Answer : Because the program have to know what data type of a variable and 
                      the value of the variable. The value range of the int type variable in 
                     Java is around -2147483648 to 2147483647.

Monday, April 8, 2013

Concepts of Programming Languages Chapter 3



Concepts of Programming Languages Chapter 3

Name            : Erland
NIM              : 1601218035
Lecturer       : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment  : Concept of programming languages Chapter 3

Review Questions
       1.  Define syntax and semantics.
             Answer : Syntax is the form of its expressions, statements, and program units.
                             Semantics is the meaning of those expressions, statements, and 
                             program units.
       2. Who are language descriptions for?
            Answer : I think, language description is made for those who want to use it, 
                            i.e programmers.
       3. Describe the operation of a general language generator.
             Answer : Language generator is a device that can be used to generate the 
                             sentences if a language.The produced sentences is unpredictable, 
                             and a generator seems to be a device of a limited usefulness as 
                             a language descriptor.
       4. Describe the operation of a general language recognizer.
             Answer : Language Recognizers accept a Language.Recognizers are Machines. 
                             The Machines take a string as input. The Machines will accept 
                             the input if when run, the Machine stops at an accept state. 
                             Otherwise the input is rejected. If a Machine M recognizes all 
                              strings in Language L, and accepts input provided by a given
                              string S, M is said to accept S. Otherwise M is said to reject S. S 
                              is in L if and only if M accepts S. 
       5. What is the difference between a sentence and a sentential form?
             Answer : A sentence is a sentential form that has only terminal symbols. 
                             A sentence form is every string of symbols in the derivation.

        6. Define a left-recursive grammar rule.
             Answer: A grammar is left-recursive if we can find some non-terminal 
                            A which will eventually derive a sentential form with itself as 
                            the left-symbol


        8.  Distinguish between static and dynamic semantics.
               Answer :
  • Static semantic of a language is only indirectly related to the meaning of program during execution; rather it has to do with the legal forms of programs (syntax rather than semantics). Many static semantic rules of a language state its type constraints. Static semantics is so named because the analysis is required to check these specification can be done at compile time.
  • Dynamic semantics is the meaning of the expression, statements, and program units of a programming language.

        10. What is the difference between aa synthetized and an inherited attribute?
             Answer : Synthesized attributes are used to pass semantic information 
                             up a parse tree. Inherited attributes pass semantic information 
                             down and across a tree.

        27. What is loop invariant?
             Answer : loop invariant is an invariant used to prove properties of loops 
                             and, by extension, algorithms employing loops (usually correctness). 
                             Informally, a loop invariant is a statement of the conditions that 
                             should be true on entry into a loop and that are guaranteed to 
                             remain true on every iteration of the loop. This means that on
                             exit from the loop both the loop invariant and the loop 
                             termination condition can be guaranteed.

Problem Sets

1. Syntax error and semantic error are two types of compilation error. 
    Explain the difference between the two in a program with examples.
     Answer:  -Syntax error is an error that caused due to missing or misspelling 
                        when writing a function.
                        i.e.  printf("Error")
                              the example above is missing a semicolon which caused  
                              syntax error.
                      -  Semantic error is a logical error due to wrong logical statement.
                          i.e. pi = 1.52
                                it's wrong because pi is constant value of 3.14
                            
3. Rewrite the BNF of Example 3.4 to represent operator – and operator / instead of 
     operator + and operator *.
     Answer:
<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 *= operator of java.
      Answer : 
<assign> → <id> = <expr> 
 <id> → A | B | C 
<expr> → <expr> += <term>׀ <term>
<term> → <term> *= <factor>׀ <factor>
 <factor> → ( <expr> )׀ <id> 

6. Use the grammar in example 3.2,show a parse tree for each of the following statements.
     Answer: 

  • A=A *(B*(C+A))
  • B=C *(A+C *B)


  • A = A+(B*(C))
7. Using the grammar in example 3.4 , show a parse tree for each of
       the following statement
       Answer :


  •      A=(A*B)+C


  • A = B*C +A
  • A=A+(B*C)

  • A=B*(C+(A*B))



8.Prove that the following grammar is ambiguous :
   <S> -> <A>
   <A>-><A>*<A>|<id>
   <id>->x|y|z
   Answer : 
   The grammar above is ambiguous because it can has 2 distinct parse.



10. Describe, in english, the language defined by the following grammar:


   <S> -> <X><Y>
   <X>->x<X>|x
   <Y>->y<Y>|y

Answer : <S> is the start symbol ,in the start symbol exist 2 string of <X> and <Y>.
                 The string <X> can be replaced with x<X> or with x.The same can be done 
                 with string <Y>.The output may be like this xxxyyy or xy , but the output 
                 cannot be xyxy.