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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:13:40+00:00 2026-05-27T05:13:40+00:00

Given a CFG S –> a S b | c | d I wanna

  • 0

Given a CFG

S --> a S b | c | d

I wanna write a predicate like, grammar(‘S’, sentence) which generates all possible

sentences like
sentence=acb,
sentence=acd,
sentence=c,
sentence=ab......................

Using left most derivation, if the encountered symbol is terminal it should print out that terminal, and if the encountered symbol is non terminal ‘S’, it should backtrack and substitute and one of the grammar a S b or c or d and repeat the process.

I dont want any code…just help me with some tips how to start with

  • 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-27T05:13:41+00:00Added an answer on May 27, 2026 at 5:13 am

    Let’s use DCGs to encode your grammar literally!

    s --> [a], s, [b] | [c] | [d].
    
    ?- phrase(s,Xs).
       loops. % ERROR: Out of local stack
    

    Seems that this query does not terminate. I.e. Prolog’s very simple execution strategy did not find a solution. On the other hand, think of it: Your grammar describes an infinite set of sentences. If you are enumerating an infinite set it is easy to start "at the wrong end". That’s what Prolog actually does here.

    But things are not that bad at all. What about enumerating all sentences of a fixed length. I will try 5:

    ?- length(Xs,5), phrase(s,Xs).
       Xs = "aacbb"
    ;  Xs = "aadbb"
    ;  false.
    

    In this case, all sentences are found and Prolog even assures us that there are no further sentences.

    ?- length(Xs,4), phrase(s,Xs).
       false.
    

    There are no sentences of length 4.

    We can now enumerate all sentences, by length.

    ?- length(Xs,N), phrase(s,Xs).
       Xs = "c", N = 1
    ;  Xs = "d", N = 1
    ;  Xs = "acb", N = 3
    ;  Xs = "adb", N = 3
    ;  Xs = "aacbb", N = 5
    ;  Xs = "aadbb", N = 5
    ;  Xs = "aaacbbb", N = 7
    ; ... .
    

    What kind of derivation did we use here? Honestly, I don’t know and I don’t care. What is important to know is when Prolog will terminate. In this case, it will terminate, if the length is known. And that is all we need to know to guarantee that we have a fair enumeration of the infinite set. Things are even slightly better: s//0 will also terminate in cases where the length is not known like

    ?- Xs = [a,a,b|_], phrase(s,Xs).
       false.
    ?- Xs = [a,a,c|_], phrase(s,Xs).
       Xs = "aacbb"
    ;  false.
    ?- dif(X,Y), Xs = [X,Y|_], phrase(s,Xs).
       X = a, Y = c, Xs = "acb"
    ;  X = a, Y = d, Xs = "adb"
    ;  false.
    

    Edit: I got some questions about the toplevel answers using "acb" for a list [a,c,b]: Please refer to this answer for an explanation and to library(double_quotes).

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

Sidebar

Related Questions

Write a function lv: cfg -> (blabel -> ide set) , which computes the
I write a hibernate.cfg.xml file almost the same from hibernate books like: <property name=hibernate.dialect>org.hibernate.dialect.MySQL5Dialect</property>
I'm trying to write a CFG over the alphabet Σ = {a,b} for all
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given an absolute or relative path (in a Unix-like system), I would like to
Given 2 rgb colors and a rectangular area, I'd like to generate a basic
I have a config.cfg which I parse using the python-module ConfigParser. In one section
I've inserted the given context free grammar into the database using assert(....) If the
Given an extension method like this: Public Sub RehydrateTo(Of T As New)(ByVal input As
Given a graph of n nodes that are all interconnected on a coordinate plane,

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.