erikrose / parsimonious

The fastest pure-Python PEG parser I can muster
MIT License
1.79k stars 126 forks source link

How to get specific node from print. #196

Closed pixelRena closed 2 years ago

pixelRena commented 2 years ago

I am new to using this parser, but what I wanted to do was print the part that shows "sentence" or "digits" or "words". When printing you get something like this for example:

<Node called "**sentence**" matching "Flying High!">
<Node called "**words**" matching "Flying">
<Node called "**punctuations**" matching "!">

I wanted to see if there is a way to get the part that says "words" after the "called" or "punctuations".

I am planning to implement something that will count how many times it has seen "words" being called in the tree, but not sure if that is even possible to begin with. So if sees "sentence" called count += 1, if sees "punctuations" called add 1 to count Hopefully this doesn't sound confusing, I would really appreciate some help as a beginner.

Code Example: `

grammar = Grammar("""
sentence = (digits/words/punctuations) space* sentence*
digits = ~"[0-9]+"
punctuations = ','/'('/')'/'.'/'!'/'['/']'/'?'
space = ~"\s"
words = "Flying"/"High"
""")

print(grammar.parse("Flying High!")

`