Created
October 26, 2016 04:27
-
-
Save Gitmoko/e03cb53ba9e69a0722a320ff948debf3 to your computer and use it in GitHub Desktop.
parser_by_c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#include<stdio.h> | |
#include<ctype.h> | |
#include<stdlib.h> | |
#include<string.h> | |
char str[512]; | |
int digit(int*pos) { | |
int ret = 0; | |
while (isdigit(str[*pos])) { | |
ret *= 10; | |
ret += (str[*pos] - '0'); | |
(*pos)++; | |
} | |
return ret; | |
} | |
void skip(int*pos) { | |
while (str[*pos] == ' ' || str[*pos] == '\t') { | |
(*pos)++; | |
} | |
} | |
void next(int* pos) { | |
(*pos)++; | |
skip(pos); | |
} | |
int expr(int*pos,int val); | |
int func(int* pos,int val) { | |
int locpos = *pos; | |
int ret = val; | |
skip(&locpos); | |
if (str[locpos] == '(') { | |
next(&locpos); | |
ret = expr(&locpos,0); | |
skip(&locpos); | |
if (str[locpos] != ')') { | |
goto err; | |
} | |
next(&locpos); | |
} | |
else { | |
int digitposend = locpos; | |
ret = digit(&digitposend); | |
if (digitposend == locpos) { | |
goto err; | |
} | |
locpos = digitposend; | |
} | |
*pos = locpos; | |
return ret; | |
err: | |
*pos = -1; | |
return 0; | |
} | |
int muldiv(int*pos, int val) { | |
int locpos = *pos; | |
int ret = val; | |
int flag = str[locpos] == '*' ? 1 : (str[locpos] == '/' ? -1 : 0); | |
if (!flag) { | |
goto end; | |
} | |
next(&locpos); | |
int tmpret = func(&locpos, ret); | |
flag > 0 ? (ret *= tmpret) : (ret /= tmpret); | |
if (locpos == -1) { | |
goto err; | |
} | |
skip(&locpos); | |
ret = muldiv(&locpos, ret); | |
goto end; | |
end: | |
*pos = locpos; | |
return ret; | |
err: | |
*pos = -1; | |
return 0; | |
} | |
int term(int* pos,int val) { | |
int locpos = *pos; | |
skip(&locpos); | |
int ret = func(&locpos,val); | |
if (locpos == -1) { | |
goto err; | |
} | |
skip(&locpos); | |
ret = muldiv(&locpos, ret); | |
goto end; | |
end: | |
*pos = locpos; | |
return ret; | |
err: | |
*pos = -1; | |
return 0; | |
} | |
int addsub(int*pos, int val) { | |
int locpos = *pos; | |
int ret = val; | |
skip(&locpos); | |
int flag = str[locpos] == '+' ? 1 : (str[locpos] == '-' ? -1 : 0); | |
if (!flag) { | |
goto end; | |
} | |
next(&locpos); | |
ret += flag*term(&locpos, val); | |
if (locpos == -1) { | |
goto err; | |
} | |
skip(&locpos); | |
ret = addsub(&locpos, ret); | |
goto end; | |
end: | |
*pos = locpos; | |
return ret; | |
err: | |
*pos = -1; | |
return 0; | |
} | |
int expr(int* pos,int val) { | |
int locpos = *pos; | |
skip(&locpos); | |
int ret = term(&locpos,val); | |
if (locpos == -1) { | |
goto err; | |
} | |
skip(&locpos); | |
ret = addsub(&locpos, ret); | |
goto end; | |
end: | |
*pos = locpos; | |
return ret; | |
err: | |
*pos = -1; | |
return 0; | |
} | |
int main() { | |
scanf("%511s[^\n] ", str); | |
int len = strlen(str); | |
int pos = 0; | |
int ret = expr(&pos,0); | |
if (pos >= 0){ | |
printf("%d\n", ret); | |
} | |
else { | |
printf("err\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment