PLY/Yacc/BNF help

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Posts: 26
Joined: Wed Jan 16, 2013 5:27 pm UTC
Location: Durham, NC

PLY/Yacc/BNF help

Postby davedrowsy » Thu Jan 24, 2013 6:00 pm UTC

I'm working on a parser in Python for a music programming language I'm developing. In the process, I'm learning how to use PLY for the first time. I successfully built the lexer, which correctly tokenizes each symbol of MPL code. I'm having some difficulty with the parser, though... wondering if those of you with PLY, Yacc and/or BNF experience could help me with this.

I'm wondering if it's possible to create grammar syntax rules that include an infinite number of "things"... for example, I want to write a "chord" rule that includes an infinite number of "notes" (tokens matching the "note" rule), divided by the "/" character. So a chord could be "note / note," "note / note / note," "note / note / note / note / note / note / note / note / note / note" (epic chord!), and so on.

Can I do something like this?

Code: Select all

chord   :    note / note
        |    note / chord

Would that work the way I'm thinking? or is there another way to do it?

As a side note, I wonder if it wouldn't be too difficult to just write my parser from scratch/regex instead of using PLY? I started doing that anyway until someone suggested that PLY would make things easier. The syntax of my language isn't all that complicated. Basically the notes, chords, and other musical events are presented in order, and the parser needs to go through them one by one and add them (in order) to a MIDI file using the mingus module. I'm kind of considering ditching PLY and just doing it by hand... any thoughts on that?

Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Re: PLY/Yacc/BNF help

Postby EvanED » Thu Jan 24, 2013 6:51 pm UTC

davedrowsy wrote:Can I do something like this?

Code: Select all

chord   :    note / note
        |    note / chord

Yep. If you wanted to allow single notes as a chord -- abusing the term a bit, but whatever -- you could say chord : note | note / chord.

(I'm assuming you have the concrete syntax of PLY correct; I am not familiar. But in terms of the basic idea of writing a recursive rule, you're spot on.)

Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests