Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to run this yacc calculator program in linux using putty, but when I

ID: 3678460 • Letter: I

Question

I'm trying to run this yacc calculator program in linux using putty, but when I compile I get the error "undefined reference to yylex", any solutions.

Code Here:

%{
#include <stdio.h>
int regs[26];
int base;
%}
%start list
%token DIGIT LETTER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS /*supplies precedence for unary minus */
%% /* beginning of rules section */
list: /*empty */
|
list stat ' '
|
list error ' '
{
yyerrok;
}
;
stat: expr
{
printf("%d ",$1);
}
|
LETTER '=' expr
{
regs[$1] = $3;
}
;
expr: '(' expr ')'
{
$$ = $2;
}
|
expr '*' expr
{
$$ = $1 * $3;
}
|
expr '/' expr
{
$$ = $1 / $3;
}
|
expr '%' expr
{
$$ = $1 % $3;
}
|
expr '+' expr
{
$$ = $1 + $3;
}
|
expr '-' expr
{
$$ = $1 - $3;
}
|
expr '&' expr
{
$$ = $1 & $3;
}
|
expr '^' expr
{
$$ = $1 ^ $3;
}
|
number
;
number: DIGIT
{
$$ = $1;
base = ($1==0) ? 8 : 10;
} |
number DIGIT
{
$$ = base * $1 + $2;
}
;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s ",s);
}
yywrap()
{
return(1);
}

Explanation / Answer

The below program is compiled and is 100% working :

calc.yacc

calc.lex

If you still have any issuedoubt , please comment. Happy to help !!!