0

I am constantly getting yyerror : syntax error no matter the input file and yyparse() is always returning 1. I am having trouble finding what the issue is. I believe my BNF and input should be correct but I just keep getting this error and its driving me crazy.

%{
#include <stdio.h>
int yylex(void);
int yyerror(char *);
%}
%token LEFTPAREN RIGHTPAREN ASSIGNMENT SEMICOLON IF THEN ELSE BEGIN END WHILE
%token DO PROGRAM VAR AS INT BOOL WRITEINT READINT NUMBER LITERAL
%token OP2 OP3 OP4 IDENTIFIER
%start program
%%
program : PROGRAM declerations BEGIN statementSequence END
;
declerations : VAR IDENTIFIER AS type SEMICOLON declerations |
;
type : INT | BOOL
;
statementSequence : statement SEMICOLON statementSequence |
;
statement : assignment | ifStatement | whileStatement | writeInt
;
assignment : IDENTIFIER ASSIGNMENT expression | IDENTIFIER ASSIGNMENT READINT
;
ifStatement : IF expression THEN statementSequence elseClause END
;
elseClause : ELSE statementSequence |
;
whileStatement : WHILE expression DO statementSequence END
;
writeInt : WRITEINT expression
;
expression : simpleExpression | simpleExpression OP4 simpleExpression
;
simpleExpression : term OP3 term | term
;
term : factor OP2 factor | factor
;
factor : IDENTIFIER | NUMBER | LITERAL | LEFTPAREN expression RIGHTPAREN
;
%%
int yyerror(char *s) {
  printf("yyerror : %s\n",s);
}
int main(){
  printf("%d\n", yyparse());
  return 0;
}
  • 2
    Please add sample input to the question – stark Apr 14 '22 at 21:03
  • 1
    The issue could be with the lexer, which you have not presented. If it is tokenizing differently than you expect (or if it is failing) then your grammar probably doesn't matter. – John Bollinger Apr 14 '22 at 21:26
  • 1
    The first thing you should do (after you read the ["Debugging Your Parser"](https://www.gnu.org/software/bison/manual/bison.html#Debugging) chapter in the bison manual) is [enable bison traces](https://stackoverflow.com/a/50821204/1566221) and see what is actually going on. – rici Apr 15 '22 at 03:05
  • 1
    Why don't you print some useful information in `yyerror()`? Like an input line number and a rule that had produced the error. Without seeing actual input and error, SO users can not tell you what the problem is. – user14063792468 Apr 15 '22 at 03:31

0 Answers0