Given the following grammar:
Derive and give the parse trees for:
For B = A + C
For C = (A * C) + B,
Given the following grammar:
For the grammar:
The denotational mappings are:
Compute weakest precondition
$ \frac{1 - a}{b} < 2$
$ \frac{12-x}{y} < 12 $
if x < 0 then
y = y - 1
else
y = y + 1
{y > 0}
$y > 1$
Find the regular expressions for:
Sketch out the top down parsing function for the rule $ T \to T * F $
T(){
T()
while next_token == '*'{
F()
}
}
Show the parse including the stack for id + id
Grammar:
The parsing table is:
Stack | Input | Action |
---|---|---|
0 | id + id\$ | S5 |
0 id 5 | + id \$ | R6 |
0 F 3 | + id \$ | R4 |
0 T 2 | + id \$ | R2 |
0 E 1 | + id \$ | S6 |
0 E 1 + 6 | id \$ | S5 |
0 E 1 + 6 id 5 | \$ | R6 |
0 E 1 + 6 F 3 | \$ | R4 |
0 E 1 + 6 T 9 | \$ | R1 |
0 E 1 | \$ | accept |
Show the parse including the stack for id * id
Grammar:
The parsing table is:
Stack | Input | Action |
---|---|---|
0 | id * id\$ | S5 |
0 id 5 | * id \$ | R6 |
0 F 3 | * id \$ | R4 |
0 T 2 | * id \$ | S7 |
0 T 2 * 7 | id \$ | S5 |
0 T 2 * 7 id 5 | \$ | R6 |
0 T 2 * 7 F 10 | \$ | R3 |
0 T 2 | \$ | R2 |
0 E 1 | \$ | accept |
What is the output of the following code? Given a short description in English as to what is being stored in the tables
funcs={}
for i=1,10 do
table.insert(funcs, function() return i*i end)
end
funcs[2]()
funcs[3]()
4
9
What is the output of this code and why?
function wrapper(a)
local function square(a)
return a^2
end
function cube(a)
return a^3
end
return square(a)
end
print(wrapper(2))
print(cube(2))
print(square(2))
4
8
ERROR
What feature is the following code showing, and what is it meant as a compromise to?
public interface Camera{
//functions here with no definition...
//ex:
//public void takePicture();
}
public interface MobilePhone{
//functions here with no definition...
//ex:
//public void makeCall();
}
public class CameraPhone implements Camera, MobilePhone{
//functions here...
}
This is showing using interfaces, which is a compromise to multiple inheritence
What technique is being shown below and what is the output of main?
class Point {
protected int x, y;
public Point() { this(0); }
public Point(int x0) { this(x0, 0); }
public Point(int x0, int y0) { x = x0; y = y0; }
public Point(Point p) { this(p.x, p.y); }
public int getX() { return x; }
public int getY() { return y; }
public int setX(int x0) { x = x0; }
public int setY(int y0) { y = y0; }
public void print() { System.out.println("Point"); }
}
public class Circle extends Point {
private int r;
public Circle(Point p) { this(p, 0); }
public Circle(Point p, int r0) { super(p); r = r0; }
public Circle() { this(0); }
public Circle(int x0) { this(x0, 0); }
public Circle(int x0, int y0) { this(x0, y0, 0); }
public Circle(int x0, int y0, int r0) { super(x0, y0); r = r0; }
public Circle(Circle c) { this(c.x, c.y, c.r); }
public int getR() { return r; }
public int setR(int r0) { r = r0; }
public void print() { System.out.println("Circle"); }
public static void main(String args[]) {
Point p = new Point();
Point c = new Circle();
p.print();
c.print();
}
}
Polymorphism is being shown, the output is
Point
Circle
Write a function that takes in an ArrayList or a LinkedList of any numeric type and returns the mean.
If time permits, finish Java example from previous lecture
Many of today's examples came from Rosetta Code