Lecture 3 - LexicalAnalysis
Lecture 3 - LexicalAnalysis
ISBN
0-321-49362-1
Chapter 4 Topics
Introduction Lexical Analysis
4-2
Introduction
Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal description of the syntax of the source language (BNF)
4-3
Syntax Analysis
The syntax analysis portion of a language processor nearly always consists of two parts:
A low-level part called a lexical analyzer (mathematically, a finite automaton based on a regular grammar) A high-level part called a syntax analyzer, or parser (mathematically, a push-down automaton based on a context-free grammar, or BNF)
4-4
4-5
4-6
Lexical Analysis
A lexical analyzer is a pattern matcher for character strings A lexical analyzer is a front-end for the parser Identifies substrings of the source program that belong together - lexemes
Lexemes match a character pattern, which is associated with a lexical category called a token sum is a lexeme; its token may be IDENT
4-7
4-8
Copyright 2008 Barrett All rights reserved. Copyright 2007 Addison-Wesley.R. Bryant. All rights
reserved
JFlex Example
vulcan6% cat PL0.jflex %% %{ private void echo () { System . out . print (yytext ()); } public int position () { return yycolumn; } %} %class PL0Lexer %function nextToken %type Token %unicode %line %column %eofval{ { return new Token (Token . EOF); } %eofval}
Copyright 2008 Barrett R. Bryant. All rights reserved Copyright 2007 Addison-Wesley. All rights reserved.
Copyright 2008 Barrett R. Bryant. All rights reserved Copyright 2007 Addison-Wesley. All rights reserved.
Copyright 2008 Barrett R. Bryant. All rights reserved Copyright 2007 Addison-Wesley. All rights reserved.
Copyright 2007 Addison-Wesley. Bryant. All rights reserved Copyright 2008 Barrett R. All rights reserved.
Copyright 2008 Barrett R. Bryant. All rights reserved Copyright 2007 Addison-Wesley. All rights reserved.
Copyright 2008 Barrett R. All rights reserved. Copyright 2007 Addison-Wesley. Bryant. All rights reserved
public Token (int symbol, String lexeme) { this . symbol = symbol; this . lexeme = lexeme; }
public int symbol () { return symbol; } public String lexeme () { return lexeme; }
Copyright 2007 Addison-Wesley. Bryant. All rights reserved Copyright 2008 Barrett R. All rights reserved.
Copyright 2007 Addison-Wesley. Bryant. All rights reserved Copyright 2008 Barrett R. All rights reserved.
();
Copyright 2008 Barrett R. Bryant. All rights reserved Copyright 2007 Addison-Wesley. All rights reserved.
Copyright 2007 Addison-Wesley. Bryant. All rights reserved Copyright 2008 Barrett R. All rights reserved.