<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
shunting	B-Application
yard	I-Application
algorithm	I-Application
is	O
a	O
method	O
for	O
parsing	B-Language
arithmetical	O
or	O
logical	O
expressions	O
,	O
or	O
a	O
combination	O
of	O
both	O
,	O
specified	O
in	O
infix	O
notation	O
.	O
</s>
<s>
It	O
can	O
produce	O
either	O
a	O
postfix	O
notation	O
string	O
,	O
also	O
known	O
as	O
Reverse	O
Polish	O
notation	O
(	O
RPN	O
)	O
,	O
or	O
an	O
abstract	B-Data_Structure
syntax	I-Data_Structure
tree	I-Data_Structure
(	O
AST	O
)	O
.	O
</s>
<s>
Dijkstra	O
first	O
described	O
the	O
shunting	B-Application
yard	I-Application
algorithm	I-Application
in	O
the	O
Mathematisch	O
Centrum	O
report	O
.	O
</s>
<s>
Like	O
the	O
evaluation	O
of	O
RPN	O
,	O
the	O
shunting	B-Application
yard	I-Application
algorithm	I-Application
is	O
stack-based	O
.	O
</s>
<s>
There	O
is	O
also	O
a	O
stack	B-Application
that	O
holds	O
operators	O
not	O
yet	O
added	O
to	O
the	O
output	O
queue	B-Application
.	O
</s>
<s>
The	O
shunting	B-Application
yard	I-Application
algorithm	I-Application
will	O
correctly	O
parse	B-Language
all	O
valid	O
infix	O
expressions	O
,	O
but	O
does	O
not	O
reject	O
all	O
invalid	O
expressions	O
.	O
</s>
<s>
For	O
example	O
,	O
is	O
not	O
a	O
valid	O
infix	O
expression	O
,	O
but	O
would	O
be	O
parsed	B-Language
as	O
.	O
</s>
<s>
The	O
shunting	B-Application
yard	I-Application
algorithm	I-Application
was	O
later	O
generalized	O
into	O
operator-precedence	B-Application
parsing	I-Application
.	O
</s>
<s>
After	O
reading	O
the	O
expression	O
,	O
pop	O
the	O
operators	O
off	O
the	O
stack	B-Application
and	O
add	O
them	O
to	O
the	O
output	O
.	O
</s>
<s>
At	O
the	O
end	O
of	O
reading	O
the	O
expression	O
,	O
pop	O
all	O
operators	O
off	O
the	O
stack	B-Application
and	O
onto	O
the	O
output	O
.	O
</s>
<s>
If	O
the	O
symbol	O
is	O
an	O
operator	O
,	O
it	O
is	O
pushed	O
onto	O
the	O
operator	O
stack	B-Application
b	O
)	O
,	O
d	O
)	O
,	O
f	O
)	O
.	O
</s>
<s>
If	O
the	O
operator	O
's	O
precedence	O
is	O
lower	O
than	O
that	O
of	O
the	O
operators	O
at	O
the	O
top	O
of	O
the	O
stack	B-Application
or	O
the	O
precedences	O
are	O
equal	O
and	O
the	O
operator	O
is	O
left	O
associative	O
,	O
then	O
that	O
operator	O
is	O
popped	O
off	O
the	O
stack	B-Application
and	O
added	O
to	O
the	O
output	O
g	O
)	O
.	O
</s>
<s>
Finally	O
,	O
any	O
remaining	O
operators	O
are	O
popped	O
off	O
the	O
stack	B-Application
and	O
added	O
to	O
the	O
output	O
i	O
)	O
.	O
</s>
<s>
there	O
is	O
an	O
operator	O
o2	O
at	O
the	O
top	O
of	O
the	O
operator	O
stack	B-Application
which	O
is	O
not	O
a	O
left	O
parenthesis	O
,	O
</s>
<s>
while	O
the	O
operator	O
at	O
the	O
top	O
of	O
the	O
operator	O
stack	B-Application
is	O
not	O
a	O
left	O
parenthesis	O
:	O
</s>
<s>
while	O
the	O
operator	O
at	O
the	O
top	O
of	O
the	O
operator	O
stack	B-Application
is	O
not	O
a	O
left	O
parenthesis	O
:	O
</s>
<s>
if	O
there	O
is	O
a	O
function	O
token	O
at	O
the	O
top	O
of	O
the	O
operator	O
stack	B-Application
,	O
then	O
:	O
</s>
<s>
while	O
there	O
are	O
tokens	O
on	O
the	O
operator	O
stack	B-Application
:	O
</s>
<s>
To	O
analyze	O
the	O
running	O
time	O
complexity	O
of	O
this	O
algorithm	O
,	O
one	O
has	O
only	O
to	O
note	O
that	O
each	O
token	O
will	O
be	O
read	O
once	O
,	O
each	O
number	O
,	O
function	O
,	O
or	O
operator	O
will	O
be	O
printed	O
once	O
,	O
and	O
each	O
function	O
,	O
operator	O
,	O
or	O
parenthesis	O
will	O
be	O
pushed	O
onto	O
the	O
stack	B-Application
and	O
popped	O
off	O
the	O
stack	B-Application
once	O
—	O
therefore	O
,	O
there	O
are	O
at	O
most	O
a	O
constant	O
number	O
of	O
operations	O
executed	O
per	O
token	O
,	O
and	O
the	O
running	O
time	O
is	O
thus	O
O(n )	O
—	O
linear	O
in	O
the	O
size	O
of	O
the	O
input	O
.	O
</s>
<s>
The	O
shunting	B-Application
yard	I-Application
algorithm	I-Application
can	O
also	O
be	O
applied	O
to	O
produce	O
prefix	O
notation	O
(	O
also	O
known	O
as	O
Polish	O
notation	O
)	O
.	O
</s>
<s>
To	O
do	O
this	O
one	O
would	O
simply	O
start	O
from	O
the	O
end	O
of	O
a	O
string	O
of	O
tokens	O
to	O
be	O
parsed	B-Language
and	O
work	O
backwards	O
,	O
reverse	O
the	O
output	O
queue	B-Application
(	O
therefore	O
making	O
the	O
output	O
queue	B-Application
an	O
output	O
stack	B-Application
)	O
,	O
and	O
flip	O
the	O
left	O
and	O
right	O
parenthesis	O
behavior	O
(	O
remembering	O
that	O
the	O
now-left	O
parenthesis	O
behavior	O
should	O
pop	O
until	O
it	O
finds	O
a	O
now-right	O
parenthesis	O
)	O
.	O
</s>
