1
votes

I'm trying to parse a sequence of statements, with the following rule: statements must be separated by at least one newline, and the sequence may be padded with at least zero newlines. For example,

\n
stmt\n
stmt\n
\n
stmt

So far I've come up with this Yacc grammar,

stmt_list:
    %empty
|   stmt_list stmt seps
;

seps:
    sep
| seps sep
;

but it doesn't match my example since my grammar expects a newline at the end. Is there are standard way of parsing this? Thanks.

1

1 Answers

3
votes

A simple (and traditional) solution is to allow empty statements:

program  : statement
         | program '\n' statement

statement: %empty
         | statement_type_1
         | ...

That doesn't quite provide the same parse tree, since it forces you to ignore an empty statement :) But it has the virtue of being simple.

Otherwise, you're stuck with something like:

program       : statement_list opt_newlines

statement_list: opt_newlines
              | statement_list newlines statement

opt_newlines  : %empty
              | opt_newlines '\n'

newlines      : '\n' opt_newlines