SlideServe
Menu
Préférences
© SlideServe · v2026

Lex: A Convenient Lexical Analyzer with Regular Expressions

jacklynd avatar
15 diapositives · 367 Views · Jan 07, 2020

Learn how a Lex program recognizes strings and associated actions like identifying integers, identifiers, and keywords using regular expressions. Explore the internal structure of Lex.

Lex: A Convenient Lexical Analyzer with Regular Expressions - slide 1
01 / 15Lex: A Convenient Lexical Analyzer with Regular Expressions
1 / 17

Télécharger la présentation

Lex: A Convenient Lexical Analyzer with Regular Expressions
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Lex Costas Busch - LSU

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

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

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

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

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

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

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

  9. Default action: ECHO; Prints the string identified to the output Costas Busch - LSU

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

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

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

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

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

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