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 3
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
6. Define a left-recursive grammar rule
Answer:
A left-recursive grammar rule means that when a grammar rule has its left-hand side(LHS) also appearing at the beginning of its right hand side(RHS) so therefore the rule is said to be left-recursive.
7. What three extensions are common to most EBNF’s?
Answer:
-The first one is denotes an optional part of an RHS, which is delimited by brackets.
-The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether.
-The third common extension deals with multiple-choice options.
-The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether.
-The third common extension deals with multiple-choice options.
8. Distinguish between static and dynamic semantics
Answer:
The static semantics defines restrictions on the structure of valid texts that are hard or impossible to express in standard syntactic formalism.And The dynamic semantics (also known as execution semantics) of a language defines how and when the various constructs of a language should produce a program behavior.
9. What purpose do predicates serve in an attribute grammar?
Answer:
Predicate functions, which state the static semantic rules of the language, are associated with grammar rules. A false predicate function value indicates a violation of the syntax or static semantics rules of the language.
10. What is the difference between a synthesized and an inherited attribute?
Answer:
-A synthesized attribute associated with the nonterminals <var> and <expr>. It is used to store the actual type, int or real, of a variable or expression. In the case of a variable, the actual type is intrinsic.
-An inherited attribute associated with the nonterminal <expr>. It is used to store the type, either int or real, that is expected for the expression, as determined by the type of the variable on the left side of the assignment statement
PROBLEM SET
6. Using the grammar in Example 3.2, show a parse tree and a leftmost
derivation for each of the following statements:
a. A = A * (B + (C * A))
b. B = C * (A * C + B)
c. A = A * (B + (C))
Answer:
a. A = A * (B + C * A))
<assign> → <id> = <expr>
→ A = <expr>
→ A = <expr> * <expr>
→ A = <id> * <expr>
→ A = A * <expr>
→ A = A * ( <expr> )
→ A = A * ( <expr> + <expr>)
→ A = A * ( <id> + <expr>)
→ A = A * ( B + (<expr>)
→ A = A * ( B + (<expr> * <expr>))
→ A = A * ( B + (<id> * <expr>))
→ A = A * ( B + (C * <expr>))
→ A = A * ( B + (C * <id> )
→ A = A * ( B + (C * A))
b. B = C * (A * C + B)
<assign> → <id> = <expr>
→ B = <expr>
→ B = <id> * <expr>
→ B = C * <expr>
→ B = C * ( <expr> )
→ B = C * ( <id> * <expr>)
→ B = C * ( A * <expr>)
→ B = C * ( A * <id> + <expr>)
→ B = C * ( A * C + <expr>)
→ B = C * ( A * C + <id>)
→ B = C * ( A * C + B)
c. A = A * (B + (C))
<assign> → <id> = <expr>
→ A = <expr>
→ A = <id> * <expr>
→ A = A * <expr>
→ A = A * (<expr>)
→ A = A * ( <id> + <expr>)
→ A = A * ( B + <expr>)
→ A = A * ( B + (<expr>))
→ A = A * ( B + (<id>))
→ A = A * ( B + (C))
7. Using the grammar in Example 3.4, show a parse tree and a leftmost
derivation for each of the following statements:
a. A = ( A + B ) * C
b. A = B + C + A
c. A = A * (B + C)
d. A = B * (C * (A + B))
Answer:
a. A = ( A + B ) * C
<assign> → <id> = <expr>
→ A = <expr>
→ A = <term>
→ A = <term> * <term>
→ A = <factor> * <term>
→ A = (<expr>) * <term>
→ A = (<term>) * <term>
→ A = (<term>+ <term>) * <term>
→ A = (<factor> + <term>) * <term>
→ A = (<id> + <term>) * <term>
→ A = ( A + <term>) * <term>
→ A = ( A + <factor>) * <term>
→ A = ( A + <id>) * <term>
→ A = ( A + B ) * <term>
→ A = <expr>
→ A = <term>
→ A = <term> * <term>
→ A = <factor> * <term>
→ A = (<expr>) * <term>
→ A = (<term>) * <term>
→ A = (<term>
→ A = (<factor> + <term>) * <term>
→ A = (<id> + <term>) * <term>
→ A = ( A + <term>) * <term>
→ A = ( A + <factor>) * <term>
→ A = ( A + <id>) * <term>
→ A = ( A + B ) * <term>
→ A = ( A + B ) * C
b. A = B + C + A
<assign> → <id> = <expr>
→ A = <expr>
→ A = <expr> + <term>
→ A = <expr> + <term> + <term>
→ A = <term> + <term> + <term>
→ A = <factor> + <term> + <term>
→ A = <id> + <term> + <term>
→ A = B + <term> + <term>
→ A = B + <factor> + <term>
→ A = B + <id> + <term>
→ A = B + C + <term>
→ A = B + C + <factor>
→ A = B + C + <id>
→ A = B + C + A
→ A = <expr>
→ A = <expr> + <term>
→ A = <expr> + <term> + <term>
→ A = <term> + <term> + <term>
→ A = <factor> + <term> + <term>
→ A = <id> + <term> + <term>
→ A = B + <term> + <term>
→ A = B + <factor> + <term>
→ A = B + <id> + <term>
→ A = B + C + <term>
→ A = B + C + <factor>
→ A = B + C + <id>
→ A = B + C + A
c. A = A * (B + C)
<assign> → <id> = <expr>
→ A = <expr>
→ A = <term>
→ A = <term> * <factor>
→ A = <factor> * <factor>
→ A = <id> * <factor>
→ A = A * <factor>
→ A = A * ( <expr> )
→ A = A * ( <expr> + <term> )
→ A = A * ( <term> + <term> )
→ A = A * ( <factor> + <term> )
→ A = A * ( <id> + <term> )
→ A = A * ( B + <term> )
→ A = A * ( B + <factor> )
→ A = A * ( B + <id> )
→ A = A * ( B + C )
→ A = <expr>
→ A = <term>
→ A = <term> * <factor>
→ A = <factor> * <factor>
→ A = <id> * <factor>
→ A = A * <factor>
→ A = A * ( <expr> )
→ A = A * ( <expr> + <term> )
→ A = A * ( <term> + <term> )
→ A = A * ( <factor> + <term> )
→ A = A * ( <id> + <term> )
→ A = A * ( B + <term> )
→ A = A * ( B + <factor> )
→ A = A * ( B + <id> )
→ A = A * ( B + C )
d. A = B * (C * (A + B))
<assign> → <id> = <expr>
→ A = <expr>
→ A = <term>
→ A = <term> * <factor>
→ A = <factor> * <factor>
→ A = <id> * <factor>
→ A = B * <factor>
→ A = B * ( <expr> )
→ A = B * ( <term>)
→ A = B * ( <term> * <factor>)
→ A = B * ( <factor> * <factor>)
→ A = B * ( <id> * <factor>)
→ A = B * ( C * <factor>)
→ A = B * ( C * ( <expr> ) )
→ A = B * ( C * ( <expr> + <term> ) )
→ A = B * ( C * ( <term> + <term> ) )
→ A = B * ( C * ( <factor> + <term> ) )
→ A = B * ( C * ( <id> + <term> ) )
→ A = B * ( C * (A + <term> ) )
→ A = B * ( C * (A + <factor> ) )
→ A = B * ( C * (A + <id> ) )
→ A = B * ( C * (A + B ) )
→ A = <expr>
→ A = <term>
→ A = <term> * <factor>
→ A = <factor> * <factor>
→ A = <id> * <factor>
→ A = B * <factor>
→ A = B * ( <expr> )
→ A = B * ( <term>)
→ A = B * ( <term> * <factor>)
→ A = B * ( <factor> * <factor>)
→ A = B * ( <id> * <factor>)
→ A = B * ( C * <factor>)
→ A = B * ( C * ( <expr> ) )
→ A = B * ( C * ( <expr> + <term> ) )
→ A = B * ( C * ( <term> + <term> ) )
→ A = B * ( C * ( <factor> + <term> ) )
→ A = B * ( C * ( <id> + <term> ) )
→ A = B * ( C * (A + <term> ) )
→ A = B * ( C * (A + <factor> ) )
→ A = B * ( C * (A + <id> ) )
→ A = B * ( C * (A + B ) )
3. Prove that the following grammar is ambiguous:
<S> => <A>
<A> => <A> + <A> | <id>
<id> => a | b | c
Answer:
There are several other characteristics of a grammar that are sometimes useful in determining whether a grammar is ambiguous.1 They include the following: (1) if the grammar generates a sentence with more than one leftmost derivation and (2) if the grammar generates a sentence with more than one rightmost derivation.
The grammar above is ambiguous because the sentence
A = B + C + A
has two distinct parse trees.The grammar specifies slightly less syntactic structure.
9. Modify the grammar of Example 3.4 to add a unary minus operator that
has higher precedence than either + or *.
Answer:
<assign> → <id> = <expr>
<id> → A | B | C
<expr> → <expr> - <term>
<expr> + <term>
| <term>
<term> → <term> * <factor>
| <factor>
<factor> → ( <expr> )
| <id>
5. Describe, in English, the language defined by the following grammar:
<S> => <A> <B> <C>
<A> => a <A> | a
<B> => b <B> | b
<C> => c <C> | c
Answer:
Chapter 2
Name : Romy Hermawan
NIM : 1801378902
Class : LD001
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
REVIEW QUESTIONS
6. What hardware
capability that first appeared in the IBM 704 computer
strongly affected the
evolution of programming languages? Explain why
Answer:
The
Hardware Capability that IBM 704 had is
the indexing and floating-point.Its capabilities prompted the development of
Fortran because it was able to support floating-point operations hardware.
7. In what year was the
Fortran design project begun?
Answer:
May
1954
8. What was the primary
application area of computers at the time Fortran
was designed?
Answer:
Mathematics
9. What was the source
of all of the control flow statements of Fortran I?
Answer:
They
were based on 704 Instructions
10. What was the most
significant feature added to Fortran I to get Fortran
II?
Answer:
The
Independent Compilation of Subroutines
Chapter 1
Lecturer: Ir. Tri Djoko Wahjono,M.Sc
6. In What Language is most of UNIX written?
Answer:
UNIX is the first operating system that used C language. So, UNIX written in C language. Why C? Because C is easy to read, reliable, and flexible.
7. What is the disadvantage of having too many features in a language?
Answer::
too many features in a language is useless. First, it reduces computer memory. Programmer will be difficult to learn it and it's not familiar for some programmer.
8.How can user-defined operator overloading harm and the readability of a program?
Answer:
Because the built in operator has the precision and compiler knows all the precision between the operators, and it works on that precision. User can also create its own operator but the compiler does not come to know how to make precision of this operator. Therefore we don’t use user-defined operator.
9. What is one example of a lack of orthogonality in the design of C?
Answer:
C Language is one of language that inconsistent in its treatment of concept and language structure. its why learn C language is difficult enough. And an example of a lack of orthogonality in C is a struck can be returned from a function but an array cannot. C has two kinds of structures data arrays and struct.
10. What language used orthogonality as a primary design criterion?
Answer:
Language that used orthogonality as a primary design criterion is ALGOL 68.6. What common programming language statement, in your opinion, is most detrimental to readability?
Answer:
It's poorly written and undocumented code is commonly problem that hard to follow.
7. Java uses a right brace to mark the end of all compound statements. What are the arguments for and against this design?
Answer:
Java uses a right brace to mark the end of all compound statements. The argument for using the right brace to close all compounds is simplicity a right brace always terminates a compound. The argument against it is that when you see a right brace in a program the location of its matching left brace is not always obvious in part because all multiple-statement control constructs end with a right brace
8.Many Language distinguish between uppercase and lowercase letters in user-defined names. What are the pros and cons of this design decision?
Answer;
so far i know the uppercase and lowercase in C language to distinguish one variable to another variable.
Pros: Maybe Uppercase and lowercase helpful to give clarity and expand the number of such names.
Cons: it's prone to error by users. Hard to identify the fault location if an error occurs.
9.Explain the different aspects of the cost of a programming language.
Answer:
Some thing that affect the cost of programming language is efficiency.Time, accuracy, security, and usability are the cost of programming language.
10.What are the arguments for writing efficient programs even though hardware is relatively inexpensive?
Answer:
Talk about an efficient program is not separated from time and memory. A Program can be said to be efficient if the program does not take up a lot of memory and fast in terms of running time.
10. What language used orthogonality as a primary design criterion?
Answer:
Language that used orthogonality as a primary design criterion is ALGOL 68.6. What common programming language statement, in your opinion, is most detrimental to readability?
Answer:
It's poorly written and undocumented code is commonly problem that hard to follow.
7. Java uses a right brace to mark the end of all compound statements. What are the arguments for and against this design?
Answer:
Java uses a right brace to mark the end of all compound statements. The argument for using the right brace to close all compounds is simplicity a right brace always terminates a compound. The argument against it is that when you see a right brace in a program the location of its matching left brace is not always obvious in part because all multiple-statement control constructs end with a right brace
8.Many Language distinguish between uppercase and lowercase letters in user-defined names. What are the pros and cons of this design decision?
Answer;
so far i know the uppercase and lowercase in C language to distinguish one variable to another variable.
Pros: Maybe Uppercase and lowercase helpful to give clarity and expand the number of such names.
Cons: it's prone to error by users. Hard to identify the fault location if an error occurs.
9.Explain the different aspects of the cost of a programming language.
Answer:
Some thing that affect the cost of programming language is efficiency.Time, accuracy, security, and usability are the cost of programming language.
10.What are the arguments for writing efficient programs even though hardware is relatively inexpensive?
Answer:
Talk about an efficient program is not separated from time and memory. A Program can be said to be efficient if the program does not take up a lot of memory and fast in terms of running time.