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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:32:42+00:00 2026-05-18T20:32:42+00:00

I have defined simple grammar for parsing string and number using Treetop as below.

  • 0

I have defined simple grammar for parsing string and number using Treetop as below.

grammar Simple
    rule value
        number / string
    end 

    rule string
        word space string
        /
        word
    end

    rule word
        [0-9a-zA-Z]+
    end

    rule number
        [1-9] [0-9]*
    end

    rule space
        ' '+
    end
end

Ruby:

parser = SimpleParser.new
parser.parse('123abc wer') # => nil

I expect the parser to return string node but look like the parser could not understand the input. Any idea would be appreciated.

  • 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-18T20:32:42+00:00Added an answer on May 18, 2026 at 8:32 pm

    In Treetop (and PEGs in general, actually) the choice operator is ordered, unlike most other parsing formalisms.

    So, in

    rule value
      number / string
    end
    

    you are telling Treetop that you prefer number over string.

    Your input starts with 1, which matches both number and string (through word), but you told Treetop to prefer the number interpretation, so it parses it as a number. When it comes to the a in the input, it has no more rules to apply, and thus it returns nothing (nil), because in Treetop it is an error to not consume the entire input stream.

    If you simply reverse the order of the choice, the entire input will interpreted as a string instead of a number:

    SyntaxNode+String0 offset=0, "123abc wer" (word,space,string):
      SyntaxNode offset=0, "123abc":
        SyntaxNode offset=0, "1"
        SyntaxNode offset=1, "2"
        SyntaxNode offset=2, "3"
        SyntaxNode offset=3, "a"
        SyntaxNode offset=4, "b"
        SyntaxNode offset=5, "c"
      SyntaxNode offset=6, " ":
        SyntaxNode offset=6, " "
      SyntaxNode offset=7, "wer":
        SyntaxNode offset=7, "w"
        SyntaxNode offset=8, "e"
        SyntaxNode offset=9, "r"
    

    Or, you could keep the order as it is, but allow the value rule to be matched multiple times. Either insert a new top-level rule like this:

    rule values
      value+
    end
    

    or modify the value rule like this:

    rule value
      (number / string)+
    end
    

    Which will give you an AST roughly like this:

    SyntaxNode offset=0, "123abc wer":
      SyntaxNode+Number0 offset=0, "123":
        SyntaxNode offset=0, "1"
        SyntaxNode offset=1, "23":
          SyntaxNode offset=1, "2"
          SyntaxNode offset=2, "3"
          SyntaxNode+String0 offset=3, "abc wer" (word,space,string):
            SyntaxNode offset=3, "abc":
              SyntaxNode offset=3, "a"
              SyntaxNode offset=4, "b"
          SyntaxNode offset=5, "c"
        SyntaxNode offset=6, " ":
          SyntaxNode offset=6, " "
        SyntaxNode offset=7, "wer":
          SyntaxNode offset=7, "w"
          SyntaxNode offset=8, "e"
          SyntaxNode offset=9, "r"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined simple grammar for parsing string and number using Treetop as below.
I'm using WPF with .NET 3.0. I have a relatively simple DataTemplate defined as
I have a VERY simple 3D space defined in WPF, which defines a 3D
I have defined a very simple database as follow : I'm using the hibernate
I have defined a fieldset in HTML, and applied the following (simple) CSS rules
I have a pretty simple UIScrollView defined inside Interface Builder. I've pasted in some
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Well, the question is kinda simple. I have a object defined as: public class
I have a simple grammar: grammar sample; options { output = AST; } assignment
I have a defined a simple template class. In this class I define a

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.