Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:48:31+00:00 2026-06-09T19:48:31+00:00

I’m trying to write a grammar to parse a simple language to describe drum

  • 0

I’m trying to write a grammar to parse a simple language to describe drum loops, using Clojure and amotoen. The language looks like this:–

# Test Loop
# this is a comment

BPM: 100

Samples:
 BD: bd.wav
 SD: sd.wav
CHH: chh.wav
CSH: csh.wav

Body:
 BD: /---/---/---/---
 SD: ---/--/--/-/--/-
CHH: --/---/---/---/-
CSH: /---------------

# this is another comment

I’ve defined the grammar as follows:–

(def g {
        :Whitespace '(| \space \newline \tab \, :Comment)
        :_* '(* :Whitespace)
        :_ [:Whitespace '(* :Whitespace)]
        :Comment [\# '(* (% \newline)) \newline]
        :BPM [\B \P \M \: :_* '(* :Number) \newline]
        :Number '(* :Digit)
        :Digit (a/lpegs '| "0123456789")
        :Samples [\S \a \m \p \l \e \s \: \newline '(* :SampleDef)]
        :SampleDef [:_* :Name \: :_* :File \newline]
        :Name '(* (% \:))
        :File '(* (% \newline))
        :Body [\B \o \d \y \: \newline '(* :Pattern)]
        :Pattern [:_* :Name \: :_* '(* (| \/ \-)) '(| \newline \$)]
        :Document [:_* :BPM :_* :Samples :_* :Body :_* \$]
       })

When I call pegasus on each part of a sample file individually, they are parsed correctly. For example:–

(pprint
  (a/pegasus
    :Body
    g
    (a/wrap-string
      "Body:\n
        BD: /---/---/---/---\n
        SD: ---/--/--/-/--/-\n
       CHH: --/---/---/---/-\n
       CSH: /---------------\n")))

However, when I call (pprint (a/pegasus :Document g (a/wrap-string (slurp "sample.orc")))), all I get is nil. Similarly if I replace (a/wrap-string (slurp "sample.orc")) with a string containing the text contained in sample.orc.

So, my question is: can anyone spot what’s wrong with my grammar? I’m all out of ideas, and I’ve been staring at it for a few days now. I’m sure it’s something embarrassingly simple, but I just can’t see it!

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:48:32+00:00Added an answer on June 9, 2026 at 7:48 pm

    The :Samples rule consumes the :Body, :File can be empty and end-of-input should be marked by :$ not \$. Here’s an amended grammar:

    (def g                                                                                                                                                                  
      {                                                                                                                                                                     
       :Whitespace '(| \space \tab \, :Comment)                                                                                                                             
       :n* '(* (| \newline :Comment))                                                                                                                                       
       :_* '(* :Whitespace)                                                                                                                                                 
       :_ [:Whitespace '(* :Whitespace)]                                                                                                                                    
       :Comment [\# '(* (% \newline)) \newline]                                                                                                                             
       :BPM [\B \P \M \: :_* '(* :Number) \newline]                                                                                                                         
       :Number '(* :Digit)                                                                                                                                                  
       :Digit (a/lpegs '| "0123456789")                                                                                                                                     
       :Samples [\S \a \m \p \l \e \s \: \newline '(* :SampleDef)]                                                                                                          
       :SampleDef [:_* :Name \: :_* :File \newline]                                                                                                                         
       :Name '[(% \:) (* (% \:))]                                                                                                                                           
       :File '[(% \newline) (* (% \newline))]                                                                                                                               
       :Body [\B \o \d \y \: \newline '(* :Pattern)]                                                                                                                        
       :Pattern [:_* :Name \: :_* '(* (| \/ \-)) '(| \newline :$)]                                                                                                          
       :Document [:n* :BPM :n* :Samples :n* :Body :n* :$]                                                                                                                   
       })
    
    ;; sample.orc contains the example input from the question text
    (pprint (a/pegasus :Document g (a/wrap-string (slurp "sample.orc"))))
    
    ;; output:
    {:Document
     [{:n*
       ({:Comment [\# (\space \T \e \s \t \space \L \o \o \p) \newline]}
        {:Comment
         [\#
          (\space
           \t
           \h
           \i
           \s
           \space
           \i
           \s
           \space
           \a
           \space
           \c
           \o
           \m
           \m
           \e
           \n
           \t)
          \newline]}
        \newline)}
      {:BPM
       [\B
        \P
        \M
        \:
        {:_* {:Whitespace \space}}
        {:Number ({:Digit \1} {:Digit \0} {:Digit \0})}
        \newline]}
      {:n* \newline}
      {:Samples
       [\S
        \a
        \m
        \p
        \l
        \e
        \s
        \:
        \newline
        ({:SampleDef
          [{:_* {:Whitespace \space}}
           {:Name [\B \D]}
           \:
           {:_* {:Whitespace \space}}
           {:File [\b (\d \. \w \a \v)]}
           \newline]}
         {:SampleDef
          [{:_* {:Whitespace \space}}
           {:Name [\S \D]}
           \:
           {:_* {:Whitespace \space}}
           {:File [\s (\d \. \w \a \v)]}
           \newline]}
         {:SampleDef
          [{:_* ()}
           {:Name [\C (\H \H)]}
           \:
           {:_* {:Whitespace \space}}
           {:File [\c (\h \h \. \w \a \v)]}
           \newline]}
         {:SampleDef
          [{:_* ()}
           {:Name [\C (\S \H)]}
           \:
           {:_* {:Whitespace \space}}
           {:File [\c (\s \h \. \w \a \v)]}
           \newline]})]}
      {:n* \newline}
      {:Body
       [\B
        \o
        \d
        \y
        \:
        \newline
        ({:Pattern
          [{:_* {:Whitespace \space}}
           {:Name [\B \D]}
           \:
           {:_* {:Whitespace \space}}
           (\/ \- \- \- \/ \- \- \- \/ \- \- \- \/ \- \- \-)
           \newline]}
         {:Pattern
          [{:_* {:Whitespace \space}}
           {:Name [\S \D]}
           \:
           {:_* {:Whitespace \space}}
           (\- \- \- \/ \- \- \/ \- \- \/ \- \/ \- \- \/ \-)
           \newline]}
         {:Pattern
          [{:_* ()}
           {:Name [\C (\H \H)]}
           \:
           {:_* {:Whitespace \space}}
           (\- \- \/ \- \- \- \/ \- \- \- \/ \- \- \- \/ \-)
           \newline]}
         {:Pattern
          [{:_* ()}
           {:Name [\C (\S \H)]}
           \:
           {:_* {:Whitespace \space}}
           (\/ \- \- \- \- \- \- \- \- \- \- \- \- \- \- \-)
           \newline]})]}
      {:n*
       (\newline
        {:Comment
         [\#
          (\space
           \t
           \h
           \i
           \s
           \space
           \i
           \s
           \space
           \a
           \n
           \o
           \t
           \h
           \e
           \r
           \space
           \c
           \o
           \m
           \m
           \e
           \n
           \t)
          \newline]})}
      :$]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.