James Schofield
2004-08-01 04:46:30 UTC
I thought I'd post my solutions for the S02 practice final. I haven't
had a chance to verify these with anyone else, but I hope they're mostly
correct. Feel free to post any corrections :)
1a)
addi $30, $30, -4
sw $4, 0($30)
jal mystery
addi $30, $30, 4
add $6, $0, $1
1b)
add $4, $0, $9
sll $9, $9, 2
add $9, $9, $3
lw $9, 0($9)
2a)
Define: I = ii(ii)* ; M = m(mm)* ; L = lambda
Then, the RE is: (M|L)(IM)*(I|L)
2b)
Transitions (start state, input character, end state):
A,x,B
A,y,D
B,z,B
B,y,C
D,y,C
D,z,BD
BD,z,BD
BD,y,c
Accepting states: A and C
2c)
<S> -> <A><S>
<S> ->
<A> -> <B><C>
<B> -> x<B>
<B> ->
<C> -> y
<C> -> z
3a)
Show two distinct parse trees to represent the same input.
3b)
<E> -> <F>o<E>
<E> -> <F>
<F> -> <F>^<G>
<F> -> <G>
<G> -> 1
<G> -> 2
4a)
NT N? First Follow t v x y z EOF
<S> n tvxy EOF 1 2 1 1 E E
<A> y x tyz 3 E 4 3 3 E
<B> y yz t 6 E E 5 6 E
<C> y z t 7 E E E 8 E
<D> y xy t 8 E 8 8 8 E
4b)
<S> -> a<A>
<A> -> a<B>
<A> -> <B>
<B> -> c<C>
<C> -> c
<C> ->
4c)
<S> -> c<A>
<A> -> ab<A>
<A> ->
5a)
I don't think we covered this this term...
5b)
Pushes the symbol in "next" onto the stack; reads the next input symbol
into "next".
5c)
Pops the right-hand side of a rule off the stack, and pushes on the
left-hand side of the rule.
5d)
No, it would not be possible to construct the LR(1) DFA if the CFG was
ambiguous.
6)
Notes about my implementation: I use $1, $2, and $4 as scratch registers
(i.e. I don't worry about backing them up). <Expr> always leaves its
result in $1.
Attributes:
<Stmts>.code String Synthetic
ID.name {A,B} Synthetic
<Stmt>.code String Synthetic
<Expr>.code String Synthetic
<Stmts>1 -> <Stmts>2 <Stmt> SEMI
<Stmts>1.code = <Stmts>2.code + <Stmt>.code
<Stmts> ->
<Stmts>.code = ".word A\n" +
".word B\n"
<Stmt> -> IN ID
<Stmt>.code = "\ttrap 5\n" +
"\tsw $2, 0(" + ID.name + ")\n"
<Stmt> -> OUT <Expr>
<Stmt>.code = <Expr>.code +
"\tadd $4, $1, $0\n" +
"\ttrap 1\n"
<Stmt> -> ID ASSIGN <Expr>
<Stmt>.code = <Expr>.code +
"\tsw $1, 0(" + ID.name + ")\n"
<Expr>1 -> <Expr>2 MULOP ID
<Expr>1.code = <Expr>2.code +
"lw $2, 0(" + ID.name + ")\n" +
"mult $1, $2\n" +
"mflo $1\n"
<Expr> -> ID
<Expr>.code = "lw $1, 0(" + ID.name + ")\n"
7a) 55
7b) 11EF
7c) 1100 0001
7d) 1011 1111
8)
Address Hex
00000000 6007001C
00000004 ACE90004
00000008 2331FFF6
0000000C 02979820
00000010 94130000
00000014 0BFFFFFC
00000018 03E00008
0000001C FFFFFFF1
Relocation:
LLO, -, 00000000
Imports:
OFFSET16, 00000010, on
Exports:
im, 00000000
x, 00000018
9a) 0
9b) ((1 2) (1 2 3 4))
9c) ("scheme" "is" "fun") [ha :P]
9d) 35
9e) ((1 2) ((1 2 3 4)))
10)
(define parse
(lambda (in)
(cond
((null? in) #t)
((equal? (car in) 'y) (parse (cdr in)))
((null? (cdr in)) #f)
((and (equal? (car in) 'x) (equal? (cadr in) 'x)) (parse (cddr in)))
(else #f)
)))
11)
(define iterate
(lambda (proc n)
(lambda (x)
(letrec
((do-iterate
(lambda (proc n x)
(if (> n 0)
(proc (do-iterate proc (- n 1) x))
x
))))
(do-iterate proc n x)
))))
12)
(define M
(lambda (n)
(letrec
((M-it
(lambda (i x3 x2 x1)
(if (> i 0)
(M-it (- i 1) x2 x1 (+ x1 (* 2 x3)))
x3
))))
(M-it n 0 1 3)
)))
had a chance to verify these with anyone else, but I hope they're mostly
correct. Feel free to post any corrections :)
1a)
addi $30, $30, -4
sw $4, 0($30)
jal mystery
addi $30, $30, 4
add $6, $0, $1
1b)
add $4, $0, $9
sll $9, $9, 2
add $9, $9, $3
lw $9, 0($9)
2a)
Define: I = ii(ii)* ; M = m(mm)* ; L = lambda
Then, the RE is: (M|L)(IM)*(I|L)
2b)
Transitions (start state, input character, end state):
A,x,B
A,y,D
B,z,B
B,y,C
D,y,C
D,z,BD
BD,z,BD
BD,y,c
Accepting states: A and C
2c)
<S> -> <A><S>
<S> ->
<A> -> <B><C>
<B> -> x<B>
<B> ->
<C> -> y
<C> -> z
3a)
Show two distinct parse trees to represent the same input.
3b)
<E> -> <F>o<E>
<E> -> <F>
<F> -> <F>^<G>
<F> -> <G>
<G> -> 1
<G> -> 2
4a)
NT N? First Follow t v x y z EOF
<S> n tvxy EOF 1 2 1 1 E E
<A> y x tyz 3 E 4 3 3 E
<B> y yz t 6 E E 5 6 E
<C> y z t 7 E E E 8 E
<D> y xy t 8 E 8 8 8 E
4b)
<S> -> a<A>
<A> -> a<B>
<A> -> <B>
<B> -> c<C>
<C> -> c
<C> ->
4c)
<S> -> c<A>
<A> -> ab<A>
<A> ->
5a)
I don't think we covered this this term...
5b)
Pushes the symbol in "next" onto the stack; reads the next input symbol
into "next".
5c)
Pops the right-hand side of a rule off the stack, and pushes on the
left-hand side of the rule.
5d)
No, it would not be possible to construct the LR(1) DFA if the CFG was
ambiguous.
6)
Notes about my implementation: I use $1, $2, and $4 as scratch registers
(i.e. I don't worry about backing them up). <Expr> always leaves its
result in $1.
Attributes:
<Stmts>.code String Synthetic
ID.name {A,B} Synthetic
<Stmt>.code String Synthetic
<Expr>.code String Synthetic
<Stmts>1 -> <Stmts>2 <Stmt> SEMI
<Stmts>1.code = <Stmts>2.code + <Stmt>.code
<Stmts> ->
<Stmts>.code = ".word A\n" +
".word B\n"
<Stmt> -> IN ID
<Stmt>.code = "\ttrap 5\n" +
"\tsw $2, 0(" + ID.name + ")\n"
<Stmt> -> OUT <Expr>
<Stmt>.code = <Expr>.code +
"\tadd $4, $1, $0\n" +
"\ttrap 1\n"
<Stmt> -> ID ASSIGN <Expr>
<Stmt>.code = <Expr>.code +
"\tsw $1, 0(" + ID.name + ")\n"
<Expr>1 -> <Expr>2 MULOP ID
<Expr>1.code = <Expr>2.code +
"lw $2, 0(" + ID.name + ")\n" +
"mult $1, $2\n" +
"mflo $1\n"
<Expr> -> ID
<Expr>.code = "lw $1, 0(" + ID.name + ")\n"
7a) 55
7b) 11EF
7c) 1100 0001
7d) 1011 1111
8)
Address Hex
00000000 6007001C
00000004 ACE90004
00000008 2331FFF6
0000000C 02979820
00000010 94130000
00000014 0BFFFFFC
00000018 03E00008
0000001C FFFFFFF1
Relocation:
LLO, -, 00000000
Imports:
OFFSET16, 00000010, on
Exports:
im, 00000000
x, 00000018
9a) 0
9b) ((1 2) (1 2 3 4))
9c) ("scheme" "is" "fun") [ha :P]
9d) 35
9e) ((1 2) ((1 2 3 4)))
10)
(define parse
(lambda (in)
(cond
((null? in) #t)
((equal? (car in) 'y) (parse (cdr in)))
((null? (cdr in)) #f)
((and (equal? (car in) 'x) (equal? (cadr in) 'x)) (parse (cddr in)))
(else #f)
)))
11)
(define iterate
(lambda (proc n)
(lambda (x)
(letrec
((do-iterate
(lambda (proc n x)
(if (> n 0)
(proc (do-iterate proc (- n 1) x))
x
))))
(do-iterate proc n x)
))))
12)
(define M
(lambda (n)
(letrec
((M-it
(lambda (i x3 x2 x1)
(if (> i 0)
(M-it (- i 1) x2 x1 (+ x1 (* 2 x3)))
x3
))))
(M-it n 0 1 3)
)))