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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:43:37+00:00 2026-06-16T20:43:37+00:00

recently I’ve been playing with DCG in Prolog but I’ve been facing some issues

  • 0

recently I’ve been playing with DCG in Prolog but I’ve been facing some issues regarding how exactly does it work. For instance, I have this small grammar:

<atom> :: <letter> <atom_part> | <letter>
<atom_part> :: <letter> | <digit> | <letter> <atom_part> | <digit> <atom_part>
<letter>:: 'a' | 'b' ... |'Z'
<digit> :: '0' |...|'9'

Which, If I’m not mistaken, is any string of letters or numbers that must start with a letter.
Anyway, my attempt to parse it is the following:

letter("a") --> "a".
number(X) --> number(X).
...
%etc
programme(I) --> atomm(I).
atomm(C) --> letter(Ch).
atomm(C) --> numb(Ch).
atomm((E)) --> atomm_part(E).
atomm_part(E1,E2) --> atomm(E1),!,atomm(E2).

Here I think is clear that the last 2 lines are wrong. That’s really because I’m not sure how to make the ‘recursive call’ so the parser goes again to check if the next character in the string is a number or a string. How can I correct this? Thanks in advance!

btw, i’m using swi-prolog

  • 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-06-16T20:43:38+00:00Added an answer on June 16, 2026 at 8:43 pm

    Your grammar seems more complex than needed, and you could simplify it using ‘epsilon’ (empty production, in DCG is []). That apart, you should keep the ‘program’ more adherent to the specification.

    atom --> letter, atom_part | letter.
    atom_part --> letter | digit | letter, atom_part | digit, atom_part.
    letter --> "a" | "b" | /* omissis... */ "Z".
    digit --> [D], {memberchk(D, "0123456789")}.
    

    You can see how similar is to the original specification.
    With that

    ?- phrase(atom, "a").
    true ;
    false.
    
    ?- phrase(atom, "3a").
    false.
    
    ?- phrase(atom, "a3").
    true ;
    false.
    

    letter and digit show different ways to match single characters. digit it’s simpler if you need to capture values from input, as done in your code. But because enumerating 26*2 characters it’s error prone, please consider using code_type/2

    atom(A) --> letter(L), atom_part(P), {A=[L|P]} | letter(L), {A=[L]}.
    atom_part(P) --> letter(L), {P=[L]} | digit(D), {P=[D]} | letter(L), atom_part(A), {P=[L|A]} | digit(D), atom_part(A), {P=[D|A]}.
    letter(L) --> [L], {code_type(L, alpha)}.
    digit(D) --> [D], {memberchk(D, "0123456789")}.
    

    Also consider that usually alternatives in Prolog are encoded in this way

    atom([L|P]) --> letter(L), atom_part(P).
    atom([L]) --> letter(L).
    

    The simpler form allows moving the ‘data construction’ in head pattern.

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

Sidebar

Related Questions

Recently, when I tried to show the results of my work (some Flex app)
Recently I read this Wikipedia article regarding the Dining Philosophers problem but I am
Recently there was some upgrade happened from frame work 2.0 to 4.0, so after
Recently, I was asked to compile some of my Android projects, based on the
Recently I've used some features that require modern OpenGL context (specifically ARB_debug_output requires to
Recently I've been doing a lot of these enum Thing { /* etc etc
Recently there have been a lot of move towards MVVM framework due to the
Recently I've been doing things like this: import Tkinter class C(object): def __init__(self): self.root
Recently at my day-job were were instructed that any comments regarding our stored procedures
Recently I have been working with a SQL Server database and I was trying

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.