0% found this document useful (0 votes)
13 views26 pages

Question 1

Uploaded by

nhoxbin03122003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views26 pages

Question 1

Uploaded by

nhoxbin03122003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Question 1.

Discuss all of possible results of //1

int f(int& n)
{
n++;
return n;
}

int X = 1;
X + f(X) + X; //1

Question 2. Supposed that we want to develop a program as follows.

Input: three numbers of a,b and c.


Operations:
If a is greater than b and square root of (a-b) is greater than c then evaluate square root of (a-
b)*(b-c)

Implement the program in case (i) short circuit Boolean is supported; and (i) short circuit
Boolean is not supported.

Question 3. Rewrite in ordinary and polish postfix the following expresions

a. i = i +1
b. i++
c. ++i
d. a + ++i + f(i)
e. a + i++ + f(i)
Question 1. Discuss all of possible results of //1

int f(int& n)
{
n++;
return n;
}

int X = 1;
X + f(X) + X; //1

Strategy 1: evaluate the value of variable first, then call the function
1 + f(X) + 1
1 +2 + 1
4

Strategy 2: call the function first, then evaluate the values of variables

X+2+X
2+2+2
6

Strategy 3: evaluate from left to right

1 + f(X) + X
1+2+X
1+2+2
5

Question 2. Supposed that we want to develop a program as follows.

Input: three numbers of a,b and c.


Operations:
If a is greater than b and square root of (a-b) is greater than c then evaluate square root of (a-
b)*(b-c)

Implement the program in case (i) short circuit Boolean is supported; and (i) short circuit
Boolean is not supported.

Case (i)
if ((a>b) && (sqrt(a-b)>c) sqrt((a-b)*(b-c))

Case (ii)

if ((a>b)
{
if (sqrt(a-b)>c) sqrt((a-b)*(b-c))

}
else

Question 3. Rewrite in ordinary and polish postfix the following expresions

a. i = i +1
ii1+=
b. i++
i = i +1

c. ++i
i++
ii1+=

i = i +1

d. a + ++i + f(i)

i = i +1;
a + i + f(i);

ii1+=;
a i i f + +;

e. a + i++ + f(i)

a + i + f(i);
i = i +1;

a i i f + +;
ii1+=;
Lexical analyst (Regex)
(a+b)*=[ab]*
Write the regular expression for the language accepting all the string which are starting with 1
and ending with 0, over ∑ = {0, 1}.
R = 1 (0+1)* 0
Write the regular expression for the language starting and ending with a and having any having
any combination of b's in between.
1. R = a b* b
Write the regular expression for the language starting with a but not having consecutive b's.
1. R = {a + ab}* có a hoặc ab lặp lại hoặc có cả 2 lặp lại trong chuỗi
Write the regular expression for the language accepting all the string in which any
number of a's is followed by any number of b's is followed by any number of c's.

As we know, any number of a's means a* any number of b's means b*, any number of c's
means c*. Since as given in problem statement, b's appear after a's and c's appear after
b's. So the regular expression could be:

1. R = a* b* c*

Write the regular expression for the language over ∑ = {0} having even length of the
string.

Solution:

The regular expression has to be built for the language:

1. L = {ε, 00, 0000, 000000, ......}


The regular expression for the above language is:

1. R = (00)*

2. Write the regular expression for the language having a string which should have
atleast one 0 and alteast one 1.

3. Solution:

he regular expression will be:

1. R = [(0 + 1)* 0 (0 + 1)* 1 (0 + 1)*] + [(0 + 1)* 1 (0 + 1)* 0 (0 + 1)*]

Write the regular expression for the language L over ∑ = {0, 1} such that all the string do
not contain the substring 01.

Solution:

The Language is as follows:

1. L = {ε, 0, 1, 00, 11, 10, 100, .....}

The regular expression for the above language is as follows:

1. R = (1* 0*)

Example 9:
Write the regular expression for the language containing the string over {0, 1} in which
there are at least two occurrences of 1's between any two occurrences of 1's between
any two occurrences of 0's.

Solution: At least two 1's between two occurrences of 0's can be denoted by (0111*0)*.

Similarly, if there is no occurrence of 0's, then any number of 1's are also allowed. Hence
the r.e. for required language is:

1. R = (1 + (0111*0))*

Example 10:
Write the regular expression for the language containing the string in which every 0 is
immediately followed by 11.

Solution:

The regular expectation will be:

1. R = (011 + 1)*

For the following languages on ∑ = {a,b}. Develop the following regex

1. All strings with exactly one a.

2. All strings with no more than three a’s

3. For the following languages on ∑ = {a,b}

a. L = {w: |w| mod 3 = 0}


b. L = {w: |w| mod 5 ≠ 0}
c. L = {w: n (w) mod 3 > 1}
a

4. a : n ≥ 0, n ≠ 4
n

For the following languages on ∑ = {a,b}. Develop the following regex

1. All strings with exactly one a.

2. All strings with no more than three a’s

3. For the following languages on ∑ = {a,b}

a. L = {w: |w| mod 3 = 0}


b. L = {w: |w| mod 5 ≠ 0} độ dài chuỗi ab
c. L = {w: n (w) mod 3 > 1} số lần xuất hiện a
a

4. a : n ≥ 0, n ≠ 4
n
SOLUTION:
1. (b)*a(b)*
2.
- 0a: b*
- 1a: b*ab*
- 2a: b*ab*ab*
- 3a: b*ab*ab*ab*
🡪 b* | b*(ab*)* | b*ab*ab* | b*ab*ab*ab* 🡪 b*(ab*){0,3}

3.
a. ([ab][ab][ab])* 🡪 [ab]{3n} with n >= 0
b. ([ab][ab][ab][ab][ab])* 🡪 [ab]{5n} (with n >= 0)
c. n (w) mod 3 > 1 🡪 n (w) = 3k + 2 (k >= 0)
a a

🡪 b*ab*(ab*ab*ab*)*ab* 🡪 b*ab*(ab*){3n}ab* (with n >= 0)

4. b* | b*ab* | b*aab* | b*aaab* 🡪 b*(a){0,3}b*


O Vuong la mui ten ->

In a regular expression, x* means zero or more occurrence of x.

It can generate {e, x, xx, xxx, xxxx, .....}

In a regular expression, x + means one or more occurrence of x. It can generate {x, xx, xxx,
xxxx, .....}

1.

2.
3.

4. Which string can be generated by the following grammar?


5. Grammar

S→AB
A → a A a | 𝜖ϵ
B→bB|b

aabb
Syntax tree?

6. Grammar

S→AB
A→aA|a
B → b B b | 𝜖ϵ

aabb
Syntax tree?

7. Show a derivation tree, together with the corresponding leftmost and rightmost
derivations of the string aabbbb with the grammar

S 🡪 AB | λ
A 🡪 aB
B 🡪 Sb
1.

2.

3.
4. Which string can be generated by the following grammar?

5. Grammar

S→AB
A → a A a | 𝜖ϵ
B→bB|b

aabb
Syntax tree?
6. Grammar

S→AB
A→aA|a
B → b B b | 𝜖ϵ

aabb
Syntax tree?

7. Show a derivation tree, together with the corresponding leftmost and rightmost
derivations of the string aabbbb with the grammar

S 🡪 AB | λ
A 🡪 aB
B 🡪 Sb

SOLUTIONS:
1. C (cannot terminate s)
2. C (s -> t, then cannot terminate t)
3. D
4. C
5. Syntax tree:

6. Syntax tree:
7.

Target: aabbbb
S 🡪 AB | eps
A 🡪 aB
B 🡪 Sb

Leftmost: S -> AB -> aBB -> aSbB -> aABbB -> aaBBbB ->aaSbBbB -> aabBbB -> aabSbbB -
> aabbbB -> aabbbSb -> aabbbb
Left bên trái qua
Rightmost: S -> AB -> ASb -> Ab -> aBb -> aSbb -> aABbb -> aASbbb -> aAbbb -> aaBbbb -
> aaSbbbb -> aabbbb
Right bên phải qua
1.
Question 1

2. Question 2
3. Question 3

4. On the following grammars,

a. Compare the precedence between + and *


b. State the associativity of all operations
c. Evaluate 3-4-5*2

G1:

S 🡪 S+T | S-T |T
T 🡪 T*F | T/F |F
F🡪 num
G2:

S 🡪 S*T | S-T |T
T 🡪 F+T | F/T |F
F🡪 num

G3:

S🡪 S*T |S/T |S+T |T


T🡪 T-T |F
F 🡪 num

G4:

S🡪 T*S |S-T |S+T |T


T🡪 T/T |F

F 🡪 num
1. Question 1

2. Question 2
3. Question 3

4. On the following grammars,

a. Compare the precedence between + and *


b. State the associativity of all operations
c. Evaluate 3-4-5*2

G1:

S 🡪 S+T | S-T |T
T 🡪 T*F | T/F |F
F🡪 num
G2:

S 🡪 S*T | S-T |T
T 🡪 F+T | F/T |F
F🡪 num

G3:

S🡪 S*T |S/T |S+T |T


T🡪 T-T |F
F 🡪 num

G4:

S🡪 T*S |S-T |S+T |T


T🡪 T/T |F
F 🡪 num

SOLUTION:
1. C
2. D
3. B
4.
G1:

S 🡪 S+T | S-T |T
T 🡪 T*F | T/F |F
F🡪 num

a. * has higher precedence than +


b. ‘+-*/‘ is left-associative
c. 3 - 4 - 5*2 = 3 – 4 – 10 = -1 – 10 = -11

G2:

S 🡪 S*T | S-T |T
T 🡪 F+T | F/T |F
F🡪 num

a. + has higher precedence than *


b. ‘*-‘ is left- associative, ‘+/’ is right- associative
c. 3 – 4 – 5*2 = -1 – 5*2 = -6*2 = -12
G3:

S🡪 S*T |S/T |S+T |T


T🡪 T - T | F
F 🡪 num

a. + and * has the same precedence, but the operations will be executed from left to right
(which means we will do * before +)
b. ‘*/+’ is left- associative, ‘-‘ both left and right associative
c. Ambiguous grammar rule with ‘-‘ operator? 🡪 -12 and 8

G4:

S🡪 T*S |S-T |S+T |T


T🡪 T/T |F
F 🡪 num

a. + and * has the same precedence, but the operations will be executed from left to right
(which means we will do * before +)
b. ‘*’ is right-associated, ‘-+’ is left- associative, ‘/’ is left and right associative
c. 3 – 4 – 5*2 cannot be parsed 🡪 it is invalid string

The Problem
01 <?php
02 class PayPal {
03
04 public function __construct() {
05 // Your Code here //
06 }
07
08 public function sendPayment($amount) {
09 // Paying via Paypal //
10 echo "Paying via PayPal: ". $amount;
11 }
12 }
13
14 $paypal = new PayPal();
15 $paypal->sendPayment('2629');
In the above code, you can see that we are utilizing a PayPal class to simply pay the
amount. Here, we are directly creating the object of the PayPal class and paying via
PayPal. You have this code scattered in multiple places. So we can see that the
code is using the $paypal->sendPayment('<amount here>'); method to pay.
Some time ago, PayPal changed the API method name from sendPayment to payAmount.
This should clearly indicate a problem for those of us who have been using
the sendPayment method. Specifically, we need to change all sendPayment method
calls to payAmount. Imagine the amount of code we need to change and the time we
need to spend on testing each of the features once again.

1. Rewrite the code in Java


2. Use Adapter Pattern to change $paypal->sendPayment('2629'); into $paypal-
>pay('2629'); and the code will never change even if Paypal changed their
API from payAmount to depositMoney, for instance.
3. Apart from Paypal, now we have a MoneyBooker class, which offers
payMoney(int) to perform online transactions. Extend your Adapter-based
program to handle this situation.

The Problem
01 <?php
02 class PayPal {
03
04 public function __construct() {
05 // Your Code here //
06 }
07
08 public function sendPayment($amount) {
09 // Paying via Paypal //
10 echo "Paying via PayPal: ". $amount;
11 }
12 }
13
$paypal = new PayPal();
14
$paypal->sendPayment('2629');
15

In the above code, you can see that we are utilizing a PayPal class to simply pay the
amount. Here, we are directly creating the object of the PayPal class and paying via
PayPal. You have this code scattered in multiple places. So we can see that the
code is using the $paypal->sendPayment('<amount here>'); method to pay.
Some time ago, PayPal changed the API method name from sendPayment to payAmount.
This should clearly indicate a problem for those of us who have been using
the sendPayment method. Specifically, we need to change all sendPayment method
calls to payAmount. Imagine the amount of code we need to change and the time we
need to spend on testing each of the features once again.

1. Rewrite the code in Java


2. Use Adapter Pattern to change $paypal->sendPayment('2629'); into $paypal-
>pay('2629'); and the code will never change even if Paypal changed their
API from payAmount to depositMoney, for instance.
3. Apart from Paypal, now we have a MoneyBooker class, which offers
payMoney(int) to perform online transactions. Extend your Adapter-based
program to handle this situation.

Solution:

1. Equivalent code in Java:

class PayPal {
public PayPal() {
// Your Code here //
}

public void sendPayment(String amount) {


// Paying via PayPal //
System.out.println("Paying via PayPal: " + amount);
}
}

public class Main {


public static void main(String[] args) {
PayPal paypal = new PayPal();
paypal.sendPayment("2629");
}
}

2. My solution is to define an Adapter class, inherited from an interface.


In this Adapter class, we will define method pay and call API sendPayment (or
payAmount, depositMoney). The code used from the client will never change, just
change the code inside Adapter class.

<?php

// Define the PayPal general interface (polymorphism purpose)


interface PaymentGateway {
public function pay($amount);
}

// PayPal adapter implementing the PaymentGateway interface


class PayPalAdapter implements PaymentGateway {
private $paypal;

public function __construct(PayPal $paypal) {


$this->paypal = $paypal;
}

public function pay($amount) {


// Adapt the method call to the new API
$this->paypal->sendPayment($amount);
}
}

// Existing PayPal class


class PayPal {
public function __construct() {
// Your Code here //
}

public function sendPayment($amount) {


// Paying via Paypal //
echo "Paying via PayPal: ". $amount;
}
}

// Usage
$paypal = new PayPal();
$paypalAdapter = new PayPalAdapter($paypal);
$paypalAdapter->pay('2629');

3. Extending Adapter-based program:

<?php
// Define the PayPal general interface (polymorphism purpose)
interface PaymentGateway {
public function pay($amount);
}

// PayPal adapter implementing the PaymentGateway interface


class PayPalAdapter implements PaymentGateway {
private $paypal;

public function __construct(PayPal $paypal) {


$this->paypal = $paypal;
}

public function pay($amount) {


// Adapt the method call to the PayPal API
$this->paypal->sendPayment($amount);
}
}

// MoneyBooker adapter implementing the PaymentGateway interface


class MoneyBookerAdapter implements PaymentGateway {
private $moneyBooker;

public function __construct(MoneyBooker $moneyBooker) {


$this->moneyBooker = $moneyBooker;
}

public function pay($amount) {


// Adapt the method call to the MoneyBooker API
$this->moneyBooker->payMoney($amount);
}
}

// Existing PayPal class


class PayPal {
public function __construct() {
// Your Code here //
}

public function sendPayment($amount) {


// Paying via Paypal //
echo "Paying via PayPal: ". $amount;
}
}

// MoneyBooker class
class MoneyBooker {
public function __construct() {
// Your Code here //
}

public function payMoney($amount) {


// Paying via MoneyBooker //
echo "Paying via MoneyBooker: ". $amount;
}
}

// Usage
$paypal = new PayPal();
$moneyBooker = new MoneyBooker();

$paypalAdapter = new PayPalAdapter($paypal);


$moneyBookerAdapter = new MoneyBookerAdapter($moneyBooker);

$paypalAdapter->pay('2629');
$moneyBookerAdapter->pay('1500');

 Java code for reference (if needed):


2.
// Define the PaymentGateway interface
interface PaymentGateway {
void pay(String amount);
}

// PayPal adapter implementing the PaymentGateway interface


class PayPalAdapter implements PaymentGateway {
private PayPal paypal;

public PayPalAdapter(PayPal paypal) {


this.paypal = paypal;
}

public void pay(String amount) {


// Adapt the method call to the new API
paypal.sendPayment(amount);
}
}

// Existing PayPal class


class PayPal {
public PayPal() {
// Your Code here //
}

public void sendPayment(String amount) {


// Paying via PayPal //
System.out.println("Paying via PayPal: " + amount);
}
}

// Usage
public class Main {
public static void main(String[] args) {
PayPal paypal = new PayPal();
PayPalAdapter paypalAdapter = new PayPalAdapter(paypal);
paypalAdapter.pay("2629");
}
}

3.
// Define the PaymentGateway interface
interface PaymentGateway {
void pay(String amount);
}

// PayPal adapter implementing the PaymentGateway interface


class PayPalAdapter implements PaymentGateway {
private PayPal paypal;

public PayPalAdapter(PayPal paypal) {


this.paypal = paypal;
}

public void pay(String amount) {


// Adapt the method call to the PayPal API
paypal.sendPayment(amount);
}
}

// MoneyBooker adapter implementing the PaymentGateway interface


class MoneyBookerAdapter implements PaymentGateway {
private MoneyBooker moneyBooker;

public MoneyBookerAdapter(MoneyBooker moneyBooker) {


this.moneyBooker = moneyBooker;
}

public void pay(String amount) {


// Adapt the method call to the MoneyBooker API
moneyBooker.payMoney(amount);
}
}

// Existing PayPal class


class PayPal {
public PayPal() {
// Your Code here //
}

public void sendPayment(String amount) {


// Paying via Paypal //
System.out.println("Paying via PayPal: " + amount);
}
}

// Existing MoneyBooker class


class MoneyBooker {
public MoneyBooker() {
// Your Code here //
}

public void payMoney(String amount) {


// Paying via MoneyBooker //
System.out.println("Paying via MoneyBooker: " + amount);
}
}

// Usage
public class Main {
public static void main(String[] args) {
PayPal paypal = new PayPal();
MoneyBooker moneyBooker = new MoneyBooker();

PayPalAdapter paypalAdapter = new PayPalAdapter(paypal);


MoneyBookerAdapter moneyBookerAdapter = new
MoneyBookerAdapter(moneyBooker);

paypalAdapter.pay("2629");
moneyBookerAdapter.pay("1500");
}
}

You might also like