def p_xxx(p): "xxx : xxx2" p[0] = [p[1]] def p_elements2(p): "xxx : xxx xxx2" p[0] = p[1] + [p[2]] i would use this as template and then have the uncion macro what ever you want to call it replace the xxx and xxx2 1 by 1