help-flex
[Top][All Lists]
Advanced

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

RE: Bison/Flex - C++ - String input


From: Matt Carter
Subject: RE: Bison/Flex - C++ - String input
Date: Tue, 19 Feb 2002 14:12:30 -0500

Hi Stephane,

The answers to your questions are "yes".  Here is what you want to do in
order to use flex++ with pure bison:

1) Tell flex++ that you want to have your own scanner class derived from
yyFlexLexer.  To do this, put:
  %option yyclass="MyScannerClass"
in your .l file.  Now, flex++ will generate MyScannerClass::yylex() for you.

2) A bison-generated parser does not call MyScannerClass::yylex()
automatically.  It only knows how to call a global function yylex().
Therefore, write a global wrapper function yylex() that takes two
parameters:
  YYSTYPE *new_token
  MyScannerClass *my_scanner
and calls my_scanner.yylex() .

3) By default, pure bison only passes one parameter (YYSTYPE *) to yylex().
So, you have to tell bison to pass an instance of your scanner as an
additional parameter to yylex() by putting this in your .y file:
  #define YYLEX_PARAM <my scanner instance>
where <my scanner instance> is an expression that specifies an instance of
MyScannerClass being used for this parse.

4) Recall that YYSTYPE is the union generated by bison's %union directive.
(You can also define it explicitly using "union YYSTYPE { ... };".)  This
data structure is used for the semantic values of all tokens and syntactic
groupings in your parser.

5) Since MyScannerClass::yylex() does not take any parameters, communication
of data between your parser and your scanner is currently limited to
MyScannerClass::yylex()'s return value.  However, since you're creating your
own scanner class, you can add fields and methods to solve this problem.
Add a private variable "YYSTYPE *lval_ptr" to MyScannerClass.  Add a method
to MyScannerClass called set_lval_ptr(YYSTYPE *lval_ptr_in) defined as:
   { lval_ptr = lval_ptr_in; }

6) Put:
  my_scanner.set_lval_ptr(new_token);
at the top of your global yylex() function.

7) Now, all your scanner actions can write to the scanner class's private
variable *lval_ptr, and they'll be writing to your parser's $1, $2, $3, ...
variables.

-Matt

-----Original Message-----
From: address@hidden [mailto:address@hidden
Sent: Tuesday, February 19, 2002 4:47 AM
To: Matt Carter
Subject: RE: Bison/Flex - C++ - String input



Thanks a lot for the info.

Did you go through his example ?
I've got the point and I've tried to adapt it to my situation and I am
facing a problem which is
not exercised in his example:
- How do I save the value of yytext from the scanner for use within the
parser ? (replacement of yylval)
- Can I still have the notion of %union in Bison in order to give "Node*"
type to most of my non-terminals ?

If you have any idea ?

Thanks in advance
Regards
Stephane

From: Matt Carter <address@hidden>  on 18/02/2002 11:18 EST

                                                                          
 To:   "'address@hidden'"                                            
       <address@hidden>                                              
       address@hidden                                                 
                                                                          
 cc:   address@hidden                                                  
                                                                          
                                                                          
                                                                          
                                                                          
                                                                          
                                                                          
 Subje                                    RE: Bison/Flex - C++ - String   
 ct:                                      input                           
                                                                          





Markus Mottl did up a nice example of using bison and flex++ in a
thread-safe manner.  You can download it at:
http://miss.wu-wien.ac.at/~mottl/cpp_sources/reent.tar.gz

-----Original Message-----
From: address@hidden [mailto:address@hidden
Sent: Monday, February 18, 2002 10:50 AM
To: address@hidden
Cc: address@hidden
Subject: Bison/Flex - C++ - String input


Hi folks,

after 72 hours of intensive search throughout google, I call for help.

I have succesfuly created a small Flex/Bison program allowing me to parse
a boolean expression and build a binary tree which can be recursively
evaluated
(big deal ...).

I need to integrate this into a C++ server architecture.

I am looking for a good example of integration of Bison with Flex++ or a
good
example of threadsafe integration of bison/flex. This example needs to
parse
a string as input and should completly ban global variables.

I must admit, I have found one potentially valid example (in the archive of
MacTech)
but it uses static variables and I wonder if it is really the only choice.

If I can I'd like to avoid using Bison++ which is not GNU and anyway rather
difficult
to manage. However, if someone has a complete example (I insist on that one
since C++
Annotations example is not complete), it would still help me.

Thanks for any help.
Regards
Stephane
_________________________________________________________________
Stephane TABARY
aMaDEUS Development - New Generation Inventory - DEV/GCO/GCC/NGI/MPP
address@hidden


_______________________________________________
Help-flex mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/help-flex









reply via email to

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