# HG changeset patch # User Masataka Kohagura # Date 1433774409 -32400 # Node ID 883e3473a9f5f15114b9c33e0d43a06441be16ce # Parent f540de861cd64ebd2a3e7cb0c834d54ad9837130 fix diff -r f540de861cd6 -r 883e3473a9f5 c/regexParser/main.cc --- a/c/regexParser/main.cc Mon Jun 08 15:47:52 2015 +0900 +++ b/c/regexParser/main.cc Mon Jun 08 23:40:09 2015 +0900 @@ -1,29 +1,186 @@ +/* + Very Simple Calculator + $Id$ + */ + #include -#include -#include -typedef struct node { - struct node *left; - struct node *right; - int type; - int value; -} Node, *NodePtr; +static char *ptr,*last_ptr; +static int value,lvalue; +static int last_token; +static int variable[48]; + +static int expr(); +static int aexpr(); +static int mexpr(); +static int term(); +static int token(); +static void error(char *); -NodePtr newNode(int type, int value, NodePtr left, NodePtr right) { - NodePtr d = (NodePtr)malloc(sizeof(Node)); - d->type = type; - d->value = value; - d->left = left; - d->right = right; - return d; +static int +token() +{ + int c,d; + + last_ptr = ptr; /* for error position */ + c= *ptr; + if(!c) { + last_token = EOF; + return last_token; + } + ptr++; + if (c<=' ') { /* comment */ + while(*ptr++); + ptr--; + last_token = EOF; + last_ptr = ptr; + return last_token; + } + + if('0'<=c && c<='9') { /* Decimal */ + d = c-'0'; + while((c= *ptr++)) { + if('0'<=c && c<='9') { + d = d*10 + (c - '0'); + } else { + break; + } + } + c && ptr--; + value = d; + last_token = '0'; + return last_token; + + } else if ('a'<=c && c<='z') { /* variable */ + value = c-'a'; /* return variable reference */ + last_token = 'v'; + return last_token; + } else { + last_token = c; + return last_token; + return c; + } +} + +static int +expr() +{ + int d,assign; + + d = aexpr(); + assign = lvalue; + switch(last_token) { + case '>': + d = (d > aexpr()); + return d; + case '=': + if(assign>=0) { + d = expr(); + variable[assign] = d; + return d; + } else { + error("Bad assignment"); + return 0; + } + case ')': + return d; + case EOF: + return d; + default: + error("Bad expression"); + return d; + } } -int main(int argc, char **argv) { +static int +aexpr() +{ + int d; + + d = mexpr(); + switch(last_token) { + case '-': + d -= aexpr(); + return d; + case '+': + d += aexpr(); + return d; + default: + return d; + } +} + +static int +mexpr() +{ + int d; + d = term(); + switch(last_token) { + case '*': + d *= mexpr(); + return d; + case '/': + d /= mexpr(); + return d; + default: + return d; + } +} + +static int +term() +{ + int d; - for (int i = 1; i < argc ; i++) { + lvalue= -1; + token(); + if(last_token==EOF) { + error("Term expected"); + } + switch(last_token) { + case '0': + d = value; + token(); + return d; + case 'v': + d = lvalue = value; + token(); + return variable[d]; + case '(': + d = expr(); + if(last_token != ')') { + error("Unbalanced parenthsis"); + } + token(); + return d; + default: + token(); + error("Unknown term"); + return 0; + } +} +static int lineno = 0; + +void +error(char *msg) +{ + fprintf(stderr,"%s on line %d\n",msg, lineno); +} + +int +main() +{ + int d; + char buf[BUFSIZ]; + + while (fgets(buf,BUFSIZ,stdin)) { + ptr = buf; + d = expr(); + printf("%s = 0x%08x = %d\n",buf,d,d); + fflush(stdout); + lineno++; } - return 0; }