flex/bison(lex/yacc) help needed.

flex/bison(lex/yacc) help needed.

Post by M Sweg » Thu, 29 May 1997 04:00:00



Hi,
        I'm not sure if this is the group to post a question about
flex/bison(lex/yacc) problems. I'm having trouble parsing multiple
line in a file with a grammar I wrote.
Note: flex is 2.5.4 and bison 1.25 this has been tried on linux and
solaris 2.5.2 and also using lex/yacc.
The lex/grammar must parse multiple lines of the sort that follows:

bx*123*ab*a 1b*;
bx**3*cd2;
bx*;

Now the ";" isn't important and will eventually be removed but the
lex file is:

%{
#include        "y.tab.h"     /* or the appropriate file bison generates */
#define token(x)        x
%}
%%

^bx     { return token(ID); }
\*      { return token(SEPARATOR); }
[0-9]+  { return token(NUMBER); }
[A-Z ]+ { return token(ALPHA); }
[A-Za-z0-9 ]+ { return token{ALPHNUM); }
;               { return token(TERMINATOR); }
\n      /* ignore */
.       { return token(ERROR); }

The bison/yacc file is:

%token ID
%token SEPARATOR
%token NUMBER
%token ALPHA
%token ALPHNUM
%token TERMINATOR
%token ERROR

%%

strt
        : line TERMINAL
        ;

line
        : ID SEPARATOR fieldexpression
        ;

fieldexpression
        : fields
        | fieldexpression SEPARATOR fields
        ;

fields
        : /* null */
        | NUMERIC
        | ALPHA
        | ALPHNUM
        ;

Of course the test.c containing the main() just defines a few
externs such as yyparse=1, yylen, yytext[], and yylineno and call yyparse
with a function for errors defined as yyerror().

main()
{
        yyparse();

Quote:}

yyerror(s)
        char *s;
{
        fprintf(stdout, " parse error at lineno=%d for token=%s\n",
                yylineno, yytext);

Quote:}

Also not the lex code has been tested and will return or not return the
proper tokens. It is only when the lex and parser are trying to parse
more than two lines.

--
        Mike,

 
 
 

flex/bison(lex/yacc) help needed.

Post by M Sweg » Thu, 29 May 1997 04:00:00


Hi,
        I'm not sure if this is the group to post a question about
flex/bison(lex/yacc) problems. I'm having trouble parsing multiple
line in a file with a grammar I wrote.
Note: flex is 2.5.4 and bison 1.25 this has been tried on linux and
solaris 2.5.2 and also using lex/yacc.
The lex/grammar must parse multiple lines of the sort that follows:

bx*123*ab*a 1b*;
bx**3*cd2;
bx*;

Now the ";" isn't important and will eventually be removed but the
lex file is:

%{
#include        "y.tab.h"     /* or the appropriate file bison generates */
#define token(x)        x
%}
%%

^bx     { return token(ID); }
\*      { return token(SEPARATOR); }
[0-9]+  { return token(NUMBER); }
[A-Z ]+ { return token(ALPHA); }
[A-Za-z0-9 ]+ { return token{ALPHNUM); }
;               { return token(TERMINATOR); }
\n      /* ignore */
.       { return token(ERROR); }

The bison/yacc file is:

%token ID
%token SEPARATOR
%token NUMBER
%token ALPHA
%token ALPHNUM
%token TERMINATOR
%token ERROR

%%

strt
        : line TERMINAL
        ;

line
        : ID SEPARATOR fieldexpression
        ;

fieldexpression
        : fields
        | fieldexpression SEPARATOR fields
        ;

fields
        : /* null */
        | NUMERIC
        | ALPHA
        | ALPHNUM
        ;

Of course the test.c containing the main() just defines a few
externs such as yyparse=1, yylen, yytext[], and yylineno and call yyparse
with a function for errors defined as yyerror().

main()
{
        yyparse();

Quote:}

yyerror(s)
        char *s;
{
        fprintf(stdout, " parse error at lineno=%d for token=%s\n",
                yylineno, yytext);

Quote:}

Also not the lex code has been tested and will return or not return the
proper tokens. It is only when the lex and parser are trying to parse
more than two lines.

--
        Mike,


 
 
 

flex/bison(lex/yacc) help needed.

Post by Ulrich Teiche » Sun, 01 Jun 1997 04:00:00



Quote:>Hi,
>    I'm not sure if this is the group to post a question about
>flex/bison(lex/yacc) problems.

I think comp.compilers would be better, but well....

 I'm having trouble parsing multiple

Quote:>line in a file with a grammar I wrote.
>Note: flex is 2.5.4 and bison 1.25 this has been tried on linux and
>solaris 2.5.2 and also using lex/yacc.

[del del]
yacc file:

Quote:>%%
>strt
>    : line TERMINAL
>    ;

You should use a recursive rule here to allow multiple line to be parsed.
Like :

strt    : oneline
        | oneline strt

oneline : line TERMINAL

Sorry for the delay, but I was offline for two days (on business travel
to and from Berlin).

Uli
--

Stormweg 24                | listening to: Burning Desire (Funeral Oration)
24539 Neumuenster, Germany | Back With You/What Am I Supposed To Do (Buzzcocks)

 
 
 

1. Object-oriented lex(flex) and yacc(bison) for c++?

I'm interested in using lex and yacc to implement a parser for one of
my projects, and would really like to know if there's something out
there that facilitates something a little easier to incorperate it into
my c++ code.  I'd like to have a Parser class, which uses the two
utilities.  What's out there that might help, or be of interest?

Any suggestions or pointers would be greatly appreciated...

[I prefer email responses, since I don't have time to read news often.]

-Christian


2. No floppy device file running Fedora Core 3

3. lookinf for Flex.Lex and Bison/Yacc ...

4. Cardinal VGA765 under X

5. flex/lex/bison/yacc parser for C ?

6. anybody know where i can get a unix Micro Focus Cobol to gnu C translator ?

7. Yacc/Lex vs Bison/Flex

8. un.h missing sun_len

9. lex/flex and yacc/bison

10. lexer/parser generators other than lex/yacc/flex/bison

11. Lex (Flex) and Yacc (or Bison) questions.

12. flex,lex,yacc & bison doc???

13. Lex/Yacc Substitute (not bison/flex)