pic Specification
Concrete Syntax of pic
A pic
program is a sequence
of <element>
s. The concrete syntax of the subset of pic
you are to use for
this assignment is given in the following grammar:
<element> ::= <shape> <attributes> ;
| <direction> ;
<shape> ::= box | circle | move | arrow | line
<direction> ::= right | left | up | down
<attributes> ::= <string> <attributes>
| <direction> <attributes>
| <empty>
In this grammar, <string>
represents a string constant contained in double
quotes. The <attributes>
can be a (possibly <empty>
) list, where each
elements of the list is either a <string>
or a <direction>
.
For example, Figure 1 shows the concrete syntax tree for
this pic
command, which appears in the diagram.pic
program from lab:
arrow "step 3" right down;
Semantics of pic
Current position and direction
At any given point in a pic
program, there is a current position (on the page)
and a current direction (left, right, up, or down).
By themselves, the commands left;
and right;
and up;
and down;
just change the
current direction.
Shapes: Drawing + updating position
The box
and circle
commands draw a box or circle, respectively, adjacent to
the current position. Exactly how the shape is positioned depends on the current
direction. If the current direction is “right”, then the middle of the left side
of the box is placed at the current position (so that the box is to the right
of the current position); similarly, if the current direction is “down” then the
middle of the top edge of the box is placed at the current position (so that the
box is below the current position). After the shape is drawn, the current
position is changed to the opposite side of the shape.
The line
and arrow
commands draw a line or arrow, respectively, starting at the
current position and extending (by default) in the current direction. The current position
is then changed to the far end of the new line or arrow. For the arrow
command , the
“far end” is the tip of arrow’s triangle, and the line for the arrow ends at the base of
the arrow’s triangle. The move
command changes the current position just like line
,
though no line is actually drawn.
Attributes
Some commands can have a list of “attributes” that modify the command. In our limited subset of the language, the only attributes are strings or directions.
Shape commands (including the move
) can have string attributes.
The string attributes are drawn as text, one line per
string, centered at the geometric center of the shape being drawn.
The line
, move
, and arrow
commands additionally use their direction
attributes to override the current direction, so the command move right
is
the same as the sequence right; move
. More interestingly, multiple direction
attributes are additive, thus, line right down
draws a diagonal line right and
down, while line right right
draws a line twice as far to the right as normal.
If line
, move
, or arrow
has at least one direction attribute, the current direction
afterwards will be the last of the direction attributes (down and
right respectively in the previous two examples).
If line
, move
, or arrow
have any direction attributes, those attributes replace
the existing direction: When computing the net result of the specified directions, pic
ignores the existing direction and does not add it to the attributes.
pic
ignores any direction attributes for box
and circle
.