Doubts in backtracking function animation, for call sequence flow.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.