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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:07:21+00:00 2026-05-25T22:07:21+00:00

I’m trying to solve DCG grammar in prolog and succeeded upto a point, i’m

  • 0

I’m trying to solve DCG grammar in prolog and succeeded upto a point, i’m stuck in evaluating the expressions involving braces like these.
expr( T, [’(’, 5, +, 4, ’)’, *, 7], []),

expr(Z) --> num(Z).
expr(Z) --> num(X), [+], expr(Y), {Z is X+Y}.
expr(Z) --> num(X), [-], expr(Y), {Z is X-Y}.
expr(Z) --> num(X), [*], expr(Y), {Z is X*Y}.
num(D) --> [D], {number(D)}.

eval(L, V, []) :- expr(V, L, []).
  • 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-25T22:07:22+00:00Added an answer on May 25, 2026 at 10:07 pm

    The parsers implemented by Prolog’s DCG grammars are recursive-descent LL(something) (predictive) grammars. It walks the input from left to right and produces a leftmost derivation as it goes. They’re easy to craft but the grammar’s must conform to a few restrictions:

    They cannot be left-recursive. Infinite recursion can/will result. This means that at least one symbol (token) has to be removed from the input stream prior to following a recursive path. Refactoring grammars to remove left-recursion is a fairly mechanical exercise, albeit tedious. See any decent compiler book on how to do that.

    Operator precedence is typically built into the structure of the grammar itself. Here’s BNF notation showing one way of defining a recursive descent grammar for the parsing/evaluation of simple arithmetic expressions:

    ArithmeticExpression     : AdditiveExpression
                             ;
    
    AdditiveExpression       : MultiplicativeExpression
                             | MultiplicativeExpression '+' AdditiveExpression
                             | MultiplicativeExpression '-' AdditiveExpression
                             ;
    
    MultiplicativeExpression : ExponentialExpression
                             | ExponentialExpression '*' MultiplicativeExpression
                             | ExponentialExpression '/' MultiplicativeExpression
                             | ExponentialExpression '%' MultiplicativeExpression
                             ;
    
    ExponentialExpression    : UnaryExpression
                             | UnaryExpression '^' ExponentialExpression
                             ;
    
    UnaryExpression          : '-' UnaryExpression
                             | AtomicExpression
                             ;
    
    AtomicExpression         : '(' ArithmeticExpression ')'
                             | ['0'..'9']+
                             ;
    

    The term at each level of operator precedence is built from expressions of the next higher order of precedence. So an arbitrary arithmetic expression is just a single additive expression.

    Each additive expression is 1 or more multiplicative expressions, joined by addition and subtraction operators.

    Each multiplicative expression is 1 or more exponential expressions, joined by multiplication, division and remainder operators.

    Each exponential expression is a unary expression with an option exponent operator followed by another unary expression.

    Each unary expression is either an atomic expression, or a unary minus followed by another unary expression.

    Each atomic expression is either an arbitrary arithmetic expression, enclosed in parentheses, or an unsigned integer token.

    Translation of the above into Prolog’s DCG syntax should be trivial. How to evaluate the term represented by each clause in the grammar should be self-evident.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.