Classify the grammar according to the Chomsky hierarchy - grammar

S ->bA|aB A ->a|aS|bAA B ->b|bS|aBB
I thought this is the type of context-free grammar type, but I am not sure why it's not unrestricted grammar or context-sensitive grammar. In my opinion, context-free it's a special kind of context-sensitive or unrestricted grammar. I don't know how to explain why it's not the other two types.
If someone can help with it, thank you!

The Chomsky hierarchy is a hierarchy, not a partition. It was originally proposed as a series of restrictions, with regular grammars being the most restricted. So each level is a restricted subset of the previous level, and that's how you should think of it
Or you could think of it like the taxonomic hierarchy: a kangaroo is a marsupial, a mammal, a vertebrate and an animal. That's a containment series, in which each level refines the previous one; the kangaroo doesn't stop being a mammal when it's classified as a marsupial.

Related

How to find the language of a NFA

As above, I have a transition graph, but I'm not sure how to find the language of it, seems to me that there are a lot of possibilities, but I must be misunderstanding somehow. My understanding is that any word that leads from the initial to the final state is accepted. Surely there are a lot of different ways to achieve this. aab, ab, abb, abbaab. As I understand it, a language is a set of all the possible words, but if there are a vast amount of possible words, how can you find the language? Im a first year University student, this is part of my homework, but I'm not just trying to get you to do it for me, I want to understand it - if this doesn't make sense/ is in the wrong place I apologise in advance, thanks. Here's my graph
Some regular languages are finite - they contain a finite number of strings. When you have a finite number of things, that means you can count them all and get to the end eventually; you can write them all down if they're words in a language. Writing down all the words in a language is a way of giving an extensive definition of a language.
However - there are languages which are not finite. They do not contain any number of words you can count from beginning to end, or ever completely write down. If you think of all natural numbers (1, 2, …, 100, …) as strings in the language of decimal representations, clearly there are not finitely many of them. There are infinitely many. You cannot give extensive definitions of infinite languages (except, possibly, by suggestive use of the ellipsis, as I have done in my example). In these cases, you must describe the strings which are included and/or excluded and rely on the reader to deduce membership for any particular case.
Giving a finite automaton is one perfectly reasonable way of giving a criterion according to which membership in the language can be determined: run the string through the automaton and see if it is accepted. In this sense, asking what the language of a finite automaton is can be viewed as trivial: it accepts the language of strings that leave the finite automaton in an accepting state.
Another way of describing the language is to give a grammar or, for regular languages, a regular expression. These are not necessarily more or less helpful ways of describing a language than the finite automaton you already have is.
Typically, in coursework, when you are asked to describe the language of a finite automaton, you are probably being asked to give a plain, English-language definition - a simple one - of the strings, and possibly provide some set-builder notation. That's what we'll try to do here.
Your NFA loops in q0, accepting any number of a, until it sees at least one a. If it sees a b before an a, it crashes. After that, if it sees at least one b, it can accept; it can see any number of b, but after the initial run of a, it can never again see two a in a row. The machine accepts only if it ends with a b.
Taken together, this might be a description in plain English that is good for this language:
All strings that begin with at least one a, which end with b and which do not contain the substring aa after the first instance of b.
A regular expression for this language is aa*(bb*a)*.

Many instances of a terminal symbol in a BNF grammar

given a grammar like
<term>::= x[i]+exp(x[i]) | x[i]
<i>::= 1|2|3
Does a way exist to force the use of the same "i" in one solution of non terminal symbol ? So, I want to avoid solutions like x[1]+exp(2) or x[3]+exp(1)
Does a way exist to avoid that the same "i" is used in one solution of non terminal symbol ?So, I want to avoid solutions like x[1]+exp(1)
No, that's not possible with a context-free grammar.
This is essentially what "context-free" means. Every non-terminal in a production can be expanded independently without regard to the context in which it appears.
Of course, if i really only has three possible values, you can enumerate the finite number of legal productions, according to any definition of "legal" which you find convenient. But that gets really messy when the number of possibilities increases.
The most convenient solution is generally to accept the base syntax and check for concordance (or difference) in the associated semantic rule. That also allows for better error messages.

Is it, and if so why, wrong that these two regular grammars are different?

I'm tasked with writing a regular grammar based on a regular expression.
Given the regular expression a*b can be written as S -> b | aS
Is it incorrect that ba* as a regular grammar is S -> b | Sa?
I'm told the correct answer is in fact S -> bA, A -> ^| aA but I don't see the difference myself.
An explanation would be greatly appreciated!
IIRC, both your answer and the one being called "correct" are correct. See this. What you have constructed is a "left regular grammar", while the proponent(s) of the "correct" answer obviously prefer a "right regular grammar". There are other arbitrary rules that may be held more or less pedantically, like the "no empty productions" rule, but they don't really affect the class of regular languages, just the compactness of the grammar you use for a particular language, as your example highlights - a single production with two alternatives vs. two productions, one with a single clause, and one with two alternatives, one of which is empty.

How to prove that a grammar is ambigous/unambigous?

If we have the the grammer
how to correctly prove that this is unambigious? In my opinion it's hard to describe it so is there a mathematical or logic proof for that?
And then how to proove that L(G) = G? Next thing where it is hard for me to give a logic proof.
Hope somebody can help.
You can prove that a grammar is unambiguous by constructing a deterministic parser. You can prove that it is ambiguous by finding a sentence with two different parse trees (or, better said, with two different leftmost or rightmost derivations).
There is no definitive algorithm for producing such proofs, because the ambiguity of a grammar is undecidable.
I have no idea what you mean by proving that L(G) = G. That's clearly not true, because L(G) is a set of strings, while G is a grammar. The two objects are from different universes, so they cannot be equal. Perhaps you meant proving that some set of strings S is equal to L(G)? Again, this problem is, in general, undecidable, but in many useful cases you can construct such a proof. A common strategy is to use induction on the length of the string.

ANTLR AST and Visitor Pattern

I am trying to understand how to apply Visitor pattern once I have an AST generated from ANTLR Grammar. Is it different approach than traversing a tree?
For example, assume I have the following AST (From this question):
If I want to appened FUNDEF ids to all its VARDECL ids, my approach is to do a tree traversal to find all VARDECL inside BLOCK and add a new child with (FUNDEF id + old_name) and remove the old one.
Is this the correct approach or would somehow visitor pattern work best? If visitor pattern, what would be the sample code in this specific case?
Thanks in advance!
I doubt the visitor pattern would help here. It is used when you have a data structure and take this to every node for changes. A typical case for this as described in "Head First Design Patterns" by Freeman & Freeman is ordering a Coffee. This is your object that visits each compositor for additional information (milk, sugar).
Your original idea of using a tree walk to modify the AST is probably the best option you have.