+ All Categories
Home > Documents > Scanning & Regular Expressions

Scanning & Regular Expressions

Date post: 20-Jan-2016
Category:
Upload: ave
View: 26 times
Download: 0 times
Share this document with a friend
Description:
Scanning & Regular Expressions. CPSC 388 Ellen Walker Hiram College. Scanning. Input: characters from the source code Output: Tokens Keywords: IF, THEN, ELSE, FOR … Symbols: PLUS, LBRACE, SEMI … Variable tokens: ID, NUM Augment with string or numeric value. TokenType. - PowerPoint PPT Presentation
23
Scanning & Regular Expressions CPSC 388 Ellen Walker Hiram College
Transcript
Page 1: Scanning & Regular Expressions

Scanning & Regular Expressions

CPSC 388Ellen WalkerHiram College

Page 2: Scanning & Regular Expressions

Scanning

• Input: characters from the source code

• Output: Tokens– Keywords: IF, THEN, ELSE, FOR …– Symbols: PLUS, LBRACE, SEMI …– Variable tokens: ID, NUM

•Augment with string or numeric value

Page 3: Scanning & Regular Expressions

TokenType

• Enumerated type (a c++ construct)Typedef enum {IF, THEN, ELSE …} TokenType

• IF, THEN, ELSE (etc) are now literals of type TokenType

Page 4: Scanning & Regular Expressions

Using TokenType

void someFun(TokenType tt){ … switch (tt){ case IF: … break; case THEN: … break; … }

Page 5: Scanning & Regular Expressions

Token Class (partial)

class Token {public: TokenType tokenval; string tokenchars; double numval;}

Page 6: Scanning & Regular Expressions

Interlude: References and Pointers

• Java has primitives and references– Primitives are int, char, double, etc.

– References “point to” objects

• C++ has only primitives– But, one of the primitives is “address”, which serves the purpose of a reference.

Page 7: Scanning & Regular Expressions

Interlude: References and Pointers

• To declare a pointer, put * after the typechar x; // a characterchar *y; // a pointer to a character

• Using pointers:x = ‘a’; y = &x; //y gets the address of x*y = ‘b’; //thing pointed at by y becomes ‘b’;

//note that x is now also b!

Page 8: Scanning & Regular Expressions

Interlude: References and Pointers

• Continuing the example…cout << x << endl; // prints bcout << *y << endl; // prints bcout << y << endl; // prints a hex address

cout << &x << endl; // same as abovecout << &y << endl; // a different address - where the pointer is stored

Page 9: Scanning & Regular Expressions

GetToken(): A scanning function

• Token *getToken(istream &sin)– Read characters from sin until a complete token is extracted, return (a pointer to) the token

– Usually called by the parser

– Note: version in the book uses global variables and returns only the token type

Page 10: Scanning & Regular Expressions

Using GetToken

Token *myToken = GetToken(cin);while (myToken != NULL){ //process the token

switch (myToken->TokenType){ //cases for each token type }

myToken = GetToken(cin);

}

Page 11: Scanning & Regular Expressions

Result of GetToken

for (int i = 0 ; i < 100 ; i++){

for (int i = 0 ; i < 100 ; i++){

for (int i = 0 ; i < 100 ; i++){

TokenType: FOR

TokenType: LPAREN

Page 12: Scanning & Regular Expressions

Tokens and Languages

• The set of valid tokens of a particular type is a Language (in the formal sense)

• More specifically, it is a Regular Language

Page 13: Scanning & Regular Expressions

Language Formalities

• Language: set of strings • String: sequence of symbols• Alphabet: set of legal symbols for strings– Generally is used to denote an alphabet

Page 14: Scanning & Regular Expressions

Example Languages

• L1 = {aa, ab, bb} , = {a, b}• L2 = {,ab, abab, … }, = {a, b}• L3 = {strings of N a’s where N is an odd integer}, = {a}

• L4 = { } (one string with no symbols)

• L5 = { } (no strings at all)• L5 = Ø

Page 15: Scanning & Regular Expressions

Denoting Languages

• Expressions (regular languages only)

• Grammars– Set of rewrite rules that express all and only the strings in the language

• Automata– Machines that “accept” all and only the strings in the language

Page 16: Scanning & Regular Expressions

Primitive Regular Expressions

– L() = {} (no strings)

• – L() = {} (one string, no symbols)

• a where a is a member of – L(a) = {a} (one string, one symbol)

Page 17: Scanning & Regular Expressions

Combining Regular Expressions

• Choice: r | s (sometimes r+s)– L(r | s) = L(r ) L(s)

• Concatenation: rs – L(rs) = L(r)L(s) – All combinations of 1 from r and 1 from s

• Repetition: r*– L(r*) = L(r )L(rr) L(rrr ) …– 0 or more strings from r concatenated

Page 18: Scanning & Regular Expressions

Precedence

• Repetition before concatenation

• Concatenation before choice• Use parentheses to override

• aa* vs. (aa)*• ab|c vs. a(b|c)

Page 19: Scanning & Regular Expressions

Example Languages

• L1 = {aa, ab, bb} , = {a, b}• L2 = {,ab, abab, … }, S = {a, b}• L3 = {strings of N a’s where N is an odd integer}, S = {a}

• L4 = { } (one string with no symbols)

• L5 = { } (no strings at all)• L5 = Ø

Page 20: Scanning & Regular Expressions

R.E.’s for Examples

• L1 = aa | ab | bb• L1 = a(a|b) | bb• L1 = aa | (a|b) b• L2 = (ab)* not ab* !• L3 = a(aa)*

Page 21: Scanning & Regular Expressions

What are these languages?

• a* | b* | c*• a*b*c*• (a*b*)*• a(a|b)*c• (a|b|c)*bab(a|b|c)*

Page 22: Scanning & Regular Expressions

What are the RE’s?

• In the alphabet {a,b,c}:– All strings that are in alphabetical order

– All strings that have the first a before the first b, before the first c, e.g. ababbabca

– All strings that contain “abc”– All strings that do not contain “abc”

Page 23: Scanning & Regular Expressions

Extended Reg. Exp’s

• Additional operations for conveniencer+ = rr* (one or more reps). ( any character in the alphabet)

.* = any possible string from the alphabet

[a-z] = a|b|c|…|z[^aeiou] = b|c|d|f|g|h|j...


Recommended