1 / 76

Syntax and Semantics in Programming Languages

Understand the rules and composition of programming languages, including syntax and semantics. Learn about grammars, backus-naur form, parse trees, and more.

dbone
Télécharger la présentation

Syntax and Semantics in Programming Languages

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. CSCE 330Programming Language StructuresChapter 2: Syntax(Slides based on Tucker and Noonan) Fall 2010 Marco Valtorta mgv@cse.sc.edu A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth

  2. Syntax and Semantics • Syntax is the set of rules that specify the composition of programs from letters, digits and other characters. • Semantics is the set of rules that specify what the result/outcome of a program is. • Problems with English language description of Syntax and Semantics: • verbosity • ambiguity

  3. Contents 2.1 Grammars 2.1.1 Backus-Naur Form 2.1.2 Derivations 2.1.3 Parse Trees 2.1.4 Associativity and Precedence 2.1.5 Ambiguous Grammars 2.2 Extended BNF 2.3 Syntax of a Small Language: Clite 2.3.1 Lexical Syntax 2.3.2 Concrete Syntax 2.4 Compilers and Interpreters 2.5 Linking Syntax and Semantics 2.5.1 Abstract Syntax 2.5.2 Abstract Syntax Trees 2.5.3 Abstract Syntax of Clite

  4. Thinking about Syntax • The syntax of a programming language is a precise description of all its grammatically correct programs. • Precise syntax was first used with Algol 60, and has been used ever since. • Three levels: • Lexical syntax • Concrete syntax • Abstract syntax

  5. Levels of Syntax • Lexical syntax = all the basic symbols of the language (names, values, operators, etc.) • Concrete syntax = rules for writing expressions, statements and programs. • Abstract syntax = internal representation of the program, favoring content over form. E.g., • C: if ( expr ) ... discard ( ) • Ada: if ( expr ) then discard then

  6. 2.1 Grammars • A metalanguage is a language used to define other languages. • A grammar is a metalanguage used to define the syntax of a language. • Our interest: using grammars to define the syntax of a programming language.

  7. 2.1.1 Backus-Naur Form (BNF) • Stylized version of a context-free grammar (cf. Chomsky hierarchy) • Sometimes called Backus Normal Form • First used to define syntax of Algol 60 • Now used to define syntax of most major languages

  8. BNF History In Java: <IfThenStat> ::= if (<Expr>) <Stat> <IfThenElseStat> ::= if (<Expr>) <StatNoShortIf> else <Stat>

  9. BNF Grammar Set of productions: P terminal symbols: T nonterminal symbols: N start symbol: A production has the form whereand

  10. Consider the grammar: binaryDigit 0 binaryDigit 1 or equivalently: binaryDigit 0 | 1 Here, | is a metacharacter that separates alternatives. Example: Binary Digits

  11. Consider the grammar: IntegerDigit | Integer Digit Digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 We can derive any unsigned integer, like 352, from this grammar. 2.1.2 Derivations

  12. A 6-step process, starting with: Integer Derivation of 352 as an Integer

  13. Use a grammar rule to enable each step: Integer  Integer Digit Derivation of 352 (step 1)

  14. Replace a nonterminal by a right-hand side of one of its rules: Integer  Integer Digit  Integer 2 Derivation of 352 (steps 1-2)

  15. Each step follows from the one before it. Integer  Integer Digit  Integer 2  Integer Digit 2 Derivation of 352 (steps 1-3)

  16. Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2 Derivation of 352 (steps 1-4)

  17. Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2 Derivation of 352 (steps 1-5)

  18. You know you’re finished when there are only terminal symbols remaining. Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2 3 5 2 Derivation of 352 (steps 1-6)

  19. Integer  Integer Digit  Integer Digit Digit  Digit Digit Digit 3 Digit Digit 3 5 Digit 3 5 2 This is called a leftmost derivation, since at each step the leftmost nonterminal is replaced. (The first one was a rightmost derivation.) A Different Derivation of 352

  20. Notation for Derivations Integer * 352 Means that 352 can be derived in a finite number of steps using the grammar for Integer. 352 L(G) Means that 352 is a member of the language defined by grammar G. L(G) = { T* | Integer * } Means that the language defined by grammar G is the set of all symbol strings  that can be derived as an Integer.

  21. 2.1.3 Parse Trees • A parse tree is a graphical representation of a derivation. The root of the tree is the start symbol. Each internal node of the tree corresponds to a step in the derivation. The children of a node represent a right-hand side of a production. Each leaf node represents a symbol of the derived string, reading from left to right.

  22. Integer Integer Digit E.g., The stepInteger  Integer Digitappears in the parse tree as:

  23. Parse Tree for 352 as an Integer Figure 2.1

  24. The following grammar defines the language of arithmetic expressions with 1-digit integers, addition, and subtraction. Expr  Expr + Term | Expr – Term | Term Term  0 | ... | 9 | ( Expr ) Arithmetic Expression Grammar

  25. Parse of the String 5-4+3 Figure 2.2

  26. Contents 2.1 Grammars 2.1.1 Backus-Naur Form 2.1.2 Derivations 2.1.3 Parse Trees 2.1.4 Associativity and Precedence 2.1.5 Ambiguous Grammars 2.2 Extended BNF 2.3 Syntax of a Small Language: Clite 2.3.1 Lexical Syntax 2.3.2 Concrete Syntax 2.4 Compilers and Interpreters 2.5 Linking Syntax and Semantics 2.5.1 Abstract Syntax 2.5.2 Abstract Syntax Trees 2.5.3 Abstract Syntax of Clite

  27. 2.1.4 Associativity and Precedence • A grammar can be used to define associativity and precedence among the operators in an expression. E.g., + and - are left-associative operators in mathematics; * and / have higher precedence than + and - . • Consider the grammar G1: Expr -> Expr + Term | Expr – Term | Term Term -> Term * Factor | Term / Factor | Term % Factor | Factor Factor -> Primary ** Factor | Primary Primary -> 0 | ... | 9 | ( Expr )

  28. Parse of 4**2**3+5*6+7 for Grammar G1 Figure 2.3

  29. Precedence Associativity Operators 3 right ** 2 left * / % \ 1 left + - Note: These relationships are shown by the structure of the parse tree: highest precedence at the bottom, and left-associativity on the left at each level. Associativity and Precedence for Grammar G1 Table 2.1

  30. 2.1.5 Ambiguous Grammars • A grammar is ambiguous if one of its strings has two or more different parse trees. E.g., Grammar G1 above is unambiguous. • C, C++, and Java have a large number of • operators and • precedence levels • Instead of using a large grammar, we can: • Write a smaller ambiguous grammar, and • Give separate precedence and associativity (e.g., Table 2.1)

  31. Expr -> Expr Op Expr | ( Expr ) | Integer Op -> + | - | * | / | % | ** Notes: G2 is equivalent to G1. i.e., its language is the same. G2 has fewer productions and nonterminals than G1. However, G2 is ambiguous. An Ambiguous Expression Grammar G2

  32. Ambiguous Parse of 5-4+3 Using Grammar G2 Figure 2.4

  33. IfStatement -> if ( Expression ) Statement | if ( Expression ) Statement else Statement Statement -> Assignment | IfStatement | Block Block -> { Statements } Statements -> Statements Statement | Statement The Dangling Else

  34. With which ‘if’ does the following ‘else’ associate? if (x < 0) if (y < 0) y = y - 1; else y = 0; Answer: either one! Example of Dangling Else

  35. The Dangling Else Ambiguity Figure 2.5

  36. Solving the dangling else ambiguity • Algol 60, C, C++: associate each else with closest if; use {} or begin…end to override. • Algol 68, Modula, Ada: use explicit delimiter to end every conditional (e.g., if…fi) • Java: rewrite the grammar to limit what can appear in a conditional: IfThenStatement -> if ( Expression ) Statement IfThenElseStatement -> if ( Expression )StatementNoShortIf else Statement The category StatementNoShortIf includes all except IfThenStatement.

  37. 2.2 Extended BNF (EBNF) • BNF: • recursion for iteration • nonterminals (abstractions) for grouping • EBNF: additional metacharacters • { } for a series of zero or more • ( ) for a list, must pick one • [ ] for an optional list; pick none or one

  38. EBNF Examples Expression is a list of one or more Terms separated by operators + and - Expression-> Term{ (+ | -)Term} IfStatement -> if (Expression)Statement[elseStatement] C-style EBNF lists alternatives vertically and uses opt to signify optional parts. E.g., IfStatement: if ( Expression ) Statement ElsePartopt ElsePart: else Statement

  39. We can always rewrite an EBNF grammar as a BNF grammar.E.g., A-> x { y } z can be rewritten: A -> x A' z A' -> | y A' (Rewriting EBNF rules with ( ), [ ] is left as an exercise.) While EBNF is no more powerful than BNF, its rules are often simpler and clearer. EBNF to BNF

  40. Syntax Diagram for Expressions with Addition Figure 2.6 includes subtraction

  41. Contents 2.1 Grammars 2.1.1 Backus-Naur Form 2.1.2 Derivations 2.1.3 Parse Trees 2.1.4 Associativity and Precedence 2.1.5 Ambiguous Grammars 2.2 Extended BNF 2.3 Syntax of a Small Language: Clite 2.3.1 Lexical Syntax 2.3.2 Concrete Syntax 2.4 Compilers and Interpreters 2.5 Linking Syntax and Semantics 2.5.1 Abstract Syntax 2.5.2 Abstract Syntax Trees 2.5.3 Abstract Syntax of Clite

  42. 2.3 Syntax of a Small Language: Clite • Motivation for using a subset of C: Grammar Language (pages) Reference Pascal 5 Jensen & Wirth C 6 Kernighan & Richie C++ 22 Stroustrup Java 14 Gosling, et. al. • The Clite grammar fits on one page (Figure 2.7 on p.38 [T]; next 3 slides), so it’s a far better tool for studying language design.

  43. Fig. 2.7Clite Grammar: Statements Program int main ( ){ Declarations Statements } Declarations  {Declaration } Declaration  Type Identifier [ [ Integer ] ]{ , Identifier [ [ Integer ] ] }; Type  int | bool | float | char Statements  {Statement } Statement  ; | Block | Assignment | IfStatement | WhileStatement Block  { Statements } Assignment  Identifier [ [ Expression ] ]=Expression ; IfStatement  if (Expression ) Statement [else Statement ] WhileStatement  while (Expression ) Statement

  44. Fig. 2.7Clite Grammar: Expressions Expression  Conjunction {|| Conjunction } Conjunction  Equality {&& Equality } Equality  Relation [ EquOp Relation ] EquOp  == | != Relation  Addition [ RelOp Addition ] RelOp  < | <= | > | >= Addition  Term { AddOp Term } AddOp  + | - Term  Factor { MulOp Factor } MulOp  * | / | % Factor  [ UnaryOp ]Primary UnaryOp  - | ! Primary  Identifier [[ Expression ]] | Literal | ( Expression ) | Type ( Expression )

  45. Fig. 2.7Clite grammar: lexical level Identifier  Letter { Letter | Digit } Letter  a | b | ... | z | A | B | ... | Z Digit  0 | 1 | ... | 9 Literal  Integer | Boolean | Float | Char Integer  Digit { Digit } Boolean  true | False Float  Integer . Integer Char  ‘ ASCII Char ‘

  46. Issues Not Addressed by this Grammar • Comments • Whitespace • Distinguishing one token <= from two tokens < = • Distinguishing identifiers from keywords like if • These issues are addressed by identifying two levels: • lexical level • syntactic level • All issues above are lexical ones

  47. 2.3.1 Lexical Syntax • Input: a stream of characters from the ASCII set, keyed by a programmer. • Output: a stream of tokens or basic symbols, classified as follows: • Identifiers e.g.,Stack, x, i, push • Literals e.g., 123, 'x', 3.25, true • Keywordsbool char else false float if int main true while • Operators e.g., = || && == != < <= > >= + - * / ! • Punctuation; , { } ( ) • “A token is a logically cohesive sequence of characters representing a single symbol” [T, p.60]

  48. in eot ident. colon ident. becomes op. intlit ident. y 1 : Integer y + in := Scan: Divide Input into Tokens let var y: Integerin !new year y := y+1 Scan is a synonim of lexically analyze Tokens are “words” in the input, for example keywords, operators, identifiers, literals, etc. An example mini Triangle source program: scanner let var ident. ... let var y ...

  49. Developing a Scanner The scanner will return an array of Tokens: public class Token { byte kind; String spelling; final static byte IDENTIFIER = 0; INTLITERAL = 1; OPERATOR = 2; BEGIN = 3; CONST = 4; ... ... public Token(byte kind, String spelling) { this.kind = kind; this.spelling = spelling; if spelling matches a keyword change my kind automatically } ... }

  50. Whitespace Whitespace is any space, tab, end-of-line character (or characters), or character sequence inside a comment No token may contain embedded whitespace (unless it is a character or string literal) Example: >= one token > = two tokens

More Related