Compiler Engineering Lab#2

Views:
 
Category: Education
     
 

Presentation Description

Building a Lexical Analyzer (ID, Keywords, Operational Operators, Arithmetic Operators)

Comments

Presentation Transcript

Compiler Engineering:

Lab # 2 : Lexical Analyzer (cont.) Compiler Engineering University of Dammam Girls’ College of Science Department of Computer Science Compiler Engineering Lab

A Language for Specifying Lexical Analyzer :

Identifiers Keywords Relational Operators Arithmetic Operators A Language for Specifying Lexical Analyzer 18-22/2/12 Department of Computer Science - Compiler Engineering Lab 2

Identifiers & Keywords:

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 3

Identifiers & Keywords:

Identifiers & Keywords If the lexeme is keyword return the value of the keyword Store each character in array for both regular identifier and keywords D efine 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 4

Token Buffer:

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 5

Manifest Constant:

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 6

KeyWords (Tokens & Values):

Use these definitions: IF 310 THEN 320 ELSE 330 RELOP 300 AROP 250 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 7

RELOP ( Relation Operations ):

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 8

operators:

Relational Operators: > = , > , == , <= , > , != TOKEN RELOP Token Value GE , GT , EQ, LE , LT ,NE operators 18-22/2/12 Department of Computer Science - Compiler Engineering Lab 9

operators:

Arithmetic Operators: = , + , - ,* , / TOKEN AROP Token Value ASS , SUM , SUB , MUL , DIV operators 18-22/2/12 Department of Computer Science - Compiler Engineering Lab 10

Isalpha()():

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 11

isalnum( ):

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 12

Questions?:

Questions? Thank you for listening  18-22/2/12 Department of Computer Science - Compiler Engineering Lab 13

authorStream Live Help