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();
}

Recommended Answers

All 7 Replies

you need to ask a question, if you want an answer.

You also need to format your code so we can read it.

Use Lex and Yacc (or Bison) to make your life easier.

no body can help me??

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.

commented: Excellent effort, despite the poor state of the original post. +17

good thing Duoas is some kind of mindreader.
:P

as for myself, I still dont know what the question was

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:

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.