3
votes

Is there a way to define a multi-line production with the following syntax ? PLY expects : before ID implying one production per line.

def p_envvar(p):
   ''' 
   envvar : EV                    \
            ID                    \
            COLON                 \ 
            INT_VAL               \ 
            BOX_OPEN              \ 
            INT_VAL               \ 
            SEP                   \ 
            INT_VAL               \ 
            BOX_CLOSE             \ 
            STRING_VAL            \ 
            INT_VAL               \ 
            INT_VAL               \ 
            DUMMY_NODE_VECTOR     \ 
            comma_identifier_list \ 
            SEMICOLON              
   '''
2

2 Answers

2
votes

You should use the "pipe" to separate alternate rules:

def p_envvar(p):
    ''' 
    envvar : EV
           | ID
           | COLON
           | INT_VAL
           | BOX_OPEN
           | INT_VAL
           | SEP
           | INT_VAL
           | BOX_CLOSE
           | STRING_VAL
           | INT_VAL
           | DUMMY_NODE_VECTOR
           | comma_identifier_list
           | SEMICOLON  
    '''

But using so many alternates hints to me that you probably need to simplify, (you can declare different functions which simplify to the same state:

def p_envar_ev():
    """envvar : EV"""

def p_envvar_id():
    """envvar : ID"""

... etc. Which is easier to read than using a big if block to handle each possible case.

0
votes

There is a way to spread a long docstring over multiple lines in a way that PLY understands it:

def p_expr(p):
    ("""process_type : this is a very long """
     """ production rule """
     """ that does not fit on a single line """)

Credit for this magic goes to the OP of this. Notice the parentheses: they are much better than escaping with \, because they are compliant with PEP 8, so syntax checkers don't complain.