Operator-precedence parser: Difference between revisions

Content deleted Content added
Added alternatives to Dijkstra's Shunting Yard (description requires accompanying external links)
Tom Duff (talk | contribs)
Rewrote parenthesise.c to emphasize the essence of the algorithm.
Line 83:
<pre>
#include <stdio.h>
#include <string.h>
char *intable[] = { NULL, "^", "*", "/", "+", "-", "" };
int main(int argc, char **argv[]){
char *outtable[] = { "))))\n", ")^(", "))*((", "))/((", ")))+(((", ")))-(((", "((((" };
int i, tab;
int main(int argc, char **argv)
 
{
printf("((((");
int i, tab;
for (argv[i=0]=""1; i <!= argc; i++) {
if( strcmp(argv[i], "^")==0) printf(")^(");
for (tab = 0; tab < (sizeof(outtable)/sizeof(char *)); tab += 1) {
else if ((!argv[i]) || (tab && !strcmp(argv[i], intable[tab]"*")==0) printf(") {)*((");
else if(strcmp(argv[i], "/")==0) printf("%s))/((", outtable[tab]); break;
} else if (tab && !*intablestrcmp(argv[tabi], "+")==0) printf("%s)))+(((", argv[i]);
else if(strcmp(argv[i], "-")==0) printf(")))-(((");
}
else printf("%s", argv[i]);
}
printf("))))\n");
return 0;
}
</pre>