Questions tagged [lr-grammar]

LR(k) grammars are grammars that can be parsed bottom-up from the left-to-right, producing a rightmost derivation, using k tokens of lookahead. LR(k) parsers are among the most powerful deterministic parsers available, but are often too large to use in practice.

178 questions
3
votes
1 answer

How can an LR(0) parser ever leave state 0?

I've read Wikipedia's explanation at least a dozen times, but I'm still confused at how an LR(0) parser ever leaves state 0. Wikipedia's example, with its explanation, says: The parser starts out with the stack containing just the initial state…
user541686
  • 205,094
  • 128
  • 528
  • 886
3
votes
1 answer

How can I resolve a reduce reduce conflict:

The following (simplified) Bison grammar produces a reduce reduce conflict: expr : '(' expr ')' | ID | fn ; arg_list : ID | arg_list ID ; fn : '(' ')' fnbody | '(' arg_list ')' fnbody …
Aaron Yodaiken
  • 19,163
  • 32
  • 103
  • 184
2
votes
1 answer

LR(1) item sets for left recursive grammar

I read several papers about creating LR(1) item sets, but none of them pertained to left recursive grammars, such as those used for parsing expressions. If I had the following grammar, E -> E + T | T T -> T * F | F F -> (E) | NUMBER How would I go…
ishaangupte
  • 248
  • 2
  • 11
2
votes
1 answer

Epsilon(ε) productions and LR(0) grammars and LL(1) grammars

At many places (for example in this answer here), I have seen it is written that an LR(0) grammar cannot contain ε productions. Also in Wikipedia I have seen statements like: An ε free LL(1) grammar is also SLR(1). Now the problem which I am facing…
Abhishek Ghosh
  • 597
  • 7
  • 18
2
votes
1 answer

Why are all LL(1) grammars LR(1)?

It's a widely-known fact that any LL(1) grammar is also LR(1), but I can't seem to find a rigorous proof of this anywhere. I've heard some high-level overviews of the proof (for example, that since an LL(1) grammar has its productions determined…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2
votes
1 answer

Is QML Grammar LALR(1)?

Here is a QML grammar (extracted from https://github.com/kropp/intellij-qml/blob/master/grammars/qml.bnf): /* identifier, value, integer and float are terminals */ qml ::= object /* Simplified */ object ::= type body body ::= '{'…
Swordow
  • 35
  • 6
2
votes
2 answers

LR(0)/SLR/LR(1) parsing - how a production is chosen?

I'm trying to wrap my head around parser theory, and I keep finding the same example in different sources. The grammar goes approximately like this (simplified): E = T E = E + T T = 0..9 So supposedly a string 2 + 2 will be parsed as such ("|"…
Vindicar
  • 354
  • 2
  • 9
2
votes
1 answer

Display valid LR(0) items

I have to create a C++ program to display the valid LR(0) items in SLR parsing in compiler design. Till now I am able to take the grammar as an input from the user and find its closure. But i am not able to proceed further with the goto…
Antrromet
  • 15,294
  • 10
  • 60
  • 75
2
votes
1 answer

Is this grammar (for a small subset of Lua) ambiguous?

I have the following grammar, which is a small subset of "The Complete Syntax of Lua": chunk -> | chunk stat. stat -> var `=´ exp. var -> Name | exp `[´ exp `]´. exp -> var | exp `(´ exp `)´ | `(´ exp `)´. According to a context-free grammar…
user200783
  • 13,722
  • 12
  • 69
  • 135
2
votes
1 answer

Understand whether a grammar is LR(1) with no parsing table

I've found out an exercise that require a trick to understand whether a grammar is LR(1) with no parsing table operations. The grammar is the followed: S -> Aa | Bb A -> aAb | ab B -> aBbb | abb Do you know what is the trick behind? Thanks, :)
kamauz
  • 153
  • 7
2
votes
1 answer

Is it possible to transform this grammar to be LR(1)?

The following grammar generates the sentences a, a, a, b, b, b, ..., h, b. Unfortunately it is not LR(1) so cannot be used with tools such as "yacc". S -> a comma a. S -> C comma b. C -> a | b | c | d | e | f | g | h. Is it possible to transform…
user200783
  • 13,722
  • 12
  • 69
  • 135
2
votes
1 answer

Constructing AST during LR parsing

I have written an LR(1) parser that can successfully parse strings in the language of my grammar into a Concrete Syntax Tree, but I am now trying to construct an Abstract Syntax Tree. I am using an inheritance design for my AST nodes: struct ASTNode…
Sarathi
  • 1,023
  • 1
  • 14
  • 22
2
votes
0 answers

Is it possible to parse LR(1) grammars without constructing the parse table?

Usually LR(1) parsers construct the parse table offline, but this is not possible in the case of extensible syntax. Incrementally rebuilding the parse table is possible, but requires an expensive "garbage collection" step. A possibility that…
klkblake
  • 377
  • 2
  • 9
2
votes
1 answer

How do I translate LR(1) Parse into a Abstract syntax tree?

I have coded a table driven LR(1) parser and it is working very well however I am having a bit of a disconnect on the stage of turing a parse into a syntax tree/abstract syntax tree. This is a project that I m very passionate about but I have really…
2
votes
4 answers

How to handle x*, x+, or x? regex-like operators in an LR parser?

I have implemented recursive descent and PEG-like parsers in the past, where you could do things like this: Path -> Segment+ Segment -> Slash Name Segment -> / Name -> /\w+/ Slash -> / where Segment+ means "match one or more Segment" and there's a…
Lance
  • 75,200
  • 93
  • 289
  • 503