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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:31:54+00:00 2026-05-26T22:31:54+00:00

I have a simple grammar such as S::=a S b S::=[] (empty string) Now

  • 0

I have a simple grammar such as

S::=a S b
S::=[] (empty string)

Now i want to write a parser for the above grammar like

cfg('S', [a,'S',b])

which generates a sentence aaabbb by left most derivation.

I’m not good enough to handle dcg/cfg in prolog.
So pls help me with this example so that i can go ahead and try something bigger.

  • 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-26T22:31:55+00:00Added an answer on May 26, 2026 at 10:31 pm

    Consider this DCG code:

    s-->[].
    s-->[a],s,[b].
    

    to run a predicate you defined by DCG you should add two more arguments at the end: the “input” and what it’s left. If you want to recognize the whole list you simply put []. So, when you run it you get:

    38 ?- s(C,[]).
    C = [] ;
    C = [a, b] ;
    C = [a, a, b, b] ;
    C = [a, a, a, b, b, b] ;
    C = [a, a, a, a, b, b, b, b] ;
    ...
    

    If you wanted some sort of “return” string you could add it as an extra arg. To write prolog code in a dcg clause you use {}:

    s('')-->[].
    s(S)-->
        [a],s(SI),[b],
        { atomic_list_concat([a,SI,b],S)}.
    

    and you get:

    40 ?- s(R,X,[]).
    R = '',
    X = [] ;
    R = ab,
    X = [a, b] ;
    R = aabb,
    X = [a, a, b, b] ;
    R = aaabbb,
    X = [a, a, a, b, b, b] ;
    R = aaaabbbb,
    X = [a, a, a, a, b, b, b, b] ;
    R = aaaaabbbbb,
    ...
    

    we generated all the strings that are recognized by this grammar; usually you just want to check if a string is recognized by the grammar. to do that you simply put it as input:

    41 ?- s([a,b],[]).
    true 
    
    42 ?- s([a,b,b],[]).
    false.
    

    note that we put the S::=[] rule first otherwise prolog would fall in a infinite loop if you asked to generate all the solutions. This problem might not be trivial to solve in more complex grammars. To get the solutions you can use length/2:

    ?- length(X,_),s(X,[]).
    X = [] ;
    X = [a, b] ;
    X = [a, a, b, b] ;
    X = [a, a, a, b, b, b] ;
    X = [a, a, a, a, b, b, b, b] 
    

    even if your code is:

    s-->[].
    s-->[a],s,[b].
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a simple JSP parser by using Treetop. Now, I have
I have this simple grammar for a C# like syntax. I can't figure out
I have a simple ANTLR grammar, which I have stripped down to its bare
I have defined simple grammar for parsing string and number using Treetop as below.
I have defined simple grammar for parsing string and number using Treetop as below.
I have an ANTLR3 simple grammar parser that takes short lines of text and
I have a simple grammar which allows the user to define some objects with
I have simple SSIS package which reads data from flat file and insert into
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have simple SSIS package in which On Error event handler I have created

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.