Вы находитесь на странице: 1из 4

Compiler Construction

For
COMPUTER SCIENCE STUDENTS

Third Class
Compiled by
Adnan Alam Khan

1
WRITE2ADNANALAMKHAN@GMAIL.COM
Lexical Analysis
Lexical analyzer is analyzing the weight character by character.
Lets take an example.
int max (x,y)
int x,y
/* find maximum of x and y*/
{
Return(x>y ? x:y);
}
Now here int is a lexim what about max is also lexim but here lexim will use as a
token.
In scanning it will eliminate comments and white spaces.
It means you want to say if we write c-program in one line this compiler will gave
you no error and execute the code.
How to convert Lexim into Tokens?
Lexical Analysis has 3 main role
1.Convert Lexim into tokens
2.Remove comments
3.Remove white spaces.
One important thing about lexical analyzer is line by line conversion.
Is there any error in LA?
No because it does not find any error in token or Lexim
Q# Calculate Token in the following program
int max (x,y)
int x,y
/* find maximum of x and y*/
{
Return(x>y ? x:y);
}
Solution:
|int| max| (|x|,|y|)|
int| x|,|y|
/* find maximum of x and y*/
{|
return| (|x|>|y| ? | x|:|y|)|;|
}|
Now count blue lines they are 25 it means 25 token are present in this program.
Q#2 How many tokens are there in this particular line?
Printf(%d hay ,&x);
Solution: 1
Printf| (| |%d| hay| | , |&|x|)|;|
Now count 11 token which is wrong. Why because entire string in will consider as
one token.
Count again 1=printf 2=( 3= 4= , 5=& 6=x 7= ) 8= ;
Syntax analyzer (SA) or parser analyzer(PA):
Input -SA is a grammar
First Grammar
Second input then it will check SA.

2
WRITE2ADNANALAMKHAN@GMAIL.COM
What is Grammar in SA?
G=(V,T,P,S)
V=Variable T=Terminal P=production S=Taught symbol

Example of a grammar.
E E+E
/E*E
/id
Solution:
V={E} ; T={+,*,id}
Example of a grammar left most derivation.(LMD)
E E+E
/E*E
/id
Solution:
EE+E
Eid+E here Left E will replace by word id
Eid+E*E but here E also * with other E
Eid+id*E but here left E will replace by word id
Eid+id*id finally left E will replace by word id

Is there any Right most derivation also?


Sol: yes RMD exist
EE+E
EE+E*E but here E also * with other E
EE+E*id but here right E will replace by word id
EE+id*id but here right E will replace by word id
Eid+id*id finally remaining right E will replace by word id
Q What is Regular Expression?

Sometimes r may contains character that have special meanings called


Metacharacters or Metasymbols. In other words member of alphabet. Last but not
the least escape characters that turns off the special meanings, common
characters are backslash and quotes.
Page 44

3
WRITE2ADNANALAMKHAN@GMAIL.COM
4
WRITE2ADNANALAMKHAN@GMAIL.COM

Вам также может понравиться