help-bison
[Top][All Lists]
Advanced

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

Some Help Understanding Bison Grammar


From: Juan Ceasar
Subject: Some Help Understanding Bison Grammar
Date: Sat, 1 Oct 2016 23:14:01 -0400

Evening All,

I’m writing a Bison parser and, being relatively new to this, I’m confused 
about how to tie my scanner (for flex) together with my bison (version 3.04 so 
I can use C++) parser.  For example, in my scanner.l file I have the following:

 #define INT_TOKEN(tok, val)   YIELD_TOKEN(tok, val, int)
 #define NUM_TOKEN(tok)        YIELD_TOKEN(tok, YY_NUM, double)
 #define STR_TOKEN(tok)        YIELD_TOKEN(tok, YY_TXT, std::string)
 #define SEP_TOKEN(tok)        return yytext[0];
 #define LITERAL               return yytext[0];

 #define YY_USER_ACTION        yyloc->columns(yyleng);

%}

%%

/* code to be executed at every yylex() call */
%{
  yyloc->step();
%}

sig                      STR_TOKEN(SIG)
fact                     STR_TOKEN(FACT)
hint                     STR_TOKEN(HINT)

[[:digit:]]+             NUM_TOKEN(NUM)
[[:alpha:]][[:alnum:]_]* STR_TOKEN(NAME)

+                      INT_TOKEN(SIGN, ast::SIGN_PLUS)
-                      INT_TOKEN(SIGN, ast::SIGN_MINUS)
*                      INT_TOKEN(SIGN, ast::SIGN_MULTIPLY)
/                      INT_TOKEN(SIGN, ast::SIGN_DIVISION)
=                      INT_TOKEN(SIGN, ast::EQUAL)

{                        INT_TOKEN(SIGN, ast::LBRACE)
(                        INT_TOKEN(SIGN, ast::LPAREN)
:                        INT_TOKEN(SIGN, ast::COLON)
}                        INT_TOKEN(SIGN, ast::RBRACE)
)                        INT_TOKEN(SIGN, ast::RPAREN)

Where my ast::XXX are enum’s that I’ve declared in my C++ header file…

But then I naively tried to used a rule in my parser.y file such as:

object: SIG NAME RBRACE list LBRACE { $$ = new Object( $2, $4 ); }
;

But then I get the error in transpiling:

$ make
/usr/local/bin/bison parser.y
parser.y:115.30-35: error: symbol LBRACE is used, but is not defined as a token 
and has no rules
 object: SIG NAME RBRACE list LBRACE { $$ = new Object( $2, $4 ); }



And so I’m a little confused, I could of course declare LBRACE and so forth as 
tokens in the parser.y, like so (these are all the tokens in my parser.y file):

%token       END   0 "end of file"

%token FACT "fact"
%token SIG "sig"
%token HINT “hint”

%token <std::string> NAME
%token <double> NUM
%token <int> SIGN
             REL

But then, how does that tie in with the flex scanner??  Where should I be tying 
these two pieces together?  Shouldn’t they use the same enum value (the 
integer)?



Thanks for any help and advice you can give.












reply via email to

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