473,387 Members | 1,925 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,387 software developers and data experts.

Pyparsing: Specify grammar at run time

I run into another issue with my grammar:

My input record contains a common part and an extended part. Based on
the value of the common part, the extended part will be different. So,
I am thinking of parsing the common part first and check the common's
value and then parse again for the rest of the record. How do I tell
pyparsing to start the 2nd parse at the exact location where the 1st
parse left off?

######################################
from pyparsing import *

common = Word('aA').setResultsName('value')
extend1 = Word('b')
extend2 = Word('c')

result = common.parseString(record)
if result.value == 'a':
result1 = extend1.parseString(???)
else:
result2 = extend2.parseString(???)
######################################

Thanks,
Khoa
May 17 '06 #1
2 1496
"Khoa Nguyen" <kh*********@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
I run into another issue with my grammar:

My input record contains a common part and an extended part. Based on
the value of the common part, the extended part will be different. So,
I am thinking of parsing the common part first and check the common's
value and then parse again for the rest of the record. How do I tell
pyparsing to start the 2nd parse at the exact location where the 1st
parse left off?

######################################
from pyparsing import *

common = Word('aA').setResultsName('value')
extend1 = Word('b')
extend2 = Word('c')

result = common.parseString(record)
if result.value == 'a':
result1 = extend1.parseString(???)
else:
result2 = extend2.parseString(???)
######################################

Thanks,
Khoa

Any reason you can't construct a grammar that looks like:

allData = "a"
May 17 '06 #2

"Khoa Nguyen" <kh*********@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
I run into another issue with my grammar:

My input record contains a common part and an extended part. Based on
the value of the common part, the extended part will be different. So,
I am thinking of parsing the common part first and check the common's
value and then parse again for the rest of the record. How do I tell
pyparsing to start the 2nd parse at the exact location where the 1st
parse left off?

######################################
from pyparsing import *

common = Word('aA').setResultsName('value')
extend1 = Word('b')
extend2 = Word('c')

result = common.parseString(record)
if result.value == 'a':
result1 = extend1.parseString(???)
else:
result2 = extend2.parseString(???)
######################################
Any reason you can't specify a grammar like this?

all = ( 'a' + extend1 ) | ( 'A' + extend2 )

Grammars *can* be made dynamic at runtime - see the implementation of
countedArray for a placeholder Forward element, which is modified at parse
time using a parse action. But I'd avoid this kind of trickery if you can -
it's just too obscure.

-- Paul
May 17 '06 #3

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

Similar topics

1
by: Paul McGuire | last post by:
***This is of especial interest for those who are using the pyparsing module, and have defined grammars that make use of CaselessLiteral.*** One of the bugfix requests I recently got for...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
10
by: Paul McGuire | last post by:
I just published my first article on ONLamp, a beginner's walkthrough for pyparsing. Please check it out at http://www.onlamp.com/pub/a/python/2006/01/26/pyparsing.html, and be sure to post any...
4
by: Bytter | last post by:
Hi, I'm trying to construct a parser, but I'm stuck with some basic stuff... For example, I want to match the following: letter = "A"..."Z" | "a"..."z" literal = letter+ include_bool := "+"...
3
by: Steven Bethard | last post by:
Within a larger pyparsing grammar, I have something that looks like:: wsj/00/wsj_0003.mrg When parsing this, I'd like to keep around both the full string, and the AAA_NNNN substring of it, so...
13
by: 7stud | last post by:
To the developer: 1) I went to the pyparsing wiki to download the pyparsing module and try it 2) At the wiki, there was no index entry in the table of contents for Downloads. After searching...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
1
by: Steve | last post by:
Hi All (especially Paul McGuire!) Could you lend a hand in the grammar and paring of the output from the function win32pdhutil.ShowAllProcesses()? This is the code that I have so far (it is...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.