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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:51:33+00:00 2026-06-11T06:51:33+00:00

I am having trouble avoiding left-recursion in this simple expression parser I’m working on.

  • 0

I am having trouble avoiding left-recursion in this simple expression parser I’m working on. Essentially, I want to parse the equation ‘f x y’ into two expressions ‘f x’ and ‘(f x) y’ (with implicit parentheses). How can I do this while avoiding left-recursion and backtracking? Does there have to be an intermediate step?

#!/usr/bin/env ruby
require 'rubygems'
require 'treetop'
Treetop.load_from_string DATA.read

parser = ExpressionParser.new

p parser.parse('f x y').value

__END__
grammar Expression
   rule equation
      expression (w+ expression)*
   end
   rule expression
      expression w+ atom
   end
   rule atom
      var / '(' w* expression w* ')'
   end
   rule var
      [a-z]
   end
   rule w
      [\s\n\t\r]
   end
end
  • 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-11T06:51:35+00:00Added an answer on June 11, 2026 at 6:51 am

    You haven’t given enough information about your desired result. In particular, do you expect “f(a b) y” to parse as “(f(a(b))) y”? I assume you do… which means that a function not followed by an open parenthesis has arity one.

    So you want to say:

    rule equation
      expression w* var / expression w* parenthesised_list
    end
    rule parenthesised_list
      '(' w* ( expression w* )+ ')'
    end
    

    If on the other hand you have external (to the grammar) knowledge of the arity of f, and you want to iterate “expression” exactly that many times – as happens in parsing TeX for example – then you will need to use a semantic predicate &{|s| …} inside the iterated expression list). Beware that the argument passed to the block of a sempred is not a SyntaxNode (which cannot yet be constructed because this sequence sub-rule has not yet succeeded) but the accumulated array of nodes so far in the sequence. The truthiness of the block return value dictates the parse result and can stop the iteration.

    Another tool you might consider using is lookahead (!stuff_I_dont_expect_to_follow or &stuff_that_must_follow).

    You can also ask such questions in http://groups.google.com/group/treetop-dev

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble with this..I'm sure I'm missing something simple but I simply want the
Having trouble framing this assertion in Rspec for my rails app. A User has
Having trouble getting this syntax right: SELECT DISTINCT id FROM metadata WHERE (meta_key =
Having trouble with this -- a couple of other related posts out there, but
Having trouble with an sql statement to retrieve a record between two timestamps. This
I am having trouble figuring out exactly how to approach this problem and am
Having trouble grouping active forum sum results by wildcards. Ultimately, I want all the
Having trouble with this code, wondering if I'm trying to achieve something impossible. I'm
Having trouble with selectors/transversing. Basically, I want the popup div to open when the
Having trouble finding a solution for my situation here. Sorry if this has been

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.