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

UP Lecture 10

UP Lecture 10

Compilers
Grammars
(202502)

Javier Gonzalez-Sanchez

December 13, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs Compilers Lecture 10: Backus-Naur Form and Syntax Diagrams Dr.

    Javier Gonzalez-Sanchez jgonzalezs@up.edu.mx
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Outline Language

    Lexical Analysis (Lexer) Rules Symbols Token Tools Regular Expression DFA Syntactic Analysis (Parser) Grammar (Rules) Terminal Non-terminal Tools BNF (Backus-Naur Form) Syntax Diagrams
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs BNF (Backus-Naur

    Form) Formal, mathematical way to specify grammars ▪ → or ::= is defined as ▪ | or operator ▪ <nonterminal> or use uppercases ▪ terminal (lowercases) * John Backus and Peter Naur
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Extended BNF

    Extended BNF include notation to indicate: ▪ 0 or more occurrences {…} ▪ 1 or more occurrences + ▪ 0 or 1 occurrences […] ▪ Use of parentheses for grouping ( ) * Niklaus Wirth
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Test Yourselves

    5 integer / operator 5 integer Non-Terminals Terminals ::= 'integer' ( '+' | '-' | '*' | '/' ) 'integer' Expression Rules Start Symbol (root) Assignment ::= 'id' '=' Expression x ID = operator Assignment Expression Assignment
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Test Yourselves

    Grammar rule for calling a method: ▪ draw(x, y, z); ▪ print (a, b, c, d); ▪ done(); ▪ foobar (one, two, three, four, five); ▪ sqrt (x);
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Test Yourselves

    BNF <call_method> → identifier(<identifiers>); | identifier(); <identifiers> → identifier | identifier,<identifiers> EBNF <call_method> → identifier ('('<identifiers>')' | '('')' ) ';' <identifiers> → identifier | identifier,<identifiers> EBNF <call_method> → identifier'('[<identifiers>]')' ';' <identifiers> → identifier | identifier,<identifiers> EBNF <call_method> → identifier'('[<identifiers>]')' ';' <identifiers> → identifier { ,identifier }
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Notation ▪

    Ovals → Represent Terminals (Literals, Keywords, or Tokens) ▪ Rectangles → Represent Non-Terminals (Rules or Expressions) ▪ Lines → Represent the Flow of Parsing (Connections) : move left to right ▪ Lines Above (a baseline) → move right to left (usually a loop)
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Test Yourselves

    5 integer / operator 5 integer Non-Terminals Terminals ::= 'integer' ( '+' | '-' | '*' | '/' ) 'integer' Expression Rules Start Symbol (root) Assignment ::= 'id' '=' Expression x ID = operator Assignment Expression Assignment
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Grammar <PROGRAM>

    → '{' <BODY> '}’ <BODY> → {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'} <ASSIGNMENT> → identifier '=' <EXPRESSION> <VARIABLE> → ('int'|'float'|'boolean'|'char’|'string'|'void')identifier <WHILE> → 'while' '(' <EXPRESSION> ')' <PROGRAM> <IF> → 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>] <RETURN> → 'return' <PRINT> → ’print’ ‘(‘ <EXPRESSION> ‘)’ <EXPRESSION> →
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Homework |

    Test Yourselves Syntax Diagrams for Java: - Class - Attributes - Methods - Parameters - Instruction (stop here) Do not forget static, final, public, private, data-types, abstract, extends, implements …
  12. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. jgonzalezs@up.edu.mx Spring 2025 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.