Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Gabriele Farina on January 15, 2009 in actionscript

AsYacc - first alpha release

Before going further with discussing how to add runtime scripting support to a Flash applications, I'd like to share with you the source code of a porting of Yacc I did a while ago. I did some simple modification to make Yacc able to generate ActionScript 3.0 source code instead of C. It works quite well and supports a lot standard Yacc features. There might be some issues - report them to me and I'll try to fix them :)

You can easilly find documentation about Yacc searching Google (you can start here for example).

Usually Yacc is used in conjunction with a Scanner generator (like Lex/Flex), but I didn't to any porting of commonly used Scanner generators yet.

Here you can download the sources. The source code should be portable and compilable on all the most common platform, but I didn't tested it on Windows yet. To compile the source code on Mac or Linux, cd to the source code directory and type use:

gcc *.c -o AsYacc

Once you have the compiled binary file, you can run the Parser generator using:

./AsYacc -Hpackage=it.sephiroth.test grammar.y

Where grammar.y is a text file that contains the grammar of a language defined using the proper syntax (see the docs online for all the detailed information you may need).

Here you can download a simple calculator example that uses the RegexLexer described previously to implement the Scanner. For the ones who might be interested, here is the grammar used:

/* Infix notation calculator. */ 
%{ 
%}

%token NUM 
%left '-' '+' 
%left '*' '/' 
%left NEG 
%right '^' /* exponentiation */
 
%%
input:
	exp			{ trace( $1 ); }
	;

exp:
	 NUM 			{ $$ = $1; }
	| exp '+' exp 		{ $$ = $1 + $3; } 
	| exp '-' exp 		{ $$ = $1 - $3; }
	| exp '*' exp 		{ $$ = $1 * $3; } 
	| exp '/' exp 		{ $$ = $1 / $3; } 
	| '-' exp %prec NEG 	{ $$ = -$2; } 
	| exp '^' exp 		{ $$ = Math.pow( $1, $3 ); } 
	| '(' exp ')' 		{ $$ = $2; } 
	;
%%

 

Bookmark and Share

 

 

3 comments
first release? quite great.
Hi Max, sorry but I didn't see your comment before. I've never had this kind of issue, but probably you can avoid the problem by grouping some rules togheter (ie: keywords or symbols), and then generate different tokens using custom rules. Could you please explain me more in depth your situation ? Maybe I can help you more
Hi Gabriele,
thanks four your code, I can use it very well.
But I have one problem: If I add more than 31 rules to the class ExprLexer it does not work anymore. It seems that there is a restriction to 32 patterns. The pattern becomes too large which is built in RegexLexer::buildRegExp(). Do you have any idea?

Best Regards
Max



Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Type the characters you see in the picture above.





 

TrackBacks

TrackBack URL for this entry: http://www.sephiroth.it/cgi-bin/mt/mt-tb.cgi/261