Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.

Similar presentations


Presentation on theme: "Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical."— Presentation transcript:

1 Syntax(1)

2 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical syntax Concrete syntax Abstract syntax

3 3 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, favouring content over form.

4 4 Grammars  A metalanguage is a language used to define other languages.  A grammar is a metalanguage used to define the syntax of a language.  Backus-Naur Form (BNF) version of a context-free grammar

5 5 BNF Grammar  Set of terminal symbols: T  Set of nonterminal symbols: N  Set of productions: P  Start symbol: S N

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

7 7 Derivations  A grammar for integers: Integer  Digit | Integer Digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9  How we can derive any unsigned integer, like 352, from this grammar?

8 8 Driving the Integer “352” using Integers Grammar Integer  Integer Digit  Integer 2  Integer Digit 2  Integer 5 2  Digit 5 2  3 5 2 Integer  Integer Digit  Integer Digit Digit  Digit Digit Digit  3 Digit Digit  3 5 Digit  3 5 2 rightmost derivation leftmost derivation

9 9 Parse Trees  A parse tree is a graphical representation of a derivation. Each internal node of the tree corresponds to a step in the derivation. Each child of a node represents a right- hand side of a production. Each leaf node represents a symbol of the derived string, reading from left to right.

10 10 Example: The step Integer  Integer Digit  appears in the parse tree as: Integer Digit

11 11 Example: Parse Tree for “352” as an Integer Integer  Digit | Integer Digit Digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

12 12 Arithmetic Expression Grammar  The following grammar defines the language of arithmetic expressions with 1-digit integers, addition, and subtraction.  Example : Parse of the String 5-4+3 Expr  Expr + Term | Expr – Term | Term Term  0 |... | 9 | ( Expr )

13 13 Example: Parse of the String 5-4+3 Expr  Expr + Term | Expr – Term | Term Term  0 |... | 9 | ( Expr )

14 14 Precedence  A grammar can be used to define and precedence among the operators in an expression. * and / have higher precedence than + and -.  Example: expr → expr + term | expr – term | term term → term * factor | term \ factor | term % factor |factor factor → digit | (expr) digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

15 15 Associativity  A grammar can be used to define associativity among the operators in an expression. + and - are left-associative operators  Example: Parse of 4**2**3+5*6+7 Expr → Expr + Term | Expr – Term | Term Term → Term * Factor | Term / Factor |Term % Factor | Factor Factor → Primary ** Factor | Primary Primary → 0 |... | 9 | ( Expr )

16 16 Example Parse of 4**2**3+5*6+7 Expr → Expr + Term | Expr – Term | Term Term → Term * Factor | Term / Factor | Term % Factor | Factor Factor → Primary ** Factor | Primary Primary → 0 |... | 9 | ( Expr )

17 17 Ambiguous Grammars  A grammar can have more than one parse tree.  Consider next grammar  Example: Draw all a parse tree of (9-5+2) using previous grammar string → string + string | string – string |0|1|2|3|4|5|6|7|8|9

18 18 string 259+- ( 9 – 5 ) + 29 - ( 5 + 2 ) string 952-+

19 19 Another Example: The Dangling Else  With which ‘if’ does the following ‘else’ associate if (x < 0) if (y < 0) y = y - 1; else y = 0;

20 20 If statement Grammar IfStatement → if ( Expression ) Statement | if ( Expression ) Statement else Statement Statement → Assignment | IfStatement | Block Block → { Statements } Statements → Statements Statement | Statement

21 21 The Dangling Else Ambiguity

22 22 C and C++ Solution to dangling else ambiguity  Associate each else with closest if  Use { } to override

23 23 Extended BNF (EBNF)  Additional metacharacters Braces { }  zero or more occurrences of the symbols that are enclosed within braces. Parentheses( )  Enclose a series of alternatives from which one must be chosen Brackets [ ]  for an optional list; pick none or one

24 24 EBNF Example  BNE Expr → Expr + Term | Expr – Term| Term  EBNE Expr → Term {(+ | -) Term}  EBNE A → x { y } z  BNE A → x A' z A' → | y A'

25 25 Syntax Diagram  A version of EBNE  Example: Expr → Term {(+ | -) Term}


Download ppt "Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical."

Similar presentations


Ads by Google