top of page
Search
  • Writer's picturedavidcarew19

What is a lexer and parser?

I would definitely be rusty on this stuff, because I do not program computer language compilers, nor interpreters. Here’s what I (sort of) remember:


A lexer is different than a parser, and they are (or used to be) often coded separately—as two separate “passes” of a compiler, for example. Parsers do syntax analysis, to be sure that the syntax rules of a language are followed— for example if every opening parenthesis ‘(‘ character must be balanced by a later closing parenthesis ‘)’ character, it is the parser that notices and kicks an error when parentheses “don’t balance”.


By contrast, lexers analyze computer programming text for semantic meaning. If you meant to assign the result of expression x + y to the variable A, but you accidentally wrote only:

A = x +

and omitted the ‘y’ or whatever other expression which was supposed to be added to ‘x’ in order to form a value to be assigned to ‘A’… it would be the “lexer” (or lexicographical analyzer) that would notice and throw an error in that case.


For simple, “toy” languages (the sort that are assigned in Computer Science classes to be completed in a single semester by an individual student), often the parsing and the “lexing” can be done together, so that you can have a single “lexer and parser” component of a programming language.


However, I believe that in most real, commercial language tools used by other programmers in their work, the parser and the lexer tend to be separate, as is the code generation step that (in the case of a compiler) actually emits machine code that can be linked into an executable image for some computer to execute.

16 views0 comments

Recent Posts

See All

There are Three...

There are 3 Dave Carew's in my life: There is the one I try not to be, the one I wish I was, and the one that I actually am. Occasionally (always) I find it in my heart to wish that the one I am, was

HTML+JS for a Blackjack Basic Strategy Drill App

<html> <!-- Bstr-Ref-Drill-v7.html This one-page, self-contained BlackJack learning HTML with embeddedded JavaScript is copyright � David Carew, 2008. It is associated with the book authored by David

Post: Blog2_Post
bottom of page