Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have to submitt my assingments in collage and following question i am not able to solve....i need help.....
Q. Discuss usefulness of CONTEXT FREE GRAMMAR. Construct context free grammar for all conditional and looping statements in C-language.
Thanks and bye !
Dilip

Try a google search on 'context free grammar' and you will find a good amount of discussion on this topic. Also, try posting to the programming forum too.
-jim

Hi Dilip,
go through the reply.May be it'll be of some useA context-free grammar is a formal grammar in which every production rule is of the form
V -> w
where V is a nonterminal symbol and w is a string consisting of terminals and/or non-terminals. The term "context-free" comes from the feature that the variable V can always be replaced by w, no matter in what context it occurs. A formal language is context-free if there is a context-free grammar which generates it.
Context-free grammars are important because they are powerful enough to describe the syntax of programming languages; in fact, almost all programming languages are defined via context-free grammars. On the other hand, context-free grammars are simple enough to allow the construction of efficient parsing algorithms which for a given string determine whether and how it can be generated from the grammar.
___________________________________________
Here is a context-free grammar for syntactically correct infix algebraic expressions in the variables x, y and z:S -> T + S | T - S | T
T -> T * T | T / T | ( S ) | x | y | z
This grammar can for example generate the string "( x + y ) * x - z * y / ( x + x )".Regards

A) Context Free Grammar for if……..else………if statement
---à if ( ) else if ( )= Statement
= Expression
Non-terminal symbols are and
Terminal symbols are if and else
is the Start symbolB) Context Free Grammar for Switch statement
---à switch ( )
---à case :
= Statement
= Expression
Non-terminal symbols are and
Terminal symbols are switch and case
The symbol at the end of the first line is the production
is the Start symbolC) Context Free Grammar for Do……….while statement
---à do while ( ) ;
= Statement
= Expression
Non-terminal symbols are and
Terminal symbols are do and while
is the Start symbolHope this helps
Regards

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |