Selamat Datang di Blog Saya. Blog ini dibuat untuk memenuhi tugas dari dosen saya tercinta Bapak Ir.Tri Djoko Wahjono , M.Sc . Kiranya Blog ini bisa menambah pengetahuan bagi kalian. Isi blog ini mengenai konsep bahasa pemrogramman. Dan blog ini di sponsori oleh Bina Nusantara Alam Sutera.
Chapter 8
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
REVIEW QUESTION:
6. What is unusual about Python’s design of compound statements?
Answer:
Python uses indentation to specify compound statements. For example,if x > y :
x = y
print “case 1″
equally indent statements are grouped as one compound statement.
7. Under what circumstances must an F# selector have an else clause?
Answer:
An F# selector have an “else” clause if the “if” expression does return a value.
8. What are the common solutions to the nesting problem for two-way
selectors?
Answer:
The else clause would be paired with the second then clause. The disadvantage of using a rule rather than some syntactic entity is that although the programmer may have meant the else clause to be thealternative to the first then clause and the compiler found the structure syntactically correct, its semantics is the opposite.
Another way to avoid the issue of nested selection statements is to use an alternative means of forming compound statements.
9. What are the design issues for multiple-selection statements?
Answer:
– What is the form and type of the expression that controls the selection?– How are the selectable segments specified?
– How are the case values specified?
– How should unrepresented selector expression values be handled, if at all?
– Is execution flow through the structure restricted to include just a single selectable segment?
10. Between what two language characteristics is a trade-off made when
deciding whether more than one selectable segment is executed in one
execution of a multiple selection statement?
Answer:
The C# switch statement differs from that of its C-based predecessors in two ways. First, C# has a static semantics rule that disallows the implicit execution of more than one segment. The rule is that every selectable segment must end with an explicit unconditional branch statement: either a break,which transfers control out of the switch statement, or a goto, which can transfer control to one of the selectable segments (or virtually anywhere else).
As with C, if there is no break at the end of the selected segment, execution continues into the next segment.
PROBLEM SET:
Answer:
The potential readability problem is the typing errors. It’s very possible to occur if we don’t type the code carefully.
Answer:
statement that allows multiple exits. Read the paper and write an operational
semantics description of the statement
Answer:
Operational semantics are a category of formal programming language semantics in which certain desired properties of a program, such as correctness, safety or security, are verified by constructing proofs from logical statements about its execution and procedures, rather than by attaching mathematical meanings to its terms (denotational semantics).
Answer:
The primary argument for using Boolean expressions exclusively as control expressions is the reliability that results from disallowing a wide range of types for this use. In C, for example, an expression of any type can appear as a control expression, so typing errors that result in references to variables of incorrect types are not detected by the compiler as errors. No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.
Answer:
Ada was designed for military grade software development. The idea is that whenever you modify code in such a way that a new case emerges (for example adding a new value for an enumeration type), you are forced to manually revisit (and therefore re-validate) all the case statements that analyze it. Having a "default" is risky: you may forget that there is a case somewhere where the new case should not have been handled by the default.Chapter 7
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
REVIEW QUESTION:
6. What associativity rules are used by APL?
Answer :
all operators have equal precedence and all operators associate right to left.
7. What is the difference between the way operators are implemented in
C++ and Ruby?
Answer:
all of the arithmetic, relational, and assignment operators, as well as array indexing, shifts, and bitwise logic operators, areimplemented as methods in Ruby.
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:
A coercion is an implicit type conversion.
10. What is a conditional expression?
Answer:
a conditional expression is a statement that contains if-then-else to perform a conditional expression assignment.
PROBLEM SET:
6. Should C’s single-operand assignment forms (for example, ++count)
be included in other languages (that do not already have them)? Why or
why not?
Answer:
Yes C should, because it will ease the increment or even decrement while we use in looping rather than manually by the assigning, and also by using that we can easily know that it is not operation, instead it is an increment or decrement which is usually used in repetition.7. Describe a situation in which the add operator in a programming language would not be commutative.
Answer:
It wouldn’t be commutative when it deals with the negative integers. Recall that we can consider subtraction as addition in which one of two operator is a negative integer.
It wouldn’t be commutative when it deals with the negative integers. Recall that we can consider subtraction as addition in which one of two operator is a negative integer.
8. Describe a situation in which the add operator in a programming language would not be associative.
Answer:
It is not associative when it includes the other operator with higher precedence like the multiplication and division.
9. Assume the following rules of associativity and precedence for expressions:
Show the order of evaluation of the following expressions by parenthesizing
all subexpressions and placing a superscript on the right parenthesis
to indicate order. For example, for the expression
a + b * c + d
the order of evaluation would be represented as
((a + (b * c)1)2 + d)3
a. a * b – 1 + c
b. a * (b – 1) / c mod d
c. (a – b) / c & (d * e / a – 3)
d. -a or c = d and e
e. a > b xor c or d <= 17
f. -a + b
Answer:
(a) ( ( ( a * b )1 – 1 )2 + c )3
(b) ( ( ( a * ( b – 1 )1 )2 / c )3 mod d )4
(c) ( ( ( a – b )1 / c )2 & ( ( ( d * e )3 / a )4 – 3 )5 )6
(d) ( ( ( – a )1 or ( c = d )2 )3 and e )4
(e) ( ( a > b )1 xor ( c or ( d <= 17 )2 )3 )4
(f) ( – (a + b)1 )2
10. Show the order evaluation of the expression of problem 9, assuming that there are no precedence rules and all operators associate right to left.
Answer:
(a) ( a * ( b – ( 1 + c )1 )2 )3
(b) ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
(c) ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
(d) ( – ( a or ( c = ( d and e )1 )2 )3 )4
(e) ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
(f) ( – ( a + b )1 )2
Posted by Unknown
Chapter 5
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
REVIEW QUESTION
6. What is the l-values of a variables? What is the r-values?
Answer:
The l-values of a variable is its address while he r-values of a variable is its 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/ value, or between an operation and a symbol. Binding time is the time at which a binding takes place.
8. After language design and implementation [what are the four times bindings can take place in a program?]
Binding is an association between an attribute and an entity, such as between a variable and its type/ value, or between an operation and a symbol. Binding time is the time at which a binding takes place.
8. After language design and implementation [what are the four times bindings can take place in a program?]
Answer:
compile time: a variable in java bound to a particular data type
load time: a variable bound to a storage cell when a program is loaded into memory
link time: a call to a library subprogram is bound to the subprogram code
run time: certain variables declared in pascal and in c++ functions
9. Define static binding and dynamic binding.
compile time: a variable in java bound to a particular data type
load time: a variable bound to a storage cell when a program is loaded into memory
link time: a call to a library subprogram is bound to the subprogram code
run time: certain variables declared in pascal and in c++ functions
9. Define static binding and dynamic binding.
Answer:
A static binding is if it first occurs before run time and remains unchanged throughout program execution. A dynamic binding is if it first occurs during execution or can change during execution of the program.
10. What are the advantages and disadvantages of implicit declarations?
A static binding is if it first occurs before run time and remains unchanged throughout program execution. A dynamic binding is if it first occurs during execution or can change during execution of the program.
10. What are the advantages and disadvantages of implicit declarations?
Answer:
The advantage is write ability.
The advantage is write ability.
PROBLEM SET
6. Consider the following JavaScript skeletal program:
// The main program
var x;
function sub1() {
var x;
function sub2() {
. . .
}
}
function sub3() {
. . .
}
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
a. Assuming static coping, in the following, which declaration of x is the correct one for a reference to x?
i. Sub1
ii. Sub2
iii. Sub3
b. Repeat part , but assume dynamic scoping.
i. Sub1
ii. Sub2
iii. Sub3
b. Repeat part , but assume dynamic scoping.
Answer:
a. Static :
1. Sub1 : sub1
2. Sub2 : sub1
3. Sub3: main
b. Dynamic :
1. Sub1 : sub1
2. Sub2 : sub1
3. Sub3: sub1
1. Sub1 : sub1
2. Sub2 : sub1
3. Sub3: main
b. Dynamic :
1. Sub1 : sub1
2. Sub2 : sub1
3. Sub3: sub1
7. Assume the following JavaScript program was interpreted using
static-scoping rules. What value of x is displayed in function sub1?
Under dynamic-scoping rules, what value of x is displayed in function
sub1?
var x;
function sub1() {
document.write("x = " + x + "<br />");
}
function sub2() {
var x;
x = 10;
sub1();
}
x = 5;
sub2();
Answer:
Static scope: x=5, Dynamic scoping: x=10.
8. Consider the following JavaScript:
var x, y, z;
function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
...
}
...
}
function sub3() {
var a, x, w;
...
}
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2 and sub3, assuming static scoping is used.
Answer:
Sub1: a(sub1), y(sub1), z(sub1), x(main).
Sub2: a(sub2), b(sub2), z(sub2), y(sub1), x(main)
Sub3: a(sub3), x(sub3), w(sub3), y(main), z(main)
9. Consider the following Python program:
x = 1;
y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
. . .
def sub2():
global x;
a = 13;
x = 15;
w = 17;
. . .
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
. . .
. . .
List all the variables, along with the program units where they are
declared, that are visible in the bodies of sub1, sub2, and sub3, assumingstatic scoping is used.
x = 1;
y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
. . .
def sub2():
global x;
a = 13;
x = 15;
w = 17;
. . .
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
. . .
. . .
List all the variables, along with the program units where they are
declared, that are visible in the bodies of sub1, sub2, and sub3, assumingstatic scoping is used.
Answer:
point 1 : x = 1(main), y = 9 (sub1), z = 11(sub1) ,a = 7(sub1);
point 2 : x =15(sub2), w = 17(sub2), a = 13(sub2), y = 9(sub1);
point 3 : x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
point 4 : x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
point 2 : x =15(sub2), w = 17(sub2), a = 13(sub2), y = 9(sub1);
point 3 : x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
point 4 : x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
10. Consider the following C program:
void fun(void) {
int a, b, c; /* definition 1 */
. . .
while (. . .) {
int b, c, d; /*definition 2 */
. . . 1
while (. . .) {
int c, d, e; /* definition 3 */
. . . 2
}
. . . 3
}
. . . 4
}
For each of the four marked points in this function, list each visible variable,
along with the number of the definition statement that defines it.
int a, b, c; /* definition 1 */
. . .
while (. . .) {
int b, c, d; /*definition 2 */
. . . 1
while (. . .) {
int c, d, e; /* definition 3 */
. . . 2
}
. . . 3
}
. . . 4
}
For each of the four marked points in this function, list each visible variable,
along with the number of the definition statement that defines it.
Answer:
Point 1: a:1, b:2, c:2, d:2
Point 2: a:1, b:2, c:3, d:3, e:3
Point 3: a:1, b:2, c:2, d:2
Point 1: a:1, b:2, c:2, d:2
Point 2: a:1, b:2, c:3, d:3, e:3
Point 3: a:1, b:2, c:2, d:2
Chapter 6
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
REVIEW QUESTION:
6. What are the advantages of user-defined enumeration types?Answer:
Improved writability and readibility, No arithmetic operations are legal on enum types and no enumeration types can be assigned values outside its defined range.
7. In what ways are the user-defined enumeration types of C# more reliable than those of C++?
Answer:
C# enumeration types are like those of C++, except that they are never coerced to integer. So, operations on enumeration types are restricted to those that make sense. Also, the range of values is restricted to that of the particular enumeration type.
8.What are the design issues for array
9.What happens when an nonexistent element of an array is referenced in Perl ?
10. What happens when a nnexistent element of an array is referenced in Perl?
7. In what ways are the user-defined enumeration types of C# more reliable than those of C++?
Answer:
C# enumeration types are like those of C++, except that they are never coerced to integer. So, operations on enumeration types are restricted to those that make sense. Also, the range of values is restricted to that of the particular enumeration type.
8.What are the design issues for array
Answer:
What types are legal for subscripts?
Are subscripting expressions in element references range checked?
When are subscript ranges bound?
When does array allocation take place?
Are ragged or rectangular multidimensioned arrays allowed, or both?
Can arrays be initialized when they have their storage allocated?
What kinds of slices are allowed, if any?
What types are legal for subscripts?
Are subscripting expressions in element references range checked?
When are subscript ranges bound?
When does array allocation take place?
Are ragged or rectangular multidimensioned arrays allowed, or both?
Can arrays be initialized when they have their storage allocated?
What kinds of slices are allowed, if any?
9.What happens when an nonexistent element of an array is referenced in Perl ?
Answer:
– If you try to append non-existent elements from an array to another one, the initial array will grow as needed, even though the elements to append do not exist.10. What happens when a nnexistent element of an array is referenced in Perl?
Answer:
A reference to a nonexistent ele-ment in Perl yields undef, but no error is reported.
A reference to a nonexistent ele-ment in Perl yields undef, but no error is reported.
SET PROBLEM:
6. Explain all of the differences between Ada’s subtypes and derived types.
Answer:
8. What are all of the differences between the enumeration types of C++ and those of Java?
An Ada subtype is a possibly range-constrained version of an existing type. A subtype is type equivalent with its parent type. while a derived type is a new type that is based on some previously defined type with which it is not equivalent, although it may have identical structure. Derived types inherit all the properties of their parent types.
7. What significant justification is there for the -> operator in C and C++?
Answer:
The only justification for the -> operator in C and C++ is writability. It is slightly easier to write p -> q than (*p).q.
Answer :
In Java they can include fields, constructors, and methods. The possible values of an enumeration are the only possible instances of the class. All enumeration types inherit to String, as well as a few other methods. An array of the instances of an enumeration type can be fetched with the static method values. The internal numeric value of an enumeration variable can be fetched with the ordinal method. No expression of any other type can be assigned to an enumeration variable. Also, an enumeration variable is never coerced to any other type.
Answer :
Advantage : Unconstrained variant records in Ada allow the values of their variants to change types during execution.
Disadvantage: The type of the variant can be changed only by assigning the entire record, including the discriminant.
10. Multidimensional arrays can be stored in row major order, as in C++, or
in column major order, as in Fortran. Develop the access functions for
both of these arrangements for three-dimensional arrays
Answer:
Let the subscript ranges of the three dimensions be named min(1), min(2), min(3), max(1), max(2), and max(3). Let the sizes of the subscript ranges be size(1), size(2), and size(3).
Assume the element size is 1.
Assume the element size is 1.
Row Major:
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) +((i-min(1))*size(3) + (j-min(2)))*size(2) + (k-min(3))
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) +((i-min(1))*size(3) + (j-min(2)))*size(2) + (k-min(3))
Column Major:
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) +((k-min(3))*size(1) + (j-min(2)))*size(2) + (i-min(1))
location(a[i,j,k]) = (address of a[min(1),min(2),min(3)]) +((k-min(3))*size(1) + (j-min(2)))*size(2) + (i-min(1))
Posted by Unknown
Chapter 4
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
REVIEW QUESTION
6. What is a state transition diagram?
Answer:
A state transition diagram, or just state diagram, is a directed graph. The
nodes of a state diagram are labeled with state names. The arcs are labeled with
the input characters that cause the transitions among the states.
nodes of a state diagram are labeled with state names. The arcs are labeled with
the input characters that cause the transitions among the states.
Answer:
-Suppose we need a lexical analyzer that recognizes only arithmetic expressions,
including variable names and integer literals as operands. Assume that
the variable names consist of strings of uppercase letters, lowercase letters, and digits but must begin with a letter. Names have no length limitation. The first
thing to observe is that there are 52 different characters (any uppercase or lowercase letter) that can begin a name, which would require 52 transitions from the transition diagram’s initial state. However, a lexical analyzer is interested only in determining that it is a name and is not concerned with which specific
name it happens to be. Therefore, we define a character class named LETTER
for all 52 letters and use a single transition on the first letter of any name.
8. What are two distinct goals of syntax analysis?
Answer:
to detect syntax errors in a given program and to proccedure a parse tree, or possibly only the information required to build such a tree, for given program.
Answer:
to detect syntax errors in a given program and to proccedure a parse tree, or possibly only the information required to build such a tree, for given program.
9. Describe the differences between top-down and bottom-up parsers?Answer:
syntax analyzers are either top-down, meaning they construct left most derivations and a parse tree in top-down order, which mean the tree is built from the root downward to leaves.
bottom-up meaning case they construct the reverse of a rightmost derivation and a parse tree in bottom-up order which mean the parse tree is built from leaves upward to the root.
syntax analyzers are either top-down, meaning they construct left most derivations and a parse tree in top-down order, which mean the tree is built from the root downward to leaves.
bottom-up meaning case they construct the reverse of a rightmost derivation and a parse tree in bottom-up order which mean the parse tree is built from leaves upward to the root.
10. Describe the parsing problem for a top-down parser.
Answer:
1. Only judges grammaticality.
2. Stops when it finds a single derivation.
3. No semantic knowledge employed.
4. No way to rank the derivations.
5. Problems with left-recursive rules.
6. Problems with ungrammatical sentences.
PROBLEM SET
6. Given the following grammar and the right sentential form, draw a parse tree and show the phrases and simple phrases, as well as the handle.
S→AbB | bAc A→Ab | aBB B→Ac | cBb | c a.
a. aAcccbbc
b. AbcaBccb
c. baBcBbbc
a. aAcccbbc = S -> AbB -> aBBbB -> aAcBbB -> aAccBbbB -> aAcccbbc
b. AbcaBccb = S -> AbB -> AbcBb -> AbcAcb -> AbcaBBcb -> AbcaBccb
c. baBcBbbc = S -> bAc -> baBBc -> baBcBbc -> baBcBbbc
7. Show a complete parse, including the parse stack content, input string, and action for the string id * (id+id),using the grammar and parse table in section 4.5.3
Answer:
Stack
|
Input
|
Action
|
0
|
id * (id + id) $
|
Shift 5
|
0id5
|
* (id + id) $
|
Reduce 6 (Use GOTO[0, F])
|
0F3
|
* (id + id) $
|
Reduce 4 (Use GOTO[0, T])
|
0T2
|
* (id + id) $
|
Reduce 2 (Use GOTO[0, E])
|
0T2*7
|
(id + id) $
|
Shift 7
|
0T2*7(4
|
id + id ) $
|
Shift 4
|
0T2*7(4id5
|
+ id ) $
|
Shift 5
|
0T2*7(4F3
|
+ id ) $
|
Reduce 6 (Use GOTO[4, F])
|
0T2*7(4T2
|
+ id ) $
|
Reduce 4 (Use GOTO[4, T])
|
0T2*7(4E8
|
+ id ) $
|
Reduce 2 (Use GOTO[4, E])
|
0T2*7(4E8+6
|
id ) $
|
Shift 6
|
0T2*7(4E8+6id5
|
) $
|
Shift 5
|
0T2*7(4E8+6F3
|
) $
|
Reduce 6 (Use GOTO[6, F])
|
0T2*7(4E8+6T9
|
) $
|
Reduce 4 (Use GOTO[6, T])
|
0T2*7(4E8
|
) $
|
Reduce 1 (Use GOTO[4, E])
|
0T2*7(4E8)11
|
$
|
Shift 11
|
0T2*7F10
|
$
|
Reduce 5 (Use GOTO[7, F])
|
0T2
|
$
|
Reduce 5 (Use GOTO[0, T])
|
0E1
|
$
|
Reduce 2 (Use GOTO[0, E])
|
8. Show a complete parse, including the parse stack contents, input string, and action for the string (id + id) * id, using the grammar and parse table in Section 4.5.3.
Answer:
Answer:
<while_stmt> -> WHILE ‘(‘ (<arith_expr> | <logic_expr>) ‘)’ <block> <block> -> <stmt> | ‘{‘ <stmt> {<stmt>} ‘}’
10. Write an EBNF rule that describes the for statement of Java or C++. Write the recursive-descent subprogram in Java or C++ for this rule.
Answer:
Assume the following non-terminals are given: <type>, <id>, <literal>, <assign>, <expr>, and <stmt_list>.
<for> -> for ‘(‘ [[<type>] <id> = <expr> {, [<type>] <id> = <expr>}] ; [<expr>] ; [<expr> {, <expr>}] ‘)’ ‘{‘ <stmt_list> ‘}’