Formal grammar and arity

I have the following grammar:S --> LR .L --> aL .R --> bR .This grammar generates the language a^n b^k, where n,k > 0.I want a grammar that generates the language a^n b^n where n>0, somy goal is to obtain a grammar in order to ensure that the number of a is always equal of b, but still keeping the non-terminals L and R.Is there a way to do this?...Read more

BNF grammar that generates language L

I spend a lot of time but still can't understand how to solve this. I have the answer. Can you tell me how he come up with it? Is there a specific rules or standard construct I can follow? I know the rules for union and concatenation but not when it's reversed....Read more

Tips for creating "Context Free Grammar"

I am new to CFG's,Can someone give me tips in creating CFG that generates some languageFor example L = {am bn | m >= n} What I got is: So -> a | aSo | aS1 | e S1 -> b | bS1 | 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....Read more

grammar - Why the need for terminals? Is my solution sufficient enough?

I'm trying to get my head around context free grammars and I think I'm close. What is baffling me is this one question (I'm doing practise questions as I have an exam in a month's time):I've come up with this language but I believe it's wrong. S --> aSb | A | BA --> aA | ΣB --> bB | ΣApparently this is the correct solution:S --> aSb | aA | bBA --> aA | ΣB --> bB | ΣWhat I don't quite understand is why we have S --> aSb | aA | bB and not just S --> aSb | A | B. What is the need for the terminals? Can't I just call A inste...Read more

grammar - chomsky hierarchy in plain english

I'm trying to find a plain (i.e. non-formal) explanation of the 4 levels of formal grammars (unrestricted, context-sensitive, context-free, regular) as set out by Chomsky.It's been an age since I studied formal grammars, and the various definitions are now confusing for me to visualize. To be clear, I'm not looking for the formal definitions you'll find everywhere (e.g. here and here -- I can google as well as anyone else), or really even formal definitions of any sort. Instead, what I was hoping to find was clean and simple explanations that d...Read more

Writing GNF grammar for a CFL

Hello I would like to ask you this question. I was supposed to compute (manually) a grammar in Greibach Normal Form, that generates the languageL = {ai bj ck | i + j = 2k and k >= 1} I really have no idea. Can someone please help me? Thanks in advanceChriss...Read more

grammar - Theory: Is the given language context free or not?

In a recent test , I was asked to recognize if the below language is context free:According to me, it is context free, and can be accepted by the below context free grammar, where S is the start symbol and Y is a non-terminal:However, my answer was considered to be wrong and so apparently this language is not context free.I'm confident about my answer, but the response has got me confused. Is my understanding correct? Please let me know if I've missed something....Read more

Relation between Left factoring a grammar and removing epsilon

Let's say I'd using below grammar for compiler S -> a | aBif I perform left factoring on it, it would be like (e is epsilon) S -> aC C -> B | e then I want to remove epsilon which ends up being like S -> a | aC C -> Bnotice that it looks like I again need to perform left factoring, and doing so infinitely left factoring and removing epsilon back and forth. Am I doing something wrong ?? Is it require to remove both left factoring and epsilon on grammar for compiler?...Read more

lexical elements of grammar

There is 4 lexical elements of grammarG = (S, N, T, P)Where G = Grammar, S = Start Symbol, N = Non-Terminals, T = Terminals, P = Production rulesI wanted to know if N is always equal with P because as I know P are lexemes which may replace with other lexemesSo in this example:<program> --> <stmts><stmts> --> <stmt> | <stmt> ; <stmts><stmt> --> <var> = <expr><var> --> a | b | c | d<expr> --> <term> + <term> | <term> - <term><term> -->...Read more

Formal Languages - Grammar

I am taking a Formal Languages and Computability class and am having a little trouble understanding the concept of grammar. One of my assignment questions is this:Take ∑ = {a,b}, and let na(w) and nb(w) denote the number of a's and b's in the string w, respectively. Then the grammar G with productions:S -> SSS -> λS -> aSbS -> bSagenerates the language L = {w: na(w) = nb(w)}.1) The language in the example contains an empty string. Modify the given grammar so that it generates L - {λ}.I am thinking that I should modify the condition ...Read more

Can a context-sensitive grammar have an empty string?

In one of my cs classes they mentioned that the difference between context-free grammar and context-sensitive grammar is that in CSG, then the left side of the production rule has to be less or equal than the right side. So, one example they gave was that context-sensitive grammars can't have an empty string because then the first rule wouldn't be satisfied. However, I have understood that regular grammars are contained in context-free, context-free are contained in context-sensitive, and context-sensitive are contained in recursive enumerable ...Read more

A grammar that accepts the empty set by the rule S->S

This was a homework assignment problem which I know I have incorrectly answered. I gave:S -> ''meaning that S yields the empty string. I know that the empty set and empty string are not the same. According to my professor, the answer is:S -> SNow, that answer seems strange to me:It will never terminate.It isn't so much a language as the absence of one.I understand from a strictly mathematical standpoint, I'm not going to get anywhere with number two. However, is it required for a language to terminate? Having a language that CAN go o...Read more

yacc - Error recovery in an LALR(1) grammar

I'm using some parser and lexer generating tools (similar to Lex and Bison, but for C#) to generate programs that parse strings into abstract syntax trees that can later be evaluated.I wanted to do error recovery (i.e. report in the produced abstract sentence tree that there are missing tokens and such). I had two approaches in mind to structuring the generated grammars, and I was wondering which approach was better/more flexible/wouldn't have conflicts (the .y and .lex files are generated based on a description of the calculator).The calculat...Read more

yacc - Add error checking via production rules to LALR(1) grammar to handle all inputs

I have a grammar that represents expressions. Let's say for simplicity it's:S -> EE -> T + E | TT -> P * T | PP -> a | (E)With a, +, *, ( and ) being the letters in my alphabet.The above rules can generate valid arithmetic expressions containing parenthesis, multiplication and addition using proper order of operations and associativity.My goal is to accept every string, containing 0 or more of the letters of my alphabet. Here are my constraints: The grammar must "accept" all strings contained 0 or more letters of my alphabet.New ...Read more