开发者

Python PLY zero or more occurrences of a parsing item

开发者 https://www.devze.com 2022-12-30 03:14 出处:网络
I am using 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 into the yacc code. This is my function s开发者_开发

I am using 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 into the yacc code. This is my function s开发者_开发技巧o 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 zero or more EXPR's. How can I do this?


How about this:

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


Are you sure you want a Context Free Grammar and not a Parsing Expression Grammar? Also, in my experience the design of PLY couples the grammar and parsing and post-processing very badly so I would recommend an implementation with more modular design.

0

精彩评论

暂无评论...
验证码 换一张
取 消