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
9
votes
4 answers

Online resources for writing a parser-generator

I want to write a parser-generator for educational purposes, and was wondering if there are some nice online resources or tutorials that explain how to write one. Something on the lines of "Let's Build a Compiler" by Jack Crenshaw. I want to write…
Anand
  • 7,654
  • 9
  • 46
  • 60
8
votes
1 answer

Relationship between LR(0), LL(0), LALR(1), etc?

I'm really struggling to unterstand the relationship between: LR(0) LL(0) LALR(1) SLR(1) LR(1) LL(1) I'm pretty sure LALR(1) and SLR(1) are subsets of LR(1), but I'm lost about the others. Are they all exclusive? Is LL(0) a subset of…
7
votes
0 answers

Happy: Order of Productions Removes R/R Conflicts

I have a grammar that, depending on the order of productions, happy reports 3 reduce/reduce conflicts or none. The minimal example I can find is: %tokentype {Token} %token int { Int } '-' { Neg } both { Both } %nonassoc…
sfogarty
  • 71
  • 1
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
7
votes
1 answer

How to use Warshall's algorithm for transitive closure to determine canonical LR(1) parser closures?

I'm trying to implement Warshall's algorithm to quickly compute LR(1) closures. I think I understand how it works for LR(0): The nodes of the graph are LR items, like A → B • C The edges are "transitions" starting from A → B • C to C → • D The…
user541686
  • 205,094
  • 128
  • 528
  • 886
7
votes
1 answer

What is the closure of a left-recursive LR(0) item with epsilon transitions?

Let's say I have this grammar: A: ε | B 'a' B: ε | B 'b' What is considered to be the closure of the item A: • B 'a'? In other words, how do I deal with the epsilon transitions when figuring out closures?
user541686
  • 205,094
  • 128
  • 528
  • 886
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?
6
votes
1 answer

Preferring shift over reduce in parser for language without statement terminators

I'm parsing a language that doesn't have statement terminators like ;. Expressions are defined as the longest sequence of tokens, so 5-5 has to be parsed as a subtraction, not as two statements (literal 5 followed by a unary negated -5). I'm using…
Moritz Mahringer
  • 1,240
  • 16
  • 28
6
votes
1 answer

What is an LR(2) parser? How does it differ from an LR(1) parser?

I'm familiar with LR(1) parsers, which are usually taught in traditional compilers courses. I know that LR(2) parsers exist, but I've never seen one constructed before. How is an LR(2) parser constructed? How does it differ from an LR(1) parser?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
6
votes
1 answer

Isn't an LR(0) parser using lookaheads as well?

An LL(1)-parser needs a lookahead-symbol for being able to decide which production to use. This is the reason why I always thought the term "lookahead" is used, when a parser looks at the next input token without "consuming" it (i.e. it can still be…
fishbone
  • 3,140
  • 2
  • 37
  • 50
6
votes
2 answers

LR(1) parser state size still an issue?

Historically, LALR(1) parsers were preferred over LR(1) parsers because of resource requirements required by the large number of states generated by LR(1) parsers. It's hard to believe that this continues to be an issue in today's computing…
tgoneil
  • 1,522
  • 3
  • 19
  • 30
6
votes
2 answers

Building parse trees with shift-reduce parsing

I'm experimenting with parsing on my free time, and I wanted to implement a shift-reduce parser for a very very simple grammar. I've read many online articles but I'm still confused on how to create parse trees. Here's an example of what I want to…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
5
votes
1 answer

Is every LL(1) grammar also an LR(0) grammar?

I know that every LL(1) is also an LR(1). But what about the relationship between LL(1) and LR(0), can a LL(1) be a LR(0) as well?
bopia
  • 59
  • 1
  • 6
5
votes
1 answer

LR(k) to LR(1) grammar conversion

I am confused by the following quote from Wikipedia: In other words, if a language was reasonable enough to allow an efficient one-pass parser, it could be described by an LR(k) grammar. And that grammar could always be mechanically transformed…
user1095108
  • 14,119
  • 9
  • 58
  • 116
4
votes
1 answer

Does there exist an LR(k) grammar with no LL(1) equivalent

I haven't been able to find an answer to this yet. Are there grammars that are context free and non-ambiguous that can't be converted to LL(1)? I found one production that I couldn't figure out how to convert into LL(1): the parameter-type-list…
1
2
3
11 12