Why pegjs not match Expression when __ not matched? Program = __/Expression

99 views Asked by At

Example code:

Program = __/Expression

Expression = .*

__ = [ \t\r\n]*
test is

2 * (3 + 4)
hahah hahhah
def hahah

In my mind , pegjs while match Expression when __ is not matched? But this get a error

Line 1, column 1: Expected [ \t\r\n] or end of input but "2" found. Expected behavior:

I want to know why it is not work. And I what to know is it possible to get all function callees in js use pegjs?

Actual behavior: A parse error: Line 1, column 1: Expected [ \t\r\n] or end of input but "2" found.

1

There are 1 answers

0
gatsbyz On BEST ANSWER

This is because you rule __ always matched, because it match empty input. You can think, that you grammar internally rewriten as follow (this is perfectly valid grammar, you can input it in pegjs online):

start = Program EOF

Program = __/Expression

Expression = .*

__ = [ \t\r\n]*
// EOF is ephemeral rule that match end of input, i.e. when nothing left in input
EOF = !.

So you input parsed as:

<'__' matched>
<'Program' matched>
<'EOF' not matched
  =>backtrack to 'start'
  =>nothing alternatives on 'start'
  =>error
>2 * (3 + 4)