Docsity

Lexical Analysis in Programming Languages: A Comprehensive Overview, Lecture notes of Compiler Design

Partial preview of the text

Download Lexical Analysis in Programming Languages: A Comprehensive Overview and more Lecture notes Compiler Design in PDF only on Docsity!

CS

Lecture 3 Lexical Analysis Instructor: Fredrik Kjolstad Slide design by Prof. Alex Aiken, with modifications

Outline

  • Informal sketch of lexical analysis
    • Identifies tokens in input string
  • Issues in lexical analysis
    • Lookahead
    • Ambiguities
  • Specifying lexers (aka. scanners)
    • By regular expressions (aka. regex)
    • Examples of regular expressions

What’s a Token?

  • A syntactic category
    • In English: noun, verb, adjective, …
    • In a programming language: Identifier, Integer, Keyword, Whitespace, …

Tokens

  • A token class corresponds to a set of strings
  • Examples
    • Identifier: strings of letters or digits, starting with a letter
    • Integer: a non-empty string of digits
    • Keyword: “else” or “if” or “begin” or …
    • Whitespace: a non-empty sequence of blanks, newlines, and tabs var i foo ports Person … Infinite set

Designing a Lexical Analyzer: Step 1

  • Define a finite set of tokens
    • Tokens describe all items of interest
      • Identifiers, integers, keywords
    • Choice of tokens depends on
      • language
      • design of parser

Example

  • Recall \tif (i == j)\n\t\tz = 0;\n\telse\n\t\tz = 1;
  • Useful tokens for this expression: Integer, Keyword, Relation, Identifier, Whitespace, (, ), =, ;
  • N.B., (, ), =, ; above are tokens, not characters

Lexical Analyzer: Implementation

  • An implementation must do two things:
    1. Classify each substring as a token
    2. Return the value or lexeme (value) of the token
      • The lexeme is the actual substring
      • From the set of substrings that make up the token
  • The lexer thus returns token-lexeme pairs
    • And potentially also line numbers, file names, etc. to improve later error messages

Example

  • Recall: \tif (i == j)\n\t\tz = 0;\n\telse\n\t\tz = 1;

True Crimes of Lexical Analysis

  • Is it as easy as it sounds?
  • Sort of… if you do not make it hard!
  • Look at some history

Lexical Analysis in FORTRAN

  • FORTRAN rule: Whitespace is insignificant
  • E.g., VAR1 is the same as VA R
  • A terrible design!
  • Historical footnote: FORTRAN Whitespace rule motivated by inaccuracy of punch card operators

Lexical Analysis in FORTRAN (Cont.)

  • Two important points:
    1. The goal is to partition the string. This is implemented by reading left-to-right, recognizing one token at a time
    2. “Lookahead” may be required to decide where one token ends and the next token begins

Lookahead

  • Even our simple example has lookahead issues
    • i vs. if
    • = vs. ==

Lexical Analysis in PL/I (Cont.)

  • PL/I Declarations: DECLARE (ARG1,.. ., ARGN)
  • (^) Cannot tell whether DECLARE is a keyword or array reference until after the ). - Requires arbitrary lookahead!

20 Lexical Analysis in C++

  • Unfortunately, the problems continue today
  • C++ template syntax: Foo
  • C++ stream syntax: cin >> var;
  • But there is a conflict with nested templates: Foo<Bar> Closing templates, not stream