100% found this document useful (1 vote)
420 views10 pages

Compiler Construction-2

This document provides an example of developing a lexical specification for a language. It includes the major parts needed for a lexical specification: the alphabet of symbols, patterns for lexical units, regular expressions to define these patterns, a transition diagram, and a transition table. Specifically, it shows an example where the language has three-letter identifiers starting with a letter followed by two letters or digits. It provides the regular expression, transition diagram with four states, and transition table to recognize these three-letter identifiers.

Uploaded by

Haseeb Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
420 views10 pages

Compiler Construction-2

This document provides an example of developing a lexical specification for a language. It includes the major parts needed for a lexical specification: the alphabet of symbols, patterns for lexical units, regular expressions to define these patterns, a transition diagram, and a transition table. Specifically, it shows an example where the language has three-letter identifiers starting with a letter followed by two letters or digits. It provides the regular expression, transition diagram with four states, and transition table to recognize these three-letter identifiers.

Uploaded by

Haseeb Arshad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

COMPILER

CONSTRUCTION
LECTURE 3
LEXICAL SPECIFICATION
• before implementing Lexical analyzer we should write lexical specification
• major parts of lexical specification

a- Alphabets in language defined by sigma

b- Pattern of lexical unit

c- regular expression

d- transition diagram

e- transition Table
Example : write Lexical Specification for Abc language having only
three letter identifiers.
Sigma = {abc..zABC..Z0..9}

Pattern:
Three letter identifier start with letter followed by two letters OR digits

Regular Expression :
(a+b+c..+z+A+B..+z) (a+b..+z+A+B..z+0+1..9)
(a+b..+z+A+B..z+0+1..9)

Regular Definition:

letter={abc..zAB...Z} Digit={012..9}
letter (letter|digit) (letter|Digit)
Transition diagram

letter letter/digit
0 1 2

letter/digit

Identifier
Transition Table
Rules for transition table
1- number of rows is equal to number of states in diagram
2- Number of cols is equal to number of alphabets defined in sigma
3- blank cell indicates no transitions or error
4- One extra col for accepting state
5- Place all zeros at accepting state row

Letter Digit state Token


0 1
1 2 2
2 3 3
3 0 0 Accepting identifier
EXAMPLE 2
Example 2 Develop Lexical Specification for xyz language

1- Identifiers - one or more letters


2- Keywords int and print
3- int-literals
4- operators = and +
5- Punctuations only ;
6- delimiters (ignored)

Sample Program1
int a;
a=25
print a
Sample program 2
int a;
int b;
int c;
a=2;
b=3;
c= a+b;
print c
Lexical Specification
Sigma={abc..zABc..Z012..9;=+space tab newline}
Letter={abc..zAbc..Z}
Digit={012..9}

1- Identifier
Pattern : At least one or more letters
RE : Letter Letter* OR Letter+
2-Keywords
Only two {int print}
covered in id definition
use same re for keywords
3- int-Literals
Pattern : Start with digit, followed by more digits
RE : +Digits digits* OR digit +
4- Operators only + and =
RE +
RE =
5- Punctuations
only ;
RE ;
5- Delimiters
space tab and newline
Alll ignored
RegularExpression (space|tab|newline)*
Transition Diagram

sp|tab|newline
letter other * id/keyword
0 1
2
digit digit
; + =
3 other 4
* intlit

5 6
=
;
+
Transition Table

State Letter Digit = + ; Delim Accepting Token


State

0 1 3 7 6 5 0
1 1 2 2 2 2 2
2 0 0 0 0 0 0 Y Id/Keyword
3 4 3 4 4 4 4
4 0 0 0 0 0 0 Y Int-Literal
5 0 0 0 0 0 0 N ;
6 0 0 0 0 0 0 N +
7 0 0 0 0 0 0 N =

Assumption
y= accepting state with input retraction
n= accepting state without input retraction
blank = not a accepting state
Assignment 2
Develop Lexical Specification for xyz language
1- Identifiers- start with letter, followed by letter or digits
2- Keywords- int, print, get
3- int-literals- start with non zero digit
07 not a int literal
70 is intliteral
4- operators = and +
5- Punctuations only ; { }
6- delimiters (ignored)

Tokenize the following programs


Sample Program1
{
int a23;
a=25
print a
}

You might also like