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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:12:29+00:00 2026-05-14T18:12:29+00:00

Parsing binary sums / products are easy, but I’m having troubles defining a grammar

  • 0

Parsing binary sums / products are easy, but I’m having troubles defining a grammar that parses

a + b * c + d + e

as

sum(a, prod(b, c), d, e)

My initial (naive) attempt generated 61 shift / reduce conflicts.

I’m using java cup (but I suppose a solution for any other parser generator would be easily translated).

  • 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-14T18:12:30+00:00Added an answer on May 14, 2026 at 6:12 pm

    The following ANTLR grammar:

    parse
      :  exp EOF
      ;
    
    exp
      :  add_exp
      ;
    
    add_exp
      :  mul_exp ('+' mul_exp)* 
      ;
    
    mul_exp
      :  atom ('*' atom)* 
      ;
    
    atom
      :  Number
      |  '(' exp ')'
      ;
    
    Number
      :  'a'..'z'
      ;
    

    parses the input a + b * c + d + e as:

    alt text http://img266.imageshack.us/img266/7099/17212574.png

    As you can see, the mul_exp is the furthest away in the tree and (using an appropriate “walk” through your tree) will be evaluated first.

    and the input a + b * (c + d) + e is parsed as:

    alt text http://img688.imageshack.us/img688/2207/89332200.png

    The images were generated with ANTLRWorks.

    EDIT:

    A tool like ANTLRWorks makes debugging a grammar a breeze! For example, if I click on the atom rule in the grammar above, the following is automatically generated and displayed at the bottom of the screen:

    alt text http://img340.imageshack.us/img340/6793/53395907.png

    Of course, that rule isn’t complex at all, but when you do get to work with more complex rules, it’s pretty darn easy to visualize them like that.

    HTH.

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

Sidebar

Related Questions

I have recently taken up the activity of parsing binary data with Python but
I'm having some difficulties while parsing a binary STL file with Python (2.7.1 32-bit
I'm parsing a binary file in javascript that is storing two pieces of information
I'm parsing a binary file format. It encodes an integer using four bytes in
Not parsing the parameters in the beginning but parse the input string read from
I have done XML parsing before but never on a massive scale. If I'm
I am parsing a binary file using a specification. The file comes in big-endian
I am parsing binary file. File size can be large. I want to search
I am parsing binary files and have to implement a CRC algorithm to ensure
I am parsing a binary log file. The log file is formatted as follows:

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.