Syntax 1 Syntax o The syntax of a

  • Slides: 25
Download presentation
Syntax (1)

Syntax (1)

Syntax o The syntax of a programming language is a precise description of all

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

Levels of Syntax o Lexical syntax all the basic symbols of the language (names,

Levels of Syntax o Lexical syntax all the basic symbols of the language (names, values, operators, etc. ) o Concrete syntax rules for writing expressions, statements and programs. o Abstract syntax internal representation of the program, favouring content over form. 3

Grammars o A metalanguage is a language used to define other languages. o A

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

BNF Grammar o o Set of terminal symbols: T Set of nonterminal symbols: N

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

Example: Binary Digits o Consider the grammar: binary. Digit 0 binary. Digit 1 o

Example: Binary Digits o Consider the grammar: binary. Digit 0 binary. Digit 1 o or equivalently: binary. Digit 0 | 1 o Here, | is a metacharacter that separates alternatives. 6

Derivations o A grammar for integers: Integer Digit | Integer Digit 0 | 1

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

Driving the Integer “ 352” using Integers Grammar Integer Digit Integer 2 Integer Digit

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

Parse Trees o A parse tree is a graphical representation of a derivation. n

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

Example: The step Integer Digit o appears in the parse tree as: Integer Digit

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

Example: Parse Tree for “ 352” as an Integer Digit | Integer Digit 0

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

Arithmetic Expression Grammar o The following grammar defines the language of arithmetic expressions with

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

Example: Parse of the String 5 -4+3 Expr + Term | Expr – Term

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

Precedence o A grammar can be used to define and precedence among the operators

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

Associativity o A grammar can be used to define associativity among the operators in

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

Example Parse of 4**2**3+5*6+7 Expr → Expr + Term | Expr – Term |

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

Ambiguous Grammars o A grammar can have more than one parse tree. o Consider

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

(9– 5)+2 9 -(5+2) string 9 string string - 5 string + 2 9

(9– 5)+2 9 -(5+2) string 9 string string - 5 string + 2 9 - 5 string + 2 18

Another Example: The Dangling Else o With which ‘if’ does the following ‘else’ associate

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

If statement Grammar If. Statement → if ( Expression ) Statement | if (

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

The Dangling Else Ambiguity 21

The Dangling Else Ambiguity 21

C and C++ Solution to dangling else ambiguity o Associate each else with closest

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

Extended BNF (EBNF) o Additional metacharacters n Braces { } o zero or more

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

EBNF Example o BNE n Expr → Expr + Term | Expr – Term|

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

Syntax Diagram o A version of EBNE o Example: n Expr → Term {(+

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