|
From: | Hans Aberg |
Subject: | Re: Help Deciphering an Example |
Date: | Tue, 4 Oct 2005 23:50:51 +0200 |
On 4 Oct 2005, at 22:48, Jason Ki wrote:
This is in regards to the thread "token value return in custom yylex ()"posted by Bernd Prager on Tuesday December 3rd, 2002. http://lists.gnu.org/archive/html/help-bison/2002-12/msg00002.html His grammar code shows the following: ----- start of example ----- %{ #include <stdio.h> #include <string.h> #include "parser.tab.h" #define TESTSTRING "word1 word2" // testpattern char *data, buffer[1024]; %} %union { int id; char *str; } %type <str> object token list %token <id> WORD ----- end of example ----- I am unsure what the previous coding really means. At first I thought that a token could either be a string or an integer. However, on acloser inspection I noticed that the it was "%type" and not "% token". Sodoes this mean that a token can only be of type integer? If this iswrong, which I'm positively sure it is, then I believe I still have yetto grasp what a token is.
Bison has a static typing system, which is turned on when %union is invoked, and at the same time the stuff in the %union clause is implemented as a C-union. One then has to figure out how to select the right union field, which is done with the %type and %token declarations above. Each grammar symbol can only have one type value, and that is written down above. It says that the token WORD has the type <id>, the grammar variables object, token, list, have type <str>.
Additionally, is yacc similar to BISON? The website below: http://epaperpress.com/lexandyacc/index.htmlimplies that yacc is the same as BISON in regards that both are softwareused to generate grammar rules for building a syntax tree.
There is a POSIX specification for what a Yacc-like program should fulfill, and Bison has a mode where it aims to compliant with that POSIX specification.
Hans Aberg
[Prev in Thread] | Current Thread | [Next in Thread] |