0
votes

I am using Bison and flex to create the grammar of my C Compiler. After running Bison Flex and Gcc. Everyting Compiles fine. When running the parser. The First line of input work correctly; BUT in the second line of the input it throws this bison (yyerror)error message:

parse error, expecting `$'

I dont have any token that has a dollar sign in it ($).

I chose not to show y code for simplicity sake; but if you need to see it I could edit it. I just want to know the causes of this error message.

edit: added Bison Code


%{
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define YYERROR_VERBOSE

int yylex();
extern FILE *yyin;
int yyerror(char *s);
%}

%token T_abstract
%token T_alias
%token T_align
%token T_asm
%token T_assert
%token T_auto
%token T_body
%token T_bool
%token T_break
%token T_byte
%token T_case
%token T_cast
%token T_catch
%token T_cdouble
%token T_cent
%token T_cfloat  
%token T_char
%token T_class
%token T_const
%token T_continue
%token T_creal
%token T_dchar
%token T_debug
%token T_default
%token T_delegate
%token T_deprecated
%token T_do
%token T_double
%token T_eles
%token T_enum
%token T_export
%token T_extern
%token T_false
%token T_final
%token T_finally
%token T_float
%token T_for
%token T_foreach
%token T_foreach_reverse
%token T_function
%token T_goto
%token T_idouble
%token T_if
%token T_ifloat
%token T_immutable
%token T_import
%token T_in
%token T_inout
%token T_int
%token T_interface
%token T_invariant
%token T_ireal
%token T_is
%token T_lazy
%token T_long
%token T_mixin
%token T_module
%token T_new
%token T_nothrow
%token T_null
%token T_out
%token T_override
%token T_package
%token T_pragma
%token T_private
%token T_protected
%token T_public
%token T_pure
%token T_real
%token T_ref
%token T_return
%token T_scope
%token T_shared
%token T_short
%token T_static
%token T_struc
%token T_super
%token T_switch
%token T_synchronized
%token T_template
%token T_this
%token T_throw
%token T_true
%token T_try
%token T_typeid
%token T_typeof
%token T_ubyte
%token T_ucent
%token T_uint
%token T_ulong
%token T_union
%token T_unittest
%token T_ushort
%token T_version
%token T_void
%token T_wchar
%token T_while
%token T_with
%token NUM

%token T_neq 
%token T_bslash
%token T_land
%token T_andeq
%token T_and
%token T_lor
%token T_oreq
%token T_or
%token T_dec
%token T_deceq
%token T_minus
%token T_inc
%token T_inceq
%token T_plus
%token T_GEQ
%token T_GR
%token T_NOTEQ
%token T_OPAREN
%token T_CPAREN
%token T_OCOR   
%token T_CCOR
%token T_SEMICOLON
%token T_EQUAL
%token T_multiply
%token T_COMA
%token T_LE
%token T_isEqual
%token DEC
%token ID
%token T_LT


%right T_EQUAL
%left T_and T_or
%left T_LE T_GEQ T_NOTEQ T_isEqual T_GR T_LT

%start prog

%%
prog:   Function
    |Declaration
    ;

Declaration:    Type Assignment T_SEMICOLON 
        | Assignment T_SEMICOLON    
        | FunctionCall T_SEMICOLON   
        ;

Assignment:     ID T_EQUAL Assignment
        | ID T_EQUAL FunctionCall
        | ID T_plus Assignment
        | ID T_minus Assignment
        | ID T_multiply Assignment
        | ID T_bslash Assignment    
        | NUM T_plus Assignment
        | NUM T_minus Assignment
        | NUM T_multiply Assignment
        | NUM T_bslash Assignment
        | T_OPAREN Assignment T_CPAREN
        | T_minus T_OPAREN Assignment T_CPAREN
        | T_minus NUM
        | T_minus ID
        | NUM
        | DEC
        | ID
        ;

FunctionCall : ID T_OPAREN T_CPAREN
    | ID T_OPAREN Assignment T_CPAREN
    ;

Type:   T_int 
    | T_float
    | T_char
    | T_double
    ;

Function: Type ID T_OPAREN ArgListOpt T_CPAREN CompoundStmt 
    ;

ArgListOpt: ArgList
    |
    ;

ArgList:  ArgList T_COMA Arg
    | Arg
    ;

Arg:    Type ID
    ;

CompoundStmt:   T_OCOR StmtList T_CCOR
    ;

StmtList:   StmtList Stmt
        |
        ;

Stmt:   WhileStmt
    | Declaration
    | ForStmt
    | IfStmt
    | T_SEMICOLON
    ;

WhileStmt: T_while T_OPAREN Expr T_CPAREN Stmt  
    | T_while T_OPAREN Expr T_CPAREN CompoundStmt 
    ;

ForStmt:T_for T_OPAREN Expr T_SEMICOLON Expr T_SEMICOLON Expr T_CPAREN Stmt 
       | T_for T_OPAREN Expr T_SEMICOLON Expr T_SEMICOLON Expr T_CPAREN CompoundStmt 
       | T_for T_OPAREN Expr T_CPAREN Stmt 
       | T_for T_OPAREN Expr T_CPAREN CompoundStmt 
    ;

IfStmt : T_if T_OPAREN Expr T_CPAREN Stmt ;

Expr:   
    | ID T_LE ID 
    | ID T_GEQ ID
    | ID T_NOTEQ ID
    | ID T_isEqual ID
    | ID T_GR ID
    | ID T_LT ID
    ;
%%


#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);
    fclose(yyin);
    return 0;
}
int yyerror(char *s) {
    printf("%s\n",s);
}    




1
Without seeing your code, it is really impossible to give any useful advice. But that doesn't look like a bison error message to me. Are you quoting it exactly?rici
yes this is a error from the yyerror() function.ale
I added my code on the questionale
Are you sure that's the exact error message you get? Bison error messages usually start with "syntax error", not "parse error" and, when the verbose option is set, contain both the expected and the actual token - not just the expected token as in your message. All that makes me think that you've retyped the message from memory instead of copy and pasting it, which means you might have made other mistakes again. Like maybe the real error message says "$end" instead of `$'.sepp2k
I don't recall bison ever producing parse error. The versions I'm familiar with produce error messages starting syntax error. Perhaps the substitution is the result of a translation.rici

1 Answers

1
votes

The error is triggered because your grammar insists that a program be exactly one Function or Declaration. If the input contains two things, then the parser will parse the first one and then complain that it is expecting end of input.

I guess your version of bison represents an EOF marker as $. (Or you're not quoting the error message precisely.)