Got a simple grammar that have to transform to LL(1) i've tried many solutions but removed the left recursion but it does not produce the same grammar.
The grammar is this:
X -> E $
E -> E E o
E -> n
Any idea how can i turn this right recursive? Is there any general algorithm?
Related
Consider the following ANTLR 4 grammar:
grammar Test;
start: e EOF;
e : e '+' e #op
| NUMBER #atom
;
NUMBER: [0-9]+;
Based on the disambiguation rules of ANTLR, in this case binary operators being left associative, the result of parsing the string 1+2+3 is ((1+2)+3). But there is another parse tree possible, namely (1+(2+3)), if you don't consider the ANTLR's default disambiguation rule.
Is there a way to get both parse trees in ANTLR? Or at least enabling a flag or something, so that it tells me that there was another parse tree and possibly print it?
Update
I understand that in ANTLR, this grammar is unambiguous, because binary operators are always left-associative, but I couldn't come up with another example. My whole point is that I'd like to get an warning (or something similar) whenever ANTLR tries to resolve the ambiguity. For example, in good old Yacc (Bison), if I have the same grammar:
s : e
;
e : e '+' e
| NUMBER
;
when generating the parser, I get the warning State 6 conflicts: 1 shift/reduce.
There's no ambiquity in this small grammar. There are 2 alts in e each with a definitive path. An ambiquity would be something like this:
e = a b | a c;
where a parser needs some lookahead to determine which path to take. But back to your parse tree question. What you want is to define a different associativity. Normally, all operators are left-associative by default leading to this parse tree:
defining the operator + to be right-associative like so:
grammar Example;
start: e EOF;
e : <assoc=right> e '+' e #op
| NUMBER #atom
;
NUMBER: [0-9]+;
leads to:
Update
In order to get notified whenever an ambiquity is found your error listeners reportAmbiguity function is triggered. Override that to do your own handling in this situation.
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, so
my 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?
In a.context free grammar, the derivations of L and R in S → L R are independent of each other. That is what "context free" means: the derivation of a non-terminal is not affected by the context in which the non-terminal occurs.
So if you want a grammar in which L and R must derive strings of equal length, it will have to be a context-sensitive grammar. No context-free grammar can do that.
Of course, there is a simple CFG for the language:
S →
S → a S b
I want to prove that this grammar is ambiguous, but I'm not sure how I am supposed to do that. Do I have to use parse trees?
S -> if E then S | if E then S else S | begin S L | print E
L -> end | ; S L
E -> i
You can show it is ambiguous if you can find a string that parses more than one way:
if i then ( if i then print i else print i ; )
if i then ( if i then print i ) else print i ;
This happens to be the classic "dangling else" ambiguity. Googling your tag(s), title & grammar gives other hits.
However, if you don't happen to guess at an ambiguous string then googling your tag(s) & title:
how can i prove that this grammar is ambiguous?
There is no easy method for proving a context-free grammar ambiguous -- in fact,
the question is undecidable, by reduction to the Post correspondence problem.
You can put the grammar into a parser generator which supports all context-free grammars, a context-free general parser generator. Generate the parser, then parse a string which you think is ambiguous and find out by looking at the output of the parser.
A context-free general parser generator generates parsers which produce all derivations in polynomial time. Examples of such parser generators include SDF2, Rascal, DMS, Elkhound, ART. There is also a backtracking version of yacc (btyacc) but I don't think it does it in polynomial time. Usually the output is encoded as a graph where alternative trees for sub-sentences are encoded with a nested set of alternative trees.
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 -> SS
S -> λ
S -> aSb
S -> bSa
generates 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 of L, something like:
L = {w: na(w) = nb(w), na, nb > 0}
That way, we indicate that the string is never empty.
2) Modify the grammar in the example so that it will generate L ∪ {anbn+1: n >= 0}.
I am not sure on how to do this one. Should that mean I make one more condition in the grammar, adding something like S -> aSbb?
Any explanation about these two questions would be greatly appreciated. I'm still trying to figure these grammar stuff out so I am not sure about my answers.
1) The question is about modifying the grammar to obtain a new language; so don't modify directly the language…
Your grammar generates the empty word because of the production:
S -> λ
So you could think of removing this production altogether. This yields the following grammar:
S -> SS
S -> aSb
S -> bSa
Unfortunately, this grammar doesn't generate a language (a bit like in induction, it misses an initial: there are no productions that only consist of terminals). To fix this, add the following productions:
S -> ab
S -> ba
2) Don't randomly try to add production rules in the hope that it's going to work. Here you want a's followed by b's. So the production rule
S -> bSa
must certainly disappear. Also, the rule
S -> SS
would produce, e.g., abab (try to see how this is obtained). So we'll have to remove it too. We're left with:
S -> λ
S -> aSb
Now this grammar generates:
λ
ab
aabb
aaabbb
etc. That's not bad at all! To get an extra trailing b, we could create a new non-terminal, say T, replace our current S by T, and add that trailing b in S:
T -> λ
T -> aTb
S -> Tb
I know that this is homework; I gave you the solutions to your homework: that's because, from the way you asked your question, it seems you're completely lost. I hope this answer will help you get on the right path!
Let n be any terminal.
Consider the following, presumably correct, representation of the kleene star over n:
N → n N | ε
(where ε is the empty terminal.)
Wikipedia says:
Every grammar in Chomsky normal form is context-free, and conversely, every context-free grammar can be transformed into an equivalent one which is in Chomsky normal form.
I cannot see how the above grammar could be transformed to CNF.
Is the grammar not context-free?
Is there in fact a way to represent it in CNF?
Fortunately, this can be written in CNF. Here is one such grammar:
S → ε | n | NA
N → n
A → n | NA
Therefore, the language is context-free.
Hope this helps!