Search code examples
pythonply

Python PLY fail to do assignment operation


I am trying to create an interpreter that take my script as input. I have some problem with writing regular expressions. One of the defined token is considering all strings as it's tokens.

import ply.lex as lex
import ply.yacc as yacc

tokens = (
    'STAIRCASE',
    'STAIRCASE_END',
    'STAIR',
    'STAIR_END',
    'TAG',
    'COLON_SYM',
    'LINE_START_SYM',
    'NONE',
    'USER_DEFINED',
    'ARRAY',
    'IS',
)

assignments = {}

t_STAIRCASE                 = r'staircase'
t_TAG                       = r'\(([a-zA-Z0-9\ ])*\)'
t_COLON_SYM                 = r' :'
t_LINE_START_SYM            = r'-'
t_STAIRCASE_END             = 'EOSC'
t_ignore                    = ' \t\n'
t_STAIR                     = 'stair'
t_STAIR_END                 = 'EOS'
t_NONE                      = 'EOP'

Here is the issue with this regular exprission

t_USER_DEFINED              = r'[a-zA-Z0-9]+'

Code continues

t_IS                        = 'is'

def t_error(t):
    print 'Illegal character "%s"' % t.value[0]
    t.lexer.skip(1)
lex.lex()

NONE, STAIRCASE, TAG, STAIRCASE_DESCRIPTION = range(4)
states = ['NONE', 'STAIRCASE','STAIRCASE_DESCRIPTION']
current_state = NONE

def x():
    print "Hi How you doing"

def p_staircase_def(t):
    """STAIRCASE_DEF         : STAIRCASE TAG COLON_SYM STAIRCASE_DESCRIPTION
                             """
    print t[0:]
    help(t)

def p_staircase_description(t):
    """STAIRCASE_DESCRIPTION : LINE_START_SYM DICTONARY STAIRCASE_DESCRIPTION
                             | STAIRCASE_END STAIR_DEF
                             """
    print t[0:]

def p_dictonary(t):
    """
    DICTONARY                : USER_DEFINED IS USER_DEFINED
                             """

HERE is my assignment operation, actually it create a dictionary of variables

    temp = { t[1] : t[2] }
    print assignments.update( temp )
def p_stair_def(t):
    """STAIR_DEF             : STAIR TAG COLON_SYM STAIR_DESCRIPTION
                             """
    print t[0:]

def p_stair_description(t):
    """STAIR_DESCRIPTION     : LINE_START_SYM DICTONARY STAIR_DESCRIPTION
                             | STAIR_END STAIR_DEF
                             | STAIR_END
                             """
    print t[0:]

def p_error(t):
    print 'Syntax error at "%s"' % t.value if t else 'NULL'
    global current_state
    current_state = NONE

yacc.yacc()

file_input = open("x.staircase","r")
yacc.parse(file_input.read())

This is a sample input that need to be accepted by my interpreter "x.staircase"

staircase(XXXX XXX XXX):
- abc is 23183       # which need to {'abc' : '23183'}
- bcf is fda
- deh is szsC
EOSC
stair(XXXX XXX XXX):
- lkm is 35
- raa is 233
EOS
stair(XXXX XXX XXX):
- faa is zxhfb
- faa is 1
EOS

Error I am getting

Syntax error at "staircase"
[Finished in 0.1s]

BELOW Code Works but the input file is not as expected.

import ply.lex as lex
import ply.yacc as yacc

tokens = (
    'STAIRCASE',
    'STAIRCASE_END',
    'STAIR',
    'STAIR_END',
    'TAG',
    'COLON_SYM',
    'LINE_START_SYM',
    'NONE',
    'USER_DEFINED',
    'ARRAY',
    'IS',
)

assignments = {}

t_STAIRCASE                 = r'staircase'
t_TAG                       = r'\(([a-zA-Z0-9\ ])*\)'
t_COLON_SYM                 = r' :'
t_LINE_START_SYM            = r'-'
t_STAIRCASE_END             = 'EOSC'
t_ignore                    = ' \t\n'
t_STAIR                     = 'stair'
t_STAIR_END                 = 'EOS'
t_NONE                      = 'EOP'

##########################################
Here is the issue with this regular exprission

It worked, If I Use this
t_USER_DEFINED              = r'a'

Instead of this
#t_USER_DEFINED              = r'[a-zA-Z0-9]+'

But, when it comes to my input file it only accept one variable called 'a'
##########################################
Code continues

t_IS                        = 'is'

def t_error(t):
    print 'Illegal character "%s"' % t.value[0]
    t.lexer.skip(1)
lex.lex()

NONE, STAIRCASE, TAG, STAIRCASE_DESCRIPTION = range(4)
states = ['NONE', 'STAIRCASE','STAIRCASE_DESCRIPTION']
current_state = NONE

def x():
    print "Hi How you doing"

def p_staircase_def(t):
    """STAIRCASE_DEF         : STAIRCASE TAG COLON_SYM STAIRCASE_DESCRIPTION
                             """
    print t[0:]
    help(t)

def p_staircase_description(t):
    """STAIRCASE_DESCRIPTION : LINE_START_SYM DICTONARY STAIRCASE_DESCRIPTION
                             | STAIRCASE_END STAIR_DEF
                             """
    print t[0:]

def p_dictonary(t):
    """
    DICTONARY                : USER_DEFINED IS USER_DEFINED
                             """

HERE is my assignment operation, actually it create a dictionary of variables

    temp = { t[1] : t[2] }
    print assignments.update( temp )

def p_stair_def(t):
    """STAIR_DEF             : STAIR TAG COLON_SYM STAIR_DESCRIPTION
                             """
    print t[0:]

def p_stair_description(t):
    """STAIR_DESCRIPTION     : LINE_START_SYM DICTONARY STAIR_DESCRIPTION
                             | STAIR_END STAIR_DEF
                             | STAIR_END
                             """
    print t[0:]

def p_error(t):
    print 'Syntax error at "%s"' % t.value if t else 'NULL'
    global current_state
    current_state = NONE

yacc.yacc()

file_input = open("x.staircase","r")
yacc.parse(file_input.read())

Give below is a sample input("x.staircase") works. But as you can see all USER_DEFINED is always a

staircase(XXXX XXX XXX):
- a is a       # which need to {'abc' : '23183'}
- a is a
- a is a
EOSC
stair(XXXX XXX XXX):
- a is a
- a is a
EOS
stair(XXXX XXX XXX):
- a is a
- a is a 1
EOS

Solution

  • Please (re-)read the description of how Ply's lexer recognises tokens in the Ply manual. Pay particular attention to the ordering rules; since pattern variables are sorted longest to shortest, the t_USER_DEFINED pattern is tried before any keyword pattern (such as staircase) so none of the keywords will be recognised. (This is why shortening t_USER_DEFINED to a single character changes lexical behaviour.)

    There's a good clue that this is the problem and not the assignment production: the error message is triggered at the token staircase, long before the assignment is encountered. You would have gotten another clue by printing t.type as well as t.value in your p_error function. (Or, of course, by testing the tokeniser before trying to parse anything.)

    If you read through to the end of the section of the Ply manual I linked, you'll find a suggestion for how to handle keyword tokens, using a scanner function and auxiliary dictionary of keywords. I strongly suggest you use that as your model.

    Also note that you require a colon to be preceded by a space character:

    t_COLON_SYM                 = r' :'
    

    But your sample input does not have spaces before colons.