LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2023, 08:58 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
Doubts in backtracking function animation, for call sequence flow.


In the slides' based animation given for backtracking, here (with code here, in the
web-page; have few doubts, in the code restated below, and the execution as shown in the call #5.

Code:
#include <stdio.h>
    char *next;
    int calls = 0;

    int eat(char c) {
      calls++;
      if (*next == c) {
        next++;
        return 1;
      }
      return 0;
    }
      
    int e() {   
        char* save = next;    
        if(eat('(') && e() && eat('+') && e() && eat(')')) 
            return 1;   
        else next = save;    
        if(eat('(') && e() && eat('*') && e() && eat (')'))  
            return 1;   
        else next = save;    
        if (v()) 
            return 1;   
        else 
            next = save;    
        return 0; 
    }
    int v() {
        char* save = next;
        calls++;
  
        if (eat('x')) return 1;
        else next = save;

        if (eat('y')) return 1;
        else next = save;

        return 0;
    }

    void main()
    {
      next = "((((x*x)*x)*x)*x)";
      printf("%i\n", e());
      printf("%i\n", calls);
    }
Request correction, and comments, for my analysis, and doubts, below:

1. In the code, the two operators, i.e. '+', '*'; are of equal precedence. Though, am confused if am correct in describing the grammar for the code.

e -> e + e | e * e | v

v -> x | y


2. For the call #5, unable to understand how the execution flow jumped to the the second *if* statement, though tried multiple times to figure out the same.


[1]: https://www.cs.york.ac.uk/fp/lsa/lectures/backtrack.pdf
[2]: https://www.cs.york.ac.uk/fp/lsa/lectures/backtrack.c
[3]: https://www.cs.york.ac.uk/fp/lsa/

Last edited by ajiten; 07-28-2023 at 09:14 AM.
 
Old 07-28-2023, 11:50 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
ad1
Code:
e -> '(' e '+' e ')' | '(' e '*' e ')' | v
v -> 'x' | 'y'
ad2
Guess you mean how the stepping-back happens. Well the `next= save` statement undoes the effect of the previous statements.

Last edited by NevemTeve; 07-28-2023 at 11:56 AM.
 
Old 07-28-2023, 12:35 PM   #3
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Code:
e -> '(' e '+' e ')' | '(' e '*' e ')' | v
v -> 'x' | 'y'
Guess you mean how the stepping-back happens. Well the `next= save` statement undoes the effect of the previous statements.
Thanks a lot, but the input given in main() is: next = "((((x*x)*x)*x)*x)"
So, the grammar should be:
Code:
e -> ( e + e ) | ( e * e ) | v
v -> x | y
But, the rules are supporting your grammar, as in the function : e()
Code:
if (eat('(') && e() && eat('+') && e() && eat(')')) return 1;
  else next = save;

  if (eat('(') && e() && eat('*') && e() && eat(')')) return 1;
  else next = save;

and the function : v()
Code:
if (eat('x')) return 1;
  else next = save;

  if (eat('y')) return 1;
  else next = save;


But, why the input then is not instead:
Code:
 next = "'(''(''(''(''x''*''x'')''*''x'')''*''x'')''*''x'')'"
Seems that for terminal symbols : (, ), +, *, the input is inside quotes, as character input; just like the input is inside double quotes, as it is a string of characters.

Edit: Have verified at : https://stackoverflow.com/a/3683613, still want to keep my response, as a good reminder.

================================

Q.2. So, by stepping-back, the else branch is "implicitly' taken; that executes the (retracting/backtracking) `next= save` statement to undo the effect of the previous statement (i.e., in the earlier if statement).
It would have been better had the slides shown the else branch's backtracking action explicitly, though now it is obvious.

Last edited by ajiten; 07-28-2023 at 12:47 PM.
 
Old 07-28-2023, 12:58 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Sorry for the misunderstanding, I'll use bold/blue for character-literals:

Code:
e -> ( e + e ) | ( e * e ) | v
v -> x | y

Last edited by NevemTeve; 07-28-2023 at 02:34 PM.
 
2 members found this post helpful.
Old 07-28-2023, 01:23 PM   #5
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Sorry for the misunderstanding, I'll use bold/blue for character-literals:

Code:
e -> ( e + e ) | ( e * e ) | v
v -> x | y
Please tell why you removed single quotes, from enclosing all terminal symbols (i.e., the character-literals); as have understood that in C, they are enclosed by the same; just like a string is enclosed by double quotes.
 
Old 07-28-2023, 02:04 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Ideally, you use different notations for meta-characters and literal characters. Maybe an example helps.

without distinction you couldn't guess which characters are literals:
Code:
S -> (A(B|C)D)
with distinction:
Code:
S -> ( A '(' B | C ')' D )
or
Code:
S -> (A(B|C)D)
 
2 members found this post helpful.
Old 07-28-2023, 02:16 PM   #7
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Ideally, you use different notations for meta-characters and literal characters. Maybe an example helps.

without distinction you couldn't guess which characters are literals:
Code:
S -> (A(B|C)D)
with distinction:
Code:
S -> ( A '(' B | C ')' D )
or
Code:
S -> (A(B|C)D)
But, here in the grammar, no meta-characters are there; none. The confusion can arise only when same symbol is used for both: meta-character, character-literal.
Might be you mean to say that in the grammar, there is no need to enclose character literal, with single quotes. Then, if am correct, am clear.
 
Old 07-28-2023, 02:22 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> But, here in the grammar, no meta-characters are there; none.

How about the |vertical bar that means 'or'?
Code:
e -> ( e + e ) | ( e * e ) | v
 
1 members found this post helpful.
  


Reply

Tags
compiler



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
youtube video url series backtracking jr_bob_dobbs General 16 11-21-2020 08:07 AM
Flow control none is not working as expected after we change flow control software Pavan Kumar Reddy Linux - Software 0 07-12-2014 01:45 PM
[SOLVED] Backtracking to sanity volkerdi Slackware 135 06-21-2013 02:19 PM
Serial Driver HW FIFO over flow and 64K tty buffer over flow harsh_electro Linux - General 0 12-07-2012 10:44 PM
I guess a simple backtracking problem spank Programming 15 08-27-2003 11:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration