Lably
Skip to content

Lexical analysis

Conversion of character sequences into token sequences in computer science

  • 14 min read
  • 9 chapters

Related on Lably

Through Identifier (computer languages), Operator (computer programming) and Data type

Rule-based programs

A rule-based program, performing lexical tokenization, is called tokenizer, lexer or scanner, although scanner is also a term for the first stage of a lexer. A lexer forms the first phase of a compiler frontend in processing. Analysis generally occurs in one pass. Lexers and parsers are most often used for compilers, but can be used for other computer language tools, such as prettyprinters or linters. Lexing can be divided into two stages: the scanning, which segments the input string into syntactic units called lexemes and categorizes these into token classes, and the evaluating, which converts lexemes into processed values.

Lexers are generally quite simple, with most of the complexity deferred to the syntactic analysis or semantic analysis phases, and can often be generated by a lexer generator, notably lex or derivatives. However, lexers can sometimes include some complexity, such as phrase structure processing to make input easier and simplify the parser, and may be written partly or fully by hand, either to support more features or for performance.

Sign in to keep reading

8 more chapters are free with a Lably account.

More in Computing and communication