Compiler Construction-2
Compiler Construction-2
CONSTRUCTION
LECTURE 3
LEXICAL SPECIFICATION
• before implementing Lexical analyzer we should write lexical specification
• major parts of lexical specification
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
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
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)