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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

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

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

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

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

View Full DocumentRight Arrow Icon
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 nce 1970: since 1970: Lex Lex generates C code for a lexical analyzer, or scanner Lexuses patterns that match strings in the input and converts the strings to tokens Yacc Yacc generates C code for syntax analyzer, or parser Yacc uses grammar rules that allow it to analyze tokens from Lex and create a syntax tree ex ivides 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
Background image of page 6
Compilation Flow Project 1 (lex) Project 2 (yacc) Project 3 (yacc) Project 4 (yacc) Page 7 Introduction to Compiler Design, Spring 2010
Background image of page 7

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

View Full DocumentRight Arrow Icon
How How Lex Lex help help It constructs FAs internally from regular xpressions hich are provided by users, and expressions which are provided by users, and generates an efficient C codes to recognize them ots of techniques you learned are applied Lots of techniques you learned are applied Lex program Lex / Flex P {action A } (REs for Tokens) (input) RE Æ NFA NFA Æ DFA 1 1 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)
Background image of page 8
A A Lex Lex Example Example To recognize “integers” and “real numbers” %% 0|[1-9][0-9]* { rintf "an integer "); Result: > 012 34.56 789 0.1 n intege printf("an integer\n"); } (0|[1-9][0-9]*)\.[0-9]* { printf("a real number\n"); an integer an integer others real number } . { printf("others\n"); a real number others an integer thers } %% others a real number Page 9 Introduction to Compiler Design, Spring 2010
Background image of page 9

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

View Full DocumentRight Arrow Icon
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”);}
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 56

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online