Skip to content

Grammar for lookahead matching backwards when parsing a play #129

Answered by igordejanovic
TakodaS asked this question in Q&A
Discussion options

You must be logged in to vote

Syntactic predicates (&|!) are non-consuming. You need to consume those APPEARS and DISAPPEARS keywords like:

    appears =  caps_sentence "APPEARS:"
    disappears =  caps_sentence "DISAPPEARS:"

But, that brings another problem. caps_sentence will eat those keywords. So to prevent it from eating APPEARS and DISAPPEARS we use negative lookahead. The full working example is (notice the use of !keywords in caps:

from arpeggio.cleanpeg import ParserPEG

grammar = r"""
    grammar = line_end* scene+ EOF
    scene = scene_start action+ scene_end
    scene_start = "SCENE" number+ "BEGIN" line_end
    scene_end= "END SCENE" line_end*
    action = ( background/disappears/appears/dialogue ) line_…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TakodaS
Comment options

Answer selected by TakodaS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #128 on October 26, 2024 13:25.