Search code examples
algorithmparsinggrammarll-grammar

"one or more" with LL parser


Let's say my grammar is:

file = line, {line}
line = ..., "\n"

If I want to build a LL parser for that grammar, how should I implement the "one or more line"?

I was thinking about changing the grammar to this:

file = line
line = ..., "\n", nl
nl = line
   | <end of file>

My lines would be nested. Is this the most elegant/efficient way to solve the problem ?


Solution

  • Close. Typically just like this:

    file = line, morelines
    morelines = e | line, morelines
    line = ..., "\n"
    

    Where e is the epsilon or empty symbol