Lexical analysis

Flex Playground

Run a flex specification on sample input in the browser and step through each match yylex() makes.

  • Lecture reference: Lexical Analysis III (cont’d) · slides 2–11
  1. Flex spec scanner.l, through Flex, gives lex.yy.c
  2. lex.yy.c, through C/C++ compiler, gives a.out
  3. input stream, through a.out, gives sequence of tokens

Build and run

flex spec.lgcc lex.yy.c -lfl./a.out < data.txt

On macOS, link with -ll instead of -lfl.

How a flex scanner is built. This page runs the spec and data.txt below in the browser, the way ./a.out < data.txt would. Lecture reference: Lexical Analysis III (cont’d) · slide 3 Lecture reference: Lexical Analysis III (cont’d) · slide 8

spec.l flex specification

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.
  • Definitions lines 1–8 · 3 definitions
  • Rules lines 9–16 · 3 rules
  • User code lines 17–21
  • C code copied into lex.yy.c as written

Example 3: numbers, identifiers, other. Classifies the input with the regular definitions DIGIT, LETTER, and ID.

Lecture reference: Lexical Analysis III (cont’d) · slide 10

data.txt input

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.

20 characters · 1 line · ends with a newline

Sample inputs

No rule matches the newline, so the default rule copies it to the output.

./a.out < data.txt exit status 0
[stdout] ident: count
other:  
other: =
other:  
ident: count
other:  
other: +
other:  
number: 10
other: ;
[ECHO] 
  • copied by ECHO

yylex() was called once and returned 0.