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)
- Flex spec scanner.l, through Flex, gives lex.yy.c
- lex.yy.c, through C/C++ compiler, gives a.out
- 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.
./a.out < data.txt would. Lecture reference: Lexical Analysis III (cont’d) Lecture reference: Lexical Analysis III (cont’d)spec.l flex specification
- 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
data.txt input
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() calls
yylex() was called once and returned 0.