1. University of Dammam
Girls’ College of Science
Department of Computer Science
Compiler Engineering Lab
COMPILER
ENGINEERING
LAB # 2 : LEXICAL ANALYZER (CONT.)
2. • Identifiers
• Keywords
• Relational
A LANGUAGE
FOR
Operators
SPECIFYING • Arithmetic
LEXICAL
ANALYZER
Operators
Department of Computer Science -
18-22/2/12 2
Compiler Engineering Lab
3. IDENTIFIERS & KEYWORDS
• if the first character isalpha..
• It might be keyword or identifier .
• First you have to check if it is keyword , if it
is not it is going to be an identifier.
• use (strcmp) to compare the string in array
with key words.
Department of Computer Science -
18-22/2/12 3
Compiler Engineering Lab
4. IDENTIFIERS & KEYWORDS
• If the lexeme is keyword return the value
of the keyword
• Store each character in array for both
regular identifier and keywords
• Define an array call it (lexbuf) to store the
characters of IDs’ and keywords
• An identifier should be..
{alpha}({alpha}|{digit}|[ _ ])* <= BSIZE
Department of Computer Science -
18-22/2/12 4
Compiler Engineering Lab
5. • Analyzer can not process
when the buffer is full,
parser can not process
when the buffer is empty
• the interaction between
TOKEN BUFFER
the two constrained by the
lexical analyzer size of the buffer
and the parser
form a producer – • Buffer hold just one token
consumer pair • Interaction can be
implemented by making
the lexical analyzer a
procedure called by the
parser
Department of Computer Science -
18-22/2/12 5
Compiler Engineering Lab
6. • Define these Manifest
Constants :
• BSIZE 128
MANIFEST
CONSTANT • EOF ‘0’
• NONE - 1
• NUM 256
• ID 257
Department of Computer Science -
18-22/2/12 6
Compiler Engineering Lab
7. Use these definitions:
IF 310
THEN 320
KEYWORDS
(TOKENS & ELSE 330
VALUES) RELOP 300
* Be careful with case-
sensitive lexemes.
AROP 250
ASS 253
SUM 254
SUB 255
MUL 259
DIV 258
Department of Computer Science -
18-22/2/12 7
Compiler Engineering Lab
8. RELOP ( RELATION OPERATIONS )
• Read one character if it matches one of
the RELOP, read one more …
• Declare a char array call it (tvl) [2] to store
the RELOP value
• Use (strcpy) to copy values and (strcmp)
to compare values.
Department of Computer Science -
18-22/2/12 8
Compiler Engineering Lab
9. • Relational Operators:
>= , > , == , <=
, > , !=
OPERATORS
TOKEN RELOP
Token Value
GE , GT , EQ, LE ,LT ,NE
Department of Computer Science -
18-22/2/12 9
Compiler Engineering Lab
10. • Arithmetic Operators:
= , + , - ,* , /
OPERATORS TOKEN
AROP
Token Value
ASS , SUM , SUB , MUL ,
DIV
Department of Computer Science -
18-22/2/12 10
Compiler Engineering Lab
11. ISALPHA()()
# include <ctype.h>
Description
• Classifies an alphabetical character.
• isalpha is a macro that classifies ASCII-
coded integer values by table lookup. For
the default C locale, c is a letter (A to Z or
a to z).
Return Value:
isalpha returns nonzero if c is a letter.
Department of Computer Science -
18-22/2/12 11
Compiler Engineering Lab
12. ISALNUM( )
# include <ctype.h>
Description
• Tests for an alphanumeric character.
• isalnum is a macro that classifies ASCII-
coded integer values by table lookup. c is
a letter (A to Z or a to z) or a digit (0 to 9).
Return Value:
• isalnum returns nonzero if c is a letter or
a digit.
Department of Computer Science -
18-22/2/12 12
Compiler Engineering Lab
13. QUESTIONS?
Thank you for listening
Department of Computer Science -
18-22/2/12 13
Compiler Engineering Lab