07/07/25 18:25:09
>>888 yaccの続き。次で最後です。
expression: expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression { if($3==0.0) yyerror("Divide by Zero");
else $$ = $1 / $3; }
| '-' expression %prec UMINUS { $$ = -$2; }
| '(' expression ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| NAME { $$ = $1->value; }
| NAME '(' expression ')' { if( $1->funcptr ) $$ = ($1->funcptr)($3);
else {printf("%s not a function.\n", $1->name);
}
}
;
%%
/* look up a symbol table entry, add if not present */
struct symtab *symlook(char *s)
{
char *p;
struct symtab *sp;
for(sp=symtab; sp<&symtab[NSYMS]; sp++) {
/* is it already here? */
if( sp->name && !strcmp(sp->name, s) ) return sp;
/* is it free */
if( !sp->name ) { sp->name = strdup(s);
return sp;}
/* otherwise continue to next */
}
yyerror("Too many symbols");
exit(1); /* cannot continue */
} /* end of symlook */