Lex
•Lex is a program that generates lexical analyzer. It is used with YACC parser
generator.
•The lexical analyzer is a program that transforms an input stream into a sequence
of tokens.
•It reads the input stream and produces the source code as output through
implementing the lexical analyzer in the C program.
The function of Lex is as follows
•Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler runs the
lex.1 program and produces a C program lex.yy.c.
•Finally C compiler runs the lex.yy.c program and produces an object program a.out.
•a.out is lexical analyzer that transforms an input stream into a sequence of tokens.
Lex
Lex File Format
A Lex program is separated into three sections by %% delimiters. The formal of Lex source is as
follows:
{ definitions }
%%
{ rules }
%%
{ user subroutines }
Definitions include declarations of constant, variable and regular definitions.
Rules define the statement of form p1 {action1} p2 {action2}....pn {action}.
Where pi describes the regular expression and action1 describes the actions what
action the lexical analyzer should take when pattern pi matches a lexeme.
User subroutines are auxiliary procedures needed by the actions. The subroutine can be loaded
with the lexical analyzer and compiled separately.
Context Free Grammar
Context free grammar is a formal grammar which is used to generate all possible strings in a given formal language.
Context free grammar G can be defined by four tuples as:
G= (V, T, P, S)
Where,
G describes the grammar
T describes a finite set of terminal symbols.
V describes a finite set of non-terminal symbols
P describes a set of production rules
S is the start symbol.
In CFG, the start symbol is used to derive the string. You can derive the string by repeatedly replacing a non-terminal by the
right hand side of the production, until all non-terminal have been replaced by terminal symbols.
Context Free Grammar
Production rules:
S → aSa
S → bSb
S → c
Now check that abbcbba string can be derived from the given CFG.
S aSa
⇒
S abSba
⇒
S abbSbba
⇒
S abbcbba
⇒
By applying the production
S → aSa, S → bSb recursively and finally applying the production S → c,
we get the string abbcbba.
Context Free Grammar
Derivation is a sequence of production rules. It is used to get the input string through these production rules. During parsing
we have to take two decisions. These are as follows:
•We have to decide the non-terminal which is to be replaced.
•We have to decide the production rule by which the non-terminal will be replaced.
We have two options to decide which non-terminal to be replaced with production rule.
Left-most Derivation
In the left most derivation, the input is scanned and replaced with the production rule from left to right. So in left most
derivatives we read the input string from left to right.
Example:
Production rules:
S = S + S
S = S - S
S = a | b |c
Input:
a - b + c
Derivation
The left-most derivation is:
S = S + S
S = S - S + S
S = a - S + S
S = a - b + S
S = a - b + c
Right Most Derivation
In the right most derivation, the input is scanned and replaced with the production rule from right to left. So in right most
derivatives we read the input string from right to left.
Example:
S = S + S
S = S - S
S = a | b |c
Input:
a - b + c
The right-most derivation is:
S = S - S
S = S - S + S
S = S - S + c
S = S - b + c
S = a - b + c
Parse tree
•Parse tree is the graphical representation of symbol. The symbol can be terminal or non-terminal.
•In parsing, the string is derived using the start symbol. The root of the parse tree is that start symbol.
•It is the graphical representation of symbol that can be terminals or non-terminals.
•Parse tree follows the precedence of operators. The deepest sub-tree traversed first. So, the operator in the parent node has
less precedence over the operator in the sub-tree.
The parse tree follows these points:
All leaf nodes have to be terminals.
All interior nodes have to be non-terminals.
In-order traversal gives original input string.
Parse Tree
Example:
Production rules:
T= T + T | T * T
T = a|b|c
Input:
a * b + c
Parse Tree
Step 1:
Step 2:
Step 3:
Step 3:
Step 4:
Ambiguity
A grammar is said to be ambiguous if there exists more than one leftmost derivation or
more than one rightmost derivative or more than one parse tree for the given input
string. If the grammar is not ambiguous then it is called unambiguous.
Example:
S = aSb | SS
S = ∈
For the string aabb, the above grammar generates two parse trees:
If the grammar has ambiguity then it is not good for a compiler construction. No method can automatically detect
and remove the ambiguity but you can remove ambiguity by re-writing the whole grammar without ambiguity.

More Related Content

DOCX
8-Practice problems on operator precedence parser-24-05-2023.docx
PPTX
Chapter3pptx__2021_12_23_22_52_54.pptx
PPTX
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
PPTX
Lefmost rightmost TOC.pptx
PPT
Context free grammar
PDF
Syntax analysis
PPT
SYNTAX ANALYSIS, PARSING, BACKTRACKING IN COMPILER DESIGN
PDF
Syntax Analysis_73 pages notes IIIT Manipur.pdf
8-Practice problems on operator precedence parser-24-05-2023.docx
Chapter3pptx__2021_12_23_22_52_54.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
Lefmost rightmost TOC.pptx
Context free grammar
Syntax analysis
SYNTAX ANALYSIS, PARSING, BACKTRACKING IN COMPILER DESIGN
Syntax Analysis_73 pages notes IIIT Manipur.pdf

Similar to Lexhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pptx (20)

PDF
3b. LMD & RMD.pdf
PPTX
2.4 derivations and languages
PDF
syntaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pdf
PDF
Ch2_Compilers A Simple One-Pass Compiler.pdf
PPTX
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
PPT
Chapter 3 -Syntax Analyzer.ppt
PPT
Module 11
PPT
Syntax analysis and Run time Environment
PPTX
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
PPTX
Syntactic specification is concerned with the structure and organization of t...
PPTX
CONTEXT FREE GRAMMAR
PPTX
compiler design syntax analysis top down parsing
PPTX
chapter 4 context-freegrammarintroduction context-freegramma
PPT
PPTX
Context Free Grammar presentation 2025.pptx
PPTX
Syntactic Analysis in Compiler Construction
PPTX
Context free grammar
PPTX
Syntax_Analysis_Syntax analysis_NLP.pptx
PPTX
3. Syntax Analyzer.pptx
PPT
Compiler design lessons notes from Semester
3b. LMD & RMD.pdf
2.4 derivations and languages
syntaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pdf
Ch2_Compilers A Simple One-Pass Compiler.pdf
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
Chapter 3 -Syntax Analyzer.ppt
Module 11
Syntax analysis and Run time Environment
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
Syntactic specification is concerned with the structure and organization of t...
CONTEXT FREE GRAMMAR
compiler design syntax analysis top down parsing
chapter 4 context-freegrammarintroduction context-freegramma
Context Free Grammar presentation 2025.pptx
Syntactic Analysis in Compiler Construction
Context free grammar
Syntax_Analysis_Syntax analysis_NLP.pptx
3. Syntax Analyzer.pptx
Compiler design lessons notes from Semester
Ad

Recently uploaded (20)

PDF
Auris, Corolla (EM3026E) Overall Electrical Wiring Diagram.pdf
PDF
Bobcat t40140, t40180 telescopic handler service repair manual (sn b33 j11001...
PPTX
TDC-15-S-02 Road and Traffic Rules (Version 2).pptx
PPTX
IMPRESSION MAKING IN FIXED PARTIAL DENTURE.pptx
PDF
dozer bulldozer Komatsu d85 px 15e0 Technical manual.pdf
PDF
SIH-2023-winning-PPT.pdfASDFDSFGDSGSDFGSDFGDF
PDF
Bobcat tl38.70 telescopic handler service repair manual sn b3 zu11001 and abo...
PDF
Why Has Vertical Farming Recently Become More Economical.pdf.pdf
PPTX
TDC-15-S-01 Introduction to Driving (Version 2).pptx
PDF
2007 POLARIS Dragon RMK TWO STROKE SNOWMOBILE Service Repair Manual.pdf
PDF
John Deere 5055e Engine Repair Manual Tm900319.pdf
DOC
Bài tập bổ trợ I-learn Smart World 9 HK1 Unit 4 (HS) (1).doc
PDF
Caterpillar CAT 420F BACKHOE LOADER (LKH00001-UP) Operation and Maintenance M...
PPTX
ADULT Nutrition.pptxxxxxxxxxxxxxxxxxxxxxx
PDF
ISO_9001_Quality_Induction_1720263786.ppt
PDF
CASE CX55B Mini Excavator Service Repair Manual Instant Download.pdf
PDF
Lecture 10 geo_merged.pdfegrgeregrregrgeregs
PDF
BOBCAT 442 muffler SN 522311001 & Above.pdf
PPT
fy10_sh-20856-10_Machine_Guarding (1).ppt
PDF
John Deere 460E Articulated Dump Trucks Diagnostic Technical Manual.pdf
Auris, Corolla (EM3026E) Overall Electrical Wiring Diagram.pdf
Bobcat t40140, t40180 telescopic handler service repair manual (sn b33 j11001...
TDC-15-S-02 Road and Traffic Rules (Version 2).pptx
IMPRESSION MAKING IN FIXED PARTIAL DENTURE.pptx
dozer bulldozer Komatsu d85 px 15e0 Technical manual.pdf
SIH-2023-winning-PPT.pdfASDFDSFGDSGSDFGSDFGDF
Bobcat tl38.70 telescopic handler service repair manual sn b3 zu11001 and abo...
Why Has Vertical Farming Recently Become More Economical.pdf.pdf
TDC-15-S-01 Introduction to Driving (Version 2).pptx
2007 POLARIS Dragon RMK TWO STROKE SNOWMOBILE Service Repair Manual.pdf
John Deere 5055e Engine Repair Manual Tm900319.pdf
Bài tập bổ trợ I-learn Smart World 9 HK1 Unit 4 (HS) (1).doc
Caterpillar CAT 420F BACKHOE LOADER (LKH00001-UP) Operation and Maintenance M...
ADULT Nutrition.pptxxxxxxxxxxxxxxxxxxxxxx
ISO_9001_Quality_Induction_1720263786.ppt
CASE CX55B Mini Excavator Service Repair Manual Instant Download.pdf
Lecture 10 geo_merged.pdfegrgeregrregrgeregs
BOBCAT 442 muffler SN 522311001 & Above.pdf
fy10_sh-20856-10_Machine_Guarding (1).ppt
John Deere 460E Articulated Dump Trucks Diagnostic Technical Manual.pdf
Ad

Lexhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pptx

  • 1. Lex •Lex is a program that generates lexical analyzer. It is used with YACC parser generator. •The lexical analyzer is a program that transforms an input stream into a sequence of tokens. •It reads the input stream and produces the source code as output through implementing the lexical analyzer in the C program.
  • 2. The function of Lex is as follows •Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler runs the lex.1 program and produces a C program lex.yy.c. •Finally C compiler runs the lex.yy.c program and produces an object program a.out. •a.out is lexical analyzer that transforms an input stream into a sequence of tokens.
  • 3. Lex
  • 4. Lex File Format A Lex program is separated into three sections by %% delimiters. The formal of Lex source is as follows: { definitions } %% { rules } %% { user subroutines } Definitions include declarations of constant, variable and regular definitions. Rules define the statement of form p1 {action1} p2 {action2}....pn {action}. Where pi describes the regular expression and action1 describes the actions what action the lexical analyzer should take when pattern pi matches a lexeme. User subroutines are auxiliary procedures needed by the actions. The subroutine can be loaded with the lexical analyzer and compiled separately.
  • 5. Context Free Grammar Context free grammar is a formal grammar which is used to generate all possible strings in a given formal language. Context free grammar G can be defined by four tuples as: G= (V, T, P, S) Where, G describes the grammar T describes a finite set of terminal symbols. V describes a finite set of non-terminal symbols P describes a set of production rules S is the start symbol. In CFG, the start symbol is used to derive the string. You can derive the string by repeatedly replacing a non-terminal by the right hand side of the production, until all non-terminal have been replaced by terminal symbols.
  • 6. Context Free Grammar Production rules: S → aSa S → bSb S → c Now check that abbcbba string can be derived from the given CFG. S aSa ⇒ S abSba ⇒ S abbSbba ⇒ S abbcbba ⇒ By applying the production S → aSa, S → bSb recursively and finally applying the production S → c, we get the string abbcbba.
  • 7. Context Free Grammar Derivation is a sequence of production rules. It is used to get the input string through these production rules. During parsing we have to take two decisions. These are as follows: •We have to decide the non-terminal which is to be replaced. •We have to decide the production rule by which the non-terminal will be replaced. We have two options to decide which non-terminal to be replaced with production rule. Left-most Derivation In the left most derivation, the input is scanned and replaced with the production rule from left to right. So in left most derivatives we read the input string from left to right. Example: Production rules: S = S + S S = S - S S = a | b |c Input: a - b + c
  • 8. Derivation The left-most derivation is: S = S + S S = S - S + S S = a - S + S S = a - b + S S = a - b + c
  • 9. Right Most Derivation In the right most derivation, the input is scanned and replaced with the production rule from right to left. So in right most derivatives we read the input string from right to left. Example: S = S + S S = S - S S = a | b |c Input: a - b + c The right-most derivation is: S = S - S S = S - S + S S = S - S + c S = S - b + c S = a - b + c
  • 10. Parse tree •Parse tree is the graphical representation of symbol. The symbol can be terminal or non-terminal. •In parsing, the string is derived using the start symbol. The root of the parse tree is that start symbol. •It is the graphical representation of symbol that can be terminals or non-terminals. •Parse tree follows the precedence of operators. The deepest sub-tree traversed first. So, the operator in the parent node has less precedence over the operator in the sub-tree. The parse tree follows these points: All leaf nodes have to be terminals. All interior nodes have to be non-terminals. In-order traversal gives original input string.
  • 11. Parse Tree Example: Production rules: T= T + T | T * T T = a|b|c Input: a * b + c
  • 12. Parse Tree Step 1: Step 2: Step 3: Step 3: Step 4:
  • 13. Ambiguity A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivative or more than one parse tree for the given input string. If the grammar is not ambiguous then it is called unambiguous. Example: S = aSb | SS S = ∈ For the string aabb, the above grammar generates two parse trees: If the grammar has ambiguity then it is not good for a compiler construction. No method can automatically detect and remove the ambiguity but you can remove ambiguity by re-writing the whole grammar without ambiguity.