473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

parsing


I would like to use Python to parse a *python-like* data description
language. That is, it would have it's own keywords, but would have a
syntax like Python. For instance:

Ob1 ('A'):
Ob2 ('B'):
Ob3 ('D')
Ob3 ('E')
Ob2 ('C')

I'm looking for the ':' and indentation to provide nested execution so I
can use a description like the one above to construct an object tree.

In looking at the parser and tokenize sections of the Python Language
Services (http://docs.python.org/lib/language.html), it looks as though
this will only parse Python keywords. Is there a way to tap into Python
parsing at a lower level so that I can use it to parse my own keywords?

Thanks,
Todd Moyer


Jul 18 '05 #1
2 2160

"Todd Moyer" <tm****@inventa.com> wrote in message
news:ma*************************************@pytho n.org...

I would like to use Python to parse a *python-like* data description
language. That is, it would have it's own keywords, but would have a
syntax like Python. For instance:

Ob1 ('A'):
Ob2 ('B'):
Ob3 ('D')
Ob3 ('E')
Ob2 ('C')

I'm looking for the ':' and indentation to provide nested execution so I
can use a description like the one above to construct an object tree.

In looking at the parser and tokenize sections of the Python Language
Services (http://docs.python.org/lib/language.html), it looks as though
this will only parse Python keywords. Is there a way to tap into Python
parsing at a lower level so that I can use it to parse my own keywords?


Perhaps the following copied from another article in another thread will
help
From: "Bram Stolk" <br**@nospam.sara.nl>
Subject: Re: Parsing C Preprocessor files
(I have not checked his code and results, just copy and paste)
================
I would like to thank the people who responded on my question about
preprocessor parsing. However, I think I will just roll my own, as I
found out that it takes a mere 16 lines of code to create a #ifdef tree.

I simply used a combination of lists and tuples. A tuple denotes a #if
block (startline,body,endline). A body is a list of lines/tuples.

This will parse the following text:

Top level line
#if foo
on foo level
#if bar
on bar level
#endif
#endif
#ifdef bla
on bla level
#ifdef q
q
#endif
#if r
r
#endif
#endif

into:

['Top level line\n', ('#if foo\n', ['on foo level\n', ('#if bar\n', ['on
bar level\n'], '#endif\n')], '#endif\n'), ('#ifdef bla\n', ['on bla
level\n', ('#ifdef q\n', ['q\n'], '#endif\n'), ('#if r\n', ['r\n'],
'#endif\n')], '#endif\n')]

Which is very suitable for me.

Code is:

def parse_block(lines) :
retval = []
while lines :
line = lines.pop(0)
if line.find("#if") != -1 :
headline = line
b=parse_block(lines)
endline = lines.pop(0)
retval.append( (headline, b, endline) )
else :
if line.find("#endif") != -1 :
lines.insert(0, line)
return retval
else :
retval.append(line)
return retval

And pretty pretting with indentation is easy:

def traverse_block(block, indent) :
while block:
i = block.pop(0)
if type(i) == type((1,2,3)) :
print indent*"\t"+i[0],
traverse_block(i[1], indent+1)
print indent*"\t"+i[2],
else :
print indent*"\t"+i,
Jul 18 '05 #2

Try YAML

http://yaml.org/

Example YAML file from their site :

import yaml
yaml.load( data below )
et voilà... it's done...

read the site, it's really worth a look. This format is great for data
serialization, and very human-readable unlike XML.

invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.

I would like to use Python to parse a *python-like* data description
language. That is, it would have it's own keywords, but would have a
syntax like Python. For instance:

Ob1 ('A'):
Ob2 ('B'):
Ob3 ('D')
Ob3 ('E')
Ob2 ('C')

I'm looking for the ':' and indentation to provide nested execution so I
can use a description like the one above to construct an object tree.

In looking at the parser and tokenize sections of the Python Language
Services (http://docs.python.org/lib/language.html), it looks as though
this will only parse Python keywords. Is there a way to tap into Python
parsing at a lower level so that I can use it to parse my own keywords?

Thanks,
Todd Moyer


--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 18 '05 #3

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

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
0
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
5
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
7
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for...
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.