Question

3.) a. Explain the difference between context free and context sensitive languages and grammars. Provide an...

3.) a. Explain the difference between context free and context sensitive languages and grammars. Provide an example of a context free language and a context sensitive language (that is not Context free) b. Explain the differences in the grammar representation (i.e. specifically state what grammar constructs are allowed in a Context Sensitive Grammar as compared to a Context Free Grammar)

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

A)

If a grammar has any productions that have something other than just one nonterminal on the LHS, it’s context-sensitive; otherwise, it’s context free. Recall what productions say: if you want to, you can replace what’s on the LHS with what’s on the RHS. The additional stuff on the left is the context in which a replacement can take place.

Let’s give some more, um, context. Recall that the left hand side of a production in a formal grammar is a non-empty string of either terminal or nonterminal symbols that has at least one nonterminal in it. Let’s call a production “context free” if its LHS consists of just one nonterminal, and “context sensitive” otherwise. Then we can more briefly say a grammar is context free iff all its productions are context free; having any context sensitive productions makes the grammar context sensitive. A context free production lets you replace the nonterminal on the LHS with the stuff on the RHS no matter what. It doesn’t matter what’s around it.

B)

Context-free grammar is a formal grammar in which every production(rewrite) rule is a form of V→w Where V is a single nonterminal symbol and w is a string of terminals and/or non-terminals. w can be empty

Context-sensitive grammar is a formal grammar in which left-hand sides and right hand sides of any production (rewrite) rules may be surrounded by a context of terminal and nonterminal symbols.

Explanation

An important detail here is that grammars do not accept strings; they generate strings. Grammars are descriptions of languages that provide a means for generating all possible strings contained in the language. In order to tell if a particular string is contained in the language, you would use a recognizer, some sort of automaton that processes a given string and says "yes" or "no."

A context-free grammar (CFG) is a grammar where (as you noted) each production has the form A → w, where A is a nonterminal and w is a string of terminals and nonterminals. Informally, a CFG is a grammar where any nonterminal can be expanded out to any of its productions at any point. The language of a grammar is the set of strings of terminals that can be derived from the start symbol.

A context-sensitive grammar (CSG) is a grammar where each production has the form wAx → wyx, where w and x are strings of terminals and nonterminals and y is also a string of terminals. In other words, the productions give rules saying "if you see A in a given context, you may replace A by the string y." It's an unfortunate that these grammars are called "context-sensitive grammars" because it means that "context-free" and "context-sensitive" are not opposites, and it means that there are certain classes of grammars that arguably take a lot of contextual information into account but aren't formally considered to be context-sensitive.

To determine whether a string is contained in a CFG or a CSG, there are many approaches. First, you could build a recognizer for the given grammar. For CFGs, the pushdown automaton (PDA) is a type of automaton that accepts precisely the context-free languages, and there is a simple construction for turning any CFG into a PDA. For the context-sensitive grammars, the automaton you would use is called a linear bounded automaton (LBA).

However, these above approaches, if treated naively, are not very efficient. To determine whether a string is contained in the language of a CFG, there are far more efficient algorithms. For example, many grammars can have LL(k) or LR(k) parsers built for them, which allows you to (in linear time) decide whether a string is contained in the grammar. All grammars can be parsed using the Earley parser, which in O(n3) can determine whether a string of length n is contained in the grammar (interestingly, it can parse any unambiguous CFG in O(n2), and with lookaheads can parse any LR(k) grammar in O(n) time!). If you were purely interested in the question "is string x contained in the language generated by grammar G?", then one of these approaches would be excellent. If you wanted to know how the string x was generated (by finding a parse tree), you can adapt these approaches to also provide this information. However, parsing CSGs is, in general, PSPACE-complete, so there are no known parsing algorithms for them that run in worst-case polynomial time. There are some algorithms that in practice tend to run quickly, though. The authors of Parsing Techniques: A Practical Guide (see below) have put together a fantastic page containing all sorts of parsing algorithms, including one that parses context-sensitive languages.

If you're interested in learning more about parsing, consider checking out the excellent book "Parsing Techniques: A Practical Guide, Second Edition" by Grune and Jacobs, which discusses all sorts of parsing algorithms for determining whether a string is contained in a grammar and, if so, how it is generated by the parsing algorithm.

Hope this helps!

Add a comment
Know the answer?
Add Answer to:
3.) a. Explain the difference between context free and context sensitive languages and grammars. Provide an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT