[kelbt-users] make calc2.kl example a *counting* calculator

Adrian Thurston thurston at complang.org
Fri Jan 23 15:22:05 UTC 2009


Hi Elijah,

To make the new test complete the output section at the end needs to be 
updated and verified to be correct.

Adrian

Elijah Epifanov wrote:
> diff -Naur calc2eof.kl calc2c.kl
> --- calc2eof.kl	2009-01-12 23:23:01.000000000 +0300
> +++ calc2c.kl	2009-01-12 23:21:30.000000000 +0300
> @@ -1,11 +1,9 @@
> -/*
> - * @LANG: c
> - */
> -
>  #include <stdio.h>
>  #include <stdlib.h>
> 
> -struct Token { };
> +struct Token {
> +  double value;
> +};
>  struct LangEl;
> 
>  %%{
> @@ -33,14 +31,13 @@
> 
>  %% write token_defs;
> 
> -
>  %%{
> 
>  parser Parser;
> 
>  # Expect an expression.
>  start: E try {
> -	printf( "start = E;\n" );
> +	printf( "start = E(%f);\n", $1->res );
>  };
> 
>  # Expressions returns a float result;
> @@ -50,10 +47,12 @@
> 
>  # Top level of expressions support addition.
>  E: E tok_plus F try {
> -	printf( "E = E tok_plus F;\n" );
> +        $$->res = $1->res + $3->res;
> +	printf( "E(%f) = E(%f) tok_plus F(%f);\n", $$->res, $1->res, $3->res);
>  };
>  E: F try {
> -	printf( "E = F;\n" );
> +        $$->res = $1->res;
> +	printf( "E(%f) = F(%f);\n", $$->res, $1->res);
>  };
> 
>  # Factor returns a float result;
> @@ -63,10 +62,12 @@
> 
>  # Second level supports multiplication.
>  F: tok_number tok_mult F try {
> -	printf( "F = tok_number tok_mult F;\n" );
> +	$$->res = $1->value * $3->res;
> +	printf( "F(%f) = tok_number(%f) tok_mult F(%f);\n", $$->res,
> $1->value, $3->res);
>  };
> -F: tok_number try {
> -	printf( "F = tok_number;\n" );
> +F: tok_number try {
> +        $$->res = $1->value;
> +	printf( "F(%f) = tok_number(%f);\n", $$->res, $1->value);
>  };
> 
>  }%%
> @@ -101,14 +102,23 @@
>  		printf("parse error\n");
>  }
> 
> +void token( int tokid, double value )
> +{
> +	struct Token token;
> +        token.value = value;
> +	int result = parseLangEl( tokid, &token );
> +	if ( result < 0 )
> +		printf("parse error\n");
> +}
> +
>  void test0()
>  {
>  	init();
> -	token( tok_number );
> +	token( tok_number, 1 );
>  	token( tok_plus );
> -	token( tok_number );
> +	token( tok_number, 2 );
>  	token( tok_mult );
> -	token( tok_number );
> +	token( tok_number, 3 );
>  	token( Parser_tk_eof );
>  	finish();
>  	printf( "%d\n", p.errCount );
> @@ -116,7 +126,7 @@
>  void test1()
>  {
>  	init();
> -	token( tok_number );
> +	token( tok_number, 13 );
>  	token( Parser_tk_eof );
>  	finish();
>  	printf( "%d\n", p.errCount );
> @@ -124,15 +134,15 @@
>  void test2()
>  {
>  	init();
> -	token( tok_number );
> +	token( tok_number, 3 );
>  	token( tok_plus );
> -	token( tok_number );
> +	token( tok_number, 4 );
>  	token( tok_plus );
> -	token( tok_number );
> +	token( tok_number, 5 );
>  	token( tok_plus );
> -	token( tok_number );
> +	token( tok_number, 6 );
>  	token( tok_mult );
> -	token( tok_number );
> +	token( tok_number, 7 );
>  	token( Parser_tk_eof );
>  	finish();
>  	printf( "%d\n", p.errCount );
> @@ -155,7 +165,7 @@
>  void test5()
>  {
>  	init();
> -	token( tok_number );
> +	token( tok_number, 8 );
>  	token( tok_plus );
>  	token( Parser_tk_eof );
>  	finish();
> @@ -164,7 +174,7 @@
>  void test6()
>  {
>  	init();
> -	token( tok_number );
> +	token( tok_number, 9 );
>  	token( tok_mult );
>  	token( Parser_tk_eof );
>  	finish();
> @@ -173,10 +183,10 @@
>  void test7()
>  {
>  	init();
> -	token( tok_number );
> +	token( tok_number, 10 );
>  	token( tok_mult );
> -	token( tok_number );
> -	token( tok_number );
> +	token( tok_number, 11 );
> +	token( tok_number, 12 );
>  	token( Parser_tk_eof );
>  	finish();
>  	printf( "%d\n", p.errCount );
> 
> _______________________________________________
> kelbt-users mailing list
> kelbt-users at complang.org
> http://www.complang.org/mailman/listinfo/kelbt-users




More information about the kelbt-users mailing list