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

LL(1) parser implemented with stack: how to build AST?

I am currently building a parser by hand. It is a LL(1) parser. At the moment, it is a great recognizer: its function parse(List tokens) decides whether or not tokens is a member of the language or not. Now, I want to build the corresponding AST for…
8
votes
1 answer

Formal Context Free Grammar From Context Free Language

How can a formal context free grammar be generated for the following language: {ai bjck | i != j or j != k} I have following productions but can't understand it: S->AX | YC unequal b’s c’s or a’s b’s A-> aA | e …
Light
  • 199
  • 1
  • 3
  • 9
8
votes
1 answer

Making a Grammar LL(1)

I have the following grammar: S → a S b S | b S a S | ε Since I'm trying to write a small compiler for it, I'd like to make it LL(1). I see that there seems to be a FIRST/FOLLOW conflict here, and I know I have to use substitution to resolve it, but…
7
votes
3 answers

Context-free grammar describing regular expressions?

I'm trying to write a regular expression engine. I'd like to write a recursive descent parser by hand. What would a context-free grammar without left recursion for the language of regular expressions (not the languages that can be described by…
wkf
  • 842
  • 9
  • 17
7
votes
1 answer

how to remove Circular Dependency in FOLLOW set

Consider a short gramma bellow S -> Bc | DB B -> ab | cS D -> d | epsilon The FIRST set is FIRST(S) ={a,c,d} FIRST(B) = { a,c } FIRST(D)= { d, epsilon } in it the Follow(S)={ Follow(B) } and Follow(B) ={ c , Follow(S) } my question is that how…
Zabi
  • 845
  • 2
  • 9
  • 15
7
votes
1 answer

Using PLY to parse SQL statements

I know there are other tools out there to parse SQL statements, but I am rolling out my own for educational purposes. I am getting stuck with my grammar right now.. If you can spot an error real quick please let me know. SELECT = r'SELECT' FROM =…
sampwing
  • 1,238
  • 1
  • 10
  • 13
7
votes
1 answer

In a tree-sitter grammar, how do I match strings except for reserved keywords in identifiers?

This might be related to me not understanding the Keyword Extraction feature, which from the docs seems to be about avoiding an issue where no space exists between a keyword and the following expression. But say I have a fairly standard identifier…
ahelwer
  • 1,441
  • 13
  • 29
7
votes
1 answer

What are [Yield, Await, In, Return] in EcmaScript grammar

Many productions in EcmaScript are given with the following "modifiers": [Yield, Await, In, Return] Here are a few examples: ArrayLiteral[Yield, Await]: ... ElementList[Yield, Await]: ... AssignmentExpression[+In, ?Yield, ?Await] I've searched…
7
votes
1 answer

Why is this valid C? --- ({123;}) evaluates to 123

Possible Duplicate: in what versions of c is a block inside parenthesis used to return a value valid? The following is a type-safe version of a typical MAX macro (this works on gcc 4.4.5): #define max(a,b) \ ({ __typeof__ (a) _a = (a); \ …
vardhan
  • 75
  • 6
7
votes
2 answers

Keeping State in a Stateless world

I am converting a context-free grammar into Greibach Normal Form (GNF). The main transformation (from Hopcroft & Ullman) is a sequence of iterations over the indexed variables of the grammar. It is essentially "stateless". I have implemented it as a…
emi
  • 5,380
  • 1
  • 27
  • 45
7
votes
0 answers

LL(1) to LR(1) Transformation

I recently finished writing a recursive-descent parser for this LL(1) grammar (which generates a tiny subset of XML): document ::= element EOF element ::= < elementPrefix elementPrefix ::= NAME attribute elementSuffix attribute ::= NAME = STRING…
Fiery Phoenix
  • 1,156
  • 2
  • 16
  • 30
7
votes
3 answers

Grammar production class implementation in C#

Grammar by definition contains productions, example of very simple grammar: E -> E + E E -> n I want to implement Grammar class in c#, but I'm not sure how to store productions, for example how to make difference between terminal and non-terminal…
dfens
  • 5,413
  • 4
  • 35
  • 50
7
votes
1 answer

Common Lisp: A good way to represent grammar rules?

This is a Common Lisp data representation question. What is a good way to represent grammars? By "good" I mean a representation that is simple, easy to understand, and I can operate on the representation without a lot of fuss. The representation…
Roger Costello
  • 3,007
  • 1
  • 22
  • 43
7
votes
1 answer

how to parse Context-sensitive grammar?

CSG is similar to CFG but the reduce symbol is multiple. So, can I just use CFG parser to parse CSG with reducing production to multiple terminals or non-terminals? Like 1. S → a bc 2. S → a S B c 3. c B → W B 4. W B → W X 5. W X → B X 6. B X → B…
qdwang
  • 425
  • 1
  • 4
  • 13
7
votes
2 answers

Left recursion in LR(1) parsers

Can a LR(1) parser parse a grammar of this type? S -> SA | A A -> aSb | ab I'm trying to write a Java program that implements this type of parser, but I only get the right results on a grammars without left recursion.
SegFault
  • 2,020
  • 4
  • 26
  • 41