Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UP Lecture 03

UP Lecture 03

Compilers
Lexical Analysis
(202401)

Javier Gonzalez-Sanchez PRO

December 06, 2023

Transcript

29 slides
  1. Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs High-Level Languages

    X,E,G,O,O #e1,I,I,0,7 @ OPR 19, AX STO x, AX LIT 5, AX OPR 21, AX LOD #e1,AX CAL 1, AX OPR 0, AX 5 Virtual Machine (interpreter) // sorce code int x; int foo () { read (x); print (5); } main () { foo (); } Lexer Parser Semantic Analyzer Code Generation 01001010101000010 01010100101010010 10100100000011011 11010010110101111 00010010101010010 10101001010101011 Assembler compilation execution
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Key Ideas

    Lexical Alphabet Symbol String (items) Word Token Rules Regular Expression Deterministic Finite Automata
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Let’s Work

    How many strings (words), and what is the token for each? Are there errors?
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Lexical Analysis

    1. int x = 5; 2. float y = "hello; 3. String @z = "9.5"; 4. int x = cse340; 5. if ( x > 14) while (5 == 5) if (int a) a = 1; 6. x = x; for ( ; ; ); 7. y = 13.45.0; 8. int me = 99999000001111222000000111111222223443483045830948; 9. while { x != 9} (); 10. int {x} = 10;
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Line 1.

    int x = 5 ; String Word Keyword String Word Id String Word Operator String Word Integer String Word Delimiter Token → 5 items, all correct, 5 words
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Line 2.

    float y = "hello; String Word Keyword String Word Id String Word Operator String --- --- Token → 4 items, 3 correct, 3 words + 1 wrong item
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Line 3.

    String @z = "9.5" ; String Word Keyword String --- --- String Word Operator String Word Value (String Type) String Word Delimiter Token → 5 items, 4 correct, 4 words + 1 wrong item I decide to ignore @Tags capability of Java
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Line 4.

    int x = cse340 ; String Word Keyword String Word Id String Word Operator String Word Id String Word Delimiter Token → 5 items, all correct, 5 words
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Line 5.

    if ( x > 14 21 items, all correct, 21 words ) while ( 5 5 == ) if ( int a ) a = 1 ;
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Line 6.

    10 items, all correct, 10 words x = x ; for ( ; ; ) ;
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Line 7.

    y = 13.45.0 ; String Word Id String Word Operator String Word Delimiter String --- --- Token → 4 items, 3 correct, 3 words + 1 wrong item
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Line 8.

    int me = 99999000001111222000000111111222223443483045830948 ; 5 items, all correct, 5 words
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Line 9.

    9 items, all correct, 9 words while { x != 9 } ( ) ;
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs Line 10.

    7 items, all correct, 7 words int { x } = 10 ;
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Lexical Analysis

    1. How many items in the following line? int x = 5; float y = "hello; 9 Answer
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Lexical Analysis

    2. How many items in the following line? String@z="9.5";intx=cse340;if(x> 14) 14 Answer
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Lexical Analysis

    3. How many items in the following line? while() 3 Answer
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Lexical Analysis

    4. How many items in the following line? (5 == 5) if (int a) a = 1; 14 Answer
  19. Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Lexical Analysis

    5. How many items in the following line? x = x; for ( ; ; ); 10 Answer
  20. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Lexical Analysis

    6. How many items in the following line? y = 13.45.0; 4 Answer
  21. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Lexical Analysis

    7. How many items in the following line? int me =99999000001111222000000111111222223443483045830948; 5 Answer
  22. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Lexical Analysis

    8. How many items in the following line? while { x != 9} ();int {x} = 10; 16 Answer
  23. Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Lexical Analysis

    9. How many items in the following line? "hello "world" bye" 3 Answer
  24. Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Test Yourselves

    1. void metodo ( int parametro ) { } 2. char w ; int w ; float w ; 3. hola mundo :-) 4. (5 == 5) if (int a) for 5. x = x + x < x / x ( x ) 6. float == int 7. 0xFF < > 512 8. for if int while void + 1/0 9. 5e-1-1-1-1-1-x
  25. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2026 Copyright. These

    slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming