473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing: request for pointers

Hi everyone,

I would like to implement a parser for a mini-language
and would appreciate some pointers. The type of
text I would like to parse is an extension of:

http://www.websequencediagrams.com/examples.html

For those that don't want to go to the link, consider
the following, *very* simplified, example:
=======

programmer Guido
programmer "Fredrik Lundh" as effbot
programmer "Alex Martelli" as martellibot
programmer "Tim Peters" as timbot
note left of effbot: cutting sense of humor
note over martellibot:
Offers detailed note, explaining a problem,
accompanied by culinary diversion
to the delight of the reader
note over timbot: programmer "clever" as fox
timbot -Guido: I give you doctest
Guido --timbot: Have you checked my time machine?

=======
From this, I would like to be able to extract
("programmer ", "Guido")
("programmer as", "Fredrik Lundh", "effbot")
....
("note left of", "effbot", "cutting sense of humor")
("note over", "martellibo t", "Offers..." )
("note over", "timbot", 'programmer "clever" as fox')

Some observations:
1. I want to use indentation to identify blocks.
(the site I referred to uses "end note" which I don't want)
2. "keywords" (such as "programmer ", "note over")
can appear in text, and should not then be misidentified
3. I was thinking of using http://effbot.org/zone/simple-top-down-parsing.htm
as a guide; however, it is not clear to me how it could be
adapted to handle observations 1 and 2. (If it "easily" could,
just a few pointers would be enough, and I'll start from there...)
4. I want to do this only using modules in the standard Python
library, as I want to use this to learn about the basics
of parsing. So, please don't *simply* suggest to use a
third-party module, such as
[1] plex, [2] yapps, [3] pyparsing
The learning journey is more important for me than just
having a canned solution to my (current) parsing problem.

Cheers,

André

[1] http://www.cosc.canterbury.ac.nz/gre...g/python/Plex/
[2] http://theory.stanford.edu/~amitp/yapps/
[3] http://pyparsing.wikispaces.com/

Nov 11 '08 #1
2 1900
On Tue, 11 Nov 2008 11:59:50 -0800, André wrote:
4. I want to do this only using modules in the standard Python
library, as I want to use this to learn about the basics of parsing.
So, please don't *simply* suggest to use a third-party module, such
as
[1] plex, [2] yapps, [3] pyparsing
The learning journey is more important for me than just having a
canned solution to my (current) parsing problem.
Believe me, there is no canned solution to your current parsing problem.
Once you have a parser engine (e.g. pyparsing) you still have to build a
parser, and that's not necessarily trivial.

Other than that, try this:

http://docs.python.org/library/shlex.html

--
Steven
Nov 12 '08 #2
On Nov 11, 1:59*pm, André <andre.robe...@ gmail.comwrote:
Hi everyone,

I would like to implement a parser for a mini-language
and would appreciate some pointers. *The type of
text I would like to parse is an extension of:

http://www.websequencediagrams.com/examples.html

For those that don't want to go to the link, consider
the following, *very* simplified, example:
=======

programmer Guido
programmer "Fredrik Lundh" as effbot
programmer "Alex Martelli" as martellibot
programmer "Tim Peters" as timbot
note left of effbot: cutting sense of humor
note over martellibot:
* * Offers detailed note, explaining a problem,
* * accompanied by culinary diversion
* * to the delight of the reader
note over timbot: programmer "clever" as fox
timbot -Guido: I give you doctest
Guido --timbot: Have you checked my time machine?

=======
From this, I would like to be able to extract
("programmer ", "Guido")
("programmer as", "Fredrik Lundh", "effbot")
...
("note left of", "effbot", "cutting sense of humor")
("note over", "martellibo t", "Offers..." )
("note over", "timbot", 'programmer "clever" as fox')
Even if you choose not to use pyparsing, a pyparsing example might
give you some insights into your problem. See how the grammar is
built up from separate pieces. Parse actions in pyparsing implement
callbacks to do parse-time conversion - in this case, the multiline
note body is converted from the parsed list of separate strings into a
single newline-separated string.

Here is the pyparsing example:

from pyparsing import Suppress, Combine, LineEnd, Word, alphas,
alphanums,\
quotedString, Keyword, Optional, oneOf, restOfLine, indentedBlock,
\
removeQuotes,em pty,OneOrMore,G roup

# used to manage indentation levels when parsing indented blocks
indentstack = [1]

# define some basic punctuation and terminal words
COLON = Suppress(":")
ARROW = Combine(Word('-')+'>')
NL = LineEnd().suppr ess()
ident = Word(alphas,alp hanums+"-_")
quotedString.se tParseAction(re moveQuotes)

# programmer definition
progDefn = Keyword("progra mmer") + Optional(quoted String("alias") + \
Optional("as")) + ident("name")

# new pyparsing idiom - embed simple asserts to verify bits of the
# overall grammar in isolation
assert "programmer Guido" == progDefn
assert 'programmer "Tim Peters" as timbot' == progDefn

# note specification - only complicated part is the indented block
# form of the note we use a pyparsing parse action to convert the
# nested token lists into a multiline string
OF = Optional("of")
notelocn = oneOf("over under") | "left" + OF | "right" + OF
notetext = restOfLine.setN ame("notetext")
noteblock = indentedBlock(n otetext, indentstack).se tName("notebloc k")
noteblock.setPa rseAction(lambd a t:'\n'.join(tt[0] for tt in t[0]))
note = Keyword("note") + notelocn("locat ion") + ident("subject" ) +
COLON + \
(~NL + empty + notetext("note" ) | noteblock("note ") )
assert 'note over timbot: programmer "clever" as fox ' == note

# message definition
msg = ident("from") + ARROW + ident("to") + COLON + empty + notetext
("note")
assert 'Guido --timbot: Have you checked my time machine?' == msg

# a seqstatement is one of these 3 types of statements
seqStatement = progDefn | note | msg

# parse the sample text
parsedStatement s = OneOrMore(Group (seqStatement)) .parseString(se qtext)

# print out token/field dumps for each statement
for s in parsedStatement s:
print s.dump()

Prints:

['programmer', 'Guido']
- name: Guido
['programmer', 'Fredrik Lundh', 'as', 'effbot']
- alias: Fredrik Lundh
- name: effbot
['programmer', 'Alex Martelli', 'as', 'martellibot']
- alias: Alex Martelli
- name: martellibot
['programmer', 'Tim Peters', 'as', 'timbot']
- alias: Tim Peters
- name: timbot
['note', 'left', 'of', 'effbot', 'cutting sense of humor ']
- location: left
- note: cutting sense of humor
- subject: effbot
['note', 'over', 'martellibot', 'Offers ...']
- location: over
- note: Offers detailed note, explaining a problem,
accompanied by culinary diversion
to the delight of the reader
- subject: martellibot
['note', 'over', 'timbot', 'programmer "clever" as fox ']
- location: over
- note: programmer "clever" as fox
- subject: timbot
['timbot', '->', 'Guido', 'I give you doctest ']
- from: timbot
- note: I give you doctest
- to: Guido
['Guido', '-->', 'timbot', 'Have you checked my time machine?']
- from: Guido
- note: Have you checked my time machine?
- to: timbot

Best of luck in your project,
-- Paul
Nov 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2009
by: Alex Mizrahi | last post by:
Hello, All! i have 3mb long XML document with about 150000 lines (i think it has about 200000 elements there) which i want to parse to DOM to work with. first i thought there will be no problems, but there were.. first i tried Python.. there's special interest group that wants to "make Python become the premier language for XML processing"...
3
3408
by: Peter Bassett | last post by:
For some reason when I call one ASP program from another, I am not parsing out the parameters correctly. Caling ASP has <a href="foo.asp?to=name@aol.com?id=A1234?title=Sales?code=HR">blah</a> In the called ASP, I write this: response.write("Unparsed: " & Request.QueryString) response.write("To = " & Request.QueryString("to"))...
4
2297
by: silviu | last post by:
I have the following XML string that I want to parse using the SAX parser. If I remove the portion of the XML string between the <audit> and </audit> tags the SAX is parsing correctly. Otherwise SAX wouldn't do the parsing. What's wrong with this string (between <audit> and </audit> tags)? I am using SAX/Xerces 2.3.0 on Sun 8. Thanks in...
6
2110
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic question: I want to parse a string like "3.12" to get two integers 3 and 12. I wanted to use the function STRTOK() I wrote a main- and a...
4
1717
by: Hugh | last post by:
Hello, I am having some problems understanding (most likely), parsing a text file. I would like to parse a file like: block1 { stuff; ... stuffN; };
6
9442
by: Computer_Czar | last post by:
I'm trying to figure out the best way to parse an input string from a file for hex values. The string is actually Motorola S code produced by an embedded assembler. For example lines contain S1142CD0XXYYZZ... I've written similar programs in C/C++ where I save the string and use a pointer to index along the string. Well I've heard...
6
3160
by: P James | last post by:
Hi, My project has been running for 4 years in ASP/IIS (originally on NT4, then on Win2003 as of 1 year ago), using the following code to parse the request object using the XML DOM: Set oASPRequest = GetObjectContext.Item("Request") Set oRequestDOM = CreateObject("MSXML.DOMDocument") If Not oRequestDOM.Load(oASPRequest) Then Err.Raise...
5
3251
by: gamehack | last post by:
Hi all, I was thinking about parsing equations but I can't think of any generic approach. Basically I have a struct called math_term which is something like: struct math_term { char sign; int constant; int x; int y;
4
2000
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. It is greatly appreciated. I am working with Plimus for licensing my software. I can communicate with the server and I'm getting responses in XML. My question is, the XML stream that's coming back is telling if it's a successful license registration, validation, too many installs etc. How do...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.