help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Need help on simple lex and yacc programs


From: Jean-Louis Grau
Subject: Need help on simple lex and yacc programs
Date: Wed, 5 May 2004 14:07:13 +0200

Hello,

 

Lex and yacc are new tools for me. I did a small program which does not work properly.

Could you tell me what wrong with my code?

 

Here is my lex file:

 

#include <stdio.h>

#include <malloc.h>

 

// The Parser token definition

#include "pkgYacc.h"

 

 

%}

 

%%

 

^#.*\n               {printf("\nLEXER:Line: %d - This is a COMMENT line, ignore it [%s] ",yylineno,yytext);fflush(stdout);;}

 

^[\t ]*\n             {printf("\nLEXER:Line: %d - This is a BLANK line, ignore it [%s] ",yylineno,yytext);fflush(stdout);;}

 

"="                   {

                                    printf("\nLEXER:Line: %d - This is a EQUAL OPERATOR [%s] ",yylineno,yytext);fflush(stdout);;

                                    return (EQUAL);

                        }

 

\"[^"]*["]            {

                                    printf("\nLEXER:Line: %d - This is a STRING  [%s]  ",yylineno,yytext);fflush(stdout);;

                                   return (STRING);

                        }

 

 

 

-?[0-9]*\.[0-9]*  {

                                    printf("\nLEXER:Line: %d - This is a FLOAT  [%s] ",yylineno,yytext);

                                    return (FLOAT);

                        }

 

-?[0-9]*            {

                                    printf("\nLEXER:Line: %d - This is a INTEGER  [%s] ",yylineno,yytext);fflush(stdout);;

                                   return (INTEGER);

                        }

 

 

[_a-zA-Z0-9]*    {

                                    printf("\nLEXER:Line: %d - This is a DATANAME  [%s] ",yylineno,yytext);fflush(stdout);;

                                   return (DATANAME);

                        }

"\n"     ;

 

 

 

%%

int yywrap ()

{

            return 1;

}

 

 

Here is my yacc file:

%{

/*

 *

 */

#include <stdio.h>

#include <malloc.h>

 

 

%}

 

%start File

 

%token EQUAL STRING DATANAME FLOAT INTEGER

 

 

 

%%

File: integerAttr | stringAttr | floatAttr ;

 

integerAttr: DATANAME EQUAL INTEGER

{

            printf("\nPARSER: Found an attribute of type INTEGER "); fflush(stdout);

};

 

stringAttr : DATANAME EQUAL STRING

{

            printf("\nPARSER: Found an attribute of type STRING "); fflush(stdout);

};

 

floatAttr  : DATANAME EQUAL FLOAT

{

            printf("\nPARSER: Found an attribute of type FLOAT "); fflush(stdout);

};

 

 

 

 

%%

extern FILE *yyin;

 

main(int argc,char** argv)

{

            if (argc > 1)

            {

                        FILE *file;

                        file = fopen(argv[1], "r");

                        if (!file)

                        {

                                   fprintf(stderr,"could not open %s\n",argv[1]);

                                   return 1;

                        }

                        yyin = file;

                        while(!feof(yyin))

                        {

                                   yyparse();

                        }

            }

}

 

yyerror(char* s)

{

    fprintf(stderr, "%s\n", s);

            return 0;

}

 

 

 

Here is the input file I want to parse:

 

data1 = 1

data2 = "2"

data3 = 3.3

data4 = 4

 

 

Here is my output

 

 

LEXER:Line: 1 - This is a DATANAME  [data1] 

LEXER:Line: 1 - This is a EQUAL OPERATOR [=] 

LEXER:Line: 1 - This is a INTEGER  [1]

PARSER: Found an attribute of type INTEGER

LEXER:Line: 2 - This is a DATANAME  [data2]

 

 

I have used gnu bison and flex for windows to generate the C code from the lex and yacc file.

After compilation I use the command line :

Test.exe test.input

To run the parser.

 

My problem is that the parser recognise the first line as an integer attribute (PARSER: Found an attribute of type INTEGER) and just stop after this (no crash and no stuck just stop parsing…)

I really do not understand what is wrong on my parser. The lexer is working fine if it used alone.

 

 

Thanks for your help.

 

Jean-Louis

 

 

 

 

 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]