Advertisement

Scripting + Flex + Bison problems

Started by May 25, 2000 07:37 AM
9 comments, last by max621 24 years, 6 months ago
Hi, I''ve been doing my best to create a parser of my own, but it seems impossible from where I stand right now. Late last night I created a simple parser that would parse the string RUN 5 and output the command (run) and the amount (5). Though I was unable to get it to take the 5 from the string and put it into the int, but I needed a cast or something and I don''t know what to do. Also trying to do one of those example Bison / Flex programs, this is my Flex script: %% int num_lines = 0, num_chars = 0; \n ++num_lines; ++num_chars; . ++num_chars; %% main() { yylex(); printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars ); } When I compile it says num_lines and num_chars are undeclared Any help? Thanks
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
You could take a look on flipcode for a complete tutorial to writing a scripting engine.

Here''s a link to the first part

-Sex on the television doesn't hurt... unless you fall off.
-Sex on the television doesn't hurt... unless you fall off.
Advertisement
I think you're looking to do something like this:
%{/* Lexer file */int num_lines = 0, num_chars = 0;main(){yylex();printf( "# of lines = %d, # of chars = %d\n",num_lines, num_chars );}%}%%\n { ++num_lines; ++num_chars; }.  { ++num_chars; }%%  


I haven't tried it, but i think that will work.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~

Edited by - MadKeithV on May 25, 2000 8:58:18 AM
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
You tried to put variable declarations in the rules segment of the lexer. Move the variable declaration to the first segment of the file (before the first %%). You also need to wrap them in %{ %}.
Hi, could you give me exact code that you are sure will work? I am using this Bison and Flex wizard for VC++ so I dunno if it might be interfering with the stuff...
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
MadKeithV''s code should work except that main() should be defined in the third segment, not the first.

I don''t use the wizard but this is a proper lex specification:
%{int num_lines = 0;int num_chars = 0;%}%%\n	{++num_lines; ++num_chars; }.	{++num_chars; }%%main() {  yylex();  printf("% of lines = %d, # of chars = %d\n, num_lines, num_chars);} 
Advertisement
whoops - sorry about the misplacement of the main() .
I''m highly unused to plain-C programming, and I usually use my flex/bison generated files in class based windows projects.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Hey,

If you want to see more examples of good use of Flex and Bison, check out the source of my game project. It contains an XML parser, written using Flex, and a complete (although not fully bulletproof ) script language with a compiler and an interpreter (written using Flex and Bison). It''s kinda based on C and Pascal, with modules (or units, if you prefer) and stuff.

The scripting engine is also partially linked in the game itself, where you can define the GUI using an XML configuration file, and link in scripted event handlers that are called when you press a button.

You can find source, binaries and more stuff at:
http://users.pandora.be/dormeur

Dormeur.
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
Hehe SiCrane, you forgot to have a " after the newline character in the printf function
The stuff compiles alright now, but it says _main is being redifined twice in main.obj:
myparser.l.obj : error LNK2005: _main already defined in main.obj
Debug/myparser.exe : fatal error LNK1169: one or more multiply defined symbols found
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||
Dormeur: For some reason when I run the game, I need to like go over with my mouse where the buttons are to be able to see them. And than when I start playing I can hardly make anything out becuase it is all white-grey and black tone pixels, with a circle in the middle, but that circle (which is the universe I guess) is hard to distinguish.
I have a GeForce 256 DDR (3D Blaster Annihilator Pro) and DirectX 7.... maybe this will help on your game
||--------------------------||Black Hole Productionshttp://bhp.nydus.netResident expert on stuffmax621@barrysworld.com||--------------------------||

This topic is closed to new replies.

Advertisement