context-free-grammar

Tips for creating “Context Free Grammar”

人盡茶涼 提交于 2019-12-17 06:31:14
问题 I am new to CFG's, Can someone give me tips in creating CFG that generates some language For example L = {a m b n | m >= n} What I got is: S o -> a | aS o | aS 1 | e S 1 -> b | bS 1 | e but I think this area is wrong, because there is a chance that the number of b 's can be greater than a 's. 回答1: How to write CFG with example a m b n L = {a m b n | m >= n}. Language description: a m b n consist of a followed by b where number of a are equal or more then number of b . some example strings: {^

Context-free grammar in C

偶尔善良 提交于 2019-12-13 18:25:14
问题 I have an assignment to make a program in C that displays a number (n < 50) of valid, context-free grammar strings using the following context-free grammar: S -> AA|0 A -> SS|1 I had few concepts of how to do it, but after analyzing them more and more, none of them were right. For now, I'm planning to make an array and randomly change [..., A, ...] for [..., S, S, ...] or [..., 1, ...] until there are only 0s and 1s and then check whether the same thing was already randomly generated. I'm

Writing grammar rules for context sensitive elements using Pyparsing

故事扮演 提交于 2019-12-13 12:12:27
问题 I am trying to write a grammar for a set of sentences and using Pyparsing to parse it. These sentences tell what and how to search in a text file, and I need to convert them into corresponding regex search codes. However, there are some elements that are not really context-free and hence, I am finding it difficult to write production rules for them. Basically, my aim is to parse these sentences and then write regexes for them. Some examples of context-sensitive elements found in these

Context Free Grammar for which a RegEx is impossible

牧云@^-^@ 提交于 2019-12-13 07:28:33
问题 I'm trying to find out if its possible to have an example of a CFG for which it is impossible to give a Regular Expression which can accept the same language. 回答1: Any language which requires counting/remembering can't be expressed as a regular expression. For example, a language which checks balanced parenthesis: S -> { S } S S -> ε 回答2: Since a regular machine/expression has only a limited (pre-defined) number of states, it cannot "remember" (infinitely) earlier parts of the input. As such

Removing epsilon production from context-free grammar

≯℡__Kan透↙ 提交于 2019-12-13 04:25:00
问题 I am just having trouble with one part of the grammar that reads: C -> CBA | epsilon After I remove epsilon productions, I get: C -> CBA | CB | BA | B | A I'm confused as to whether this is correct or not. In the grammar, B is also a nullable variable. Do I also have to include CA and/or A in the latter grammar? Any help appreciated. 来源: https://stackoverflow.com/questions/19920349/removing-epsilon-production-from-context-free-grammar

Prove that a context-free-grammar is regular

耗尽温柔 提交于 2019-12-13 01:43:23
问题 I know that to prove that a language is non-regular one can use the pumping-lemma. I think I understand how it works, but when it comes to showing that a context-free-grammar is (or isn't regular) I'm having big problems. Here is an example of a CFG that I can't understand how to show is regular (or non-regular): i) S → NP VP ii) NP → DET N iii) VP → TV NP iv) N → N N v) N → A N vi) NP → Mary |John vii) DET → a |the |her |his viii) TV → bought |loves |misses ix) N → bike |jersey |mountain

NLTK context-free grammars

倖福魔咒の 提交于 2019-12-12 17:04:50
问题 I am just wondering how would you add an optional grammer in the rule >>> import nltk >>> nltk.app.rdparser() For example, the normal way to add a optional grammer is by putting it in parentheses: NP -> NP (PP) But in the program how would you do it? parentheses doesnt work. S Þ NP VP NP Þ NP PP | Det N VP Þ V NP PP PP Þ P NP Det Þ 'the' | 'a' N Þ 'man' | 'park' | 'dog' | 'boy' | 'girl' V Þ 'was' | 'saw' P Þ 'in' | 'under' | 'with' Thanks, Ray 回答1: NP -> NP | NP PP But note that, with this

Removing Left Recursion in a Context Free Grammar

流过昼夜 提交于 2019-12-12 15:02:03
问题 Trying to figure out removing left recursion in context free grammars. I'm used to certain forms, but this one has me a bit boggled. S --> S {S} S | (A) | a A --> {S} A | epsilon I also have to design a decent parser, which I can do. However, figuring out this left recursion (especially on the first one) has me confused. 回答1: There is an interesting Wikipedia article on left-recursion. It also has a section about removing left-recursion for non-context grammars. http://en.wikipedia.org/wiki

Why is this valid C? — ({123;}) evaluates to 123 [duplicate]

大憨熊 提交于 2019-12-12 07:58:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: in what versions of c is a block inside parenthesis used to return a value valid? The following is a type-safe version of a typical MAX macro (this works on gcc 4.4.5): #define max(a,b) \ ({ __typeof__ (a) _a = (a); \ __typeof__ (b) _b = (b); \ _a > _b ? _a : _b; }) Here, we see that this expression, max(a,b) returns the result of the expression _a > _b ? _a : _b; even though this expression is in a block. So, I

How to Prove a Grammar is ambiguous? S -> (S)|SS|()

五迷三道 提交于 2019-12-12 05:14:07
问题 I am rather new to grammars, and was wondering if someone could help me determine using a parse tree how this grammar below is ambiguous? I know that it needs to have two different strings that can be created. S -> (S)|SS|() I can def convert it to chomsky normal form and greibach, but ambiguity is perplexing to me with these. 回答1: The easiest way to prove a grammar ambiguous is to find a sentence with two different parse trees. (Or two different rightmost derivations, which is exactly the