attribute grammars and xml workshop on attribute grammars, xml, and rdf institute for media and...
TRANSCRIPT
Attribute Grammars and XML
Workshop on Attribute Grammars, XML, and RDF
Institute for Media and Communications Management
University of St. Gallen
20/21 Sept. 2000
Harold Boley, DFKI Kaiserslautern
2
“What Are” and “How Relate”Attribute Grammars and XML?
• Both are extensions of context-free grammars:– Attribute grammars via semantic (evaluation) rules
over attributes– XML via (document-markup) tags, attribute DTDs,
stylesheets, etc.
• Will be compared on two exemplary levels:– Decorated parse trees as attributed element trees– XML markups for semantic rules
3
Attribute Grammar Example:Cartesian Move Sequences
• In the Cartesian plane move stepwise in the directions north, east, south, west
• Starting from the coordinates X = 0, Y = 0 compute the end position after a finite sequence of moves
• Sample computations:– Sequence e,e,n shall compute X = 2, Y = 1– Sequence s,w,s,e shall compute X = 0, Y = -2
4
Grammar with Attributes on Nonterminals
Grammar: path path step pathstep nstep estep sstep w
Attributes: path.Ex x-coordinate of end positionpath.Ey y-coordinate of end positionstep .Dx incremental change of x-coordinatestep .Dy incremental change of y-coordinate
We will only consider synthesized attributes,often prefixed by a “”, not inherited attributes
5
Attribute Grammar with Semantic Rules
Grammar:
path
path step path
step nstep estep sstep w
Semantic Rules:
path.Ex := 0 path.Ey := 0
path1.Ex := step .Dx + path2.Ex path1.Ey := step .Dy + path2.Ey
step .Dx := 0 step .Dy := 1step .Dx := 1 step .Dy := 0step .Dx := 0 step .Dy := -1step .Dx := -1 step .Dy := 0
6
Decorated Parse Treefor Sequence e,e,n
path
pathstep
pathstep
pathstep
e
n
e
Ex = 0 Ey = 0
Ex = 0 Ey = 1
Ex = 1 Ey = 1
Ex = 2 Ey = 1
Dx = 0 Dy = 1
Dx = 1 Dy = 0
Dx = 1 Dy = 0
7
“e,e,n”-Parse Tree as XML Element
<path Ex="2" Ey="1"> <step Dx ="1" Dy="0"> e </step> <path Ex="1" Ey="1"> <step Dx ="1" Dy="0"> e </step> <path Ex="0" Ey="1"> <step Dx ="0" Dy="1"> n </step> <path Ex="0" Ey="0"> </path> </path> </path></path>
path
pathstep
pathstep
pathstep
e
n
eEx = 0 Ey = 0
Ex = 0 Ey = 1
Ex = 1 Ey = 1
Ex = 2 Ey = 1
Dx = 0 Dy = 1
Dx = 1 Dy = 0
Dx = 1 Dy = 0
Attributed XML element tree = Decorated parse tree
Attribute grammarparse results andintermediate statescan thus be representedas XML elements
8
DTD Approximation forAttribute Grammar (AG)
<!ELEMENT path (EMPTY | (step, path)) ><!ELEMENT step (n | e | s | w) ><!ELEMENT n EMPTY ><!ELEMENT e EMPTY ><!ELEMENT s EMPTY ><!ELEMENT w EMPTY >
<!ATTLIST path Ex CDATA #REQUIRED ><!ATTLIST path Ey CDATA #REQUIRED ><!ATTLIST step Dx CDATA #REQUIRED ><!ATTLIST step Dy CDATA #REQUIRED >
‘Canonical empty elements’instead of terminals
CDATAinstead ofintegers
Semantic Rules of full AGs can be added(via XML stylesheets, Java, Prolog, ...)
9
“e,e,n”-XML ElementAccording to DTD
<path Ex="2" Ey="1"> <step Dx ="1" Dy="0"> e </step> <path Ex="1" Ey="1"> <step Dx ="1" Dy="0"> e </step> <path Ex="0" Ey="1"> <step Dx ="0" Dy="1"> n </step> <path Ex="0" Ey="0"> </path> </path> </path></path>
Terminal e becomes empty element <e/>Terminal n becomes empty element <n/>
<path Ex="2" Ey="1"> <step Dx ="1" Dy="0"> <e/> </step> <path Ex="1" Ey="1"> <step Dx ="1" Dy="0"> <e/> </step> <path Ex="0" Ey="1"> <step Dx ="0" Dy="1"> <n/> </step> <path Ex="0" Ey="0"> </path> </path> </path></path>
10
path[0,0]([]).path[Nx,Ny]([Head|Tail]) :- step[Dx,Dy](Head), path[Ex,Ey](Tail), Nx .= +(Dx,Ex), Ny .= +(Dy,Ey).
step[0,1](n).step[1,0](e).step[0,-1](s).step[-1,0](w).
Prolog-like Form of AGwith Semantic Rules
Attribute values represented as “[...]”-parameters.Call path[X,Y]([e,e,n]) computes X = 2, Y = 1,
e.g. using Relfun interpreter
path.Ex := 0 path.Ey := 0
path1.Ex := step .Dx + path2.Ex path1.Ey := step .Dy + path2.Ey
path
path step path
step nstep estep sstep w
step .Dx := 0 step .Dy := 1step .Dx := 1 step .Dy := 0step .Dx := 0 step .Dy := -1step .Dx := -1 step .Dy := 0
11
Parameter-XML Form of AG via RFML-like Generic Markup
<hn> <pattop> <struc> <con>path</con> <con>0</con> <con>0</con> </struc> <tup/> </pattop></hn>
path[0,0]([]).
<hn> <pattop> <struc> <con>path</con> <var>nx</var> <var>ny</var> </struc> <tup> <var>head</var> <rest/> <var>tail</var> </tup> </pattop>
path[Nx,Ny] ([Head|Tail])
:-
<callop> <struc> <con>step</con> <var>dx</var> <var>dy</var> </struc> <var>head</var> </callop> <callop> <struc> <con>path</con> <var>ex</var> <var>ey</var> </struc> <var>tail</var> </callop> <is> <var>nx</var> <callop> <con>+</con> <var>dx</var> <var>ex</var> </callop> </is> <is> <var>ny</var> <callop> <con>+</con> <var>dy</var> <var>ey</var> </callop> </is></hn>
step[Dx,D
y](Head),path[E
x,Ey](T
ail), Nx .=
+(D
x,Ex), N
y .= +
(Dy,E
y).
<hn> <pattop> <struc> <con>step</con> <con>0</con> <con>1</con> </struc> <con>n</con> </pattop></hn>
<hn> <pattop> <struc> <con>step</con> <con>1</con> <con>0</con> </struc> <con>e</con> </pattop></hn>
<hn> <pattop> <struc> <con>step</con> <con>0</con> <con>-1</con> </struc> <con>s</con> </pattop></hn>
<hn> <pattop> <struc> <con>step</con> <con>-1</con> <con>0</con> </struc> <con>w</con> </pattop></hn>
step[0,1](n).
step[1,0](e).
step[0,-1](s).
step[-1,0](w).
Generic: User-definednonterminals (e.g. path)don’t appear as tags,only between tags
12
Attribute-XML Form of AG via RFML/MathML-likeNon-Generic Markup
<hn> <pattop> <path ex="0" ey="0"/> <tup/> </pattop></hn>
path [Ex=0, Ey=0] ([]).
<callop> <step dx="?" dy="?"/> <var>head</var></callop>
Step [Dx=?, Dy=?] (Head),
<callop> <path ex="?" ey="?"/> <var>tail</var></callop>
Path [Ex=?, Ey=?] (Tail),
<is> <var>nx</var> <callop> <+/> <var>dx</var> <var>ex</var> </callop> </is>
Nx .= +(Dx,Ex),
<is> <var>ny</var> <callop> <+/> <var>dy</var> <var>ey</var> </callop> </is></hn>
Ny .= +(Dy,Ey).
<hn> <pattop> <path nx="?" ny="?"/> <tup> <var>head</var> <rest/> <var>tail</var> </tup> </pattop>
path [Nx=?, Ny=?] ([Head|Tail])
:-
Output attributes made explicit via “?”-values
13
Conclusions
• Attribute Grammars and XML use the same parse and element trees
• There are various possible XML markups for AG’s semantic rules (cf. RFML/MathML)
• In future work an AG-markup language could be developed and applied for grammar sharing
• Further levels of the XML/AG relationships should be studied, e.g. attribute dependencies, rule-evaluation methods, and DOM-level rules