Download - Compiler Engineering Lab#2
L A B # 2 : L E X I C A L A N A LY Z E R ( C O N T. )
COMPILER ENGINEERING
University of DammamGirls’ College of ScienceDepartment of Computer Science Compiler Engineering Lab
Department of Computer Science - Compiler Engineering Lab
2
• Identifiers• Keywords• Relational Operators
• Arithmetic Operators
A LANGUAGE FOR
SPECIFYING LEXICAL
ANALYZER
18-22/2/12
Department of Computer Science - 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.
18-22/2/12
Department of Computer Science - 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
18-22/2/12
Department of Computer Science - 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 the two constrained by the size of the buffer
• Buffer hold just one token• Interaction can be
implemented by making the lexical analyzer a procedure called by the parser
lexical analyzer and the parser form a producer – consumer pair
TOKEN BUFFER
18-22/2/12
Department of Computer Science - Compiler Engineering Lab
6
• Define these Manifest Constants :• BSIZE 128• EOF ‘\0’
• NONE - 1• NUM 256• ID 257
MANIFEST CONSTANT
18-22/2/12
Department of Computer Science - Compiler Engineering Lab
7
Use these definitions:
IF 310
THEN320
ELSE 330
RELOP 300
AROP250
ASS 253
SUM 254
SUB 255
MUL 259
DIV 258
* Be careful with case-sensitive lexemes.
KEYWORDS (TOKENS & VALUES)
18-22/2/12
Department of Computer Science - 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.
18-22/2/12
Department of Computer Science - Compiler Engineering Lab
9
• Relational Operators:
>= , > , == , <=, > , !=
TOKEN RELOP Token Value GE , GT , EQ,
LE ,LT ,NE
OPERATORS
18-22/2/12
Department of Computer Science - Compiler Engineering Lab
10
• Arithmetic Operators:
= , + , - ,* , /
TOKENAROP
Token Value ASS , SUM , SUB ,
MUL , DIV
OPERATORS
18-22/2/12
Department of Computer Science - 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.
18-22/2/12
Department of Computer Science - 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.
18-22/2/12
Department of Computer Science - Compiler Engineering Lab
13
QUESTIONS?
Thank you for listening
18-22/2/12