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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:36:27+00:00 2026-05-26T15:36:27+00:00

So I have this grammar: expr(op(T,B,E)) => [term(T), binop(B), expr(E)]. expr(T) => [term(T)]. term(N)

  • 0

So I have this grammar:

  expr(op(T,B,E)) => [term(T), binop(B), expr(E)].
  expr(T) => [term(T)].

  term(N) => [num(N)].
  term(L) => [lvalue(L)].
  term(pre(O,L)) => [incrop(O), lvalue(L)].
  term(post(L,O)) => [lvalue(L), incrop(O)].
  term(E) => ['(', expr(E), ')'].

  lvalue($(E)) => [$, expr(E)].

  incrop(++) => [++].
  incrop(--) => [--].

  binop(+) => [+].
  binop(-) => [-].

  num(0) => [0].
  num(1) => [1].
  num(2) => [2].
  num(3) => [3].
  num(4) => [4].
  num(5) => [5].
  num(6) => [6].
  num(7) => [7].
  num(8) => [8].
  num(9) => [9].

and the goal is to parse the input according to the rules, and separate the remaining suffix. For example,

| ?- parse_prefix(expr(E), [$,1,+,2], S).

E = op($(1),+,2)
S = [] ? ;

E = $(op(1,+,2))
S = [] ? ;

E = $(1)
S = [+,2] ? ;

no

and

| ?- parse_prefix(expr(E), [9], S).

E = 9
S = [] ? ;

no


| ?- parse_prefix(expr(E), [9,+,$,1,+], S).

E = op(9,+,$(1))
S = [+] ? ;

E = 9
S = [+,$,1,+] ? ;

I have written the following predicates:

%Base Case: No Token, No Suffix
parse_prefix(_,[],[]).

%Direct Matching: ex) parse_prefix(num(9),[9],S)
parse_prefix(NT,[Head|Tail],S):-
    NT =>[Head],
    write('two '),
    parse_prefix(expr(E),Tail,S).
%General Matching: ex) parse_prefix(expr(E),_,S)
parse_prefix(NT,[Head|Tail],S):-
    NT => [Head1|Tail1],
    %write(Head1),
    %write('one\n'),
    parse_prefix(Head1,[Head|Tail],S).

and I’m having a lot of confusion with recursion and backtracking..

I will permanently love anyone who can help me this one.

Thank you in advance.

  • 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-26T15:36:28+00:00Added an answer on May 26, 2026 at 3:36 pm

    You are already close to a solution. It is good to define your own operator =>/2 to represent your own gammar rules and not get into conflict with –>/2. But I am having problems with the representation of grammar rule bodies. I don’t see that you distinguish terminals and non-terminals in the grammar rule bodies.

    One suggestion would be to vote for (A1,…,An) to represent a conjunction in the body, instead of [A1,..,An]. And then use [T] for terminals and NT for non-terminals. So the following rule,

    term(E) => ['(', expr(E), ')'].
    

    would then read:

    term(E) => ['('], expr(E), [')'].
    

    You can then adapt your rules and define a parse_prefix/3 as follows. I show you the terminal and the conjunction and the non-terminal case:

    parse_prefix([T],I,O) :- !, I=[T|O].
    parse_prefix((A,B),I,O) :- !, parse_prefix(A,I,H), parse_prefix(B,H,O).
    parse_prefix(NT,I,O) :- (NT => Body), parse_prefix(Body,I,O).
    

    You can add additional cases for the empty production ([]) and auxiliary conditions ({}), or make it more flexible to be able to work with terminal lists ([T1,..,Tn]). Also further control constructs are possible, but when you try to do a cut (!) things get a little bit nasty when following the meta-interpreter approach.

    Instead of writing a meta-interpreter parse_prefix/3 you could also cook your own term rewriting to finally arrive at a method that would convert the given gammar rules first into ordinary Prolog and then execute them from there. You find a simple recipe here:

    http://www.jekejeke.ch/idatab/doclet/blog/en/docs/int/jan/098_2011/097_dcg_expansion/package.html

    Bye

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

Sidebar

Related Questions

Say I have a grammar like this: expr : expr '+' expr { $$
I have written this grammar: expr : multExpr ( ('+' | '-') multExpr )*;
I have this simple grammar for a C# like syntax. I can't figure out
Say I have a Boost Spirit grammar like this, where a parent rule passes
I have an EBNF grammar that has a few rules with this pattern: sequence
I have this simple grammer: expr: factor; factor: atom (('*' ^ | '/'^) atom)*;
I have this grammar to match simple logical predicates in ANTLR. exp : or
I have this (working) LALR grammar for SABLECC: Package org.univpm.grail.sable; Helpers digit = [
perhaps I'm asking the wrong question. I have code like this: class ExpressionGrammar(Grammar): def
I have not met this type of grammar before. What does it mean? To

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.