alex / rply

An attempt to port David Beazley's PLY to RPython, and give it a cooler API.
BSD 3-Clause "New" or "Revised" License
381 stars 60 forks source link

How to setup productions for optional (0 or more) #81

Closed FrankC01 closed 6 years ago

FrankC01 commented 6 years ago

With the following EBNF:

start = MODULE SYMBOL {declaration}

declaration = INCLUDE SYMBOL
                    | VAR SYMBOL expression
                    | FUNC SYMBOL LIST {expression}

It's not clear how to declare the pg.production(...) Basically, how to indicate 0 or more occurrences of productions?

Any help would be appreciated.

FrankC01 commented 6 years ago

I think I solved by creating multiple productions to account for the variations. E.g.:

pg.production('declaration: INCLUDE SYMBOL')
pg.production('declaration: INCLUDE SYMBOL declaration')
pg.production('declaration: VAR SYMBOL expression')
pg.production('declaration: VAR SYMBOL expression declaration')
pg.production('declaration: FUNC SYMBOL LIST expression')
pg.production('declaration: FUNC SYMBOL LIST expression declaration')

Haven't written complex inputs yet but works for simple case. Is this the right direction or is there a more terse way to do?