Pyparsing: Specify grammar at run time

K

Khoa Nguyen

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
 
P

Paul McGuire

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"
 
P

Paul McGuire

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top