{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# L4-Lex - Introduction to Compiler Design Lex A Lexical...

This preview shows pages 1–11. Sign up to view the full content.

Introduction to Compiler Design Lex – A Lexical Analyzer Generator Professor Yi-Ping You Department of Computer Science http://www.cs.nctu.edu.tw/~ypyou/ Page 1 Introduction to Compiler Design, Spring 2010

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Why bother using lex and yacc? Given a string, what would you do if you want to write a program to know how many “integer” appear? Page 2 Introduction to Compiler Design, Spring 2010
Well, maybe think with FA first… others while (c = string[i]) { if ((c >= “1”) && ( < “9”)) { 0-9 others A A 1-9 (c <= “9”)) { //check some states //change state //do something S 0 A B } else if (c == “0”) { : others : : } } Page 3 Introduction to Compiler Design, Spring 2010

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Deal with more complex cases To recognize “integer in decimal system” T g i “i t g i h t To recognize “integer in hex system” To recognize “real number in decimal system” Page 4 Introduction to Compiler Design, Spring 2010
Okay, draw FA first 0 9 f A F D A 1-9 0-9, a-f, A-F . E S 0 others others F B . a-f, A-F G H C Even you survive the FA design, the implementation will still be a disaster!! Page 5 Introduction to Compiler Design, Spring 2010

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Let lex and yacc save you Do we must deal them with bare hand from the ground? Both tools are developed by AT&T for text analyzing since 1970: Lex Lex generates C code for a lexical analyzer, or scanner Le ses patterns that match strings in the inp t and con erts Lex uses patterns that match strings in the input and converts the strings to tokens Yacc Yacc generates C code for s nta anal er or parser Yacc generates C code for syntax analyzer, or Yacc uses grammar rules that allow it to analyze tokens from Lex and create a syntax tree Lex divides data into the smallest meaningful Lex divides data into the smallest meaningful elements, and yacc deals the relation between those elements Page 6 Introduction to Compiler Design, Spring 2010
Compilation Flow Project 1 (lex) P j t 2 ( ) Project 2 (yacc) Project 3 (yacc) Project 4 (yacc) Page 7 Introduction to Compiler Design, Spring 2010

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How How Lex Lex help help It constructs FAs internally from regular expressions which are provided by users, and generates an efficient C codes to recognize them Lots of techniques you learned are applied Lex program Lex / Flex P 1 {action A 1 } (REs for Tokens) (input) RE Æ NFA NFA Æ DFA P 2 {action A 2 } P n {action A n } Optimize DFA Character Stream Token stream (and errors) DFA Simulation Page 8 Introduction to Compiler Design, Spring 2010 C program (output)
A A Lex Lex Example Example To recognize “integers” and “real numbers” %% 0|[1-9][0-9]* { printf("an integer\n"); Result: > 012 34.56 789 0.1 an intege } (0|[1-9][0-9]*)\.[0-9]* { printf("a real number\n"); an integer an integer others a real number } . { printf("others\n"); } others an integer others %% a real number Page 9 Introduction to Compiler Design, Spring 2010

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A A Yacc Yacc Example Example %% i t expression : cterm | expression ‘+’ cterm {printf(“+ expression\n”);} | expression ‘-’ cterm {printf(“- expression\n”);} ; cterm : cfactor \ | cterm ‘*’ cfactor {printf(“* expression\n”);} | cterm ‘/’ cfactor {printf(“/ expression\n”);} ;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}