Docsity

OOPS - Lexical Analysis copy, Study notes of Object Oriented Programming

Partial preview of the text

Download OOPS - Lexical Analysis copy and more Study notes Object Oriented Programming in PDF only on Docsity!

 (^) The lexical analyzer breaks a sentence into a sequence of words or tokens and ignores white spaces and comments.  (^) It generates a stream of tokens from the input.  (^) This is modeled through regular expressions and the structure is recognized through finite state automata.  (^) If the token is not valid i.e., does not fall into any of the identifiable groups, then the lexical analyzer reports an error.  (^) Lexical analysis thus involves recognizing the

tokens in the source program and reporting errors, if any.

 (^) Token

  • A lexical token is a sequence of characters that can be treated as a unit in the grammar of the programming languages.
  • Example of tokens:
  • Keywords
  • Identifiers
  • Operators
  • Special characters
  • Constants
  • Example of non-tokens:
  • Comments, preprocessor directive, macros, blanks, tabs, newline,...

 (^) Since a token can represent more than one lexeme, additional information should be held for that specific lexeme. This additional information is called as the attribute of the token.  (^) For simplicity, a token may have a single attribute which holds the required information for that token.

  • For identifiers, this attribute a pointer to the symbol table, and the symbol table holds the actual attributes for that token.  (^) Some attributes:
  • <id,attr> where attr is pointer to the symbol table
  • <assgop,_> no attribute is needed (if there is only one assignment operator)
  • <num,val> where val is the actual value of the number.  (^) Token type and its attribute uniquely identifies a lexeme.  (^) Regular expressions are widely used to specify patterns.

 (^) (e.g) fi(a==f(x))….

  • fi is a misspell of keyword ‘if’ or identifier ‘fi’
  • Lexical analyzer returns code for ‘fi’ as identifier.
  • Other recovery actions are
  • Delete extraneous chars
  • Insert missing chars
  • Replacing a incorrect char by a correct char
  • Transposing two adjacent chars

 Takes a stream of characters and

identifies tokens from the lexemes.

 Eliminates comments and

redundant white pace.

 Keeps track of line numbers and

column numbers and passes them as

parameters the other phases to

enable error-reporting to the user.

 (^) Token: A sequence of characters to be

treated as a single unit.  (^) Examples of tokens.

**- Reserved words (e.g. begin, end, struct, if etc.)

  • Keywords (** integer, true etc.) **- Operators (+, &&, ++ etc)
  • Identifiers (variable names, procedure names, parameter** names) - Literal constants (numeric, string, character constants etc.) - Punctuation marks (:, , etc.)