CFG to CNF - unary/associative operators

  • Thread starter mr_m4x
  • Start date
  • Tags
    Operators
In summary: Your Name]In summary, to convert a context-free grammar into Chomsky normal form, you will need to use additional variables and productions to represent unary and binary operators and terminals separately. This will allow you to meet the requirements of CNF, which states that all productions must have two and only two variables on the right-hand side.
  • #1
mr_m4x
1
0

Homework Statement



I'm trying to convert a context free grammar into Chomsky's normal form.

These are the productions of my grammar:

S -> 0|1|a|b|S+S|S.S|S*|(S)

Where 0, 1, a, b are terminals, +, . are binary operators and *,() are unary operators.

I know that for a grammar to be in CNF, all it's productions must be terminals or must have two and only two variables distinct of the start symbol.

Homework Equations


The Attempt at a Solution



I added a new variable so I have removed the start symbol from the RHS of the productions, so now my productions look like this

S' -> S

S -> 0|1|a|b|S+S|S.S|S*|(S)

Where S' is the new start symbol. Now my problem is that I'm not 100% sure about how to solve the two unary operators since nost of the RHS of the production complies with the requirements of a CNF.

Maybe the solution looks like this...

S -> 0|1|a|b|S+S|S.S|0*|1*|a*|b*|(S+S)*|(S.S)*|(0)|(1)|(a)|(b)|(S+S)|(S.S)

?

Thanks!
 
Physics news on Phys.org
  • #2


Hello,

To convert your grammar into Chomsky normal form, you will need to use some additional variables and productions. First, let's deal with the unary operators. To convert them, you can use the following productions:

S -> S'
S' -> 0|1|a|b|S+S|S.S
S' -> S''

S'' -> S*
S'' -> (S)

This will allow you to represent the unary operators as separate variables, while still following the rules of CNF.

Next, you will need to eliminate the productions with more than two variables on the right-hand side. To do this, you can use the following productions:

S -> S'
S' -> S''S''' | S+S | S.S
S''S''' -> S''S''
S''S''' -> S*S*
S''S''' -> (S+S)*(S+S)
S''S''' -> (S.S)*(S.S)

This will allow you to represent the binary operators as separate variables, while still following the rules of CNF.

Finally, you will need to eliminate the productions with terminals on the right-hand side. To do this, you can use the following productions:

S -> S'
S' -> T
T -> 0 | 1 | a | b

This will allow you to represent the terminals as separate variables, while still following the rules of CNF.

Overall, your converted grammar will look like this:

S' -> S

S -> S''S'''
S' -> S+S | S.S
S''S''' -> S''S''
S''S''' -> S*S*
S''S''' -> (S+S)*(S+S)
S''S''' -> (S.S)*(S.S)

S'' -> S*
S'' -> (S)

S -> T
T -> 0 | 1 | a | b

I hope this helps! Let me know if you have any further questions.


 
  • #3


Your approach to add a new start symbol is correct. To handle the unary operators, you can replace them with new variables and add corresponding productions. For example, for the unary operator *, you can add a new variable X and the production X -> S*. This will result in the following productions:

S' -> S
S -> 0|1|a|b|S+S|S.S|X|(S)
X -> S*

Similarly, you can handle the unary operator () by adding a new variable Y and the production Y -> (S). Your final set of productions will then look like this:

S' -> S
S -> 0|1|a|b|S+S|S.S|X|Y
X -> S*
Y -> (S)

This set of productions satisfies the requirements of CNF, as all productions have two variables on the RHS, except for the production S' -> S. You can further simplify this by replacing the variable X and Y with a new variable Z, resulting in the following set of productions:

S' -> S
S -> 0|1|a|b|S+S|S.S|Z
Z -> S*|(S)

This set of productions is now in CNF and represents the original context-free grammar in Chomsky's normal form.
 

FAQ: CFG to CNF - unary/associative operators

What is the difference between a CFG and CNF?

A CFG (Context-Free Grammar) is a formal grammar used to describe the structure of a language. It consists of a set of production rules that define the valid syntactic patterns of the language. CNF (Chomsky Normal Form) is a specific form of CFG in which all production rules are either in the form of A -> BC or A -> a, where A, B, and C are non-terminal symbols and a is a terminal symbol. Essentially, CNF is a subset of CFG that has a simpler structure and is easier to process.

What is the process for converting a CFG to CNF?

To convert a CFG to CNF, the following steps are typically followed:

  1. Eliminate all epsilon productions (productions that result in an empty string) by creating new productions without the epsilon symbol.
  2. Remove all unit productions (productions with only one non-terminal symbol on the right-hand side) by replacing them with the productions of the non-terminal symbol they produce.
  3. Introduce new non-terminal symbols for each production rule with more than two non-terminal symbols on the right-hand side.
  4. Replace all terminals with new non-terminal symbols and create new productions for each terminal.
  5. Repeat the previous steps until all productions are in the form of A -> BC or A -> a.

What are unary operators in CFG?

Unary operators in CFG are production rules with only one non-terminal symbol on the right-hand side. They are used to generate strings of any length by repeatedly applying the same rule. For example, the production rule S -> aSb is a unary operator that generates strings of the form a^n b^n, where n is any positive integer.

What are associative operators in CFG?

Associative operators in CFG are production rules that allow for the grouping of non-terminal symbols on the right-hand side. They are used to generate strings with a specific grouping or order of symbols. For example, the production rule S -> (S)S is an associative operator that generates strings with parentheses around a group of symbols followed by another group of symbols.

Why is it important to convert a CFG to CNF?

Converting a CFG to CNF is important because it simplifies the grammar and makes it easier to analyze and process. CNF grammars have a more regular structure, which allows for more efficient parsing algorithms. They also eliminate common issues in CFGs, such as epsilon and unit productions, which can cause problems in parsing. Additionally, many applications and tools for working with formal grammars only accept CNF grammars as input.

Similar threads

Replies
3
Views
1K
Replies
2
Views
2K
Replies
43
Views
4K
Replies
7
Views
7K
Replies
3
Views
1K
Replies
4
Views
3K
Back
Top