Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PLY Lex ambiguity

I have a problem with ambiguity on tokens level.

The problem looks like this. My code looks like this so token t_UN1 has higher precedence.

t_ignore = ' \t\v\r' # whitespace 

....

def t_UN1(t): #NS_
    r'NS\_'
    return t
def t_IDENTIFIER(t):
    r'[a-zA-Z][a-zA-Z0-9_]*'
    return t

....

I would like to achieve that eg. string: NS_XYZ is identified as "IDENTIFIER" and single NS_ surrounded by white spaces is identified as "UN_1".

How shall I handle that ? Currently string NS_XYZ is simply splited into two tokens UN1 and IDENTIFIER

like image 268
user1946815 Avatar asked Aug 27 '26 06:08

user1946815


1 Answers

If you're looking to get 'single NS_ surrounded by white spaces', you can add the white space character class into your token string:

def t_UN1(t): #NS_
    r'\s+NS\_\s+'
    return t

Side note: for PLY questions, the ply-hack google group is a good place to ask PLY-related questions.

like image 81
MichaelJCox Avatar answered Aug 29 '26 21:08

MichaelJCox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!