首页 » css代写 » 操作系统代写 | COMP3021 (Spring 2021) Programming Language Paradigms

操作系统代写 | COMP3021 (Spring 2021) Programming Language Paradigms

本次香港代写主要为操作系统限时测试

Question 1    Scope and Lifetime [50 marks]

 

1(a)  [30 marks]

Variable Lifetime Scope
i at line 1 Static:

entire program execution

Global
j at line 4 Static

entire program execution

Local in function funF(), lines 5-9
k at line 5 Stack-dynamic:
from the declaration statement to the termination of funF()
Local in function funF(), lines 6-9
ptr at line 6 Stack-dynamic:

from the declaration statement to the termination of funF()

Local in function funF(), lines 7-9
The anonymous variable allocated (by malloc) at line 7 Heap-dynamic:

from the malloc statement to the deallocation statement

 Any statement with access to that variable’s address
OR

Lines 7-8 (the access to that variable’s address is lost at line 9)

Marking scheme:   2 marks per cell

 

1(b) [20 marks]

 

Example code:

void fun() {

static int counter = 0;

counter ++;

printf(“%d\n”, counter);

}

Explanation: In the above function, the static local variable counter is used to collect usage statistics, i.e., count how many times that function is called.

 

Marking scheme: 8 marks for the code, 12 points for explanation.
We accept any meaningful code example.

 

Question 2    BNF Grammar                                          [50 marks]

 

intval denotes the token for integer literal (e.g., 12345).

+ denotes the addition operator, and ´ denotes the multiplication operator.

Consider the following BNF grammar:

<expr> à intval  |  + <expr> <expr> | ´ <expr> <expr>

 

2(a)  [35 marks]

 

Marking scheme:   deduct 5 marks per each missing/incorrect tree node

 

 

2(b)  [15 marks]

 

Marking scheme:

  • 15 marks for a rigorous proof
  • 9 marks for a proof with some minor mistakes or missing arguments
  • 3 marks for the unique parse tree idea

 

This grammar is unambiguous.

 

We call a sequence of tokens to be a valid expression if it can match with the given grammar. To show that this grammar is unambiguous, we need to prove that,

given any valid expression E, there is only one parse tree of E.

 

Case I:     E has exactly 0 operators.

In this case, E must be an integer literal; otherwise, E is not a valid expression.

It is trivial that there is only one parse tree of E.

 

Case II:    E has exactly 1 operator.

In this case, E must be an operator (+ or ´) followed by two integer literals;
otherwise, E is not a valid expression.

It is trivial that there is only one parse tree of E.

 

Case III:   E has more than 1 operators.

In the given grammar, each operator (+ or ´) takes its operands on its right-hand-side only, but not on its left-hand-side. Thus, we will examine operators in E one-by-one from right to left, and show how to construct a unique parse tree. We name operators in E from left to right as opn, opn-1, …, op2, op1, where n denotes the number of operators in E.

Initially, we convert each integer literal in E to an expression (say, Ex), which corresponds to a unique subtree according to Case I.

Next, we examine operators in the order (op1, op2, …, opn-1, opn), as discussed above.

When we evaluate an operator opi, there is only one way to get its operands (i.e., the next two expressions on the right-hand-side). Then, we replace opi and its operands by an expression (say, Ei‘), which corresponds to a unique subtree according to Case II.

By repeating the above procedure, the expression E will be converted to a unique tree.


程序辅导定制C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持 Alipay WeChatPay PayPal等支付方式

E-mail: vipdue@outlook.com  微信号:vipnxx


如果您使用手机请先保存二维码,微信识别。如果用电脑,直接掏出手机果断扫描。

blank