Flex/bison keeps on giving me syntax error

Chadi To

i'm trying to make a mini compiler using lex and yacc however each time i try to test my parser i keep getting a syntax error.

Here is my lex file :

%option noyywrap
%{
    #include "y.tab.h"
    int line = 1;
    int spaceCount = 1;
    int sourceLineCount = 1;
%}

IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
WHITESPACE [ ]
DIGIT [0-9]
TAB \t
NEXTLINE \n

%%
int return INT;
float   return FLOAT;
double  return DOUBLE;
char    return CHAR;
void    return VOID;

static  return STATIC;
const   return CONST;

struct  return STRUCT;
union   return UNION;
enum    return ENUM;

if  return IF;
else    return ELSE;
switch  return SWITCH;
case    return CASE;

break   return BREAK;
default return DEFAULT;

^"#include ".+ ;
typedef return TYPEDEF;
extern  return EXTERN;

for return FOR;
while   return WHILE;
do  return DO;


"<" return LT;
">" return GT;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;


"." return DOT;
"," return COMMA;

printf return PRINTF;

return  return RETURN;

{DIGIT}+       return NUM;



{NEXTLINE} {
    line++;
}
{WHITESPACE} {
    spaceCount++;
}
.       return yytext[0];
%%

And my yacc/bison file :

%{
    #include<stdio.h>
    extern FILE *fp;
%}

%token INT FLOAT CHAR DOUBLE VOID
%token FOR WHILE DO
%token IF ELSE PRINTF
%token STRUCT ENUM UNION
%token SWITCH CASE BREAK DEFAULT
%token NUM ID
%token INCLUDE
%token DOT COMMA
%token STATIC CONST
%token TYPEDEF EXTERN
%token RETURN
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE LT GT




%% 
start:  Function 
    | Declaration
    ;

/* DECCLARATION */
Declaration: Type Assignment ';' 
    | Assignment ';'    
    | FunctionCall ';'  
    | ArrayUsage ';'    
    | Type ArrayUsage ';'   
    | StructStmt ';'    
    | error 
    ;

/* ASSIGNMENT */
Assignment: ID '=' Assignment
    | ID '=' FunctionCall
    | ID '=' ArrayUsage
    | ArrayUsage '=' Assignment
    | ID ',' Assignment
    | NUM ',' Assignment
    | ID '+' Assignment
    | ID '-' Assignment
    | ID '*' Assignment
    | ID '/' Assignment 
    | NUM '+' Assignment
    | NUM '-' Assignment
    | NUM '*' Assignment
    | NUM '/' Assignment
    | '\'' Assignment '\''  
    | '(' Assignment ')'
    | '-' '(' Assignment ')'
    | '-' NUM
    | '-' ID
    |   NUM
    |   ID
    ;

/* Function Call*/
FunctionCall : ID'('')'
    | ID'('Assignment')'
    ;
/* Function block */
Function: Type ID '(' ArgListOpt ')' CompoundStmt 
    ;
ArgListOpt: ArgList
    |
    ;
ArgList:  ArgList ',' Arg
    | Arg
    ;
Arg:    Type ID
    ;

/* Array Usage */
ArrayUsage : ID'['Assignment']'
    ;

CompoundStmt:   '{' StmtList '}'
    ;
StmtList:   StmtList Stmt
    |
    ;
Stmt:   WhileStmt
    | Declaration
    | ForStmt
    | IfStmt
    | PrintFunc
    | SwitchStmt
    | ';'
    ;

/* Type Identifier block */
Type:   INT 
    | FLOAT
    | CHAR
    | DOUBLE
    | VOID 
    ;

/* Loop Blocks */ 
WhileStmt: WHILE '(' Expr ')' Stmt  
    | WHILE '(' Expr ')' CompoundStmt 
    ;

/* For Block */
ForStmt: FOR '(' Expr ';' Expr ';' Expr ')' Stmt 
       | FOR '(' Expr ';' Expr ';' Expr ')' CompoundStmt 
       | FOR '(' Expr ')' Stmt 
       | FOR '(' Expr ')' CompoundStmt 
    ;

/* IfStmt Block */
IfStmt : IF '(' Expr ')' 
        Stmt 
    ;
/* SwitchStmnt */
SwitchStmt  : SWITCH '(' ID ')'
        CaseStmt
          | SWITCH '(' ID ')'
        CompoundStmt
        CaseStmt
        DefaultStmt
        ;
/* CaseStmt */
CaseStmt    : CASE NUM ':'
        Stmt
        BREAK ';'
        ;
/* DefaultStmt */
DefaultStmt  : DEFAULT ':'
        Stmt
        BREAK ';'
/* Struct Statement */
StructStmt : STRUCT ID '{' Type Assignment '}'  
    ;

/* Print Function */
PrintFunc : PRINTF '(' Expr ')' ';'
    ;
/*Expression Block*/
Expr:   
    | Expr LE Expr 
    | Expr GE Expr
    | Expr NE Expr
    | Expr EQ Expr
    | Expr GT Expr
    | Expr LT Expr
    | Assignment
    | ArrayUsage
    ;


%%
#include"lex.yy.c"
#include<ctype.h>
int count=0;

int main(int argc, char *argv[])
{
   yyin = fopen(argv[1], "r");

   if(!yyparse())
        printf("\nParsing complete\n");
    else
        printf("\nParsing failed\n");

    fclose(yyin);
    return 0;
}

yyerror(char *s) {
    printf("\n %d: %s avant %s\n\n",line,s, yytext );
} 

I tried to test it with multiple test files but it always seems to give me a syntax error, here is a sample of what i tried.

int main()
{
   int a;
}

in this case i got an error before "m".

rici

Nowhere in your lexical scanner do you

return ID;

which means that productions in the parser which require an ID token are never going to match anything. I suppose you just forgot to add the line which recognises identifiers. Note that main should be an ID, which is why your parse fails at that point.

There are other problems with your grammar, including the fact that it only recognizes a single function declaration (Function) or other statement (Declaration). Along with any other changes you make, you should try to give your non-terminals more meaningful names, so that other people can understand your grammar. (This might help you, too.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mysql trigger statement keeps giving syntax error

PyInstaller giving me a syntax error

Google map keeps on giving me the same error

Why is this DELETE query giving me a syntax error?

Why is `;;` giving me a syntax error in utop?

Why is MySQL giving me a syntax error?

What's wrong with my javascript? it keeps giving me an error in dreamweaver

Populating table with PHP keeps giving me common error

JAXB maven project in eclipse keeps giving me localized error messages

Java keeps giving me an error even though there is not red line thingy

can't install openjdk keeps giving me error

Deno keeps giving me the same type of error on different modules

Unity keeps giving me this error code but it worked before?

SqlCeCommand keeps giving me an exception

I am trying to create an embed but it keeps giving me an invalid syntax with the variable name

Why is this giving me a syntax error on PHP 7.2 but not PHP 7.4.5?

what is wrong in this query. It is giving me syntax error on or near TRUNCATE

Why is this Ruby on Rails program giving me a syntax error?

Why are my End values giving me a syntax error?

block iterator, embedded ruby giving me a syntax error

ACCESS keeping giving me a syntax error, when trying to create a view

Firebase deploy keeps giving me an Error: Parse Error in remoteconfig.template.json

Windows keeps giving me this Error 0x80004005: Unspecified Error during deleting a folder

Laravel SQL Query Keeps Giving SQLSTATE[42000]: Syntax error or access violation: 1064

playbook giving syntax error

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) keeps giving me not applicable error

I have an array of doubles that I need to get a median but it keeps giving me an int conversion error

passing a variable from one class to another in Java keeps giving me a null error

Boxsdk JWTAuth keeps giving me this error: 'NoneType' object has no attribute 'encode'