0

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.

4

1 回答 1

0

这是因为您的规则 __ 始终匹配,因为它匹配空输入。您可以认为,您的语法在内部重写如下(这是完全有效的语法,您可以在线将其输入 pegjs):

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 = !.

所以你输入解析为:

<'__' matched>
<'Program' matched>
<'EOF' not matched
  =>backtrack to 'start'
  =>nothing alternatives on 'start'
  =>error
>2 * (3 + 4)
于 2018-11-27T22:21:16.960 回答