Selasa, 21 Oktober 2014

Assignment 4, Robert W.Sebesta , Chapter 4.

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


1. What are three reasons why syntax analyzers are based on grammars?

Answer:
 The most commonly used syntax description on syntax analyzers is context-free grammars or BNF. Using BNF, has at least three compelling advantages.

1. BNF descriptions of the syntax of programs are clear and concise, both for humans and for software systems.
2. BNF description can be used as the direct basis for the syntax analyzer.
3. Implementations based on BNF are relatively easy to maintain because of their modularity.



2. Explain the three reasons why lexical analysis is separated from syntax analysis.
Answer:
 1. Simplicity.
Techniques for lexical analysis are less complex than those required for syntax analysis, so the lexical-analysis process can be simpler if it is separate.

2. Efficiency.
Separation facilitates the optimization of lexical analyzer, because lexical analysis requires a significant portion of total compilation time.

3. Portability
Make the syntax analyzer to be platform independent from machine- dependent parts of the software system.



3. Define lexeme and token.

Answer:   
- Lexeme : Lowest level syntactic units.
   -Tokens : Category of lexemes.



4. What are the primary tasks of a lexical analyzer?

Answer:
 Lexical analyzers extract lexemes from a given input string and produce the corresponding tokens and then they detect syntactic errors in tokens.



5. Describe briefly the three approaches to building a lexical analyzer.

Answer:
These are three distinct approaches to construct a lexical analyzer:

1. Using a software tool to generate a table for a table-driven analyzer
2. Building such a table by hand
3. Writing code to implement a state diagram description of the tokens of the language being implemented.



Problem Set


1. Perform the pairwise disjointness test for the following grammar rules.
a. A → aB | b | cBB
b. B → aB | bA | aBb
c. A → aaA | b | caB

=
a. FIRST(aB) = {a}, FIRST(b) = {b}, FIRST(cBB) = {c}, Passes the test.

b. FIRST(aB) = {a}, FIRST(bA) = {b}, FIRST(aBb) = {a}, Fails the test.

c. FIRST(aaA) = {a}, FIRST(b) = {b}, FIRST(caB) = {c}, Passes the test.

2. Perform the pairwise disjointness test for the following grammar rules.

a. S → aSB | bAA
b. A → b[aB] | a
c. B → aB | a
=
a. FIRST(aSb) = {a}, FIRST(bAA) = {b}, passes the test.

b. FIRST(b{aB}) = {b}, FIRST (a) = {a}, passes the test.

c. FIRST(aB) = {a}, FIRST(a) = {a}, Fails the test.

3. Show a trace of the recursive descent parser given in Section 4.4.1 for the string a + b * c
=
a + b * c

Call lex /* returns a */

Enter <expr>

Enter <term>

Enter <factor>

Call lex /* returns + */

Exit <factor>

Exit <term>

Call lex /* returns b */

Enter <term>

Enter <factor>

Call lex /* returns * */

Exit <factor>

Call lex /* returns c */

Enter <factor>

Call lex /* returns end-of-input */

Exit <factor>

Exit <term>

Exit <expr>

4. Show a trace of the recursive descent parser given in Section 4.4.1 for the string a * (b + c)
a * (b + c)
=

Call lex /* returns a */

Enter <expr>

Enter <term>

Enter <factor>

Call lex /* returns * */

Exit <factor>

Call lex /* return (*/

Enter <factor>

Call lex /* returns b */

Enter <expr>

Enter <term>

Enter <factor>

Call lex /* returns + */

Exit <factor>

Exit <term>

Call lex /* returns c */

Enter <factor>

Exit <term>

Call lex / *return )*/

Exit <factor>

Exit <term>

Exit <expr>

Call lex /* returns end-of-input */

Exit <factor>

Exit <term>

Exit <expr>

5. 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 – > aAb | bBA A-> ab|aAB B->aB|b
a. aaAbb
b. bBab
c. aaAbBb

=
a.

parse tree =

S

/ | \

a A b

/|\

a A B

|

b

Handles = b, aAB

Phrases = aaAbb, aaABb, aAb

Simple Phrase = b


b.

parse tree =

S

/ | \

b B A

/ \

a b

Handles = ab

Phrases = bBab, bBA

Simple phrase = ab


c.aaAbBb = aSBb = aSBB = x

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

Senin, 06 Oktober 2014

Concepts of programming languages,10th. Chapter 2, Assignment 2,

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


1. In what year was Plankalkül designed? In what year was that design published?

Answer: Plankalkul was designed in 1942 - 1945,designed in 1972.

2. What two common data structures were included in Plankalkül?

Answer: Array and Record

3. How were the pseudo codes of the early 1950s implemented?

Answer: They were implemented through pure interpreter.

4. Speedcoding was invented to overcome two significant shortcomings of the computer hardware of the early 1950s. What were they?

Answer: Non-connotative names, absolute addressing (Also: floating-point arithmetic, automatic incrementing of address register).

5. Why was the slowness of interpretation of programs acceptable in the early 1950s?

Answer: It is because the lack of floating point hardware


Problem Set:


1. What features of Plankalkül do you think would have had the greatest influence on Frotran 0 if the Fortran designer had been familiar with Plankalkül ?

The ability to pass subprograms as parameter for a subprogram. Because this, in my opinion would widen the design capability of a program written with Java.

2. Determine the capabilities of Backus’s 701 Speedcoding system, and compare them with those of a contemporary programmable hand calculator.

Short Code consists of coded version of mathematical expression that was to be evaluated. Short code can be used to code many equations such as power, square roots, addition, subtraction, division but there is no multiplication code. Short code was able to be implemented into UNIVAC I computer. In the other hand contemporary programmable calculator was able to do multiplication with its embedded programming language. So with the contemporary calculator people can solve arithmetic problem easier than using the Short Code but it is more difficult to do further improvement to the programmable hand calculator than to the Sort Code.

3. Write a short history of the A-0,A-1,and A-2 systems designed by Grace Hopper and her associates.

The A-0 system was written by Grace Hopper in 1951 and 1952 for the UNIVAC I, was the first compiler ever developed for an electronic computer. The A-0 functioned more as a loader or linker than the modern notion of a compiler The A-0 system was followed by the A-1, A-2, A-3 (released as ARITH-MATIC), AT-3 (released as MATH-MATIC) and B-0 (released as FLOW-MATIC).

4. As a research project, compare the facilities of Fortran 0 with those of the Laning and Zierler system.

Laning and Zierler system was one of the first operating algebraic compiler that capable of the accepting mathematical formula in algrebraic notation and producing equivalent machine codes. While, Fortran 0 capable to accomodate scientific and engineering applications while also can do what Laning and Zierler system do plus engineering application.

5. Which of the three original goals of the ALGOL design committee, in your opinion, was most difficult to achieve at that  time?

In my opinion the most difficult to achieve from the three original goals of the ALGOL design committee are providing the efficiency of hand-coded programs. I think this is the most difficult to achieve, because there are a lot of modifications and a lot of things to add and to fix in order to make an efficient code program.

Senin, 29 September 2014

Project Sebesta Assignment, Lecturer: Mr.Tri Djoko Wahjono, Ir.,M.sc.

Concepts of Programming Languages

Robert W. Sebesta
Assignment #1


1. Why is it useful for a programmer to have some background in language design, even though he/she may never actually design a programming language?

Answer: It will increases our capacity to use different constructs in writing programs, enables us to choose languages for projects more effectively, and makes learning new
languages easier.

2. How can knowledge of programming language characteristics benefit the whole computing community?

Answer: Knowledge of programming language will help to make an easier computing way or computing systems which are more effecient for the computing community.

3.What programming language has dominated scientific computing over the past 50 years?

Answer: Fortran

4.What programming language has dominated business applications over the past 50 years?

Answer: COBOL

5.What programming language has dominated artificial intelligence over the past 50 years?

Answer: LISP

Review Question

1.Do you believe our capacity for abstract thought is influenced by our language skills? Support your opinion.

Answer:  Yes, if we learn more language skills, it means the our skills for abstract thought are improved.

2. What are some features of specific programming language you know whose rationales are a mystery to you?

Answer: I'm just started to learn c, and a lot of c language functions and features are a mystery for me.

3. What arguments can you make against the idea of a single language for all programming domain ?

Answer: It will be definitely simpler to learn and easier to learn, as there will be only one language to be understand.

4. What arguments can you make against the idea of a single language for all programming domain? 

Answer: It will be less style and no uniqueness in the style of programming itself.

5. Name and explain another criterion by which languages can be judged (in addition to those discussed in this chapter).

Answer: Simplicty of the programs, whether the programs needed lots of instructions or code to be input. And based on how the language will be type, either it will be a complicated language which need a lot of word or not.



Samuel Andrew
1801428352