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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:29:55+00:00 2026-05-27T08:29:55+00:00

I’ve inserted the given context free grammar into the database using assert(….) If the

  • 0

I’ve inserted the given context free grammar into the database using assert(….)
If the grammar is something like

S-->a,S,b
S-->c

This grammar is inserted into the database.
I have to write a dcg to generate sentences for the cfg in the database.
For example if i define the dcg in this way myDcg(‘S’,str), the ‘S’(non terminal) should be called or substituted by aSb or c|d or so.

The problem is how can i call/substitute ‘S’ by facts from the database each time a non terminal(‘S’) is encountered to generate sentences.

Hope you understood my question, if not i will try to edit the question.


Below(Sample code) is what i wanted to do exactly
This is not dcg.

myGrammar([], []):-!.

myGrammar([T|Rest], [T|Sentence]):-
          myGrammar(Rest, Sentence).

myGrammar([NT|Rest], Sentence):-
          grammar(NT, Rest1),
          append(Rest1,Rest, NewRest),
          myGrammar(NewRest, Sentence). 

Whenever a terminal is encountered it should be printed out and when a non terminal is encountered it will backtrack.

  • 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-27T08:29:56+00:00Added an answer on May 27, 2026 at 8:29 am

    In your predicate mygrammar/2 there is a list of non-terminals and terminals in the first argument and a list of terminals in the second. It should probably succeed if the second argument is of the form of the first. So what you have here essentially is a meta interpreter for DCGs. A few suggestions:

    Your tokenizer produces currently [grammar('S',[a,'S',b]),grammar('S',[....]),..]. Let it produce [grammar('S',[t(a),nt('S'),t(b)]),grammar('S',[....]),..] instead. In this manner it’s evident what is a terminal and what is a non-terminal. And, oh, remove that !.

    myGrammar([], []).
    myGrammar([t(T)|Rest], [T|Sentence]):-
       myGrammar(Rest, Sentence).
    myGrammar([nt(NT)|Rest], Sentence):-
       grammar(NT, Rest1),
       append(Rest1,Rest, NewRest),
       myGrammar(NewRest, Sentence).
    

    DCGs, btw are a bit more general than this interpreter.

    The actual classification between non-terminals and terminals has to be done by the tokenizer.

    uppercasecode(C) :-
       between(0'A,0'Z,C).
    
    lowercasecode(C) :-
       between(0'a,0'z,C).
    

    If you are using chars (one-character atoms), you will use char_code(Char, Code) to convert between them.

    Full Unicode support is still in its infancy. Its very tricky because of all those special cases for characters like Ⓐ which is upper case but still cannot be part of an identifier. But here is how you can do it in SWI currently.

    uppercasecode(C) :-
       '$code_class'(C,upper),
       '$code_class'(C,id_start).
    
    lowercasecode(C) :-
       '$code_class'(C,id_start),
       '$code_class'(C,id_continue),
       \+ '$code_class'(C,upper).
    

    Update: In the meantime, there is char_type/2 and code_type/2 for this purpose.

    uppercasecode(C) :-
       code_class(C, upper),
       code_class(C, prolog_var_start).
    
    • 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
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in

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.