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

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie spreche ich mit Pyparsing zweideutige eingebaute Blöcke wie diese an?

by Anonymous » 14 Apr 2025, 10:34

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?

Top