Lexical Analysis
Lexical Analysis With Lex
Lexical analysis with Lex
Lex source program format
• The Lex program has three sections, separated
by %%:
declarations
%%
transition rules
%%
auxiliary code
Declarations section
• Code between %{ and }% is inserted directly into the lex.yy.c. Should
contain:
– Manifest constants (#define for each token)
– Global variables, function declarations, typedefs
• Outside %{ and }%, REGULAR DEFINITIONS are declared.
Examples:
delim [ tn]
ws {delim}+
letter [A-Za-z]
Each definition is a name
followed by a pattern.
Declared names can be used in
later patterns, if surrounded by
{ }.
Translation rules section
Translation rules take the form
p1 { action1 }
p2 { action2 }
… …
pn { actionn }
Where pi is a regular expression and actioni is a C program fragment to be
executed whenever pi is recognized in the input stream.
In regular expressions, references to regular definitions must be enclosed in
{} to distinguish them from the corresponding character sequences.

Lexical analysis-using-lex

  • 1.
  • 3.
  • 4.
  • 5.
    Lex source programformat • The Lex program has three sections, separated by %%: declarations %% transition rules %% auxiliary code
  • 6.
    Declarations section • Codebetween %{ and }% is inserted directly into the lex.yy.c. Should contain: – Manifest constants (#define for each token) – Global variables, function declarations, typedefs • Outside %{ and }%, REGULAR DEFINITIONS are declared. Examples: delim [ tn] ws {delim}+ letter [A-Za-z] Each definition is a name followed by a pattern. Declared names can be used in later patterns, if surrounded by { }.
  • 7.
    Translation rules section Translationrules take the form p1 { action1 } p2 { action2 } … … pn { actionn } Where pi is a regular expression and actioni is a C program fragment to be executed whenever pi is recognized in the input stream. In regular expressions, references to regular definitions must be enclosed in {} to distinguish them from the corresponding character sequences.