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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:45:34+00:00 2026-05-26T16:45:34+00:00

I’m currently trying to describe some basic Ruby grammar but I’m now stuck with

  • 0

I’m currently trying to describe some basic Ruby grammar but I’m now stuck with function definition. Indeed, I don’t know how to handle ‘n’ argument. Here is the code I use to handle functions containing from 0 to 2 args :

  rule function_definition
    'def' space? identifier space? '(' space? expression? space? ','? expression? space? ')'
      block
    space? 'end' <FunctionDefinition>
  end  

How could I do to handle ‘n’ argument ? Is there any recursive way to do that ?

EDIT :

I wanted to highlight the fact that I need the arguments to be in the result tree. Like :

 Argument offset=42, "arg1"
 Argument offset=43, "arg2"
 Argument offset=44, "arg3"

So I need to do a cstom SyntaxNode Subclass declaration, just like I did for function_definition rule for instance.

  • 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-26T16:45:35+00:00Added an answer on May 26, 2026 at 4:45 pm

    You want something like (untested):

    'def' space? identifier space? '(' space? ( expression ( space? ',' expression )* )? space?  ')'
    

    (NB if this is a ruby style def then the parens are also optional in the case where there are no arguments)

    Edit to demonstrate extracting the arguments from the parse tree — here I spit out the text_value of each argument (FunctionArg) syntax node but you could of course do anything:

    foo.rb:

    # Prepend current directory to load path
    $:.push('.')
    
    # Load treetop grammar directly without compilation
    require 'polyglot'
    require 'treetop'
    require 'def'
    
    # Classes for bespoke nodes
    class FunctionDefinition < Treetop::Runtime::SyntaxNode ; end
    class FunctionArg < Treetop::Runtime::SyntaxNode ; end
    
    # Some tests
    [
      'def foo() block end',
      'def foo(arg1) block end',
      'def foo(arg1, arg2) block end',
      'def foo(arg1, arg2, arg3) block end',
    ].each do |test|
      parser = DefParser.new
      tree = parser.parse( test )
      raise RuntimeError, "Parsing failed on line:\n#{test}" unless tree
      puts test
      puts "identifier=#{tree.function_identifier}"
      puts "args=#{tree.function_args.inspect}"
      puts
    end
    

    def.tt:

    grammar Def
    
      # Top level rule: a function
      rule function_definition
        'def' space identifier space? '(' space? arg0 more_args space? ')' space block space 'end' <FunctionDefinition>
        {
          def function_identifier
            identifier.text_value
          end
          def function_args
            arg0.is_a?( FunctionArg ) ? [ arg0.text_value ] + more_args.args : []
          end
        }
      end
    
      # First function argument
      rule arg0
        argument?
      end
    
      # Second and further function arguments
      rule more_args
        ( space? ',' space? argument )* 
        {
          def args
            elements.map { |e| e.elements.last.text_value }
          end
        }
      end
    
      # Function identifier
      rule identifier
        [a-zA-Z_] [a-zA-Z0-9_]*
      end
    
      # TODO Dummy rule for function block
      rule block
        'block'
      end
    
      # Function argument
      rule argument
        [a-zA-Z_] [a-zA-Z0-9_]* <FunctionArg>
      end
    
      # Horizontal whitespace (htab or space character).
      rule space
        [ \t]
      end
    
    end
    

    Output:

    def foo() block end
    identifier=foo
    args=[]
    
    def foo(arg1) block end
    identifier=foo
    args=["arg1"]
    
    def foo(arg1, arg2) block end
    identifier=foo
    args=["arg1", "arg2"]
    
    def foo(arg1, arg2, arg3) block end
    identifier=foo
    args=["arg1", "arg2", "arg3"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.