syntactic
play

Syntactic list of tokens analysis Syntactic analyzer grammar: - PowerPoint PPT Presentation

Syntactic list of tokens analysis Syntactic analyzer grammar: context free format: BNF (implementation: pushdown (stack) automaton ) parse tree (symbol table?) 90 Check that the program respects the syntax Description


  1. Syntactic list of tokens analysis Syntactic analyzer ● grammar: context free ● format: BNF ● (implementation: pushdown (stack) automaton ) parse tree (symbol table?) 90

  2. ● Check that the program respects the syntax Description ● Generate parse tree and (and maybe of the start symbol table) for code generation syntax ● Context free grammar – Parsing is represented by rules – Most commonly using BNF (or EBNF) – Rules do not depend on the context in which they appear. 91

  3. ● Notation to describe syntax ● Widely used in many areas of computing BNF (Backus-Naur ● Describe syntax by giving syntax rules Form) on how things can be composed from other things ● Terminal symbol : basic tokens coming from lexical analysis (not composed of other symbols) ● Non-terminal symbol : symbol created by a syntax rule from other symbols (both terminal and non-terminal) 92

  4. Simple <variable definition> ::= examples of identifier identifier '=' <expression> ';' syntactic (type) (name) rules <iteration statement> ::= 'while' '(' <expression> ')' <statement> <assignment> ::= identifier '=' <expression> ';' <statement> ::= <iteration statement> <statement> ::= <assignment> <statement> ::= <compound statement> Statements, <compound statement> ::= '{' <statement seq> '}' Code block <statement seq> ::= <statement> <statement seq> ::= <statement> <statement seq> 93

  5. BNF rules: <expr> ::= <expr> + <term> Example of <expr> ::= <expr> - <term> a grammar <expr> ::= <term> <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) Non-terminals expr, term, factor, number terminals number , ( , ) , + , - , * , / expr Beginning symbol 94

  6. 2 * ( 12 + 3 ) Deriving exp. <expr> ::= <expr> + <term> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 95

  7. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) Deriving exp. <expr> ::= <expr> + <term> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 96

  8. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving exp. <expr> ::= <expr> + <term> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 97

  9. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) exp. <expr> ::= <expr> + <term> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 98

  10. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. <expr> ::= <expr> + <term> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 99

  11. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. = number * ( <expr> ) <expr> ::= <expr> + <term> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 100

  12. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. = number * ( <expr> ) <expr> ::= <expr> + <term> = number * <factor> 2 * ( 12 + <expr> ::= <expr> - <term> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 101

  13. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. = number * ( <expr> ) <expr> ::= <expr> + <term> = number * <factor> 2 * ( 12 + <expr> ::= <expr> - <term> = <factor> * <factor> <expr> ::= <term> 3 ) <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 102

  14. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. = number * ( <expr> ) <expr> ::= <expr> + <term> = number * <factor> 2 * ( 12 + <expr> ::= <expr> - <term> = <factor> * <factor> <expr> ::= <term> 3 ) = <term> * <factor> <term> ::= <term> * <factor> <term> ::= <term> / <factor> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 103

  15. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. = number * ( <expr> ) <expr> ::= <expr> + <term> = number * <factor> 2 * ( 12 + <expr> ::= <expr> - <term> = <factor> * <factor> <expr> ::= <term> 3 ) = <term> * <factor> <term> ::= <term> * <factor> <term> ::= <term> / <factor> = <term> <term> ::= <factor> <factor> ::= number <factor> ::= ( <expr> ) 104

  16. 2 * ( 12 + 3 ) (lexer) = number * ( number + number ) = number * ( <factor> + <factor> ) Deriving = number * ( <term> + <term> ) = number * ( <expr> + <term> ) exp. = number * ( <expr> ) <expr> ::= <expr> + <term> = number * <factor> 2 * ( 12 + <expr> ::= <expr> - <term> = <factor> * <factor> <expr> ::= <term> 3 ) = <term> * <factor> <term> ::= <term> * <factor> <term> ::= <term> / <factor> = <term> <term> ::= <factor> = <expr> (Success!) <factor> ::= number <factor> ::= ( <expr> ) 105

  17. Expression 2 * ( 12 + 3 ) E = expression E as a parse tree Tm = term Fc = factor Tm Tm Fc Fc E E Tm Tm Fc Fc num(2) * ( num(12) + num(3) ) 106

  18. Ambiguous grammar <expr> ::= <expr> + <expr> <expr> ::= <expr> - <expr> <expr> ::= <expr> * <expr> <expr> ::= <expr> / <expr> <expr> ::= ( <expr> ) <expr> ::= number 107

  19. "Dangling else" Ambiguity if E1 then if E1 then example if E2 then if E2 then S1 S1 Pascal: else else (also C/C++) S2 S2 Grammar: <stmt> ::= <if_stmt> | ... <if_stmt> ::= if <expr> then <stmt> <if_stmt> ::= if <expr> then <stmt> else <stmt> 108

  20. <if-stmt> <if-stmt> ”Dangling else” in if <expr> then <stmt> else <stmt> if <expr> then <stmt> parse trees <if-stmt> <if-stmt> if <expr> then <stmt> if <expr> then <stmt> else <stmt> <stmt> ::= <if_stmt> | ... <if_stmt> ::= if <expr> then <stmt> <if_stmt> ::= if <expr> then <stmt> else <stmt> 109

Recommend


More recommend