Flex/Bison help

Started by
16 comments, last by kael00 14 years, 1 month ago
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.
Advertisement
What have you tried?
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.
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.
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.
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;
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?
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 <str> ID STRING// yacc rule type definitions%type <symbol> identifier string// yacc grammar rulessimple_expression      : identifier     {$$ = new TreeNode (IDENT_EXPR); $$->symbol = $1;}      | string         {$$ = new TreeNode (STR_EXPR); $$->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 <str> ID STRING NUMBER// yacc rule type definitions%type <symbol> identifier string number// yacc grammar rulessimple_expression      : identifier     {$$ = new TreeNode (IDENT_EXPR); $$->symbol = $1;}      | string         {$$ = new TreeNode (STR_EXPR); $$->symbol = $1;}      | number         {$$ = new TreeNode (NUM_EXPR); $$->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]
Again, break it down by parts. What are the tokens in 'string a = "hello";' and what would regular expressions for those be?
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).

This topic is closed to new replies.

Advertisement