📄 sym_tab.y
字号:
%{#include<stdio.h>struct node { char symbol; int value; struct node *link; }*first;%}%start S%union { char name; int value; }%token <name> LETTER%token <value> DIGIT%token ENDLINE%type <value> exp%type <value> stmt%left '+' '-' '*' '/' %left '='%% S : stmt {} ; stmt : LETTER '=' exp ENDLINE {assign($1,$3);} stmt {} | exp ENDLINE { printf("The Value Of The Expression Is :: %d\n",$1); } ; exp : LETTER { $$=lookup($1); } | DIGIT { $$=$1; } | exp '+' exp { $$ = $1 + $3;} | exp '-' exp { $$ = $1 - $3;} | exp '*' exp { $$ = $1 * $3; } | exp '/' exp { $$ = $1 / $3;} | '(' exp ')' { $$=$2; } ;%%main(){ return (yyparse());}yyerror(s)char *s;{ fprintf(stderr,"\n%s",s);}yywrap(){ return 1;}void assign(char c,int v){ struct node *t = (struct node*) malloc (sizeof(struct node) ); if(first == NULL) { first=(struct node*) malloc(sizeof(struct node)); first->symbol=c; first->value=v; first->link=NULL; } else { t->symbol=c; t->value=v; t->link=first; first=t; } printf("Symbol %c with value %d inserted\n",first->symbol,first->value); }int lookup(char c){ struct node *t = first; while(first != NULL) { if(t->symbol == c) { printf("Symbol %c is in symbol table & its value is %d\n",t->symbol,t->value); return t->value; } else t=t->link; } return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -