---
# System prepended metadata

title: SPO Lecture 9 Exercise

---

# SPO Lecture 9 Exercise 

## Individual Exercises

### Exercise 3

#### Exercise 3 (10)


![](https://i.imgur.com/aJTxCej.png)

![](https://i.imgur.com/MgxbWgo.png)

```
Start -> Stmt $
Stmt  -> id assign E
        | if lparen E rparen Stmt else Stmt fi
        | if lparen E rparen Stmt fi
        | while lparen E rparen do Stmt od
        | begin Stmts end
Stmts -> Stmts semi Stt
        | Stmt
E     -> E plus T
        | T
T     -> id
        | num
```

```
Start -> Stmt $
Stmt  -> id assign E
        | if lparen E rparen Stmt else Stmt fi
        | if lparen E rparen Stmt fi
        | while lparen E rparen do Stmt od
        | begin Stmts end
Stmts -> Stmts semi Stt
        | Stmt
E     -> E plus F
        | E minus F
        | F
F     -> F times T
        | T
T     -> id
        | num
        | minus T
```

```
Start -> Stmt_ast $
    return(ast);
    
Stmt_result ->
          id_var assign E_expr
            result <- MakeFamily(assign, var, expr)  
        | if lparen E_p rparen Stmt_s1 else Stmt_s2 fi
            result <- MakeFamily(if, p, s1, s2)
        | if lparen E_p rparen Stmt_s1 fi
            result <- MakeFamily(if, p, s1, MakeNode())
        | while lparen E_p rparen do Stmt_s1 od
            result <- MakeFamily(while, p, s1)
        | begin Stmts_s1 end
            result <- MakeFamily(block, s1)
            
Stmts_result ->
          Stmts_sofar semi Stmt_next
            result <- sofar.MakeSiblings(next)
        | Stmt_only
            result <- only
            
E_result ->
          E_a plus F_b
            result <- MakeFamily(add, a, b)
        | E_a minus F_b
            result <- MakeFamily(subtract, a, b)
        | F_a
            result <- a
            
F_result -> F_a times T_b
            result <- MakeFamily(mult, a, b)
        | T_a
            result <- a
            
T_result ->
          id_var
            result <- MakeNode(var)
        | num_val
            result <- MakeNode(val)
        | minus T_a
            result <- MakeFamily(neg, a)
```

#### Exercise 3 (12)

![](https://i.imgur.com/vsVeHr0.png)


```
Start -> List_avg $

List_result -> lparen Operands_ops rparen
            Print(count)
            | num_val
            
Operands -> Operands List
            count <- count + 1
            | List
            count <- 1
```

```
Start -> List_avg $

List_result -> lparen Operands_opscount rparen
            Print(opscount)
            | num_val
            
Operands_count -> Operands_subcount List
            count <- subcount + 1
            | List
            count <- 1
```


#### Exercise 3 (22)


![](https://i.imgur.com/tZrQgmG.png)


## Group Exercises

### Exercise 1

#### Exercise 1 (20)

![](https://i.imgur.com/J69jNxV.png)

![](https://i.imgur.com/gezpu9s.png)

Advantages:

- It is easy to read what every node does

Disadvantages:

- 
- 

#### Exercise 1 (19)
![](https://i.imgur.com/OYhx5Ro.png)

![](https://i.imgur.com/daDRpdK.png)

```
class Node
    Node parent
    Node leftmostSib, rightSib
    Node leftmostChild
    constructor(parent):
        this.parent = parent;
    end
end
    
```

#### Exercise 1 (21)
![](https://i.imgur.com/UQd5pG4.png)


