Reputation: 11
How to write a cfg for a block of code? I know how to write a cfg for a regular expression but if we have to write a cfg for following input i.e.
int a = 0 ;
How to do it?
Upvotes: 1
Views: 479
Reputation: 6406
Stick to C for now.
A block consists of either a statement (a line, essentially), or a curly brace, a list of variable declarations, then a list of statements or blocks, and another curly brace.
So start by assuming that int is the only variable type allowed,l that there are no globals or parameters, and that assignment of an arithmetical expression (eg a = 2 *b +c;) is the only statement type allowed.
Then add if statements to your grammar, then while loops. Finally function calls with parameters.
Then you've essentially got the grammar of a C-type language, the rest is just details (there's also an awkward problem with C typedefs which you will come to when you finally get round to adding them).
Upvotes: 1