Jump to content
• Advertisement

# Flex/Bison help

This topic is 3054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I've gone through the Flipcode Scripting Engine tutorial, and implemented the basic scripting engine demonstrated there. Currently it only really works with strings. If my script contains the statement,
a = "hello globe";

Then the code figures out that a is a new symbol, and assigns it the string constant specified. I want to add a second variable type for a 2 dimensional point. I'd like to be able to do this:
a = "hello globe";
b = point(4, 7);

And have the code treat the first statement the same, but figure out that the second statement is for a different variable type. Can anyone help me with this? Suggestions/advice would be awesome, flex/bison code would be greatly appreciated.

#### Share this post

##### Share on other sites
Advertisement
What have you tried?

#### Share this post

##### Share on other sites
Posting here. I've been analyzing the flex input and bison input files for a few hours now, but I don't see how to make it so that my assignment expression can be treated as more than just one variable type. I'm also unsure how to make the analyzer treat "point(4, 7)" as a variable declaration. Down the road I'm going to wonder how to make the "." operator, such that i can do "b = point(4, 7); b.x += 10;"

I'm not trying to just dump my problem here and get free work done for me. I'm actively scouring examples on Google and reading the help files; I was just hoping someone had some insight from experience.

#### Share this post

##### Share on other sites
I have used flex/bison a few times before and I always managed to figure out how to do anything I needed. Unfortunately, it's been several years since the last time I used these tools, and I would have to spend a couple of hours to refresh my memory.

Perhaps you should start from a less trivial example.

#### Share this post

##### Share on other sites
Allow me to rephrase: it's easier to modify something than come up with something from scratch. If you post something you've tried, even if you think it's crap, it'd be easier to show you how to modify that into working flex/bison code than if you posted none of your own code.

#### Share this post

##### Share on other sites
my code right now is literally the code from the flipcode tutorial, unmodified. I understand why you're asking that, but I don't even have an attempt at this to start from. I was hoping for someone with some flex/bison experience in using different types of variables, even if it isn't the "point(4, 7)" format that I specifically want. Even just:
a = "hello globe";b = 4;

#### Share this post

##### Share on other sites
Break it down by parts. What are the tokens that make up "b = point(4, 7)"? What are the regular expressions that express each of those tokens?

#### Share this post

##### Share on other sites
Alright I got it worked out to where it accepts strings or numbers. Originally I had:
// lexer rules{STR}    {StringConstant ();         /* string constant: copy contents */          return STRING;}// yacc token definitions%token &lt;str&gt; ID STRING// yacc rule type definitions%type &lt;symbol&gt; identifier string// yacc grammar rulessimple_expression      : identifier     {$$= new TreeNode (IDENT_EXPR);$$-&gt;symbol = $1;} | string {$$= new TreeNode (STR_EXPR);$$-&gt;symbol =$1;}

(with a definition for "identifier" and "string", as per the flipcode tutorials.)

I modified this to be:
// lexer rules{STR}    {StringConstant ();         /* string constant: copy contents */          return STRING;}{NUM}    {NumValue ();               /* number: copy value */          return NUMBER;}// yacc token definitions%token &lt;str&gt; ID STRING NUMBER// yacc rule type definitions%type &lt;symbol&gt; identifier string number// yacc grammar rulessimple_expression      : identifier     {$$= new TreeNode (IDENT_EXPR);$$-&gt;symbol = $1;} | string {$$= new TreeNode (STR_EXPR);$$-&gt;symbol =$1;}      | number         {$$= new TreeNode (NUM_EXPR);$$-&gt;symbol = \$1;}

(with an additional definition for "number")

This works; I can now accept numbers as well as strings.
Now, what I'm noticing, is that my virtual machine will have to have variables that can be either strings or numbers. For example, if the script is like this:
a = "hello";b = 42;

I'll get symbols for a, b, "hello", and 42. The virtual machine will get virtual assembly like this:
 1: OP_NOP  2: OP_PUSH strconst1  3: OP_GETTOP a  4: OP_DISCARD  5: OP_PUSH numvar1  6: OP_GETTOP b  7: OP_DISCARD

So at runtime, whenever I OP_GETTOP, I'd have to figure out the type of the variable and then assign it to a or b as such. This is fairly inefficient for my purposes; I'd like to know from the get-go what type of values the variables can hold. Essentially, I want to do type checking, so that my script will have to be as such:
string a = "hello";int b = 42;

But I'm struggling with getting that to work. Right now, I could have a script like this:
a;

And it would happily recognize 'a' as a symbol. Instead, I want to have to specify a type for a so that I know what it can be assigned.

... any help on that?

[Edited by - Funkymunky on March 12, 2010 12:30:21 PM]

#### Share this post

##### Share on other sites
Again, break it down by parts. What are the tokens in 'string a = "hello";' and what would regular expressions for those be?

#### Share this post

##### Share on other sites
I have broken it down. The fact that it accepts just "a;" means it doesn't care what types of symbols it accepts. How do I enforce a rule paradigm of type checking?

Look please don't be offended, but I'm not looking for help with my problem solving process. The fact that after a few days I found the answer to my initial question should demonstrate that. I'm looking for assistance from someone with specific knowledge of lex/yacc (flex/bison).

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5
• Advertisement

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!