hoaproject / Compiler

The Hoa\Compiler library.
https://hoa-project.net/
453 stars 48 forks source link

State restore error #84

Closed SerafimArts closed 6 years ago

SerafimArts commented 6 years ago

There are suspicions that the backtrack method does not work correctly and does not find the previous correct chain of rules, but I have not found the reason yet :\

Grammar

%skip T_WHITESPACE \s+
%token T_DIGIT \d+
%token T_WORD \w+

#grammar:
    digits() | words()

#digits:
    <T_DIGIT>*

#words:
    <T_WORD>*

Sample

2 3 4 a b c

Expected

>  #grammar
>  >  #digits
>  >  >  token(T_DIGIT, 2)
>  >  >  token(T_DIGIT, 3)
>  >  >  token(T_DIGIT, 4)
>  >  #words
>  >  >  token(T_WORD, a)
>  >  >  token(T_WORD, b)
>  >  >  token(T_WORD, c)

Actual

Hoa\Compiler\Exception\UnexpectedToken : Unexpected token "a" (T_WORD) at line 1 and column 7:
2 3 4 a b c
      ↑
 ~/vendor/hoa/compiler/Llk/Parser.php:1
guiled commented 6 years ago

Your grammar seems to wait a list of digits or a list of words, not both.

I would write this (and it may be NOT optimized)

#grammar:
    (digits() | words())*

Am I wrong ?

SerafimArts commented 6 years ago

@guiled omfg, you're right, this is a grammar problem =\