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
6
votes
1 answer

LR(1) - Items, Look Ahead

I am having difficulties understanding the principle of lookahead in LR(1) - items. How do I compute the lookahead sets? Say for an example that I have the following grammar: S -> AB A -> aAb | b B -> d Then the first state will look like this: S…
mrjasmin
  • 1,230
  • 6
  • 21
  • 37
6
votes
2 answers

Horizontal Markovization

I have to implement horizontal markovization (NLP concept) and I'm having a little trouble understanding what the trees will look like. I've been reading the Klein and Manning paper, but they don't explain what the trees with horizontal…
Josh Bradley
  • 4,630
  • 13
  • 54
  • 79
6
votes
1 answer

Grammar spec resolving Shift/Reduce conflicts

I'm using Jison (Bison) to create a simple markup language. I'm clearly new to this, but slight variations are working very well. I just don't understand the source of the S/R conflict. It doesn't seem matter that 'Text' is returned by two lexer…
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
6
votes
3 answers

Extension to CFG, what is it?

Consider the following extension to context-free grammars that permits rules to have in the left-hand side, one (or more) terminal on the right side of the non-terminal. That is, rules of the form: A b -> ... The right-hand side may be anything,…
false
  • 10,264
  • 13
  • 101
  • 209
6
votes
8 answers

How can I construct a grammar that generates this language?

I'm studying for a finite automata & grammars test and I'm stuck with this question: Construct a grammar that generates L: L = {a^n b^m c^m+n|n>=0, m>=0} I believe my productions should go along this lines: S->aA | aB B->bB | bC C->cC |…
andandandand
  • 21,946
  • 60
  • 170
  • 271
5
votes
2 answers

How to parse comments with EBNF grammars

When defining the grammar for a language parser, how do you deal with things like comments (eg /* .... */) that can occur at any point in the text? Building up your grammar from tags within tags seems to work great when things are structured, but…
Jagu
  • 2,471
  • 2
  • 22
  • 26
5
votes
3 answers

What is the language of this deterministic finite automata?

Given: I have no idea what the accepted language is. From looking at it you can get several end results: 1.) bb 2.) ab(a,b) 3.) bbab(a, b) 4.) bbaaa
tehman
  • 828
  • 2
  • 11
  • 34
5
votes
2 answers

How to express a context free design grammar as an internal DSL in Python?

[Note: Rereading this before submitting, I realized this Q has become a bit of an epic. Thank you for indulging my long explanation of the reasoning behind this pursuit. I feel that, were I in a position to help another undertaking a similar…
Jedidiah Hurt
  • 812
  • 9
  • 20
5
votes
1 answer

Convert EBNF Grammar to Context-Free Grammar

I have to write a JavaCUP specification, and I've been given an EBNF grammar. However, I don't know how to convert between the two. I've heard the basic ideas, but I don't really understand what I need to change, what would be the "terminals",…
muttley91
  • 12,278
  • 33
  • 106
  • 160
5
votes
3 answers

Tool for drawing parse trees?

Does anyone have a good tool for drawing parse trees arising from a context-free grammar? There is this question, but it dealt specifically with finite automata instead of parse trees. I've been using graphviz, but it's kind of annoying to have to…
Xodarap
  • 11,581
  • 11
  • 56
  • 94
5
votes
1 answer

Is there an extension to railroad diagrams to capture exceptions?

Railroad diagrams are a popular method to visualize context-free grammars and you can map Backus-Naur Form to these diagrams. But some variants of BNF, for instance W3C-BNF allow exceptions (as context-free languages are not closed under difference,…
Jakob
  • 3,570
  • 3
  • 36
  • 49
5
votes
1 answer

Need a simple Bison grammar for HTML

I've looked at the Bison help and have written this, but I'm not sure it's completely correct. Also i need an yylex() that handle Lexical Analyzer (It should be Flex tool). I know some basic things about context-free grammars. But i don't know how…
Jalal
  • 6,594
  • 9
  • 63
  • 100
5
votes
2 answers

Context free grammar for balanced parethesis

I want to design a context free grammar for the following language: L = { w e {(, )}* | w is balanced} I proposed the following grammar: S -> (S)S | E Whereas the proposed solution in the lecture is: S -> (S) | SS | E I am not able to figure…
yogeshagr
  • 819
  • 2
  • 11
  • 20
5
votes
3 answers

Is concatenation of a non regular language with a regular language always not regular?

I'd like to know if the concatenation between two language (one regular and the other not) is always not regular or it may happen that the output is a regular language. Thanks.
5
votes
1 answer

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…