help-bison
[Top][All Lists]
Advanced

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

Help with a simple grammar I can't get to work, please


From: Dustin Robert Kick
Subject: Help with a simple grammar I can't get to work, please
Date: Tue, 26 Sep 2006 08:41:17 -0500

I'm trying to make what I thought would be the simplest grammar I could specify work before moving on to more productive programs, sort of a hello bison. All it does is print some output with the input token passed to the grammar, but it works on every other line being input. Here is some sample output with debugging enabled:

__lex_yacc test copy__08:29:33__BRUNOISE-TIGRE__./fixtest
Starting parse
Entering state 0
Reading a token: hall
matched string:hall
Next token is 257 (STRING)
Shifting token 257 (STRING), Entering state 1
Reading a token: matched \n
Next token is 258 (END)
Shifting token 258 (END), Entering state 2
Reducing via rule 1 (line 22), STRING END  -> yowza
YOWZA hall
: 5
state stack now 0
Entering state 3
Reading a token: hello
matched string:hello
Next token is 257 (STRING)
parse error:hello
Error: state stack now 0
Starting parse
Entering state 0
Reading a token: matched \n
Next token is 258 (END)
parse error:hello

Starting parse
Entering state 0
Reading a token: test
matched string:test
Next token is 257 (STRING)
Shifting token 257 (STRING), Entering state 1
Reading a token: matched \n
Next token is 258 (END)
Shifting token 258 (END), Entering state 2
Reducing via rule 1 (line 22), STRING END  -> yowza
YOWZA test
: 5
state stack now 0
Entering state 3
Reading a token: ^C

the parser continues with this pattern of parsing the way I expect on the odd parses, and outputting a syntax error on the evens. I can't figure out why this won't work, can someone explain? Here are the source files and .output file.

<lex_flex("rules.l")>
%{
#include "grammar.tab.h"
#include <stdlib.h>

%}

%%

.* {yylval.string = yytext;
        printf("matched string:%s\n", yytext);
        return STRING;}

\n {printf("matched \\n\n");
        return END;}

%%

int yywrap (void)
{
return 1;
}
</lex_flex("rules.l">

<bison("grammar.y")>

#include <libc.h>
#include <math.h>
%}


%union
{
        int label;
        double dval;
        char * string;
}

%token <string> STRING

%% /* beginning of rules section */

yowza : STRING endline {printf("YOWZA %s : %d\n", $1, strlen($1));}
;

endline : '\n' {printf("AND IN THE END!\n");}
;

%% /* beginning of functions section */
void yyerror(char *s)
{
                printf ("%s:%s\n", s, yylval.string);
                fflush(stdout);
}

int main(int argc, char **argv)
{
        while (!feof(stdin))
        {
                yyparse();
        }
        exit(0);
}
</bison("grammar.y">

<"grammar.output">

Grammar
rule 1    yowza -> STRING END

Terminals, with rules where they appear

$ (-1)
error (256)
STRING (257) 1
END (258) 1

Nonterminals, with rules where they appear

yowza (5)
    on left: 1


state 0

    STRING      shift, and go to state 1

    yowza       go to state 3



state 1

    yowza  ->  STRING . END   (rule 1)

    END         shift, and go to state 2



state 2

    yowza  ->  STRING END .   (rule 1)

    $default    reduce using rule 1 (yowza)



state 3

    $           go to state 4



state 4
<"grammar.output">

Thanks for any effort spent on this.


Dustin Kick




reply via email to

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