1 / 40

Lex

Lex. Lex is a lexical analyzer. Output. Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn: ; Keyword: if Paren: ( Ident: test Oper: >. Input. Var = 12 + 9; if (test > 20) temp = 0; else while (a < 20) temp++;. Lex. For each kind of strings

teresaross
Download Presentation

Lex

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lex

  2. Lex is a lexical analyzer Output Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn: ; Keyword: if Paren: ( Ident: test Oper: > .... Input Var = 12 + 9; if (test > 20) temp = 0; else while (a < 20) temp++; Lex

  3. For each kind of strings there is a regular expression Lex Regular expressions “+” “-” “=“ /* operators */ “if” “then” /* keywords */

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

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

  6. identifiers (a|b|..|z|A|B|...|Z)+ [a-zA-Z]+

  7. Each regular expression has an action: Examples: Regular expression Action linenum++ \n prinf(“integer”); [0-9]+ [a-zA-Z]+ printf(“identifier”);

  8. Default action: ECHO; Print the string identified to the output

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

  10. Output Input Integer Identifier Identifier Integer Integer Integer 1234 test var 566 78 9800

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

  12. Output Input Integer Identifier Identifier Integer Integer Integer Error in line 3 Identifier 1234 test var 566 78 9800 + temp

  13. Lex matches the longest input string Regular Expressions “if” “ifend” ifend if ifn Input: Matches: “ifend” “if” nomatch

  14. Internal Structure of Lex Lex Minimal DFA Regular expressions NFA DFA The final states of the DFA are associated with actions

  15. Compilers

  16. Machine Code Program Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3 ... v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } ...... Compiler

  17. Compiler Lexical analyzer parser machine code program

  18. Parser knows the grammar of the programming language

  19. Parser PROGRAM -> STMT_LIST STMT_LIST -> STMT STMT_LIST | STMT; STMT -> EXPR ; | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR -> EXPR + EXPR | EXPR - EXPR | ID IF_STMT -> if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT-> while (EXPR) do STMT

  20. The parser constructs the derivation for the particular input program derivation Parser input E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 E -> E + E | E * E | INT 10 + 2 * 5

  21. derivation tree derivation E E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 + E E 10 E E * 2 5

  22. derivation tree E machine code + E E mult t1, 10, 5 add t2, 10, t1 10 E E * 2 5

  23. Parsing

  24. Parser input string derivation grammar

  25. Example: Parser derivation input ?

  26. Exhaustive Search Phase 1:

  27. Phase 2 Phase 1

  28. Phase 2 Phase 1

  29. Phase 2 Phase 3

  30. Final result of exhaustive search (Top-down parsing) Parser input derivation

  31. Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :

  32. For grammar with rules Time for phase 1: possible derivations

  33. Time for phase 2: possible derivations

  34. Time for phase : possible derivations

  35. Total time needed for string : Extremely bad!!!

  36. There exist faster algorithms for specialized grammars S-grammar: string of variables symbol appears once

  37. S-grammar example: Each string has a unique derivation

  38. For S-grammars: In the exhaustive search parsing there is only one choice in each phase Time for a phase: 1 Total time for parsing string :

  39. For general context-free grammars: There exists a parsing algorithm that parses a string in time

More Related