Presentation is loading. Please wait.

Presentation is loading. Please wait.

Costas Busch - LSU1 Lex. Costas Busch - LSU2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found the lex program takes.

Similar presentations


Presentation on theme: "Costas Busch - LSU1 Lex. Costas Busch - LSU2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found the lex program takes."— Presentation transcript:

1 Costas Busch - LSU1 Lex

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

3 Costas Busch - LSU3 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 Costas Busch - LSU4 In Lex strings are described with regular expressions “if” “then” “+” “-” “=“ /* operators */ /* keywords */ Lex program Regular expressions

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

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

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

8 Costas Busch - LSU8 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 Costas Busch - LSU9 Default action: ECHO; Prints the string identified to the output

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

11 Costas Busch - LSU11 1234 test var 566 78 9800 Input Output Integer Identifier Integer

12 Costas Busch - LSU12 % [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 Costas Busch - LSU13 1234 test var 566 78 9800 + temp Input Output Integer Identifier Integer Error in line: 3 Identifier

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

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


Download ppt "Costas Busch - LSU1 Lex. Costas Busch - LSU2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found the lex program takes."

Similar presentations


Ads by Google