Python PLY zero or more occurrences of the parsing element

I use Python with PLY to parse LISP-like S-expressions, and when parsing a function call, there can be zero or more arguments. How can I put this in yacc code. This is my function so far:

def p_EXPR(p):
    '''EXPR : NUMBER
            | STRING
            | LPAREN funcname [EXPR] RPAREN'''
    if len(p) == 2:
        p[0] = p[1]
    else:
        p[0] = ("Call", p[2], p[3:-1])

I need to replace "[EXPR]" with something that allows me to use zero or more EXPR. How can i do this?

+3
source share
2 answers

How about this:

EXPR : NUMBER
        | STRING
        | LPAREN funcname EXPR_REPEAT RPAREN
EXPR_REPEAT: /*nothing*/
        | EXPR EXPR_REPEAT
+2
source

, Context Free, Parsing? , PLY - , .

0

Source: https://habr.com/ru/post/1744625/


All Articles