Docsity

COMP 524: Lexical Analysis in Programming Language Concepts at UNC Chapel Hill - Prof. Aar, Study notes of Programming Languages

Partial preview of the text

Download COMP 524: Lexical Analysis in Programming Language Concepts at UNC Chapel Hill - Prof. Aar and more Study notes Programming Languages in PDF only on Docsity!

Lecture 3: Lexical Analysis

COMP 524 Programming Language Concepts Stephen Olivier January 20, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, J. Prins and D. Stotts

Goal of Lecture

Scanner (lexical analysis) Parser (syntax analysis) Semantic analysis & intermediate code gen. Machine-independent optimization (optional) Target code generation. Machine-specific optimization (optional) Symbol Table Character Stream Token Stream Parse Tree Abstract syntax tree Modified intermediate form Machine language Modified target language

This includes regular expressions.

Pseudo-Code Scanner (Fig 2.5)

We skip any initial white spaces we read the next character if it is a ( we look at the next character if that is a * we have a comment; we skip forward through the terminating *) otherwise we return a ( and reuse the look-ahead If it is one of the one-character tokens ([],;=+- etc.) we return that token ...

Pseudo-Code Scanner (Fig 2.5)

We skip any initial white spaces we read the next character if it is a ( we look at the next character if that is a * we have a comment; we skip forward through the terminating *) otherwise we return a ) and reuse the look-ahead If it is one of the one-character tokens ([],;=+- etc.) we return that token ...

We could just turn this into real code and

use that as the scanner, and that would be

fine for small programs...

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit* non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit* non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

“→” denotes assignment

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit* non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

Thus, digit equal “0” or “1” or “2” or ....

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit*

“ * ” denotes zero or more of this type.

non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit*

So natural number equals at least

“one non-zero digit” followed by

“zero or more digits”.

non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit*

“ ε ” means empty.

non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

Regular expressions

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 non_zero_digit → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 natural_number → non_zero_digit digit*

It means “ 0 or a natural number ” followed

by “ nothing ” or by “. and zero or more

digits concluded by a non_zero number ”

non_neg_number → (0 | natural_number ) ( (. digit* non_zero_digit ) | ε )

Regular Expression Rules

• A RE consist of:

• A character (e.g., “0”, “1”, ...)

• The empty string (i.e., “ε”)

• Two REs next to each other (e.g., “ non_negative_digit digit ”) to

denote concatenation.

• Two REs separated by “|” next to each other (e.g.,

“ non_negative_digit | digit ”) to denote one RE or the other.

• An RE followed by “*” (called the Kleene star ) to denote zero

or more iterations of the RE.

• Parentheses (in order to avoid ambiguity).

Regular Expression Rules

• A RE consist of:

• A character (e.g., “0”, “1”, ...)

• The empty string (i.e., “ε”)

• Two REs next to each other (e.g., “ non_negative_digit digit ”) to

denote concatenation.

• Two REs separated by “|” next to each other (e.g.,

“ non_negative_digit | digit ”) to denote one RE or the other.

• An RE followed by “*” (called the Kleene star ) to denote zero

or more iterations of the RE.

• Parentheses (in order to avoid ambiguity).

The set of tokens that can be recognized by

regular expressions is called a regular set.

Deterministic finite automaton (DFA)

• Every regular set can be defined by using deterministic

finite automaton ( DFA ).

• DFAs are turing machines that have a finite number of states

and deterministically move between states.

S