parser for c language help me

--------------------------------------------------------------------------------

i want parser in language


i have grammer is that

SS->AAaBBb

AA->a|b

BB->aAA

this a letter for language a ,b

int lexan()
{
int lookahead;

lookahead=getchar();

return t;
}
int match(int t)
{
if(lookahead==t)
lookahead=lexan();
else error();
return lookahead;
}
void error()
{
printf(" error syntax");
}
void SS()
{
AA();
if(lookahead=='a')
match('a');
else error();
BB();
if(lookahead=='b')
match('b');
else error();
}

void AA()
{
if(lookahead=='a')
match('a');
else error();

if(lookahead=='b')
match('b');
else error();

}
void BB()
{
if(lookahead=='a')
match('a');
else error();
AA();
}

I think you are kind of close, but you need to be careful about a couple of things.

The first problem is that the variable lookahead is local to lexan(), so your compiler should be complaining about lines 11, 12, 14, 23, 27, etc.

The second problem is that you are testing conditions too many times. Remember, match( t ) will validate t and return the next "lookahead" or error.

This second problem comes from how match() returns... It is possible for it to not match an item and for that to be a valid mismatch. Since it always complains (with error()) if the match is not made, you have had to put extra code in AA() et.al.

Finally, you have mixed a couple of concepts, methinks. lexan() should be the function to call to check the language SS and return whether or not it matches.

To help, these are my suggestions:

  1. lexan() should call SS(), which should call AA(), do its own test against 'a', call BB(), and do its own test against 'b', and return success or failure.
  2. char lookahead should remain a local variable in lexan(), but it should also be passed as a reference argument to match(), SS(), AA(), and BB() (in addition to current arguments). For example: [B]int[/B] match( [B]char[/B] t, [B]char[/B]* thelookahead ); and called with match( 'a', &lookahead )
  3. match() should return either success or failure. If it succeeded, the lookahead variable should be modified to the next input. If it failed, the lookahead variable should be left unmodified so that it can be used in the next test.

Er, I'm sorry but I got called away for about a half hour so I've completely lost my train of thought. I think I covered everything important though.

Good luck.

Comments
Excellent effort, despite the poor state of the original post.

Heh heh heh...

My brain got so big because I studied the theory of computation... :-O

You can make your brain bigger by picking up a couple of texts on the same. ;)

Then questions like this will be obvious to you too :yawn:

:)

This article has been dead for over six months. Start a new discussion instead.