Compiler Engineering Lab#2

1,002 views

Published on

Compiler Engineering Lab#2
Building a Lexical Analyzer (ID, Keywords, Operational Operators, Arithmetic Operators)

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,002
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
124
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Compiler Engineering Lab#2

  1. 1. University of DammamGirls’ College of ScienceDepartment of Computer ScienceCompiler Engineering Lab COMPILER ENGINEERING LAB # 2 : LEXICAL ANALYZER (CONT.)
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 13. QUESTIONS? Thank you for listening  Department of Computer Science -18-22/2/12 13 Compiler Engineering Lab

×