> ;=:q bjbjt+t+ %2AA]$L********$*****&***&&&****&p&*pk&CIS 324: Language Design and Implementation
Operator Precedence Parsing
1. Precedence Relations
Bottom-up parsers for a large class of context-free grammars
can be easily developed using operator grammars.
Operator grammars have the property that no production right side
is empty or has two adjacent nonterminals. This property enables the
implementation of efficient operator-precedence parsers. These
parser rely on the following three precedence relations:
RelationMeaninga < b a yields precedence to ba = b a has the same precedence as ba > b a takes precedence over bThese operator precedence relations allow to delimit the handles
in the right sentential forms: < marks the left end, = appears in
the interior of the handle, and > marks the right end.
Let assume that between the symbols ai and ai+1 there is exactly
one precedence relation. Suppose that $ is the end of the string.
Then for all terminals we can write: $ < b and b > $. If we
remove all nonterminals and place the correct precedence relation:
<, =, > between the remaining terminals, there remain strings
that can be analyzed by easily developed parser.
For example, the following operator precedence relations can
be introduced for simple expressions:
id+*$id>>>+<><>*<>>>$<<<>
Example: The input string:
id1 + id2 * id3
after inserting precedence relations becomes
$ < id1 > + < id2 > * < id3 > $
Having precedence relations allows to identify handles as follows:
- scan the string from left until seeing >
- scan backwards the string from right to left until seeing <
- everything between the two relations < and > forms the handle
Note that not the entire sentential form is scanned to find the handle.
2. Operator Precedence Parsing Algorithm
Initialize: Set ip to point to the first symbol of w$
Repeat: Let X be the top stack symbol, and a the symbol pointed to by ip
if $ is on the top of the stack and ip points to $ then return
else
Let a be the top terminal on the stack, and b the symbol pointed to by ip
if a < b or a = b then
push b onto the stack
advance ip to the next input symbol
else if a > b then
repeat
pop the stack
until the top stack terminal is related by <
to the terminal most recently popped
else error()
end
3. Making Operator Precedence Relations
The operator precedence parsers usually do not store the precedence
table with the relations, rather they are implemented in a special way.
Operator precedence parsers use precedence functions that map
terminal symbols to integers, and so the precedence relations
between the symbols are implemented by numerical comparison.
Not every table of precedence relations has precedence functions
but in practice for most grammars such functions can be designed.
Algorithm for Constructing Precedence Functions
1. Create functions fa for each grammar terminal a and for the
end of string symbol;
2. Partition the symbols in groups so that fa and gb are in the
same group if a = b ( there can be symbols in the same group
even if they are not connected by this relation);
3. Create a directed graph whose nodes are in the groups, next for each
symbols a and b do: place an edge from the group of gb to the group
of fa if a < b, otherwise if a > b place an edge from the group of
fa to that of gb;
4. If the constructed graph has a cycle then no precedence functions
exist. When there are no cycles collect the length of the longest
paths from the groups of fa and gb respectively.
Example: Consider the above table
id+*$id>>>+<><>*<>>>$<<<>
Using the algorithm leads to the following graph:
from which we extract the following precedence functions:
id+*$f4240g5130
,./KLMef}689:@D[]^ CDEJKN*,.024 ! " $ 5CJ CJ(H*6CJ,CJ,CJ,OJQJCJ6CJ(5CJ(CJ(5CJ<CJ
56CJR,-./KLMefa-$$l0b+$$
$
h8$ ,-./KLMefa89B]^a*l " % ( ) + . 1 4 7 8 : = @ C F G H J n ~
F
H
BC| o
c89B]^a*l ɨɔ-$$l0b+$$$ " % ( ) + . 1 4 7 8 : 8<<<$$l,P$$$ % ' + - . 0 1 3 4 6 : < = ? @ B C E J Q S n p q t v w | } ~
C
E
G
H
BCMTVyz5CJ$6CJ$CJ5CJ,CJCJ CJ H*5CJ CJ$6CJ(CJ,CJ(T: = @ C F G H J n ~
F
H
$$l,P$$BC| o
M
{
P
h8J
<
h8J
x
h8$ )*QRlntvxz}~
%
J
L
{
OPQVWXwx|~`abwxy}CJ(H*6CJ(5CJ,CJCJ(5CJ$EHCJ$EH6CJ$5CJ$CJ$U
M
{
PQ
P"#e(p\.HI
]PQ
P"#e(p\$$yz{FGHI6CJ
jCJ(UCJ5CJCJCJ(CJ(H*6CJ(8<<<$$l$ , $$.HI,,$$l$ , $$$$l$ , #&P 0A .!p"#$% Dd+^
c:Afig6-1.wmf2
0p) w:DHp!8eK'sJY0p) w: (/xX}L>b]XHTCF7GZ,.8
?"VC08uXCj+cҍЭӲ\?ݛ<9>=}_2_7fI31@au&댒l#_G;љv`PE6EtXDs]<]s;GD;AF!jZE)K:O(6yNf`+ c}Ж!uQu+uF"#&+D1"wEzztڜyT
*;Ãh/#Ǟu٭.otnwq:?WǏ|,0q̷n5[a2PԬ.
ʐ)K
)G6P%#h+ЯDൂ\ECNqsL#^p><NP~ѯ5#ol,R,Kq%r˔I$wR¸k\EG\NS<6LO/XPٲ~];k;kX]4Q(FIMx{?jPZ4&~t-7_?*">zEiA)Ò6LO/XP5dVI(j/qDqIdHkX3ֲzi~mXX5cp#xk* KAeWB9(^9,hsB<\;CVf6ښbmCbu&K!k,I|yZ#h+ЯDൂY0^9^G~ ?jҼ] 漆f?;TqI%uI5R4"rk)r}Wa^Mе@Do:~tG
tF?q7?:;8ljkY_wa핻߅otgmk#7J
I*<*ءhCi@=Qh!uqNַsoU<z8o
lv"Vxn 2䰹/e*I9k\lMN`λ=@ PXpB{9lp>ј__n]EC54Lp@#tGZlہ@1Ƽ^pKmr\}J
eP-aZ(um]n`Pe;04G=6W:mO\(uh$k)&Sut<ɒn6W
ʖԽ~UOz&Gh~J3kx,i(UiuJӯo~99C|}o\x?w9}H#@$2PT#v38Мr7·B=o_#'ypOS$}4Vhv@*Ҁ.f1K෮10b`hj{g4ۜ(X>8sr:X3-3?2k"e^ÞtM&IL6xn{ڿd~:@I3Ut:{K5RQoQzDzDГCKz헋^L~B?4!=ZѴ@GQ`?
xt%_D$(/)
[$@$NormalmH <A@<Default Paragraph Font2$
: P
8DCETVlnOQVX`
b
w
y
y{aoad5<lpcinq KOOVtv
QTim,
3
t
v
`eJNNikolay NikolaevC:\old_d\NIK\CIS324\324#6-2.docNikolay Nikolaev0C:\windows\TEMP\AutoRecovery save of 324#6-2.asdNikolay Nikolaev0C:\windows\TEMP\AutoRecovery save of 324#6-2.asd@-.1t--,@G:Times New Roman5Symbol3&:Arial?5 :Courier New"qh[fg[fDC$20dOperator Precedence ParsingNikolay NikolaevNikolay Nikolaev
Oh+'0 (4
P\
ht|Operator Precedence ParsingperNikolay Nikolaevce ikoikoNormal Nikolay Nikolaevce 128Microsoft Word 8.0 @ @@gC
՜.+,D՜.+,Php
Goldsmiths1Operator Precedence ParsingTitle(RZ
_PID_GUID_PID_HLINKSAN{A3320654-5C85-11D8-A610-000102ADC71F}APoFfig6-1.wmf
!#$%&'()+,-./013456789<Root Entry Fdpk>Data
1Table"WordDocument%2SummaryInformation(*DocumentSummaryInformation82CompObjjObjectPoolpkpk
FMicrosoft Word Document
MSWordDocWord.Document.89q