简体   繁体   中英

Grammar - infinite recursion

While trying to define the official grammar for the oz language, I hit an infinite recursion. I narrowed it down (I think) to these rules:

<declarationPart> ::= <variable> | <pattern> '=' <expression> | <statement>

<pattern> ::= ['!'] <variable>

in pyparser:

pattern             = Forward()
pattern     << (Optional(exclam_tkn) + variable)

declarationPart = ( variable \
            | (pattern + equal + expression) \
            | statement)

So, in declarationPart, variable can appear in variable and pattern. In the order as above, I don't have the recursion problem but the 'equal' is not detected and only the variable gets parsed. When I put 'variable' as second entry, I do have the infinite recursion.

The pattern rule is simplified - I do need it separately.

I can understand the failure mechanism, but I don't know if this is a language definition problem, or if I'm neglecting something in the grammar...

How do I make this work? Or a suggestion to debug the parsing process?

I'm not familiar with pyparsing in particular, but for general parsing purposes I would probably just combine the two rules into one, like so

<declarationPart> ::= ['!'] <variable> [ '=' <expression> ] | <statement>

and then deal with flagging eventual illegal constructs that are accidentally accepted by this grammar in the semantic analysis phase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM