c - Making calculator in yacc and lex char array -
i'm working yacc , lex, , have problem something.
i working on caluclator. calculator can :
example:
user: = 1+3 user: calc: 4
problem is, can 1 character. want maximum 3 characters.
example:
user: abc = 1+3 user: abc calc: 4
also want add more.
my code works :
user: abc = 1+3 user: abc calc: 4 user: ab calc: 4 /* because scan first character */ user: calc: 4 /* because scan first character */
part of code connecting problem :
-yacc
%{ int symbolval(char symbol); void updatesymbolval(char symbol, int val); %} %union {double num; char id;} %token <id> identifier %type <id> assignment %% assignment : identifier equals exp { updatesymbolval($1, $3); } term : /*something */ | identifier { $$ = symbolval($1); } %% /* c code */ int symbolval(char symbol) { int bucker = computesymbolindex(symbol); return symbols[bucket]; } void updatesymbolval(char symbol,int val) { int bucket = computesymbolindex(symbol); symbols[bucket] = val; } int computesymbolindex(char token) { int idx = 0; idx = token - 'a' + 26; return idx; }
i want code is correct.
but want maximum 3 characters.
maybe can array or pointers, don't know how.
my guess in lex returning single character token. way return token representing variable, , actual name it's yyval. here's code snippets of go code make work - though can't further without seeing lex file.
lex:
[a-za-z]+ { yylval.s = new std::string(yytext); return tvariable; }
yacc:
%union { str s; /* symbol name */ } %token<s> tvariable ... // on grammar: identifier: tvariable { $$ = $1; }
Comments
Post a Comment