سؤال

Here's the context: I have a file (or stream), and I want to process its header, stop, and then deal with the rest of the data differently. That is, suppose I have a stream like BEGIN GOOD GOOD GOOD GOOD GOOD DONE JUNK JUNK JUNK JUNK .... The first part (BEGIN GOOD+ DONE) matches my grammar, and I want to stop as soon as I see DONE.

AFAIK, YACC and friends want to keep consuming tokens until there are no more, so I have to write more grammar to match all the JUNK. Is there a way around this?

I'm using PLY (a Lex and Yacc implementation for Python), but I'm open to general answers here.

هل كانت مفيدة؟

المحلول

In normal yacc, you can use the yacc macro YYACCEPT in an action to immediately return a successful parse without reading any more tokens. I don't know if PLY supports that, however.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top