0

Following this old tutorial, I am trying to get a lemon parser to automatically terminate parsing on an EOL token. The relevant part of the parser looks like this:

start ::= in .
in ::= .
in ::= in commandList EOL .
{
    printf("start ::= commandList .\n");
    printf("> ");
}

Here's how I'm executing the parser with tokens scanned by Flex:

int lexCode;
do {
    lexCode = yylex(scanner);
    Parse(shellParser, lexCode, yyget_text(scanner));
    // XXX This line should not be necessary; EOL should automatically
    // terminate parsing. :-(
    if (lexCode == EOL) Parse(shellParser, 0, NULL);
} while (lexCode > 0);

I'd like to eliminate the need to check for the EOL token here, and just let the parser figure out when it's done. How do I do that?

Thanks!

theory
  • 9,178
  • 10
  • 59
  • 129

1 Answers1

1

In EBNF terms your definition of in is

in ::= (commandList EOL)*

Which allows multiple EOLs. What you want is

in ::= commandList* EOL

Which should work out to

start ::= in EOL .
in ::= .
in ::= in commandList .

Note that this does not allow for completely empty input (not even an EOL); you can tweak things as necessary if this is a problem.

ebohlman
  • 14,795
  • 5
  • 33
  • 35
  • So I tried that, and it seems to work as well as what I had before, but it does not implicitly equate to passing `0` to the `Parse()` function. So it never reduces to any of those productions. I still have to do it myself. Which is fine if that's the way it works, but if not, I'd like to figure out the proper incantation. – theory Feb 05 '13 at 03:51