help-bison
[Top][All Lists]
Advanced

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

problem with very simple gramma


From: Natalia Wolyczko
Subject: problem with very simple gramma
Date: Sat, 01 Dec 2007 23:07:01 +0100

Hello,
I'm new to bison, first of all I've tried to write a simple "hello-world" like 
program using flex/bison. 
All I want is to see "boom!" message after reading two MLAH tokens, which 
corresponds to input string of the same name i.e. "mlah".

This works fine, I'm able to see "boom!" message after typing mlah twice, the 
things which I really don't get is why I see "syntax errror" once mlah is 
entered third time? What I expected is the situation in which I see "boom!" 
every two "mlah" occurences. 
Can anybody please explain me what's going on?

$ cat fb.l
%{
#include "fb.y.h"
%}

%%

mlah  {return MLAH;}
.*  {}

%%

$ cat fb.y
%{
#include <stdio.h>
yydebug=1;
%}

%debug
%token MLAH

%%

boom: MLAH MLAH {printf("boom!\n");}
    ;

%%

$ cat fb.output 
Grammar

    0 $accept: boom $end

    1 boom: MLAH MLAH


Terminals, with rules where they appear

$end (0) 0
error (256)
MLAH (258) 1


Nonterminals, with rules where they appear

$accept (4)
    on left: 0
boom (5)
    on left: 1, on right: 0


state 0

    0 $accept: . boom $end

    MLAH  shift, and go to state 1

    boom  go to state 2


state 1

    1 boom: MLAH . MLAH

    MLAH  shift, and go to state 3


state 2

    0 $accept: boom . $end

    $end  shift, and go to state 4


state 3

    1 boom: MLAH MLAH .

    $default  reduce using rule 1 (boom)


state 4

    0 $accept: boom $end .

    $default  accept
$ ./fb
Starting parse
Entering state 0
Reading a token: 

mlah
Next token is token MLAH ()
Shifting token MLAH ()
Entering state 1
Reading a token: 
mlah
Next token is token MLAH ()
Shifting token MLAH ()
Entering state 3
Reducing stack by rule 1 (line 11):
   $1 = token MLAH ()
   $2 = token MLAH ()
boom!
-> $$ = nterm boom ()
Stack now 0
Entering state 2
Reading a token: 
mlah
Next token is token MLAH ()
syntax error
Error: popping nterm boom ()
Stack now 0
Cleanup: discarding lookahead token MLAH ()
Stack now 0
$ 

-- 
Natalia Wolyczko





reply via email to

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