Questions tagged [context-free-grammar]

In formal language theory, a context-free grammar (CFG) is a grammar subject to a special constraint: that the left-hand side (LHS) consist of a single non-terminal symbol. CFGs are capable of representing the set of context-free languages (CFLs).

1364 questions
14
votes
1 answer

Are Ruby 1.9 regular expressions equally powerful to a context free grammar?

I have this regular expression: regex = %r{\A(? a\ga | b\gb | c)\Z}x When I test it against several strings, it appears to be as powerful as a context free grammar because it handles the recursion properly. regex.match("aaacaaa") #…
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
14
votes
5 answers

Are there tools to convert between ANTLR and other forms of BNF?

Are there any tools to convert ANTLR grammar syntax to and from other BNF syntaxes? There are several forms Backus-Naur Form (BNF, EBNF, ABNF, W3C-BNF, XBNF...) with specification, e.g. see this list. The ANTLR grammar syntax only seems to be…
Jakob
  • 3,570
  • 3
  • 36
  • 49
14
votes
2 answers

Step by step elimination of this indirect left recursion

I've seen this algorithm one should be able to use to remove all left recursion. Yet I'm running into problems with this particular grammar: A -> Cd B -> Ce C -> A | B | f Whatever I try I end up in loops or with a grammar that is still indirect…
Flion
  • 10,468
  • 13
  • 48
  • 68
13
votes
2 answers

Is { w | w <> w^R } over the alphabet {0,1} a context-free language?

I'd really love your help with this deciding whether the language of all words over the alphabet {0,1} that can't be read from both sides the same way, { w | w <> wR }, is a context-free language (that is, it can be transformed into specific grammar…
Numerator
  • 1,389
  • 3
  • 21
  • 40
13
votes
1 answer

How to construct parsing table for LL(k>1)?

On the web, there is a lot of examples showing how to construct parsing tables for a context-free grammar from first/follow sets for LL(1) parser. But I haven't found anything useful related to k>1 cases. Even wikipedia gives no info about this. I…
Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
13
votes
2 answers

CFG / PEG used for Code completion?

I'm wondering if it's possible to use a CFG or PEG grammar as a basis for code completion directly without modification. I have heard that code completion is in IDE's is sometimes manipulated and massaged or even hard coded so that it performs…
BefittingTheorem
  • 10,459
  • 15
  • 69
  • 96
13
votes
1 answer

How can I prove that derivations in Chomsky Normal Form require 2n - 1 steps?

I'm trying to prove the following: If G is a Context Free Grammar in the Chomsky Normal Form, then for any string w belongs L(G) of length n ≥ 1, it requires exactly 2n-1 steps to make any derivation of w. How would I go about proving this?
12
votes
3 answers

Converting grammar to Chomsky Normal Form?

Convert the grammar below into Chomsky Normal Form. Give all the intermediate steps. S -> AB | aB A -> aab|lambda B -> bbA Ok so the first thing I did was add a new start variable S0 so now I have S0 -> S S -> AB | aB A -> aab|lambda B ->…
tehman
  • 828
  • 2
  • 11
  • 34
12
votes
2 answers

How to determine if a language is recursive or recursively enumerable?

I have to determine whether a language (for example L={a^n b^m c^s | 0<=n<=m<=s}) is regular, context-free, recursive, recursively enumerable or none of them. I know how to determine if a language is regular (find a DFA or regular expression that…
12
votes
2 answers

How do LL(*) parsers work?

I cannot find any complete description about LL(*) parser, such as ANTLR, on Internet. I'm wondering what is the difference between an LL(k) parser and an LL(*) one and why they can't support left-recusrive grammars despite their flexibility.
Fabio Strocco
  • 300
  • 4
  • 14
12
votes
1 answer

How to find FIRST and FOLLOW sets of a recursive grammar?

Suppose I have the following CFG. A -> B | Cx | EPSILON B -> C | yA C -> B | w | z Now if I try to find FIRST(C) = FIRST(B) U FIRST(w) U FIRST(z) = FIRST(C) U FIRST(yA) U {w, z} That is, I'm going in a loop. Thus I assume I have to…
Sach
  • 10,091
  • 8
  • 47
  • 84
12
votes
1 answer

Is there a fast algorithm to determine the godel number of a term of a context free language?

Suppose we have a simple grammar specification. There is a way to enumerate terms of that grammar that guarantees that any finite term will have a finite position, by iterating it diagonally. For example, for the following grammar: S ::=…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
11
votes
2 answers

what are these arrow operators in context free grammar?

I'm studying context free grammar and I'm curious what the arrow with the star and the arrow without the star mean in parts f and g where: f is false. g is true.
jfisk
  • 6,125
  • 20
  • 77
  • 113
11
votes
1 answer

NLTK ViterbiParser fails in parsing words that are not in the PCFG rule

import nltk from nltk.parse import ViterbiParser def pcfg_chartparser(grammarfile): f=open(grammarfile) grammar=f.read() f.close() return nltk.PCFG.fromstring(grammar) grammarp = pcfg_chartparser("wsjp.cfg") VP =…
Kaushal
  • 111
  • 4
11
votes
3 answers

Synthesized vs Inherited Attributes

How can I find if an attribute is synthesized or inherited from the productions of a grammar? I guess for that the attribute must be predefined in the problem -- if its value depends on child or parent nodes. But is there a way to analyse if an…
1 2
3
90 91