1 / 24

Chapter 3 Syntax Part 1

Chapter 3 Syntax Part 1. CMSC 331 Shon Vick. Syntax and Semantics. Syntax - the form or structure of the expressions – whether an expression is well formed Semantics – the meaning of an expression. Syntactic Structure.

john
Télécharger la présentation

Chapter 3 Syntax Part 1

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. Chapter 3Syntax Part 1 CMSC 331 Shon Vick

  2. Syntax and Semantics • Syntax - the form or structure of the expressions – whether an expression is well formed • Semantics – the meaning of an expression

  3. Syntactic Structure • Syntax almost always expressed using some variant of a notation called a context-free grammar (CFG) or simply grammar • BNF • EBNF • Syntax Graph

  4. A CFG has 4 parts • A set of tokens (lexemes), known as terminal symbols • A set of non-terminals • A set of rules (productions) where each production consists of a left-hand side (LHS) and a right-hand side (RHS) The LHS is a non-terminal and the RHS is a sequence of terminals and/or non-terminal symbols. • A special non-terminal symbol designated as the start symbol

  5. An example of BNF syntax for real numbers <r> ::= <ds> . <ds> <ds> ::= <d> | <d> <ds> <d> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7| 8 | 9 < > encloses non-terminal symbols ::= 'is' or 'is made up of ' or 'derives' (sometimes denoted with an arrow ->) | or

  6. Example • On the example from the previous slide: • What are the tokens? • What are the lexemes? • What are the non terminals? • What are the productions?

  7. BNF Points • A non terminal can have more than RHS or an OR can be used • Lists or sequences are expressed via recursion • A derivation is just a repeated set of production (rule) applications • Examples

  8. Example Grammar <program> -> <stmts> <stmts> -> <stmt> | <stmt> ; <stmts> <stmt> -> <var> = <expr> <var> -> a | b | c | d <expr> -> <term> + <term> | <term> - <term> <term> -> <var> | const

  9. Example Derivation <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

  10. Parse Trees • Alternative representation for a derivation • Example parse tree for the previous example stmts stmt expr var = term term + a var const b

  11. Parse Trees PS -> P | P PS P -> e | '(' PS ')' | '<' PS '>' | '[' PS ']' What’s the parse tree for this statement ? < [ ] [ < > ] >

  12. Ambiguity • Two parse trees for the same expression • Consider the following grammar string -> string + string | string - string digit -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 What are the two trees for 9 - 5 + 2

  13. EBNF - Extended BNF • Like BNF except that • Non-terminals start w/ uppercase • Parens are used for grouping terminals • Braces {} represent zero or more occurrences (iteration ) • Brackets [] represent an optional construct , that is a construct that appears either once or not at all.

  14. EBNF example Exp -> Term { ('+' | '-') Term } Term -> Factor { ('*' | '/') Factor } Factor -> '(' Exp ')' | variable | constant

  15. EBNF/BNF • EBNF and BNF are equivalent • How can {} be expressed in BNF? • How can ( ) be expressed? • How can [ ] be expressed?

  16. Syntax Graphs Terminal in circles nonterminals in rectangles; Syntax Graphs - put the terminals in circles or ellipses and put the nonterminals in rectangles; connect with lines with arrowheads e.g., Pascal type declarations type_identifier ( identifier ) , constant .. constant

  17. EBNF to BNF <goal> -> [<a>]{xy} <b> <a> -> z {z} <b> -> x [y] z

  18. EBNF to BNF <exp> -> term {+ <exp>} <term> -> <factor> {* <term>} <factor> -> NUMBER | ( <exp> )

  19. BNF to EBNF <g> - > <a> <g> -> x <b> <a> <a> -> y <a> -> x <a> <b> -> <a> <b> -> <a> <b> <b> -> y <b>

  20. Exercises With EBNF/BNF • Give a BNF/EBNF for an expression • with an odd number of a’s followed by • an even number of b’s • Give the BNF/EBNF for an expression • with some number of a’s following by 1 • plus that number of b’s

  21. Associativity Consider the following productions: list -> list + digit | (1) list - digit | digit digit -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 right -> letter = right | (2) letter letter -> a | b | .... z

  22. Associativity • What productions would be generated for the following parse trees for the input • 9 - 5 –2 • a = b = c • How can we determine this from the grammar as given

  23. Precedence • We've shown how to handle associativity in a grammar, how about precedence • Does 9 + 5 * 2 mean ( 9 + 5 ) * 2 9 + ( 5 * 2 )

  24. Precedence E -> E + T | E - T | T T -> T * F | T / F | F F -> digit | ( E ) What binds tighter? * or +

More Related