|
-
Mar 6th, 2002, 08:01 PM
#1
Thread Starter
New Member
semantic analyer & prefix expression
Hi,
I have writen a small syntax and semantic analyer to analysising +,-,* operators.The output is like using assemble instructions.For example:input "1-2*3+4;",and out put:
t0 = 1
t1 = 2
t2 = 3
t1 *= t2
t0 -= t1
t1 = 4
t0 += t1
there is not any problem.
Now I want to modify the semantic analyer,then during the semantic analysising make the prefix expression.The output like:
t0 = 1
t1 = 2
t2 = 3
t1 *= t2
t0 -= t1
t1 = 4
t0 += t1
the prefix expression is + - 1 * 2 3 4
I have tried several methods and thinking many days,but I dont find any satisfying result.
Can u help me?
The source code is:
/*The main.c and syntax analyer are omitted.*/
/*retval.c semantic analyer
*using recursive-descent parsing
*analysis + - *
*
*#define + PLUS
*#define - SUB
*#define * TIME
*#define ( LP
*#define ) RP
*/
#include <stdio.h>
#include "lex.h"
char *factor ( void );
char *term ( void );
char *expression ( void );
extern char *newname( void ); /* ÔÚname.cÖж¨Òå */
extern void freename( char *name );
statements()
{
/* statements -> expression SEMI | expression SEMI statements */
char *tempvar;
while( !match(EOI) )
{
tempvar = expression();
if( match( SEMI ) )
advance();
else
fprintf( stderr, "%d: Inserting missing semicolon\n", yylineno );
freename( tempvar );
}
}
char *expression()
{
/* expression -> term expression'
* expression' -> PLUS term expression' | epsilon
*/
char *tempvar, *tempvar2;
tempvar = term();
while( match( PLUS )|| match( SUB ) )
{
if( match( PLUS ) )
{
advance();
tempvar2 = term();
printf(" %s += %s\n", tempvar, tempvar2 );
}
else
{
advance();
tempvar2 = term();
printf(" %s -= %s\n", tempvar, tempvar2 );
}
freename( tempvar2 );
}
return tempvar;
}
char *term()
{
char *tempvar, *tempvar2 ;
tempvar = factor();
while( match( TIMES ) )
{
advance();
tempvar2 = factor();
printf(" %s *= %s\n", tempvar, tempvar2 );
freename( tempvar2 );
}
return tempvar;
}
char *factor()
{
char *tempvar;
if( match(NUM_OR_ID) )
{
/* yyleng is the printing length
* yytext is the input string
*/
printf(" %s = %0.*s\n", tempvar = newname(), yyleng, yytext );
advance();
}
else if( match(LP) )
{
advance();
tempvar = expression();
if( match(RP) )
advance();
else
fprintf(stderr, "%d: Mismatched parenthesis\n", yylineno );
}
else
fprintf( stderr, "%d: Number or identifier expected\n", yylineno );
return tempvar;
}
/*name.c create the instruction name*/
#include <stdio.h>
char *Names[] = { "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7" };
char **Namep = Names;
extern int yylineno;
char *newname()
{
if( Namep >= &Names[ sizeof(Names)/sizeof(*Names) ] )
{
fprintf( stderr, "%d: Expression too complex\n", yylineno );
exit( 1 );
}
return( *Namep++ );
}
freename(s)
char *s;
{
if( Namep > Names )
*--Namep = s;
else
fprintf(stderr, "%d: (Internal error) Name stack underflow\n",yylineno );
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|