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
7
votes
2 answers

Using Ogden’s Lemma versus regular Pumping Lemma for Context-Free Grammars

I'm learning the difference between the lemmata in the question. Every reference I can find uses the example: {(a^i)(b^j)(c^k)(d^l) : i = 0 or j = k = l} to show the difference between the two. I can find an example using the regular lemma to…
XML Slayer
  • 1,530
  • 14
  • 31
7
votes
4 answers

How is this grammar LR(1) but not SLR(1)?

I have the following grammar, which I'm told is LR(1) but not SLR(1): S ::= a A | b A c | d c | b d a A ::= d I don't understand why this is. How would you prove this?
7
votes
1 answer

How can I define an INI file grammar using the BNFC?

http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/ how should I write my labeled BNF to get BNFC to generate a INI parser for me? I have only gotten so far o__O! entrypoints File ; comment "#" ; token ID ( letter | digit | ["-_'"] )+…
Cetin Sert
  • 4,497
  • 5
  • 38
  • 76
6
votes
8 answers

Why do on-line parsers seem to stop at regexps?

I've been wondering for long why there doesn't seem to be any parsers for, say, BNF, that behave like regexps in various libraries. Sure, there's things like ANTLR, Yacc and many others that generate code which, in turn, can parse a CFG, but there…
Henrik Paul
  • 66,919
  • 31
  • 85
  • 96
6
votes
3 answers

How do I find the language from a regular expression?

How would I find the language for the following regular expressions over the alphabet {a, b}? aUb* (ab*Uc) ab*Ubc* a*bc*Uac EDIT: Before i get downvoted like crazy, i'd appreciate it if someone could show me the steps towards solving these…
user814447
6
votes
2 answers

Dutch Grammar in python's NLTK

I am working on a Dutch corpus and I want to know if NLTK has dutch grammar embedded in it so I can parse my sentences? In general does NLTK only work on English? I know that it has the Alpino dutch copora, but there is no indication that the…
Hossein
  • 40,161
  • 57
  • 141
  • 175
6
votes
3 answers

Closures and Context Free Grammars

I'm looking over my syllabus for my theoretical computer science class and within the heading of Context Free Grammars it lists "closure properties". I have looked through my textbook on this subject and found quite little. The little it does have…
Saterus
  • 539
  • 2
  • 5
  • 11
6
votes
3 answers

Generating n statements from context-free grammars

So not to reinvent the wheel, I would like to know what has already been done about generating random statements from a context-free language (like those produced by yacc, etc.). These grammars are primarily for parsing, but maybe someone has done…
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
6
votes
1 answer

How to test for texts not fitting an Instaparse-grammar (Clojure)?

I wrote a project for parsing strings using context-free grammar in Instaparse (Clojure). Now I'd like to test several input-Strings for their parsing results. Some input strings might not fit into the grammar. So far I only tested for "parsed…
Edward
  • 4,453
  • 8
  • 44
  • 82
6
votes
2 answers

Is the language A = {0^n 1^n 0^n} context free?

I was just putting some thought into different languages (as I'm reviewing for final exams coming up) and I can not think of a valid pushdown automata to handle the language A = {0^n 1^n 0^n | n >= 0}. This is not a context-free language, am I…
Tony
  • 107
  • 2
  • 6
6
votes
3 answers

What is the difference between EBNF and CFG

I understand that EBNF can be used to express Context Free Grammar, but is there any difference between the two? I am asking because there are questions that ask to convert EBNF to CFG, but as of my current understanding, they look same. Therefore,…
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
6
votes
1 answer

How to enumerate the strings of a context-free grammar?

What algorithm do you use to enumerate the strings generated by a context-free grammar? It seems doable when there is no recursion, but I can't figure out how to do it in the general case, which might contain all kinds of (possibly indirect)…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
3 answers

Unambiguous grammar for exponentiation operation

E -> E+T | E-T | T T -> T*F | T/F | F F -> i | (E) How can I modify this grammar to allow an exponentiation operation ^ so that I can write i+i^i*i? Since we know that order of operations is higher for ^ then all I know is that I must make it right…
6
votes
1 answer

Can a left associative operator be expressed in a way such that top-down LL(1) parsers can understand?

I was trying to implement a LL(1) top-down parser for a calculator language. It only allows us to sum, subtract, divide and multiply numbers. No parentheses. S -> A A -> B + A | B - A | B B -> int * B | int / B | int As this grammar…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
6
votes
1 answer

Is there a set way to determine ambiguity in a grammar?

We are learning about ambiguity in class, and the following grammar was given as an example of an ambiguous grammar. I just am not seeing how it is ambiguous. Is there a set pattern or method people use to determine ambiguity or is it just like a…