I am trying to build a very simple language using lex and yacc It can have only one int variable through assignment like this
a = 1
It can print variable like this
print a
It has to print the value only if variable name matches else it has to print error message
My lex file is
%{
#include "y.tab.h"
#include <stdlib.h>
void yyerror(char *);
%}
letter [A-z]
digit [0-9]
%%
"print" {return PRINT;}
{letter}+ { yylval.id = yytext;return IDENTIFIER;}
{digit}+ { yylval.num = atoi(yytext);return NUMBER; }
[=\n] return *yytext;
[ \t] ; /* skip whitespace */
. yyerror("invalid character");
%%
int yywrap(void) {
return 1;
}
And my yacc file is
%{
#include <stdio.h>
#include <string.h>
int yylex(void);
void yyerror(char *);
int value;
char *key;
void print_var(char *s);
%}
%union {char *id;int num;}
%start exp
%token <id> IDENTIFIER
%token <num> NUMBER
%token PRINT
%%
exp: IDENTIFIER '=' NUMBER '\n' {key = $1;value = $3;}
|PRINT IDENTIFIER '\n' {print_var($2);}
|exp IDENTIFIER '=' NUMBER '\n' {key = $2;value = $4;}
|exp PRINT IDENTIFIER '\n' {print_var($3);}
;
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
void print_var(char *s){
if(strcmp(s,key) == 0){
printf("%s:%d\n",key,value);
}else{
printf("%s not found\n",s);
}
}
int main(void) {
yyparse();
return 0;
}
But when I type something like this
a = 1
print a
I get the following error a not found