help-bison
[Top][All Lists]
Advanced

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

Help this newbie parse some strings...


From: Michael Morrison
Subject: Help this newbie parse some strings...
Date: 11 Feb 2003 18:59:18 -0800

Hi,

I've been trying to get my head around this whole flex/bison thing.  I
have a couple of books on the subject, and I can type in their examples
and everything is OK.  

However, I run into trouble when I try to come up with my own examples. 
This fact of course tells me that I'm not understanding something.  

Anyway,
I get a parse error and I'm having a hard time figuring out why.

I'm sure some kind soul will point it out - at which time I can slap my
forehead....


Thanks


Bison File--------
%{
#include <stdio.h>

#define YYDEBUG 1
%}


%debug

%token
    STRING_ONE
    STRING_TWO
    VAL_TEXT_LIST
    VAL_HEX_DIGIT
    TERMINATOR

%%


statement: STRING_ONE '=' VAL_TEXT_LIST TERMINATOR { printf("YACC:  
STRING_ONE = VAL_TEXT_LIST\n"); }
         | STRING_TWO '=' VAL_HEX_DIGIT TERMINATOR { printf("YACC:
STRING_TWO = VAL_HEX_DIGIT\n"); }
;

%%

extern int buffer_init(void);


int main()
{
    yydebug = 1;
    buffer_init();
    yyparse();
}


yyerror(char *s)
{
    printf("Error %s\n", s );
}





Here's my lex file..


%{

#include "parse.tab.h"

#undef YY_INPUT
#define YY_INPUT(b, r, ms) (r = my_input(b, ms))

%}



%%


[\t]+     /* ignore whitespace */ ;
[ ]                    { return TERMINATOR; }
string1                { return STRING_ONE; }
string2                { return STRING_TWO; }
([a-zA-Z]+,[a-zA-Z]+)+ { return VAL_TEXT_LIST; }
(0x|0X)([0-9a-fA-F])+  { return VAL_HEX_DIGIT; }
.                { return yytext[0]; }

%%


char myinput[] = { "string1=One,Two string2=0x565 " };

char *myinputptr;
int myinputlim;

int min ( int a, int b )
{
    if ( a < b )
        return a;
    else
        return b;
}

int my_input( char *buf, int maxsize )
{
    int n = min ( maxsize, myinputlim - (int)myinputptr);

    if ( n > 0 )
    {
        memcpy(buf, myinputptr, n );
        myinputptr += n;
    }

    return n;
}



int buffer_init(void)
{
    myinputptr = myinput;
    myinputlim = (int)myinputptr + sizeof (myinput);
}





And finally, the dreaded result.

Starting parse
Entering state 0
Reading a token: Next token is 257 (STRING_ONE)
Shifting token 257 (STRING_ONE), Entering state 1
Reading a token: Next token is 61 ('=')
Shifting token 61 ('='), Entering state 3
Reading a token: Next token is 262 (VAL_TEXT_LIST)
Shifting token 262 (VAL_TEXT_LIST), Entering state 5
Reading a token: Next token is 269 (TERMINATOR)
Shifting token 269 (TERMINATOR), Entering state 7
Reducing via rule 1 (line 28), STRING_ONE '=' VAL_TEXT_LIST TERMINATOR 
-> statement
YACC: STRING_ONE = VAL_TEXT_LIST
state stack now 0
Entering state 9
Reading a token: Next token is 258 (STRING_TWO)
Error parse error
Error: state stack now 0





reply via email to

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