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 6831453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:44:21+00:00 2026-05-26T22:44:21+00:00

I have a treetop grammar with only two rules: grammar RCFAE rule num [0-9]+

  • 0

I have a treetop grammar with only two rules:

grammar RCFAE
    rule num
        [0-9]+ <Num>
    end

    rule identifier
        [a-zA-Z] [a-zA-Z]* <ID>
    end
end

I’m trying to parse simple strings (“A” and “5”). The “5” is recognized as a Num if I put that rule first, and returns nil if i put that rule second. Similarly, “A” is recognized as an ID if I put that rule first, and returns nil if I put that rule second. I can’t understand how these two rules overlap in any way. It’s driving me crazy!

Is there something I’m missing or don’t understand about treetop or regular expressions? Thanks in advance for your help.

  • 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-05-26T22:44:22+00:00Added an answer on May 26, 2026 at 10:44 pm

    Treetop expects the first rule to be the “main rule”. It doesn’t try to apply all the rules you defined until one matches – it only applies the main rule and if that does not match, it fails.

    To do what you want, you need to add a main rule which might be a num or an identifier, like this:

    grammar RCFAE
        rule expression
            num / identifier
        end
    
        rule num
            [0-9]+ <Num>
        end
    
        rule identifier
            [a-zA-Z] [a-zA-Z]* <ID>
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to write a Treetop grammar to parse Simple Game Format
I have defined simple grammar for parsing string and number using Treetop as below.
I have defined simple grammar for parsing string and number using Treetop as below.
I have this spec: it 'can parse armies with only section headers' do list
I want to make a simple JSP parser by using Treetop. Now, I have
I'm trying to teach myself Ruby's Treetop grammar generator. I am finding that not
I'm trying to pick measurements out of a string in my treetop grammar. I'm
have been trying couple of hours now to make my iphone app universal. The
I have been trying to run the rake db:migrate command but it ain't working.
This is driving me farking insane. I'm trying to create a simple beginner app

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.