-1

I am trying to parse the string "###" using an EBNF grammar in TatSu (grako) of this kind:

grammar = """mask =
                  | ['()'] ['$'] {'#'} '#'
                  | ['()'] {'#'} '#%'
                  | ['()'] ['$'] {'#'} {'0'} '0' '.#' {'#'}
"""

I get an error:

tatsu.exceptions.FailedToken: (1:1) expecting '#' :
#
^
mask
start

I suspect that the search {'#'} (match '#' zero or more times) is first performed and after that cannot find '#' (required character '#'). Is it possible to somehow implement it so that at first I see the required character '#' and only after that I look for {'#'} additional characters ?

MadInc
  • 1
  • 3
  • Are you asking if it's possible or how to do it? Because "is it possible": Yea it is, anything is. If you're asking how, then I don't know and if you don't get a response or can't find it - most likely that particular niche need haven't been implemented into the library you're using and it'll be up to you to do so. – Torxed May 22 '20 at 07:47
  • There is nothing impossible in the world, therefore I know that it is possible to realize, but I need someone to direct me in the right direction – MadInc May 22 '20 at 08:14
  • I've never used that tool, but does `| ['()'] ['$'] '#' {'#'}` work? If so, then [left recursion](https://en.wikipedia.org/wiki/Left_recursion) may be the cause of your problems. Some tools work best with right recursive rules. You might need to fix the other patterns/alternatives in a similar way. – MemReflect May 22 '20 at 11:29
  • @MemReflect There may be another option like this `| ['()'] ['$'] {'#'} '#' '.' '#' {'#'}` and it will no longer work with right recursion – MadInc May 22 '20 at 12:22

1 Answers1

1

You will need to provide a &('#'|'0') positive lookahead to all the instances of the optional '#' prefixes:

    mask = ['()'] ['$'] {'#' &('#'|'0')} '#'
         | # and so on...

Notice that the repeating '0' prefix in the last line will have the same problem. You will need to add a &'0' lookahead check to it, too.

Branco Medeiros
  • 729
  • 4
  • 10
  • Thanks a lot, this is what I was looking for. I understood that there is a lookahead but there are few examples and I did not find how to use it correctly. – MadInc May 25 '20 at 04:41