Question 1
Question 1
int f(int& n)
{
n++;
return n;
}
int X = 1;
X + f(X) + X; //1
Implement the program in case (i) short circuit Boolean is supported; and (i) short circuit
Boolean is not supported.
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
1 + f(X) + X
1+2+X
1+2+2
5
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
…
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:
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:
Write the regular expression for the language L over ∑ = {0, 1} such that all the string do
not contain the substring 01.
Solution:
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:
1. R = (011 + 1)*
4. a : n ≥ 0, n ≠ 4
n
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
In a regular expression, x + means one or more occurrence of x. It can generate {x, xx, xxx,
xxxx, .....}
1.
2.
3.
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
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:
G4:
F 🡪 num
1. Question 1
2. Question 2
3. Question 3
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:
G4:
SOLUTION:
1. C
2. D
3. B
4.
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
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:
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.
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.
Solution:
class PayPal {
public PayPal() {
// Your Code here //
}
<?php
// Usage
$paypal = new PayPal();
$paypalAdapter = new PayPalAdapter($paypal);
$paypalAdapter->pay('2629');
<?php
// Define the PayPal general interface (polymorphism purpose)
interface PaymentGateway {
public function pay($amount);
}
// MoneyBooker class
class MoneyBooker {
public function __construct() {
// Your Code here //
}
// Usage
$paypal = new PayPal();
$moneyBooker = new MoneyBooker();
$paypalAdapter->pay('2629');
$moneyBookerAdapter->pay('1500');
// 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);
}
// Usage
public class Main {
public static void main(String[] args) {
PayPal paypal = new PayPal();
MoneyBooker moneyBooker = new MoneyBooker();
paypalAdapter.pay("2629");
moneyBookerAdapter.pay("1500");
}
}