Title: Attribute Grammars and XML
1Attribute Grammars and XML
Harold Boley, DFKI Kaiserslautern
- Workshop on Attribute Grammars, XML, and RDF
- Institute for Media and Communications Management
- University of St. Gallen
- 20/21 Sept. 2000
2What Are and How RelateAttribute 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
3Attribute Grammar ExampleCartesian 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
4Grammar with Attributes on Nonterminals
Grammar path ? ? path ? step path step ?
n step ? e step ? s step ? w Attributes
path.?Ex x-coordinate of end position path.?Ey
y-coordinate of end position step
.?Dx incremental change of x-coordinate step
.?Dy incremental change of y-coordinate
We will only consider synthesized
attributes, often prefixed by a ?, not
inherited attributes
5Attribute Grammar with Semantic Rules
Grammar path ? ? path ? step path step ?
n step ? e step ? s step ? 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 1 step .?Dx 1 ? step .?Dy
0 step .?Dx 0 ? step .?Dy -1 step .?Dx
-1 ? step .?Dy 0
6Decorated Parse Treefor Sequence e,e,n
path
Ex 2 Ey 1
Ex 1 Ey 1
Dx 1 Dy 0
e
Ex 0 Ey 1
Dx 1 Dy 0
e
Ex 0 Ey 0
Dx 0 Dy 1
?
n
7e,e,n-Parse Tree as XML Element
ltpath Ex"2" Ey"1"gt ltstep Dx "1" Dy"0"gt e
lt/stepgt ltpath Ex"1" Ey"1"gt ltstep Dx
"1" Dy"0"gt e lt/stepgt ltpath Ex"0"
Ey"1"gt ltstep Dx "0" Dy"1"gt n lt/stepgt
ltpath Ex"0" Ey"0"gt lt/pathgt
lt/pathgt lt/pathgt lt/pathgt
Attribute grammar parse results and intermediate
states can thus be represented as XML elements
8DTD Approximation forAttribute Grammar (AG)
lt!ELEMENT path (EMPTY (step, path))
gt lt!ELEMENT step (n e s w) gt lt!ELEMENT
n EMPTY gt lt!ELEMENT e EMPTY gt lt!ELEMENT s EMPTY
gt lt!ELEMENT w EMPTY gt lt!ATTLIST path Ex
CDATA REQUIRED gt lt!ATTLIST path Ey CDATA
REQUIRED gt lt!ATTLIST step Dx CDATA
REQUIRED gt lt!ATTLIST step Dy CDATA
REQUIRED gt
Canonical empty elements instead of terminals
CDATA instead of integers
Semantic Rules of full AGs can be added (via XML
stylesheets, Java, Prolog, ...)
9e,e,n-XML ElementAccording to DTD
ltpath Ex"2" Ey"1"gt ltstep Dx "1" Dy"0"gt e
lt/stepgt ltpath Ex"1" Ey"1"gt ltstep Dx
"1" Dy"0"gt e lt/stepgt ltpath Ex"0"
Ey"1"gt ltstep Dx "0" Dy"1"gt n lt/stepgt
ltpath Ex"0" Ey"0"gt lt/pathgt
lt/pathgt lt/pathgt lt/pathgt
ltpath Ex"2" Ey"1"gt ltstep Dx "1" Dy"0"gt
lte/gt lt/stepgt ltpath Ex"1" Ey"1"gt ltstep
Dx "1" Dy"0"gt lte/gt lt/stepgt ltpath Ex"0"
Ey"1"gt ltstep Dx "0" Dy"1"gt ltn/gt
lt/stepgt ltpath Ex"0" Ey"0"gt lt/pathgt
lt/pathgt lt/pathgt lt/pathgt
Terminal e becomes empty element lte/gt Terminal n
becomes empty element ltn/gt
10Prolog-like Form of AGwith Semantic Rules
path.?Ex 0 ? path.?Ey 0 path1.?Ex
step .?Dx path2.?Ex ? path1.?Ey step .?Dy
path2.?Ey
path ? ? path ? step path
path0,0(). pathNx,Ny(HeadTail) -
stepDx,Dy(Head), pathEx,Ey(Tail),
Nx .
(Dx,Ex), Ny . (Dy,Ey). step0,1(n). step
1,0(e). step0,-1(s). step-1,0(w).
step ? n step ? e step ? s step ? w
step .?Dx 0 ? step .?Dy 1 step .?Dx 1 ?
step .?Dy 0 step .?Dx 0 ? step .?Dy
-1 step .?Dx -1 ? step .?Dy 0
Attribute values represented as
...-parameters. Call pathX,Y(e,e,n)
computes X 2, Y 1, e.g. using Relfun
interpreter
11Parameter-XML Form of AG via RFML-like Generic
Markup
Generic User-defined nonterminals (e.g.
path) dont appear as tags, only between tags
12Attribute-XML Form of AG via RFML/MathML-likeNon-
Generic Markup
13Conclusions
- Attribute Grammars and XML use the same parse and
element trees - There are various possible XML markups for AGs
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