Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created June 4, 2014 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save practicingruby/135c7ef6c933e432bdde to your computer and use it in GitHub Desktop.
Save practicingruby/135c7ef6c933e432bdde to your computer and use it in GitHub Desktop.

A LALR parser is a parser that:

  • Parses text in a single direction. Backtracking is avoided by looking ahead in the input stream before deciding how to parse a single token. (LA = Look-ahead)

  • Parses text starting from the left-most side of the input stream. (L=Left-to-right input processing)

  • Builds a parse-tree from the bottom up by repeatedly attempting to match the right side of grammar rules, until the full input stream is used up. For example, parsing the string X + Y * Z would match the variable names first, then the multiplication expression, then finally the addition expression. (R=Right-most derivation)

To implement a LALR parser, it is necessary to build a very low-level finite state machine, which is quite difficult to do by hand. Parser generators like Yacc/Racc allow you to specify the grammar you need to parse at a very high level and then generate the state transition tables for you.

LALR parsers are powerful enough to parse the grammars for many programming languages, but because of an optimization they use to condense their lookup tables, can generate conflicts in certain edge cases that arise in complex grammars.

Because of their complexity, LALR parsers can be hard to understand and hard to debug. The use of parser generators like Yacc/Racc mitigate this somewhat, but not entirely.

Further reading:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment