Search code examples
ply

Token matching order in PLY


I have a parser written in PLY that has the following token definition

def t_COMMAND(t):
    r'create|show'
    return t

def t_SCOPE(t):
    r'user|domain'
    return t

def t_STRING(t):
    r'[a-zA-Z_@\*\.]*'
    return t

I am trying to parse the following string

show user where created_on = foo

Here is my grammar

S:COMMAND SCOPE FILTER;
FILTER:WHERE EXP |;
EXP:STRING OP STRING
...

I get a syntax error at the created_on token, probably because it gets matched as a COMMAND rather than STRING

Is there a way to make PLY take the largest possible match?


Solution

  • Found two possible approaches

    • User a reserved words tuple and append it with the token list as in Specification of tokens

    • If possible, add quotes to the STRING as '[a-zA-Z_@\*\.]*', so that it can be distinguished from COMMAND

    I chose the second approach, as I have a lot of so called reserved words.