Skip to content

Instantly share code, notes, and snippets.

@bugaevc
Created March 1, 2015 14:25
Show Gist options
  • Save bugaevc/7806efc6e3a41db577ff to your computer and use it in GitHub Desktop.
Save bugaevc/7806efc6e3a41db577ff to your computer and use it in GitHub Desktop.
Homework task #2
#include <stdio.h>
#include <stdlib.h>
void report_error(char *s)
{
printf("syntax error: expected %s\n", s);
}
void proceed(char **s, char **r)
{
**r = **s;
(*s)++;
(*r)++;
}
void parse_formula(char **s, char **r)
{
switch(**s)
{
case 'X':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
proceed(s, r);
break;
case '(':
{
proceed(s, r);
char *old_r = (*r)++;
parse_formula(s, r);
switch(**s)
{
case '+':
case '-':
case '*':
*old_r = **s;
(*s)++;
break;
default:
report_error("sign");
}
parse_formula(s, r);
if(**s != ')')
report_error("right paren");
proceed(s, r);
break;
}
default:
report_error("formula");
}
}
int main()
{
char *s = malloc(90),
*r = malloc(90),
*old_r = r;
gets(s);
parse_formula(&s, &r);
*r = 0;
printf("%s\n", old_r);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment