Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2006Costas Busch - RPI1 Lex. Fall 2006Costas Busch - RPI2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found.

Similar presentations


Presentation on theme: "Fall 2006Costas Busch - RPI1 Lex. Fall 2006Costas Busch - RPI2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found."— Presentation transcript:

1 Fall 2006Costas Busch - RPI1 Lex

2 Fall 2006Costas Busch - RPI2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found the lex program takes an action

3 Fall 2006Costas Busch - RPI3 Var = 12 + 9; if (test > 20) temp = 0; else while (a < 20) temp++; Lex program Identifier: Var Operand: = Integer: 12 Operand: + Integer: 9 Semicolumn: ; Keyword: if Parenthesis: ( Identifier: test.... Input Output

4 Fall 2006Costas Busch - RPI4 In Lex strings are described with regular expressions “if” “then” “+” “-” “=“ /* operators */ /* keywords */ Lex program Regular expressions

5 Fall 2006Costas Busch - RPI5 (0|1|2|3|4|5|6|7|8|9)+ /* integers */ /* identifiers */ Regular expressions (a|b|..|z|A|B|...|Z)+ Lex program

6 Fall 2006Costas Busch - RPI6 integers [0-9]+(0|1|2|3|4|5|6|7|8|9)+

7 Fall 2006Costas Busch - RPI7 (a|b|..|z|A|B|...|Z)+ [a-zA-Z]+ identifiers

8 Fall 2006Costas Busch - RPI8 Each regular expression has an associated action (in C code) Examples: \n Regular expressionAction linenum++; [a-zA-Z]+ printf(“identifier”); [0-9]+ prinf(“integer”);

9 Fall 2006Costas Busch - RPI9 Default action: ECHO; Prints the string identified to the output

10 Fall 2006Costas Busch - RPI10 A small lex program % [a-zA-Z]+printf(“Identifier\n”); [0-9]+printf(“Integer\n”); [ \t\n] ; /*skip spaces*/

11 Fall 2006Costas Busch - RPI11 1234 test var 566 78 9800 Input Output Integer Identifier Integer

12 Fall 2006Costas Busch - RPI12 % [a-zA-Z]+ printf(“Identifier\n”); [0-9]+ prinf(“Integer\n”); [ \t] ; /*skip spaces*/. printf(“Error in line: %d\n”, linenum); Another program %{ int linenum = 1; %} \nlinenum++;

13 Fall 2006Costas Busch - RPI13 1234 test var 566 78 9800 + temp Input Output Integer Identifier Integer Error in line: 3 Identifier

14 Fall 2006Costas Busch - RPI14 Lex matches the longest input string “if” “ifend” Regular Expressions Input: ifend if Matches: “ifend” “if” Example:

15 Fall 2006Costas Busch - RPI15 Internal Structure of Lex Lex Regular expressions NFADFA Minimal DFA The final states of the DFA are associated with actions


Download ppt "Fall 2006Costas Busch - RPI1 Lex. Fall 2006Costas Busch - RPI2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found."

Similar presentations


Ads by Google