I am new to compiler and making my own compiler for subset of c language.
flex code:
%{
#include "parser.tab.h"
%}
%option warn noyywrap noinput nounput yylineno
%%
"else" {return ELSE;}
"if" {return IF;}
"int" {return INT;}
"return" {return RETURN;}
"void" {return VOID;}
"while" {return WHILE;}
"+" {return ADD;}
"-" {return SUB;}
"*" {return MUL;}
"/" {return DIV;}
"<" {return LT;}
"<=" {return LTE;}
">" {return GT;}
">=" {return GTE;}
"==" {return EQ;}
"!=" {return NEQ;}
"=" {return ASSIGN;}
";" {return SEMI;}
"," {return COMMA;}
"(" {return ROUND_OPEN;}
")" {return ROUND_CLOSE;}
"[" {return SQUARE_OPEN;}
"]" {return SQUARE_CLOSE;}
"{" {return CURLY_OPEN;}
"}" {return CURLY_CLOSE;}
[a-zA-z]+ {return ID;}
[0-9]+ {yylval.intVal=atoi(yytext);return NUM;}
"//".* /* discard comments */
[ \t\n\b]+ { if (yytext[0] == '\n') ++yylineno; } /* discard whitespace */
%%
Bison code:
%{
extern int yylex();
void yyerror(char* err, ...);
%}
%union{
int intVal;
};
%token ELSE, IF, RETURN, VOID, INT
%token WHILE
%token ADD, SUB, MUL, DIV
%token LT, LTE, GT, GTE, EQ, NEQ
%token ASSIGN
%token SEMI, COMMA
%token ROUND_OPEN, ROUND_CLOSE, SQUARE_OPEN, SQUARE_CLOSE, CURLY_OPEN, CURLY_CLOSE
%token ID, NUM
%type <int> S
%type <int>expr
%type <int> term
%type <int> factor
%%
S: expr'\n' {$<intVal>$=$<intVal>1;printf("%d",$<intVal>$);}
expr: expr ADD term {$<intVal>$=$<intVal>1+$<intVal>2;};
expr: term {$<intVal>$=$<intVal>1};
term: term MUL factor {$<intVal>$=$<intVal>1*$<intVal>2};
term: factor {$<intVal>$=$<intVal>1};
factor:NUM {$<intVal>$=yylval.intVal};
%%
void yyerror(char* err, ...)
{
fprintf(stderr, "%s\n", err);
}
there is a main file for calling yyparse. I am just programming an arithmetic calculator first but it is giving me parse error please help.