|
From: | Bernd Prager |
Subject: | help with simple grammar please |
Date: | Sun, 25 Aug 2002 03:06:47 -0400 |
Hi,
just starting with bison and try to understand
it.
The grammar file looks like:
------ test.y ---------------------
%{
#include <stdio.h> #define YYERROR_VERBOSE %} %token WORD
%%
_expression_: word
{ printf( "_expression_: (single word >%s<)\n", $1); } | sentence { printf ("_expression_: (sentence >%s<)\n", $1); } ; /* a single word */
word: WORD { printf( "word: >%s<\n", $1); } ; sentence: '(' word _expression_
')'
{ printf( "sentence: (word >%s< _expression_ >%s<)\n", $1, $2); } ; %%
int main() { { return(yyparse()); } } int
yyerror (char *s) { fprintf (stderr, "%s\n", s); return 1; } ------ test.y ---------------------
The input line "echo "(one two)" | ./test " returns:
-------- snip --------------------
word: >one<
word: >two< _expression_: (single word >two<) sentence: (word >(null)< _expression_ >one two)<) _expression_: (sentence >(null)<) -------- snip --------------------
I was expecting something like:
-------- snip --------------------
word: >one<
_expression_: (single word >one<) word: >two< _expression_: (single word >two<) sentence: (word >one< _expression_ >two<) _expression_ (sentence >(one two)<) -------- snip -------------------- I don't understand this. Where is my error in reasoning?
Can anybody help me?
Thanks,
-- B.
|
[Prev in Thread] | Current Thread | [Next in Thread] |