1 / 29

Unit-II

Unit-II. Syntax and Semantics. TEXT BOOKS : 1. CONCEPTS OF PROGRAMMING LANGUAGES BY ROBERT.W.SEBESTA 2. PROGRMMING LANGUAGES BY LOUDEN REFERENCES : 1. PROGRMMING LANGUAGES BY GHEZZI

jael-warner
Download Presentation

Unit-II

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. Unit-II Syntax and Semantics TEXT BOOKS : 1. CONCEPTS OF PROGRAMMING LANGUAGES BY ROBERT.W.SEBESTA 2. PROGRMMING LANGUAGES BY LOUDEN REFERENCES : 1. PROGRMMING LANGUAGES BY GHEZZI 2. PROGRMMING LANGUAGES BY WATT,W.DREAMTECH 1

  2. 2

  3. 3

  4. 4

  5. Introduction Syntax and semantics provide a language’s definition Syntax: the form or structure of the expressions, statements, and program units Semantics: the meaning of the expressions, statements, and program units E.g., while (x>20) { sum = sum + x; x = x+1; } 5

  6. The General Problem of Describing Syntax: Terminology A language is a set of sentences A sentence/statement is a string of characters over some alphabet A token is a category of lexemes (e.g., identifier) A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) 6

  7. The General Problem of Describing Syntax: Terminology E.g. language: { int index, count; … index = 2 * count + 17; } token: identifier token: int literal statement lexeme 7

  8. Formal Definition of Languages Recognizers A recognition device reads input strings of the language and decides whether the input strings belong to the language Example: syntax analysis part of a compiler Generators A device that generates sentences of a language One can determine if the syntax of a particular sentence is correct by comparing it to the structure of the generator 8

  9. Formal Methods of Describing Syntax Backus-Naur Form and Context-Free Grammars (BNF form) Most widely known method for describing programming language syntax Extended BNF Improves readability and writability of BNF LECTURE-2 9

  10. BNF and Context-Free Grammars Context-Free Grammars Developed by Noam Chomsky in the mid-1950s Natural language linguist Described four classes of grammars that define four classes of languages Two classes (context-free and regular) turned out to be useful for describing the syntax of programming languages LECTURE-2 10

  11. Backus-Naur Form (BNF) Backus-Naur Form (1959) Invented by John Backus to describe Algol 58, later modified by Peter Naur BNF is equivalent to context-free grammars BNF is a metalanguage used to describe another language In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols) E.g. <assign> -> <var> = <expression> a = ; 2 * b LECTURE-2 11

  12. BNF Fundamentals Non-terminals: BNF abstractions Terminals: lexemes and tokens Grammar: a collection of rules Examples of BNF rules: <ident_list> → identifier | identifier, <ident_list> <if_stmt> → if <logic_expr> then <stmt> LECTURE-3 12

  13. Specific Rule for Describing Lists Syntactic lists are described using recursion <ident_list>  ident |ident,<ident_list> LECTURE-3 13

  14. Grammars and Derivations A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) LECTURE-3 14

  15. Another Example Grammar Derivation of “A=B*(A+C)” <assign> => <id> = <expr> =>A = <expr> => A = <id> * <expr> => A = B * <expr> => A = B * (<expr>) => A = B * (<id> + <expr>) => A = B * (A + <expr>) = > A = B * (A + <id>) => A = B * ( A + C ) <assign>  <id > = <expr> <id>  A | B | C <expr>  <id> + <expr> | <id> * <expr> | (<expr>) | <id> LECTURE-4 15

  16. Extended BNF Optional parts are placed in brackets [ ] <selection> -> if (<expression>) <statement> [else <statement>] Repetitions (0 or more) are placed inside braces { } <ident_list> -> <identifier> {, <identifier>} When a single element must be chosen from a group, the options are placed in parentheses and separted by the OR operator, |. <term> -> <term> (* | / | %)<factor> LECTURE-4 16

  17. BNF and EBNF BNF<expr>  <expr> + <term> | <expr> - <term> | <term> <term>  <term> * <factor> | <term> / <factor> | <factor> <Factor-><exp>**<factor> |<exp> EBNF<expr>  <term> {(+ | -)<term>} <term>  <factor> {(* | /) <factor>} <factor>-><exp>{**<exp>} LECTURE-4 17

  18. EBNF variations In place of the arrow, a colon is used and the RHS is placed on the next line Instead of a vertical bar to separate alternative RHSs, they are simply placed on separate lines In place of squared brackets to indicate something being optional, the subscript opt is used. E.g. ConstructorDeclarator -> SimpleName(FormalParameterListopt) Rather than using the | symbol in a parenthesized list of elements to indicate a choice, the words “one of” are used. E.g. AssignmentOperator -> one of = *= /= %= += -= <<= >>= &= |= LECTURE-5 18

  19. Parse trees One of the most attractive features of grammars is that they naturally describe the hierarchical syntactic structure of the sentences of the languages they define. these hierarchical structures are called parse trees A hierarchical representation of a derivation LECTURE-5 19

  20. Parse Tree A hierarchical representation of a derivation <assign> A=B*(A+C) <id> = <expr> A <id> * <expr> B ( <expr> ) <id> + <expr> A <id> C LECTURE-5 20

  21. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees LECTURE-5 21

  22. An Ambiguous Expression Grammar <assign> -> <id> = <expr> <id> -> A|B|C <expr> -> <expr> + <expr> |<expr> * <expr> |(<expr>) |<id> A = B + C * A a.<assign> <id> = <expr> A <expr> + <expr> <id> <expr> * <expr> B <id> <id> C A b. <assign> <id> = <expr> A <expr> * < expr> <expr> + <expr> <id> <id> <id> A B C LECTURE-5 22

  23. Unambiguous grammar An unambiguous grammar for if-then-else:BNF rule given <if_stmt>->if<logic_expr>then<stmt> |if<logic_expr>then<stmt>else<stmt> The unambiguous grammar based on these ideas: <stmt>-><matched>|<unmatched> <matched>->if<logic-expr>then<matched>else<unmatched> |any non if stmt <unmatched>->if<logic_expr>then<stmt> |if<logic-expr>then<matched>else<unmatched LECTURE-6 23

  24. Attribute grammars Attribute grammars are grammars to which have been added attributes, attribute computation functions, and predicate functions. Attribute computation functions sometimes called semantic functions, are associated with grammar rules. Predicate functions, which state some of the syntax and static semantic rules of the language, are associated with grammar rule LECTURE-6 24

  25. Denotational semantics This is the widely known method for describing the meaning of programs. The fundamental concept of denotation semantics is to define for each language entity both a mathematical object and a function that maps instances of that entity onto instances of the mathematical object LECTURE-6 25

  26. Denotational semantics(cond) The state of a program Expressions Assignment statements Logical pretest loops evaluation LECTURE-6 26

  27. Axiomatic semantics Axiomatic semantics was defined in conjunction with the development of a method to prove the correctness of programs. In a proof,each statement of a program is both preceded and followed by a logical expression that specifies constraints on program variables The notation used to describe constraints,indeed the language of axiomatic semantics,is predicate calculus LECTURE-7 27

  28. Assertions Axiomatic semantics is based on mathematical logic.the logic expressions are called predicates,or assertions An assertion immediately preceding a program statement describes the constraints on the program variables at that point in the program is called precondition assertion an assertion immediately following a statement describes the new constraints on those variables after execution of the statement is called post condition assertion LECTURE-7 28

  29. Weakest precondition:is the least restrictive precondition that will guarantee the validity of the associated postcondition The weakest precondition can be computed only by an inference rule .an axiom is a logical statement that is assumed to be true Assignment statements: sequences: selection: Logical pretest loops: Evaluation: LECTURE-7 29

More Related