Skip to content

01 Lex Introduction

Lex

Language translator, which converts

  • from lex source program having regular expression, to match tokens in input string
  • to a C program which has the function yylex() which is used to scan the input for tokens

This will be the source file for lexical analysis of a C program. It will take a C program as input.

We use regular expressions to match lexemes and generate tokens

Lex Source Code

%{
#include
C Declarations
%}
Lex Symbols
%%
Rule
%%
Auxilliary functions (optional)

Simplest Program

Default program which copies input to output

%%
%%

Compilation

lex analyzer.l
cc lex.yy.c -ll

Execution

a.out

// Takes user input
a.out < my_program.c > sample.txt

Variables

Data Type Meaning Default Value
yytext *char pointer to matched string
yyleng int length of matched string
yyin *file Input Source STDIN (console)
yyout *file Output Destination STDOUT (console)
Last Updated: 2023-01-25 ; Contributors: AhmedThahir

Comments