Just Problems 1, 2, 3, and 4 right now. I'll post answers to the remaining ones later.
%!PS-Adobe-2.0
/r 100 def
/wedge {
newpath
0 0 moveto
r -15 sin r mul lineto
r 0 15 sin r mul -90 90 arc
closepath
} def
/pie {
1 1 12 {
12 div setgray
gsave wedge
gsave fill grestore
0 setgray stroke
grestore
30 rotate
} for
} def
3 setlinewidth
pie
showpage
This program prints the picture shown on the right centered at the (0, 0) coordinate of the page, so that 3/4 of the picture is off the page.
x y r ang1 ang2 arc % append counter-clockwise arc, center (x,y), radius r, % from angle ang1 to ang2; stack empty afterward.)The program was adapted from a program in the "Blue" book (page 135). The book's program used a much trickier and more sophisticated wedge function, so I made it more basic, less tricky. The book also drew the picture first at size 1 pt (1/72 inch) and then scaled it up a great deal.
So wdege here draws a line from (0,0) to (r, r*sin(-15)) (the bottom line of the wedge). Then it draws a half-circle with center (r,0) and radius r*sin(15), from -90 degrees to 90 degrees. This finishes the curved part. Finally closepath finishes up the upper line.
The for loop: 1 1 12 { }for just repeats what is in {} starting with a loop variable equal 1, incrementing by 1 each time, until it gets to 12. This loop provides the variable on the stack at the start of the {} part.
Using the loop variable on the stack (successively 1, 2, 3, ..., 11, 12), the code "12 div setgray" produces grey levels from 1/12 up to 12/12 = 1, that is, from nearly black to white.
The following code works:
8.5 72 mul 2 div 11 72 mul 2 div translate % 306 391 translate
3 3 scale
15 rotate
You can rearrange the three lines to some extent, but you would
need a different translation if you tried to do it after the scale.
(Actually, three times as big is too big to fit on the page.)
P ----> E #
E ----> E + E
E ----> E * E
E ----> ( E ) | a | b | c
Terminals are # + * ( ) a b c
Non-terminals are P E
The sentence in part a has a unique parse tree, and thus does not demonstrate the ambiguity of this grammar. However the grammar is ambiguous. The easiest way to prove this is to display sentences that have more than one parse tree (or more than one leftmost derviation). Examples include:
a + b * c (ambiguity of operator precedence) a + b + c (ambiguity of operator accociativity)
This is an NFA because there are two transitions labeled with a ".".
As a regular expressions, we can write:
d+\.d+ | d*\.d+ | d+\.d*
In English, any sequence of digits, followed by a decimal
point, followed by any sequence of digits, except
that there must be at least one digit either before or
after the decimal point.
In each case, the function returns an integer which gives a memory address where the result of the subexpression recognized by the function will be kept at run time.
Only a single LIT quad is generated, which puts the constant 47 into a temporary memory location (generated for this purpose). The memory location is returned as an int.
Just look up rate in the symbol table, insert it if it's not there, and return its memory location. (No quads generated.) (Note: the above said "translating arithmetic expressions". If the variable is on the right side of an assignment, then you will use the memory location returned to write and ASG quad.)