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

  • Home
  • SEARCH
  • 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 7673671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:33:57+00:00 2026-05-31T16:33:57+00:00

I’m trying to write a grammar for a language which allows the following expressions:

  • 0

I’m trying to write a grammar for a language which allows the following expressions:

  1. Function calls of the form f args (note: no parentheses!)
  2. Addition (and more complex expressions but that’s not the point here) of the form a + b

For example:

f 42       => f(42)
42 + b     => (42 + b)
f 42 + b   => f(42 + b)

The grammar is unambiguous (every expression can be parsed in exactly one way) but I don’t know how to write this grammar as a PEG since both productions potentially start with the same token, id. This is my wrong PEG. How can I rewrite it to make it valid?

expression ::= call / addition

call ::= id addition*

addition ::= unary
           ( ('+' unary)
           / ('-' unary) )*

unary ::= primary
        / '(' ( ('+' unary)
              / ('-' unary)
              / expression)
          ')'

primary ::= number / id

number ::= [1-9]+

id ::= [a-z]+

Now, when this grammar tries to parse the input “a + b” it parses “a” as a function call with zero arguments and chokes on “+ b”.

I’ve uploaded a C++ / Boost.Spirit.Qi implementation of the grammar in case anybody wants to play with it.


(Note that unary disambiguates unary operations and additions: In order to call a function with a negative number as an argument, you need to specify parentheses, e.g. f (-1).)

  • 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-31T16:33:58+00:00Added an answer on May 31, 2026 at 4:33 pm

    As proposed in chat you could start out with something like:

    expression = addition | simple;
    
    addition = simple >>
        (  ('+' > expression)
         | ('-' > expression)
        );
    
    simple = '(' > expression > ')' | call | unary | number;
    
    call = id >> *expression;
    
    unary = qi::char_("-+") > expression;
    
    // terminals
    id = qi::lexeme[+qi::char_("a-z")];
    number = qi::double_;
    

    Since then I implemented this in C++ with an AST presentation, so you can get a feel for how this grammar actually build the expression tree by pretty printing it.

    All source code is on github: https://gist.github.com/2152518

    There are two versions (scroll down to ‘Alternative’ to read more


    Grammar:

    template <typename Iterator>
    struct mini_grammar : qi::grammar<Iterator, expression_t(), qi::space_type> 
    {
        qi::rule<Iterator, std::string(),  qi::space_type> id;
        qi::rule<Iterator, expression_t(), qi::space_type> addition, expression, simple;
        qi::rule<Iterator, number_t(),     qi::space_type> number;
        qi::rule<Iterator, call_t(),       qi::space_type> call;
        qi::rule<Iterator, unary_t(),      qi::space_type> unary;
    
        mini_grammar() : mini_grammar::base_type(expression) 
        {
            expression = addition | simple;
    
            addition = simple [ qi::_val = qi::_1 ] >> 
               +(  
                   (qi::char_("+-") > simple) [ phx::bind(&append_term, qi::_val, qi::_1, qi::_2) ] 
                );
    
            simple = '(' > expression > ')' | call | unary | number;
    
            call = id >> *expression;
    
            unary = qi::char_("-+") > expression;
    
            // terminals
            id = qi::lexeme[+qi::char_("a-z")];
            number = qi::double_;
        }
    };
    

    The corresponding AST structures are defined quick-and-dirty using the very powerful Boost Variant:

    struct addition_t;
    struct call_t;
    struct unary_t;
    typedef double number_t;
    
    typedef boost::variant<
        number_t,
        boost::recursive_wrapper<call_t>,
        boost::recursive_wrapper<unary_t>,
        boost::recursive_wrapper<addition_t>
        > expression_t;
    
    struct addition_t
    {
        expression_t lhs;
        char binop;
        expression_t rhs;
    };
    
    struct call_t
    {
        std::string id;
        std::vector<expression_t> args;
    };
    
    struct unary_t
    {
        char unop;
        expression_t operand;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(addition_t, (expression_t, lhs)(char,binop)(expression_t, rhs));
    BOOST_FUSION_ADAPT_STRUCT(call_t,     (std::string, id)(std::vector<expression_t>, args));
    BOOST_FUSION_ADAPT_STRUCT(unary_t,    (char, unop)(expression_t, operand));
    

    In the full code, I’ve also overloaded operator<< for these structures.


    Full Demo

    //#define BOOST_SPIRIT_DEBUG
    #include <iostream>
    #include <iterator>
    #include <string>
    
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <boost/fusion/adapted.hpp>
    #include <boost/optional.hpp>
    
    namespace qi = boost::spirit::qi;
    namespace phx= boost::phoenix;
    
    struct addition_t;
    struct call_t;
    struct unary_t;
    typedef double number_t;
    
    typedef boost::variant<
        number_t,
        boost::recursive_wrapper<call_t>,
        boost::recursive_wrapper<unary_t>,
        boost::recursive_wrapper<addition_t>
        > expression_t;
    
    struct addition_t
    {
        expression_t lhs;
        char binop;
        expression_t rhs;
    
        friend std::ostream& operator<<(std::ostream& os, const addition_t& a) 
            { return os << "(" << a.lhs << ' ' << a.binop << ' ' << a.rhs << ")"; }
    };
    
    struct call_t
    {
        std::string id;
        std::vector<expression_t> args;
    
        friend std::ostream& operator<<(std::ostream& os, const call_t& a)
            { os << a.id << "("; for (auto& e : a.args) os << e << ", "; return os << ")"; }
    };
    
    struct unary_t
    {
        char unop;
        expression_t operand;
    
        friend std::ostream& operator<<(std::ostream& os, const unary_t& a)
            { return os << "(" << a.unop << ' ' << a.operand << ")"; }
    };
    
    BOOST_FUSION_ADAPT_STRUCT(addition_t, (expression_t, lhs)(char,binop)(expression_t, rhs));
    BOOST_FUSION_ADAPT_STRUCT(call_t,     (std::string, id)(std::vector<expression_t>, args));
    BOOST_FUSION_ADAPT_STRUCT(unary_t,    (char, unop)(expression_t, operand));
    
    void append_term(expression_t& lhs, char op, expression_t operand)
    {
        lhs = addition_t { lhs, op, operand };
    }
    
    template <typename Iterator>
    struct mini_grammar : qi::grammar<Iterator, expression_t(), qi::space_type> 
    {
        qi::rule<Iterator, std::string(),  qi::space_type> id;
        qi::rule<Iterator, expression_t(), qi::space_type> addition, expression, simple;
        qi::rule<Iterator, number_t(),     qi::space_type> number;
        qi::rule<Iterator, call_t(),       qi::space_type> call;
        qi::rule<Iterator, unary_t(),      qi::space_type> unary;
    
        mini_grammar() : mini_grammar::base_type(expression) 
        {
            expression = addition | simple;
    
            addition = simple [ qi::_val = qi::_1 ] >> 
               +(  
                   (qi::char_("+-") > simple) [ phx::bind(&append_term, qi::_val, qi::_1, qi::_2) ] 
                );
    
            simple = '(' > expression > ')' | call | unary | number;
    
            call = id >> *expression;
    
            unary = qi::char_("-+") > expression;
    
            // terminals
            id = qi::lexeme[+qi::char_("a-z")];
            number = qi::double_;
    
            BOOST_SPIRIT_DEBUG_NODE(expression);
            BOOST_SPIRIT_DEBUG_NODE(call);
            BOOST_SPIRIT_DEBUG_NODE(addition);
            BOOST_SPIRIT_DEBUG_NODE(simple);
            BOOST_SPIRIT_DEBUG_NODE(unary);
            BOOST_SPIRIT_DEBUG_NODE(id);
            BOOST_SPIRIT_DEBUG_NODE(number);
        }
    };
    
    std::string read_input(std::istream& stream) {
        return std::string(
            std::istreambuf_iterator<char>(stream),
            std::istreambuf_iterator<char>());
    }
    
    int main() {
        std::cin.unsetf(std::ios::skipws);
        std::string const code = read_input(std::cin);
        auto begin = code.begin();
        auto end = code.end();
    
        try {
            mini_grammar<decltype(end)> grammar;
            qi::space_type space;
    
            std::vector<expression_t> script;
            bool ok = qi::phrase_parse(begin, end, *(grammar > ';'), space, script);
    
            if (begin!=end)
                std::cerr << "Unparsed: '" << std::string(begin,end) << "'\n";
    
            std::cout << std::boolalpha << "Success: " << ok << "\n";
    
            if (ok)
            {
                for (auto& expr : script)
                    std::cout << "AST: " << expr << '\n';
            }
        }
        catch (qi::expectation_failure<decltype(end)> const& ex) {
            std::cout << "Failure; parsing stopped after \""
                      << std::string(ex.first, ex.last) << "\"\n";
        }
    }
    

    Alternative:

    I have an alternative version that build addition_t iteratively instead of recursively, so to say:

    struct term_t
    {
        char binop;
        expression_t rhs;
    };
    
    struct addition_t
    {
        expression_t lhs;
        std::vector<term_t> terms;
    };
    

    This removes the need to use Phoenix to build the expression:

        addition = simple >> +term;
    
        term = qi::char_("+-") > simple;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some dcg grammar in prolog which will describe language of
I am trying to write a grammar for our custom rule engine which uses
I'm trying to write a LEPL grammar to describe a Boolean search language. This
Im trying to write an if function which checks the width of an image,
I'm trying to write a grammar to evaluate expressions. I've started with the given
I'm trying write a query to find records which don't have a matching record
Im trying to write a function for adding category: function addCategory() { $cname =
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a blog post which includes a code segment inside a
I'm trying to write a grammar for various time formats (12:30, 0945, 1:30-2:45, ...)

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.