Automated build and previously applied patches - patch

How can I prevent aborted builds when a previously applied patch is detected without:
simply ignoring all failed patches
requiring user input
Patch itself is capable of identifying a previously applied patch. There's got to be a way to avoid the non-zero exit status on previously applied patches, right?
This doesn't work:
yes 'n' | patch -p<w/e> -i <w/e>
Because patch reads from /dev/tty (I'm guessing) instead of stdin. Even if it did read from stdin, it still gives an exit status of 1.
It seems like I'm missing something. I can't be the first to have run into this problem.

I think I may have just solved my problem.
diff --git a/src/common.h b/src/common.h
index 9e355fe..e1b1555 100644
--- a/src/common.h
+++ b/src/common.h
## -108,8 +108,10 ## XTERN bool force;
XTERN bool batch;
XTERN bool noreverse;
XTERN bool reverse;
+XTERN bool applied;
XTERN enum { DEFAULT_VERBOSITY, SILENT, VERBOSE } verbosity;
XTERN bool skip_rest_of_patch;
+XTERN bool applied_is_cause;
XTERN int strippath;
XTERN bool canonicalize;
XTERN int patch_get;
diff --git a/src/patch.c b/src/patch.c
index a60e631..3d375b3 100644
--- a/src/patch.c
+++ b/src/patch.c
## -613,7 +613,8 ## main (int argc, char **argv)
if (fstat (fileno (rejfp), &rejst) != 0 || fclose (rejfp) != 0)
write_fatal ();
rejfp = NULL;
- somefailed = true;
+ if (! somefailed && ! (applied && applied_is_cause))
+ somefailed = true;
say ("%d out of %d hunk%s %s", failed, hunk, "s" + (hunk == 1),
skip_rest_of_patch ? "ignored" : "FAILED");
if (outname && (! rejname || strcmp (rejname, "-") != 0)) {
## -629,7 +630,7 ## main (int argc, char **argv)
rej[len - 1] = '#';
simple_backup_suffix = s;
}
- if (! dry_run)
+ if (! dry_run && ! (applied && applied_is_cause))
{
say (" -- saving rejects to file %s\n", quotearg (rej));
if (rejname)
## -706,9 +707,10 ## reinitialize_almost_everything (void)
reverse = reverse_flag_specified;
skip_rest_of_patch = false;
+ applied_is_cause = false;
}
-static char const shortopts[] = "bB:cd:D:eEfF:g:i:l"
+static char const shortopts[] = "abB:cd:D:eEfF:g:i:l"
#if 0 && defined ENABLE_MERGE
"m"
#endif
## -716,6 +718,7 ## static char const shortopts[] = "bB:cd:D:eEfF:g:i:l"
static struct option const longopts[] =
{
+ {"applied", no_argument, NULL, 'a'},
{"backup", no_argument, NULL, 'b'},
{"prefix", required_argument, NULL, 'B'},
{"context", no_argument, NULL, 'c'},
## -777,6 +780,7 ## static char const *const option_help[] =
"",
" -N --forward Ignore patches that appear to be reversed or already applied.",
" -R --reverse Assume patches were created with old and new files swapped.",
+" -a --applied Ignore error and save no rejects on applied or reversed patch.",
"",
" -i PATCHFILE --input=PATCHFILE Read patch from PATCHFILE instead of stdin.",
"",
## -869,6 +873,9 ## get_some_switches (void)
while ((optc = getopt_long (Argc, Argv, shortopts, longopts, (int *) 0))
!= -1) {
switch (optc) {
+ case 'a':
+ applied = true;
+ break;
case 'b':
make_backups = true;
/* Special hack for backward compatibility with CVS 1.9.
diff --git a/src/util.c b/src/util.c
index ee88c13..432bc5c 100644
--- a/src/util.c
+++ b/src/util.c
## -1056,6 +1056,7 ## ok_to_reverse (char const *format, ...)
{
say (" Skipping patch.\n");
skip_rest_of_patch = true;
+ applied_is_cause = true;
}
else if (force)
{
## -1079,6 +1080,7 ## ok_to_reverse (char const *format, ...)
if (verbosity != SILENT)
say ("Skipping patch.\n");
skip_rest_of_patch = true;
+ applied_is_cause = true;
}
}
}

Related

C Program to Search a Node in Binary Tree

this code is from binary search tree I don't know this code showing same output
I don't know where the problem is occurring I already tried to change the variables but it didn't work
What seems to be the problem? i already tried so many things but still not able to fix the errors.
#include <stdio.h>
#include <stdlib.h>
****// Basic struct of Tree****
struct node
{
int data;
struct node *left;
struct node *right;
};
****// Function to create a new Node****
struct node *createNode(int item)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->left = NULL;
newNode->right = NULL;
newNode->data = item;
return newNode;
}
int search(struct node *root, int value)
{
if (root == NULL)
return 0;
if (root->data == value)
return 1;
if (root->data < value)
return search(root->right, value);
else
return search(root->left, value);
}
int main()
{
**// struct node *root = NULL;**
struct node *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->right = createNode(6);
root->left->right->left = createNode(7);
root->left->right->right = createNode(8);
root->right->right->left = createNode(9);
int item = 34;
// Function to find item in the tree
int found = search(root, item);
if (found)
printf("%d value is found in the tree", item);
else
printf("%d value not found", item);
return 0;
}
The problem is that your search function expects to get a binary search tree, but your main program created a binary tree that is not a binary search tree, but this:
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
And of course, a search for 34 will anyway return 0, as it does not occur anywhere in this tree. But even if you would search for let's say 8, it would return 0.
Your search code will not work with such a tree. If you would have made a binary search tree, like for instance this one:
6
/ \
2 7
/ \ \
1 4 9
/ \ /
3 5 8
...then it would work when calling search for any value in or not in the tree. For instance, this will print "5 value is found in the tree":
int main()
{
struct node *root = createNode(6);
root->left = createNode(2);
root->right = createNode(7);
root->left->left = createNode(1);
root->left->right = createNode(4);
root->right->right = createNode(9);
root->left->right->left = createNode(3);
root->left->right->right = createNode(5);
root->right->right->left = createNode(8);
int item = 5;
// Function to find item in the tree
int found = search(root, item);
if (found)
printf("%d value is found in the tree", item);
else
printf("%d value not found", item);
return 0;
}
This function is for binary search tree (BST).
int search(struct node *root, int value)
{
if (root == NULL)
return 0;
if (root->data == value)
return 1;
if (root->data < value)
return search(root->right, value);
else
return search(root->left, value);
}
As you need to a program to work on any binary tree.
You can change it to work on an any binary tree.
int search(struct node *root, int value)
{
if (root == NULL)
return 0;
if (root->data == value)
return 1;
return search(root->right, value) || search(root->left, value);
}

Error with char parameter methods Arduino Code

I keep getting the following error message when I'm trying to run my code. I can not figure out how to correct these errors so I can test everything. Even though these are just warning messages and the code says it uploads. I don't get any messages on my serial monitor printing any statements.
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address for a 16 chars and 2 line display
const int guessPin = 7;
const int switch0Pin = 13;
const int switch1Pin = 12;
const int switch2Pin = 11;
const int switch3Pin = 10;
const int switch4Pin = 9;
int currentLetterIndex = 0;
int currentClueIndex = 0;
int currentGuess = 0;
int lastGuess = 0;
bool debug = false; // set to false to turn off serial output
unsigned long previousMillis = 0;
long restInterval = 20 * 1000; // 20 second time out to conserve battery
char clueBreak[] = "Get clue at the next house :)";
char hintBreak[] = "ah ah ah... must be patient";
char answerBreak[] = "santa";
char clue1[] = "The place we first meet?";
char hint1[] = "Company... not city";
char answer1[] = "pcc";
char clue2[] = "The play we saw in NY";
char hint2[] = "muppets... no...";
char answer2[] = "avenueq";
char clue3[] = "What we worn in Times Square";
char hint3[] = "look me in the eyes...";
char answer3[] = "diapers";
char clue4[] = "Where I said 'I Love You'";
char hint4[] = "And you later threw up on me...";
char answer4[] = "tonys";
char clue5[] = "";
char hint5[] = "+++ ++ ++++ ++++";
char answer5[] = "";
char clue6[] = "When do we do anything?";
char hint6[] = "'bye' 'see you +++++++'";
char answer6[] = "maybe tomorrow";
char clue7[] = "The animal you pet in gatlinburg";
char hint7[] = "spell the numbers...";
char answer7[] = "jellyfish";
char clue8[] = "These are only in California";
char hint8[] = "the one you used to sleep with";
char answer8[] = "seven eleven";
char clue9[] = "Where did you buy your cat?";
char hint9[] = "(-_-) R-O-B-O-T";
char answer9[] = "japan";
char clue10[] = "The ugliest car color...";
char hint10[] = "your second favorite color";
char answer10[] = "yellow";
char clue11[] = "Our first wine and paint";
char hint11[] = "__ _____ _____";
char answer11[] = "no drama llama";
const int cluesNum = 10; // clues + breaks
char * hints[] = {
hint1,
hint2,
hint3,
hint4,
hint5,
hint6,
hint7,
hint8,
hint9,
hint10,
hint11,
};
char * clues[] = {
clue1,
clue2,
clue3,
clue4,
clue5,
clue6,
clue7,
clue8,
clue9,
clue10,
clue11,
};
char * answers[] = {
answer1,
answer2,
answer3,
answer4,
answer5,
answer6,
answer7,
answer8,
answer9,
answer10,
answer11,
};
char currentLine1[17]; // need extra char for null pointer
char currentLine2[17]; // need extra char for null pointer
bool isDisplayingClue = true;
void setup()
{
pinMode(guessPin, INPUT);
pinMode(switch0Pin, INPUT);
pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);
pinMode(switch3Pin, INPUT);
pinMode(switch4Pin, INPUT);
lcd.init();
lcd.backlight();
if (debug)
{
Serial.begin(9600);
}
}
void updateDisplayFromArray(const char *str)
{
int limit = getCharSize(str)-1;
for (int i = 0; i >= 16; i++)
{
if (i <= limit)
{
currentLine1[i] = str[i];
}
if (i+16 <= limit)
{
currentLine2[i] = str[i+16];
}
}
}
int getCharSize(const char *cArray)
{
return sizeof(cArray) / sizeof(char);
}
void logger(const char *message)
{
if (debug)
{
Serial.println(message);
}
}
void restLCD()
{
lcd.setBacklight(0);
lcd.noDisplay();
}
void showLCD()
{
isDisplayingClue = false;
previousMillis = millis();
lcd.setBacklight(50);
lcd.display();
}
void TypeAnimation(const char *line1, const char *line2, int delayTime)
{
lcd.clear();
for (int i = 0; i <= getCharSize(line1)-1; i++) {
lcd.setCursor(i,0);
lcd.print(line1[i]);
delay(delayTime);
}
for (int i = 0; i <= getCharSize(line2)-1; i++) {
lcd.setCursor(i,1);
lcd.print(line2[i]);
delay(delayTime);
if (i == getCharSize(line2)-1) {
delay(1000);
}
}
}
void DelayAnimation(const char *line1, const char *line2, int delayTime)
{
setScreen(line1, line2);
delay(delayTime);
}
void setScreen(const char *line1, const char *line2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0,1);
lcd.print(line2);
}
void TestAnimations()
{
showLCD();
PlayGuessingAnimation();
PlayCorrectAnimation();
PlayIncorrectAnimation();
ResetClues();
CurrentStatus();
ShowCurrentGuess();
DisplayClue(5000);
restLCD();
delay(5000);
currentLetterIndex++;
}
void ResetClues()
{
DelayAnimation("Resetting...", "",1500);
DelayAnimation("Merry Christmas!", "My Love :)",4000);
TypeAnimation("Old Man Turtle", "has been taken",250);
TypeAnimation("again...", ":(",250);
TypeAnimation("Solve the clues", "to find him",250);
TypeAnimation("Use the button 2", "submit da letter",250);
DelayAnimation("Now for your 1st", "clue",4000);
TypeAnimation("Goodluck my love", ":)",250);
NextClue();
currentLetterIndex = 0;
currentClueIndex = 0;
}
void ShowFinalAnimation()
{
TypeAnimation("You did it!", "",250);
TypeAnimation("You saved", "Old Man Turtle",250);
TypeAnimation("Old Man Turtle", "has left yo",250);
TypeAnimation("Use the button 2", "submit da letter",250);
DelayAnimation("Now for your 1st", "clue",4000);
TypeAnimation("Goodluck my love", ":)",250);
}
void loop()
{
logger("************* NEW LOOP *************");
//PlayGuessingGame();
TestAnimations();
}
void PlayGuessingGame()
{
bool buttonState = digitalRead(guessPin);
char *state = char(buttonState);
logger(state);
ConvertGuess();
if (buttonState)
{
logger("made it inside button guess clicked");
showLCD();
CheckGuess();
}
else
{
if (lastGuess != currentGuess)
{
logger("The guess has changed");
showLCD();
ShowCurrentGuess();
lastGuess = currentGuess;
}
else
{
logger("The guess hasn't changed :(");
if (!isDisplayingClue)
{
DisplayClue(1);
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > restInterval * .7)
{
DelayAnimation(" About to enter ", "-- Rest Mode --",1500);
}
if(currentMillis - previousMillis > restInterval)
{
logger("...REST MODE ACTIVATED...");
restLCD();
}
}
}
if (currentClueIndex > cluesNum)
{
ShowFinalAnimation();
}
}
void ConvertGuess()
{
bitWrite(currentGuess,0, !digitalRead(switch0Pin));
bitWrite(currentGuess,1, !digitalRead(switch1Pin));
bitWrite(currentGuess,2, !digitalRead(switch2Pin));
bitWrite(currentGuess,3, !digitalRead(switch3Pin));
bitWrite(currentGuess,4, !digitalRead(switch4Pin));
logger("Beginning conversion of guess");
logger("Pin 0:" + char(!digitalRead(switch0Pin)));
logger("Pin 1:" + char(!digitalRead(switch1Pin)));
logger("Pin 2:" + char(!digitalRead(switch2Pin)));
logger("Pin 3:" + char(!digitalRead(switch3Pin)));
logger("Pin 4:" + char(!digitalRead(switch4Pin)));
logger("new guess is " + char(ConvertIntToLetter(currentGuess)));
}
void CheckGuess()
{
PlayGuessingAnimation();
char guessLetter = ConvertIntToLetter(currentGuess);
char cLetter = clues[currentClueIndex][currentLetterIndex];
if (guessLetter == '-')
{
ResetClues();
}
else if (guessLetter == '+')
{
CurrentStatus();
}
else if (guessLetter == '?')
{
DisplayHint(5000);
}
else if (guessLetter == cLetter)
{
currentLetterIndex++;
if (currentLetterIndex > getCharSize(clues[currentClueIndex])-1)
{
PlayNextClueAnimation();
currentLetterIndex = 0;
currentClueIndex++;
}
PlayCorrectAnimation();
}
else
{
PlayIncorrectAnimation();
}
}
void ShowCurrentGuess()
{
char guess = char(ConvertIntToLetter(currentGuess));
if (guess == '-')
{
DelayAnimation(" DANGER!!! ", "THIS WILL RESET!",5000);
DelayAnimation("Press the button", "will reset",2000);
}
else if (guess == '+')
{
DelayAnimation("This will", "Show current status",2000);
}
else if (guess == '?')
{
DelayAnimation("This will", "Show you a hint",2000);
}
else if (guess == ' ')
{
DelayAnimation("You're Guessing ", "'_' (space)",1500);
}
else
{
DelayAnimation("You're Guessing ", char(guess),1500);
}
}
void CurrentStatus()
{
char currentAnswer[17] = "";
if (currentLetterIndex == 0)
{
strcpy(currentAnswer, "nothing yet :(");
}
else
{
strcpy(currentAnswer, GetCorrectGuesses());
}
DelayAnimation("You've Guessed ", strcat(char(currentClueIndex), " Clues"), 2000);
DelayAnimation("The Current", "Clues Is...", 2000);
DisplayClue(2000);
DelayAnimation("You've got right", currentAnswer, 3000);
}
char GetCorrectGuesses()
{
char result[17];
for(int i = 0; i >= currentLetterIndex; i++)
{
result[i] = answers[currentClueIndex][i];
}
return result;
}
void DisplayClue(int delayTime)
{
updateDisplayFromArray(clues[currentClueIndex]);
DelayAnimation(currentLine1,currentLine1,delayTime);
isDisplayingClue = true;
}
void DisplayHint(int delayTime)
{
updateDisplayFromArray(hints[currentClueIndex]);
TypeAnimation(currentLine1,currentLine1,delayTime);
}
void NextClue()
{
DelayAnimation(" Are you ready?? ", " Clue #" + char(currentClueIndex+1),3000);
DisplayClue(5000);
}
void PlayGuessingAnimation()
{
TypeAnimation("That is...", "",200);
}
void PlayNextClueAnimation()
{
TypeAnimation("HOORAY!!!", "",200);
DelayAnimation("You Got It Right", " Go You ;) ",2000);
TypeAnimation("On to the next", "",200);
NextClue();
}
void PlayCorrectAnimation()
{
DelayAnimation(" CORRECT!!!! ", "",1500);
}
void PlayIncorrectAnimation()
{
DelayAnimation(" Wrong :( ", " Try Again ",2000);
}
char ConvertIntToLetter(int number)
{
int result = '0';
switch (number)
{
case 0:
result = 'a';
break;
case 1:
result = 'b';
break;
case 2:
result = 'c';
break;
case 3:
result = 'd';
break;
case 4:
result = 'e';
break;
case 5:
result = 'f';
break;
case 6:
result = 'g';
break;
case 7:
result = 'h';
break;
case 8:
result = 'i';
break;
case 9:
result = 'j';
break;
case 10:
result = 'k';
break;
case 11:
result = 'l';
break;
case 12:
result = 'm';
break;
case 13:
result = 'n';
break;
case 14:
result = 'o';
break;
case 15:
result = 'p';
break;
case 16:
result = 'q';
break;
case 17:
result = 'r';
break;
case 18:
result = 's';
break;
case 19:
result = 't';
break;
case 20:
result = 'u';
break;
case 21:
result = 'v';
break;
case 22:
result = 'w';
break;
case 23:
result = 'x';
break;
case 24:
result = 'y';
break;
case 25:
result = 'z';
break;
case 26:
result = '!';
break;
case 27:
result = '%';
break;
case 28:
result = ' ';
break;
case 29:
result = '?';
break;
case 30:
result = '-';
break;
case 31:
result = '+';
}
return result;
}
Error Messages:
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino: In function 'void PlayGuessingGame()':
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:263:17: warning: invalid conversion from 'char' to 'char*' [-fpermissive]
char *state = char(buttonState);
^~~~~~~~~~~~~~~~~
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino: In function 'void ShowCurrentGuess()':
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:378:40: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
DelayAnimation("You're Guessing ", char(guess),1500);
^~~~~~~~~~~
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:198:6: note: initializing argument 2 of 'void DelayAnimation(const char*, const char*, int)'
void DelayAnimation(const char *line1, const char *line2, int delayTime)
^~~~~~~~~~~~~~
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino: In function 'void CurrentStatus()':
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:391:44: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
strcpy(currentAnswer, GetCorrectGuesses());
~~~~~~~~~~~~~~~~~^~
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:25:0,
from sketch\ChristmasBoxV3.ino.cpp:1:
c:\program files (x86)\arduino\hardware\tools\avr\avr\include\string.h:305:14: note: initializing argument 2 of 'char* strcpy(char*, const char*)'
extern char *strcpy(char *, const char *);
^~~~~~
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:393:44: warning: invalid conversion from 'char' to 'char*' [-fpermissive]
DelayAnimation("You've Guessed ", strcat(char(currentClueIndex), " Clues"), 2000);
^~~~~~~~~~~~~~~~~~~~~~
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:25:0,
from sketch\ChristmasBoxV3.ino.cpp:1:
c:\program files (x86)\arduino\hardware\tools\avr\avr\include\string.h:248:14: note: initializing argument 1 of 'char* strcat(char*, const char*)'
extern char *strcat(char *, const char *);
^~~~~~
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino: In function 'char GetCorrectGuesses()':
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:406:10: warning: invalid conversion from 'char*' to 'char' [-fpermissive]
return result;
^~~~~~
C:\Users\jb090\Documents\Arduino\ChristmasBoxV3\ChristmasBoxV3.ino:401:8: warning: address of local variable 'result' returned [-Wreturn-local-addr]
char result[17];
^~~~~~
char and char* is very different !
const char* can be slightly more tricky, but you first have to get the difference between a text and a character.
"Hello World" is a text, and usually treated as a pointer or [const] array.
'A' is a character, and internally stored the same way as the number 65 in a variable of type char.
More:
c# (or java) and c++ are very different, too. And there's a lot of truth in the saying "c++ is for masochists" :)
Your first problem
warning: invalid conversion from 'char' to 'char*'
char *state = char(buttonState);
comes from these lines:
bool buttonState = digitalRead(guessPin);
char *state = char(buttonState);
logger(state);
logger(const char*) needs a text to log, but you have a bool. Not sure how you wrote that in c#, and casting won't help. You eventually might want something like
bool buttonState = digitalRead(guessPin);
logger(buttonState? "guessPin is set": "guessPin is LOW");
You cannot cast a variable as a char by doing char(buttonState). In C you cast as follows: (char)buttonState.
There were some minor errors in my code (like incorrect for loop syntax) that was causing me to have run time errors.

how to tokenize a string in arduino

i am using arduino due. what i am trying to do is to receive a string at serial. like this one:
COMSTEP 789 665 432 END
if the string starts with comstep, then to tokenize the string and get an integer array {789, 665, 432}.
is there anyway to do that?
P.S: im a noob at programming, so any help is appreciated.
I have a function that I wrote long ago to parse strings up in an easy manner. It is in use on several of my Arduino projects.
Sample usage:
char pinStr[3];
char valueStr[7];
int pinNumber, value;
getstrfld (parms_in, 0, 0, (char *)",", pinStr);
getstrfld (parms_in, 1, 0, (char *)",", valueStr);
pinNumber = atoi (pinStr);
value = atoi (valueStr);
The functions:
// My old stand-by to break delimited strings up.
char * getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr)
{
char *offset, *strptr;
int curfld;
offset = strptr = (char *)NULL;
curfld = 0;
strbuf += ofset;
while (*strbuf) {
strptr = !offset ? strbuf : offset;
offset = strpbrk ((!offset ? strbuf : offset), sep);
if (offset) {
offset++;
} else if (curfld != fldno) {
*retstr = 0;
break;
}
if (curfld == fldno) {
strncpy (retstr, strptr,
(int)(!offset ? strlen (strptr)+ 1 :
(int)(offset - strptr)));
if (offset)
retstr[offset - strptr - 1] = 0;
break;
}
curfld++;
}
return retstr;
}
// Included because strpbrk is not in the arduino gcc/g++ libraries
// Or I just could not find it :)
char * strpbrk (const char *s1, const char *s2)
{
const char *c = s2;
if (!*s1) {
return (char *) NULL;
}
while (*s1) {
for (c = s2; *c; c++) {
if (*s1 == *c)
break;
}
if (*c)
break;
s1++;
}
if (*c == '\0')
s1 = NULL;
return (char *) s1;
}
A light-weight approach (no strict checks on valid parses of the integers and ignoring any list elements past a fixed maximum):
char buf[32] = "COMSTEP 789 665 432 END"; // assume this has just been read
int res[8], nres = 0;
bool inlist = false;
for (char *p = strtok(buf, " "); p; p = strtok(0, " "))
if (inlist)
{
if (!strcmp(p, "END"))
{
inlist = false;
break;
}
else if (nres < sizeof(res) / sizeof(*res))
res[nres++] = atoi(p);
}
else if (!strcmp(p, "COMSTEP"))
inlist = true;
if (!inlist)
for (size_t i = 0; i < nres; ++i)
printf("%d%s", res[i], i + 1 < nres ? " " : "\n"); // do whatever

rosserial arduino hello world won't verify

Edit: Solved
SOLUTION:
I'm running Arduino 1.0.5
I fixed the problem by changing /Sketchbook/library/ros_lib/ros/node_handle.h line 260 from
}else if (topic_ == TopicInfo::ID_TX_STOP){
to
}else if (topic_ == ID_TX_STOP){
However, this gave me a new error message:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp: In member function 'size_t Print::print(const __FlashStringHelper*)':
/usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp:44:9: error: 'prog_char' does not name a type
/usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp:47:23: error: 'p' was not declared in this scope
So to fix this I edited usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp, line 44 from
const prog_char *p = (const prog_char *)ifsh;
to
const char PROGMEM *p = (const char PROGMEM *)ifsh;
Now it compiles!
Original Problem:
I've been using this tutorial to get everything set up: (http://wiki.ros.org/rosserial_arduino/Tutorials/Arduino%20IDE%20Setup) I can install everything without an issue I think, the ros_lib folder is placed in my sketchbook libraries. But when I do the next tutorial with the helloworld example, the code does not verify correctly. When I try to verify with the checkmark in the Arduino IDE, I get the following set of error codes:
In file included from /home/user/sketchbook/libraries/ros_lib/ros.h:38:0,
from HelloWorld.cpp:6:
/home/user/sketchbook/libraries/ros_lib/ros/node_handle.h: In member function 'virtual int ros::NodeHandle_::spinOnce()':
/home/user/sketchbook/libraries/ros_lib/ros/node_handle.h:260:45: error: expected unqualified-id before numeric constant
/home/user/sketchbook/libraries/ros_lib/ros/node_handle.h:260:45: error: expected ')' before numeric constant
I've reinstalled the ros_lib as well as rosserial and I keep getting this error, so I don't know what the problem is. I looked around line 260 of the node_handle.h file but I didn't notice anything out of place.
Here's node_handle.h: (spacing might be a little off)
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote prducts derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ROS_NODE_HANDLE_H_
#define ROS_NODE_HANDLE_H_
#include "std_msgs/Time.h"
#include "rosserial_msgs/TopicInfo.h"
#include "rosserial_msgs/Log.h"
#include "rosserial_msgs/RequestParam.h"
#define SYNC_SECONDS 5
#define MODE_FIRST_FF 0
/*
* The second sync byte is a protocol version. It's value is 0xff for the first
* version of the rosserial protocol (used up to hydro), 0xfe for the second version
* (introduced in hydro), 0xfd for the next, and so on. Its purpose is to enable
* detection of mismatched protocol versions (e.g. hydro rosserial_python with groovy
* rosserial_arduino. It must be changed in both this file and in
* rosserial_python/src/rosserial_python/SerialClient.py
*/
#define MODE_PROTOCOL_VER 1
#define PROTOCOL_VER1 0xff // through groovy
#define PROTOCOL_VER2 0xfe // in hydro
#define PROTOCOL_VER PROTOCOL_VER2
#define MODE_SIZE_L 2
#define MODE_SIZE_H 3
#define MODE_SIZE_CHECKSUM 4 // checksum for msg size received from size L and H
#define MODE_TOPIC_L 5 // waiting for topic id
#define MODE_TOPIC_H 6
#define MODE_MESSAGE 7
#define MODE_MSG_CHECKSUM 8 // checksum for msg and topic id
#define MSG_TIMEOUT 20 //20 milliseconds to recieve all of message data
#define ID_TX_STOP 11 //hardcode for hydro version
#include "msg.h"
namespace ros {
class NodeHandleBase_{
public:
virtual int publish(int id, const Msg* msg)=0;
virtual int spinOnce()=0;
virtual bool connected()=0;
};
}
#include "publisher.h"
#include "subscriber.h"
#include "service_server.h"
#include "service_client.h"
namespace ros {
using rosserial_msgs::TopicInfo;
/* Node Handle */
template<class Hardware,
int MAX_SUBSCRIBERS=25,
int MAX_PUBLISHERS=25,
int INPUT_SIZE=512,
int OUTPUT_SIZE=512>
class NodeHandle_ : public NodeHandleBase_
{
protected:
Hardware hardware_;
/* time used for syncing */
unsigned long rt_time;
/* used for computing current time */
unsigned long sec_offset, nsec_offset;
unsigned char message_in[INPUT_SIZE];
unsigned char message_out[OUTPUT_SIZE];
Publisher * publishers[MAX_PUBLISHERS];
Subscriber_ * subscribers[MAX_SUBSCRIBERS];
/*
* Setup Functions
*/
public:
NodeHandle_() : configured_(false) {
for(unsigned int i=0; i< MAX_PUBLISHERS; i++)
publishers[i] = 0;
for(unsigned int i=0; i< MAX_SUBSCRIBERS; i++)
subscribers[i] = 0;
for(unsigned int i=0; i< INPUT_SIZE; i++)
message_in[i] = 0;
for(unsigned int i=0; i< OUTPUT_SIZE; i++)
message_out[i] = 0;
req_param_resp.ints_length = 0;
req_param_resp.ints = NULL;
req_param_resp.floats_length = 0;
req_param_resp.floats = NULL;
req_param_resp.ints_length = 0;
req_param_resp.ints = NULL;
}
Hardware* getHardware(){
return &hardware_;
}
/* Start serial, initialize buffers */
void initNode(){
hardware_.init();
mode_ = 0;
bytes_ = 0;
index_ = 0;
topic_ = 0;
};
/* Start a named port, which may be network server IP, initialize buffers */
void initNode(char *portName){
hardware_.init(portName);
mode_ = 0;
bytes_ = 0;
index_ = 0;
topic_ = 0;
};
protected:
//State machine variables for spinOnce
int mode_;
int bytes_;
int topic_;
int index_;
int checksum_;
bool configured_;
/* used for syncing the time */
unsigned long last_sync_time;
unsigned long last_sync_receive_time;
unsigned long last_msg_timeout_time;
public:
/* This function goes in your loop() function, it handles
* serial input and callbacks for subscribers.
*/
virtual int spinOnce(){
/* restart if timed out */
unsigned long c_time = hardware_.time();
if( (c_time - last_sync_receive_time) > (SYNC_SECONDS*2200) ){
configured_ = false;
}
/* reset if message has timed out */
if ( mode_ != MODE_FIRST_FF){
if (c_time > last_msg_timeout_time){
mode_ = MODE_FIRST_FF;
}
}
/* while available buffer, read data */
while( true )
{
int data = hardware_.read();
if( data < 0 )
break;
checksum_ += data;
if( mode_ == MODE_MESSAGE ){ /* message data being recieved */
message_in[index_++] = data;
bytes_--;
if(bytes_ == 0) /* is message complete? if so, checksum */
mode_ = MODE_MSG_CHECKSUM;
}else if( mode_ == MODE_FIRST_FF ){
if(data == 0xff){
mode_++;
last_msg_timeout_time = c_time + MSG_TIMEOUT;
}
}else if( mode_ == MODE_PROTOCOL_VER ){
if(data == PROTOCOL_VER){
mode_++;
}else{
mode_ = MODE_FIRST_FF;
if (configured_ == false)
requestSyncTime(); /* send a msg back showing our protocol version */
}
}else if( mode_ == MODE_SIZE_L ){ /* bottom half of message size */
bytes_ = data;
index_ = 0;
mode_++;
checksum_ = data; /* first byte for calculating size checksum */
}else if( mode_ == MODE_SIZE_H ){ /* top half of message size */
bytes_ += data<<8;
mode_++;
}else if( mode_ == MODE_SIZE_CHECKSUM ){
if( (checksum_%256) == 255)
mode_++;
else
mode_ = MODE_FIRST_FF; /* Abandon the frame if the msg len is wrong */
}else if( mode_ == MODE_TOPIC_L ){ /* bottom half of topic id */
topic_ = data;
mode_++;
checksum_ = data; /* first byte included in checksum */
}else if( mode_ == MODE_TOPIC_H ){ /* top half of topic id */
topic_ += data<<8;
mode_ = MODE_MESSAGE;
if(bytes_ == 0)
mode_ = MODE_MSG_CHECKSUM;
}else if( mode_ == MODE_MSG_CHECKSUM ){ /* do checksum */
mode_ = MODE_FIRST_FF;
if( (checksum_%256) == 255){
if(topic_ == TopicInfo::ID_PUBLISHER){
requestSyncTime();
negotiateTopics();
last_sync_time = c_time;
last_sync_receive_time = c_time;
return -1;
}else if(topic_ == TopicInfo::ID_TIME){
syncTime(message_in);
}else if (topic_ == TopicInfo::ID_PARAMETER_REQUEST){
req_param_resp.deserialize(message_in);
param_recieved= true;
}else if(topic_ == TopicInfo::ID_TX_STOP){
configured_ = false;
}else{
if(subscribers[topic_-100])
subscribers[topic_-100]->callback( message_in );
}
}
}
}
/* occasionally sync time */
if( configured_ && ((c_time-last_sync_time) > (SYNC_SECONDS*500) )){
requestSyncTime();
last_sync_time = c_time;
}
return 0;
}
/* Are we connected to the PC? */
virtual bool connected() {
return configured_;
};
/********************************************************************
* Time functions
*/
void requestSyncTime()
{
std_msgs::Time t;
publish(TopicInfo::ID_TIME, &t);
rt_time = hardware_.time();
}
void syncTime( unsigned char * data )
{
std_msgs::Time t;
unsigned long offset = hardware_.time() - rt_time;
t.deserialize(data);
t.data.sec += offset/1000;
t.data.nsec += (offset%1000)*1000000UL;
this->setNow(t.data);
last_sync_receive_time = hardware_.time();
}
Time now(){
unsigned long ms = hardware_.time();
Time current_time;
current_time.sec = ms/1000 + sec_offset;
current_time.nsec = (ms%1000)*1000000UL + nsec_offset;
normalizeSecNSec(current_time.sec, current_time.nsec);
return current_time;
}
void setNow( Time & new_now )
{
unsigned long ms = hardware_.time();
sec_offset = new_now.sec - ms/1000 - 1;
nsec_offset = new_now.nsec - (ms%1000)*1000000UL + 1000000000UL;
normalizeSecNSec(sec_offset, nsec_offset);
}
/********************************************************************
* Topic Management
*/
/* Register a new publisher */
bool advertise(Publisher & p)
{
for(int i = 0; i < MAX_PUBLISHERS; i++){
if(publishers[i] == 0){ // empty slot
publishers[i] = &p;
p.id_ = i+100+MAX_SUBSCRIBERS;
p.nh_ = this;
return true;
}
}
return false;
}
/* Register a new subscriber */
template<typename MsgT>
bool subscribe(Subscriber< MsgT> & s){
for(int i = 0; i < MAX_SUBSCRIBERS; i++){
if(subscribers[i] == 0){ // empty slot
subscribers[i] = (Subscriber_*) &s;
s.id_ = i+100;
return true;
}
}
return false;
}
/* Register a new Service Server */
template<typename MReq, typename MRes>
bool advertiseService(ServiceServer<MReq,MRes>& srv){
bool v = advertise(srv.pub);
for(int i = 0; i < MAX_SUBSCRIBERS; i++){
if(subscribers[i] == 0){ // empty slot
subscribers[i] = (Subscriber_*) &srv;
srv.id_ = i+100;
return v;
}
}
return false;
}
/* Register a new Service Client */
template<typename MReq, typename MRes>
bool serviceClient(ServiceClient<MReq, MRes>& srv){
bool v = advertise(srv.pub);
for(int i = 0; i < MAX_SUBSCRIBERS; i++){
if(subscribers[i] == 0){ // empty slot
subscribers[i] = (Subscriber_*) &srv;
srv.id_ = i+100;
return v;
}
}
return false;
}
void negotiateTopics()
{
rosserial_msgs::TopicInfo ti;
int i;
for(i = 0; i < MAX_PUBLISHERS; i++)
{
if(publishers[i] != 0) // non-empty slot
{
ti.topic_id = publishers[i]->id_;
ti.topic_name = (char *) publishers[i]->topic_;
ti.message_type = (char *) publishers[i]->msg_->getType();
ti.md5sum = (char *) publishers[i]->msg_->getMD5();
ti.buffer_size = OUTPUT_SIZE;
publish( publishers[i]->getEndpointType(), &ti );
}
}
for(i = 0; i < MAX_SUBSCRIBERS; i++)
{
if(subscribers[i] != 0) // non-empty slot
{
ti.topic_id = subscribers[i]->id_;
ti.topic_name = (char *) subscribers[i]->topic_;
ti.message_type = (char *) subscribers[i]->getMsgType();
ti.md5sum = (char *) subscribers[i]->getMsgMD5();
ti.buffer_size = INPUT_SIZE;
publish( subscribers[i]->getEndpointType(), &ti );
}
}
configured_ = true;
}
virtual int publish(int id, const Msg * msg)
{
if(id >= 100 && !configured_)
return 0;
/* serialize message */
unsigned int l = msg->serialize(message_out+7);
/* setup the header */
message_out[0] = 0xff;
message_out[1] = PROTOCOL_VER;
message_out[2] = (unsigned char) ((unsigned int)l&255);
message_out[3] = (unsigned char) ((unsigned int)l>>8);
message_out[4] = 255 - ((message_out[2] + message_out[3])%256);
message_out[5] = (unsigned char) ((int)id&255);
message_out[6] = (unsigned char) ((int)id>>8);
/* calculate checksum */
int chk = 0;
for(int i =5; i<l+7; i++)
chk += message_out[i];
l += 7;
message_out[l++] = 255 - (chk%256);
if( l <= OUTPUT_SIZE ){
hardware_.write(message_out, l);
return l;
}else{
logerror("Message from device dropped: message larger than buffer.");
return -1;
}
}
/********************************************************************
* Logging
*/
private:
void log(char byte, const char * msg){
rosserial_msgs::Log l;
l.level= byte;
l.msg = (char*)msg;
publish(rosserial_msgs::TopicInfo::ID_LOG, &l);
}
public:
void logdebug(const char* msg){
log(rosserial_msgs::Log::ROSDEBUG, msg);
}
void loginfo(const char * msg){
log(rosserial_msgs::Log::INFO, msg);
}
void logwarn(const char *msg){
log(rosserial_msgs::Log::WARN, msg);
}
void logerror(const char*msg){
log(rosserial_msgs::Log::ERROR, msg);
}
void logfatal(const char*msg){
log(rosserial_msgs::Log::FATAL, msg);
}
/********************************************************************
* Parameters
*/
private:
bool param_recieved;
rosserial_msgs::RequestParamResponse req_param_resp;
bool requestParam(const char * name, int time_out = 1000){
param_recieved = false;
rosserial_msgs::RequestParamRequest req;
req.name = (char*)name;
publish(TopicInfo::ID_PARAMETER_REQUEST, &req);
unsigned int end_time = hardware_.time() + time_out;
while(!param_recieved ){
spinOnce();
if (hardware_.time() > end_time) return false;
}
return true;
}
public:
bool getParam(const char* name, int* param, int length =1){
if (requestParam(name) ){
if (length == req_param_resp.ints_length){
//copy it over
for(int i=0; i<length; i++)
param[i] = req_param_resp.ints[i];
return true;
}
}
return false;
}
bool getParam(const char* name, float* param, int length=1){
if (requestParam(name) ){
if (length == req_param_resp.floats_length){
//copy it over
for(int i=0; i<length; i++)
param[i] = req_param_resp.floats[i];
return true;
}
}
return false;
}
bool getParam(const char* name, char** param, int length=1){
if (requestParam(name) ){
if (length == req_param_resp.strings_length){
//copy it over
for(int i=0; i<length; i++)
strcpy(param[i],req_param_resp.strings[i]);
return true;
}
}
return false;
}
};
}
#endif
I tried commenting out line 160 and 161:
//}else if (topic_ == TopicInfo::ID_TX_STOP){
//configured_ = false;
This gave me a different error message:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp: In member function 'size_t Print::print(const __FlashStringHelper*)':
/usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp:44:9: error: 'prog_char' does not name a type
/usr/share/arduino/hardware/arduino/cores/arduino/Print.cpp:47:23: error: 'p' was not declared in this scope
So here's Print.cpp
/*
Print.cpp - Base class that provides print() and println()
Copyright (c) 2008 David A. Mellis. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 23 November 2006 by David A. Mellis
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Arduino.h"
#include "Print.h"
// Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
n += write(*buffer++);
}
return n;
}
size_t Print::print(const __FlashStringHelper *ifsh)
{
const prog_char *p = (const prog_char *)ifsh;
size_t n = 0;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) break;
n += write(c);
}
return n;
}
size_t Print::print(const String &s)
{
size_t n = 0;
for (uint16_t i = 0; i < s.length(); i++) {
n += write(s[i]);
}
return n;
}
size_t Print::print(const char str[])
{
return write(str);
}
size_t Print::print(char c)
{
return write(c);
}
size_t Print::print(unsigned char b, int base)
{
return print((unsigned long) b, base);
}
size_t Print::print(int n, int base)
{
return print((long) n, base);
}
size_t Print::print(unsigned int n, int base)
{
return print((unsigned long) n, base);
}
size_t Print::print(long n, int base)
{
if (base == 0) {
return write(n);
} else if (base == 10) {
if (n < 0) {
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
return printNumber(n, base);
}
}
size_t Print::print(unsigned long n, int base)
{
if (base == 0) return write(n);
else return printNumber(n, base);
}
size_t Print::print(double n, int digits)
{
return printFloat(n, digits);
}
size_t Print::println(const __FlashStringHelper *ifsh)
{
size_t n = print(ifsh);
n += println();
return n;
}
size_t Print::print(const Printable& x)
{
return x.printTo(*this);
}
size_t Print::println(void)
{
size_t n = print('\r');
n += print('\n');
return n;
}
size_t Print::println(const String &s)
{
size_t n = print(s);
n += println();
return n;
}
size_t Print::println(const char c[])
{
size_t n = print(c);
n += println();
return n;
}
size_t Print::println(char c)
{
size_t n = print(c);
n += println();
return n;
}
size_t Print::println(unsigned char b, int base)
{
size_t n = print(b, base);
n += println();
return n;
}
size_t Print::println(int num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(unsigned int num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(long num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(unsigned long num, int base)
{
size_t n = print(num, base);
n += println();
return n;
}
size_t Print::println(double num, int digits)
{
size_t n = print(num, digits);
n += println();
return n;
}
size_t Print::println(const Printable& x)
{
size_t n = print(x);
n += println();
return n;
}
// Private Methods /////////////////////////////////////////////////////////////
size_t Print::printNumber(unsigned long n, uint8_t base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
do {
unsigned long m = n;
n /= base;
char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
return write(str);
}
size_t Print::printFloat(double number, uint8_t digits)
{
size_t n = 0;
// Handle negative numbers
if (number < 0.0)
{
n += print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
n += print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
n += print(".");
}
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
n += print(toPrint);
remainder -= toPrint;
}
return n;
}

Are there any example of Mutual recursion?

Are there any examples for a recursive function that calls an other function which calls the first one too ?
Example :
function1()
{
//do something
function2();
//do something
}
function2()
{
//do something
function1();
//do something
}
Mutual recursion is common in code that parses mathematical expressions (and other grammars). A recursive descent parser based on the grammar below will naturally contain mutual recursion: expression-terms-term-factor-primary-expression.
expression
+ terms
- terms
terms
terms
term + terms
term - terms
term
factor
factor * term
factor / term
factor
primary
primary ^ factor
primary
( expression )
number
name
name ( expression )
The proper term for this is Mutual Recursion.
http://en.wikipedia.org/wiki/Mutual_recursion
There's an example on that page, I'll reproduce here in Java:
boolean even( int number )
{
if( number == 0 )
return true;
else
return odd(abs(number)-1)
}
boolean odd( int number )
{
if( number == 0 )
return false;
else
return even(abs(number)-1);
}
Where abs( n ) means return the absolute value of a number.
Clearly this is not efficient, just to demonstrate a point.
An example might be the minmax algorithm commonly used in game programs such as chess. Starting at the top of the game tree, the goal is to find the maximum value of all the nodes at the level below, whose values are defined as the minimum of the values of the nodes below that, whose values are defines as the maximum of the values below that, whose values ...
I can think of two common sources of mutual recursion.
Functions dealing with mutually recursive types
Consider an Abstract Syntax Tree (AST) that keeps position information in every node. The type might look like this:
type Expr =
| Int of int
| Var of string
| Add of ExprAux * ExprAux
and ExprAux = Expr of int * Expr
The easiest way to write functions that manipulate values of these types is to write mutually recursive functions. For example, a function to find the set of free variables:
let rec freeVariables = function
| Int n -> Set.empty
| Var x -> Set.singleton x
| Add(f, g) -> Set.union (freeVariablesAux f) (freeVariablesAux g)
and freeVariablesAux (Expr(loc, e)) =
freeVariables e
State machines
Consider a state machine that is either on, off or paused with instructions to start, stop, pause and resume (F# code):
type Instruction = Start | Stop | Pause | Resume
The state machine might be written as mutually recursive functions with one function for each state:
type State = State of (Instruction -> State)
let rec isOff = function
| Start -> State isOn
| _ -> State isOff
and isOn = function
| Stop -> State isOff
| Pause -> State isPaused
| _ -> State isOn
and isPaused = function
| Stop -> State isOff
| Resume -> State isOn
| _ -> State isPaused
It's a bit contrived and not very efficient, but you could do this with a function to calculate Fibbonacci numbers as in:
fib2(n) { return fib(n-2); }
fib1(n) { return fib(n-1); }
fib(n)
{
if (n>1)
return fib1(n) + fib2(n);
else
return 1;
}
In this case its efficiency can be dramatically enhanced if the language supports memoization
In a language with proper tail calls, Mutual Tail Recursion is a very natural way of implementing automata.
Here is my coded solution. For a calculator app that performs *,/,- operations using mutual recursion. It also checks for brackets (()) to decide the order of precedence.
Flow:: expression -> term -> factor -> expression
Calculator.h
#ifndef CALCULATOR_H_
#define CALCULATOR_H_
#include <string>
using namespace std;
/****** A Calculator Class holding expression, term, factor ********/
class Calculator
{
public:
/**Default Constructor*/
Calculator();
/** Parameterized Constructor common for all exception
* #aparam e exception value
* */
Calculator(char e);
/**
* Function to start computation
* #param input - input expression*/
void start(string input);
/**
* Evaluates Term*
* #param input string for term*/
double term(string& input);
/* Evaluates factor*
* #param input string for factor*/
double factor(string& input);
/* Evaluates Expression*
* #param input string for expression*/
double expression(string& input);
/* Evaluates number*
* #param input string for number*/
string number(string n);
/**
* Prints calculates value of the expression
* */
void print();
/**
* Converts char to double
* #param c input char
* */
double charTONum(const char* c);
/**
* Get error
*/
char get_value() const;
/** Reset all values*/
void reset();
private:
int lock;//set lock to check extra parenthesis
double result;// result
char error_msg;// error message
};
/**Error for unexpected string operation*/
class Unexpected_error:public Calculator
{
public:
Unexpected_error(char e):Calculator(e){};
};
/**Error for missing parenthesis*/
class Missing_parenthesis:public Calculator
{
public:
Missing_parenthesis(char e):Calculator(e){};
};
/**Error if divide by zeros*/
class DivideByZero:public Calculator{
public:
DivideByZero():Calculator(){};
};
#endif
===============================================================================
Calculator.cpp
//============================================================================
// Name : Calculator.cpp
// Author : Anurag
// Version :
// Copyright : Your copyright notice
// Description : Calculator using mutual recursion in C++, Ansi-style
//============================================================================
#include "Calculator.h"
#include <iostream>
#include <string>
#include <math.h>
#include <exception>
using namespace std;
Calculator::Calculator():lock(0),result(0),error_msg(' '){
}
Calculator::Calculator(char e):result(0), error_msg(e) {
}
char Calculator::get_value() const {
return this->error_msg;
}
void Calculator::start(string input) {
try{
result = expression(input);
print();
}catch (Unexpected_error e) {
cout<<result<<endl;
cout<<"***** Unexpected "<<e.get_value()<<endl;
}catch (Missing_parenthesis e) {
cout<<"***** Missing "<<e.get_value()<<endl;
}catch (DivideByZero e) {
cout<<"***** Division By Zero" << endl;
}
}
double Calculator::expression(string& input) {
double expression=0;
if(input.size()==0)
return 0;
expression = term(input);
if(input[0] == ' ')
input = input.substr(1);
if(input[0] == '+') {
input = input.substr(1);
expression += term(input);
}
else if(input[0] == '-') {
input = input.substr(1);
expression -= term(input);
}
if(input[0] == '%'){
result = expression;
throw Unexpected_error(input[0]);
}
if(input[0]==')' && lock<=0 )
throw Missing_parenthesis(')');
return expression;
}
double Calculator::term(string& input) {
if(input.size()==0)
return 1;
double term=1;
term = factor(input);
if(input[0] == ' ')
input = input.substr(1);
if(input[0] == '*') {
input = input.substr(1);
term = term * factor(input);
}
else if(input[0] == '/') {
input = input.substr(1);
double den = factor(input);
if(den==0) {
throw DivideByZero();
}
term = term / den;
}
return term;
}
double Calculator::factor(string& input) {
double factor=0;
if(input[0] == ' ') {
input = input.substr(1);
}
if(input[0] == '(') {
lock++;
input = input.substr(1);
factor = expression(input);
if(input[0]==')') {
lock--;
input = input.substr(1);
return factor;
}else{
throw Missing_parenthesis(')');
}
}
else if (input[0]>='0' && input[0]<='9'){
string nums = input.substr(0,1) + number(input.substr(1));
input = input.substr(nums.size());
return stod(nums);
}
else {
result = factor;
throw Unexpected_error(input[0]);
}
return factor;
}
string Calculator::number(string input) {
if(input.substr(0,2)=="E+" || input.substr(0,2)=="E-" || input.substr(0,2)=="e-" || input.substr(0,2)=="e-")
return input.substr(0,2) + number(input.substr(2));
else if((input[0]>='0' && input[0]<='9') || (input[0]=='.'))
return input.substr(0,1) + number(input.substr(1));
else
return "";
}
void Calculator::print() {
cout << result << endl;
}
void Calculator::reset(){
this->lock=0;
this->result=0;
}
int main() {
Calculator* cal = new Calculator;
string input;
cout<<"Expression? ";
getline(cin,input);
while(input!="."){
cal->start(input.substr(0,input.size()-2));
cout<<"Expression? ";
cal->reset();
getline(cin,input);
}
cout << "Done!" << endl;
return 0;
}
==============================================================
Sample input-> Expression? (42+8)*10 =
Output-> 500
Top down merge sort can use a pair of mutually recursive functions to alternate the direction of merge based on level of recursion.
For the example code below, a[] is the array to be sorted, b[] is a temporary working array. For a naive implementation of merge sort, each merge operation copies data from a[] to b[], then merges b[] back to a[], or it merges from a[] to b[], then copies from b[] back to a[]. This requires n ยท ceiling(log2(n)) copy operations. To eliminate the copy operations used for merging, the direction of merge can be alternated based on level of recursion, merge from a[] to b[], merge from b[] to a[], ..., and switch to in place insertion sort for small runs in a[], as insertion sort on small runs is faster than merge sort.
In this example, MergeSortAtoA() and MergeSortAtoB() are the mutually recursive functions.
Example java code:
static final int ISZ = 64; // use insertion sort if size <= ISZ
static void MergeSort(int a[])
{
int n = a.length;
if(n < 2)
return;
int [] b = new int[n];
MergeSortAtoA(a, b, 0, n);
}
static void MergeSortAtoA(int a[], int b[], int ll, int ee)
{
if ((ee - ll) <= ISZ){ // use insertion sort on small runs
InsertionSort(a, ll, ee);
return;
}
int rr = (ll + ee)>>1; // midpoint, start of right half
MergeSortAtoB(a, b, ll, rr);
MergeSortAtoB(a, b, rr, ee);
Merge(b, a, ll, rr, ee); // merge b to a
}
static void MergeSortAtoB(int a[], int b[], int ll, int ee)
{
int rr = (ll + ee)>>1; // midpoint, start of right half
MergeSortAtoA(a, b, ll, rr);
MergeSortAtoA(a, b, rr, ee);
Merge(a, b, ll, rr, ee); // merge a to b
}
static void Merge(int a[], int b[], int ll, int rr, int ee)
{
int o = ll; // b[] index
int l = ll; // a[] left index
int r = rr; // a[] right index
while(true){ // merge data
if(a[l] <= a[r]){ // if a[l] <= a[r]
b[o++] = a[l++]; // copy a[l]
if(l < rr) // if not end of left run
continue; // continue (back to while)
while(r < ee){ // else copy rest of right run
b[o++] = a[r++];
}
break; // and return
} else { // else a[l] > a[r]
b[o++] = a[r++]; // copy a[r]
if(r < ee) // if not end of right run
continue; // continue (back to while)
while(l < rr){ // else copy rest of left run
b[o++] = a[l++];
}
break; // and return
}
}
}
static void InsertionSort(int a[], int ll, int ee)
{
int i, j;
int t;
for (j = ll + 1; j < ee; j++) {
t = a[j];
i = j-1;
while(i >= ll && a[i] > t){
a[i+1] = a[i];
i--;
}
a[i+1] = t;
}
}

Resources