3

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              
   '''
satish
  • 329
  • 2
  • 11

2 Answers2

2

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.

swstephe
  • 1,840
  • 11
  • 17
  • 1
    "envvar" is one single production spreading on multiple lines. So i cannot use alternatives. I am looking for the feature Yacc supports by allowing comments to be inserted on each line in between a production. For e.g. envvar: T_EV /* EV_ */ T_ID /* 2: environment variable name */ T_COLON /* : */ T_INT_VAL /* 4: type: 0 int, 1 float, 0 string, 0 data */ – satish Nov 26 '13 at 19:00
  • okay, then the answer from @ioannis-filippidis is probably better. – swstephe Sep 04 '15 at 22:02
0

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.

0 _
  • 10,524
  • 11
  • 77
  • 109