Download presentation

Presentation is loading. Please wait.

Published byMollie Sharrock Modified over 2 years ago

1
Parsing for XML Developers Roger L. Costello 28 September 2014

2
Flat XML Document You might receive an XML document that has no structure. For example, this XML document contains a flat (linear) list of Book data: Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications 2

3
Give it structure to facilitate processing Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications 3

4
That’s parsing! Parsing is taking a flat (linear) sequence of items and adding structure so that the result conforms to a grammar. 4

5
Parsing Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications parse 5

6
From the book: “Parsing Techniques” Parsing is the process of structuring a linear representation in accordance with a given grammar. The “linear representation” may be: A flat sequence of XML elements a sentence a computer program a knitting pattern a sequence of geological strata a piece of music actions of ritual behavior 6

7
Grammar A grammar is a succinct description of the structure. Here is a grammar for Books: Books → Book+ Book → Title Authors Date ISBN Publisher Authors → Author+ Title → text Author → text Date → text ISBN → text Publisher → text 7

8
Parsing parser Books → Book+ Book → Title Authors Date ISBN Publisher Authors → Author+ Title → text Author → text Date → text ISBN → text Publisher → text Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications Grammar Linear representation Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications Structured representation 8

9
Parsing Techniques Over the last 50 years many parsing techniques have been created. Some parsing techniques work from the starting grammar rule to the bottom. These are called top-down parsing techniques. Other parsing techniques work from the bottom grammar rules to the starting grammar rule. These are called bottom-up parsing techniques. The following slides show how to apply a powerful bottom-up parsing technique to the Books example. 9

10
What does “powerful” mean? The previous slide said, … following slides show how to apply a powerful bottom-up parsing technique … “Powerful” means the technique can be used with lots of grammars, i.e., it can be used to generate lots of different structures. 10

11
Suppose we were to structure the XML from scratch. We might follow these steps: Parsing Techniques 11 continued on next slide

12
Follow these steps (cont.): Parsing Techniques Dick Grune Parsing Techniques Dick Grune Ceriel J.H. Jacobs Parsing Techniques Dick Grune Ceriel J.H. Jacobs 2007 continued on next slide 12

13
Follow these steps (cont.): Parsing Techniques Dick Grune Ceriel J.H. Jacobs Parsing Techniques Dick Grune Ceriel J.H. Jacobs Dover Publications and so forth, filling in the second Book then the third Book 13

14
Last step: add the last Book’s Publisher Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications last step adds this 14

15
Alternate view of the steps (a tree view) Books Book Books Book Title Books Book Title Authors Books Book Title Authors Author Books Book Title Authors Author continued on next slide 15 Author

16
Alternate view (cont.) 16 Books Book Title Authors Author Date Books Book Title Authors Author Date ISBN Books Book Title Authors Author Date ISBN Publisher continued on next slide

17
Alternate view (cont.) Books Book Title Authors Date ISBN Publisher Book and so forth, filling in the second Book then the third Book 17 Author

18
Last step: add the last Book’s Publisher Books Book Title Authors Date ISBN Publisher Book Title Authors Author Date ISBN Publisher Book Title Authors Author Date ISBN Books Book Title Authors Date ISBN Publisher Book Title Authors Author Date ISBN Publisher Book Title Authors Author Date ISBN Publisher Author last step adds this 18 Author

19
Terminology: Production Step Parsing Techniques Each step is called a production step 21

20
Top down The previous slides showed the generation of the structured XML by starting from the top (root element) down to the bottom (leaf nodes). 19

21
Bottom-up parsing In bottom-up parsing we work backward: from the last step to the first step. 20

22
Let’s begin … One production step must have been the last and its result must be visible in the linear representation. We recognize the rule Publisher → text in This gives us the final step in the production process (and the first step in bottom-up parsing): 22 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

23
Next We recognize the rule ISBN → text in This gives us the next-to-last step in the production process (and the second step in bottom-up parsing): 23 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

24
Next We recognize the rule Date → text in This gives us the third step in bottom-up parsing: 24 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

25
Next We recognize the rule Author → text in This gives us the fourth step in bottom-up parsing: 25 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

26
Next We recognize the rule Authors → Author+ in This gives us the fifth step in bottom-up parsing: 26 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

27
Next We recognize the rule Title → text in This gives us the sixth step in bottom-up parsing: 27 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

28
Next We recognize the rule Book → Title Authors Date ISBN Publisher in This gives us the seventh step in bottom-up parsing: 28 Parsing Techniques Dick Grune Ceriel J.H. Jacobs Springer Introduction to Graph Theory Richard J. Trudeau Dover Publications Introduction to Formal Languages Gyorgy E. Revesz Dover Publications

29
See the algorithm? See how we are working backwards, from the bottom grammar rules up to the starting grammar rule? In the process we are adding structure to the flat (linear) XML – neat! 29

30
Terminology: Reduction In bottom-up parsing a collection of symbols are recognized as derived from a symbol. For example, Title, Authors, Date, ISBN, Publisher is derived from Book: 30 Title, Authors, Date, ISBN, Publisher is reduced to Book So the bottom-up parsing process is a reduction process. Book Title AuthorsDate ISBN Publisher

31
Build your own bottom up parser! You now have enough knowledge that you can go off and build your own bottom-up parser. 31

32
I implemented a bottom-up parser I used XSLT to implement a bottom-up parser. If you would like to give my implementation a go, here is the XSLT program and a sample flat (linear) input XML document: parser-for-Books.xsl parser-for-Books.xsl 32

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google