Wie spreche ich mit Pyparsing zweideutige eingebaute Blöcke wie diese an?Python

Python-Programme
Anonymous
 Wie spreche ich mit Pyparsing zweideutige eingebaute Blöcke wie diese an?

Post by Anonymous »

Ich versuche, Daten im folgenden Format zu analysieren: < /p>

Code: Select all

data = """\
map=1
sub=1
int=99
foo=bar
sub=2
foo=bar
int=99
bar=qux
"""
Ich habe meine Grammatik auf das Beispiel aus dem Pyparsing repository gestützt und das habe ich bekommen:

Code: Select all

from pyparsing import *

stmt = Forward()
suite = IndentedBlock(stmt)
identifier = Word(alphas, alphanums)
key = Combine(identifier + "=" + Word(nums))
definition = key + suite

rhs = Regex(r"[a-z0-9]+")
lhs = identifier + Suppress("=") + rhs
stmt 
Der resultierende Parse-Tree ist fast korrekt: < /p>
['map=1', ['sub=1', ['int=99', ['foo', 'bar']], 'sub=2', ['foo', 'bar', 'int=99', ['bar', 'qux']]]]
Da die Map/Submap -Schlüssel einen Integer -Index -Postfix enthalten, z. = 1 , sie sind syntaktisch mit den Ganzzahlzuordnungen gleich. Dies führt dazu, dass der Parser nach eine Ganzzahl-Zuordnung als neue eingerksame Block['map=1', ['sub=1', ['int', '99', 'foo', 'bar'], ['sub=2', ['foo', 'bar', 'int', '99', 'bar', 'qux']]
< /code>
Wie kann ich diese Mehrdeutigkeit beseitigen? Vielleicht eine Art negatives Aussehen, das?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post