Presentation is loading. Please wait.

Presentation is loading. Please wait.

LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Similar presentations


Presentation on theme: "LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE"— Presentation transcript:

1 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

2 Description The state of art can be summarized by saying that a language is described by a combination of formal syntax and informal semantics. Syntax: How programs in the language are built up, grammar. Semantics: what programs mean, meaning.

3 EXAMPLE Suppose that dates are built up from digits represented by D and the symbol /, as follows: D D / D D / D D D D 01/02/2001 is a date, not identified by syntax January 2, 2001 or 1st February, 2001

4 Organization of Language Descriptions
Several Style of language description Tutorials : It provides impressions of what the main constructs of the language are and how they are meant to be used Reference Manuals : describing the syntax and semantics of a language is traditionally organized around the syntax of the language Formal Definitions : a precise description of the syntax and semantics of a language; it is aimed at specialists.

5 Over View EXPRESSION NOTATIONS ABSTRACT SYNTAX TREES LEXICAL SYNTAX
CONTEXT-FREE GRAMMARS GRAMMARS FOR EXPRESSIONS VARIANTS OF GRAMMARS

6 Expression Notations It serves 2 purposes Expressions
Introducing notations for expressions Providing examples for the syntax description methods Expressions Notation:how to combine operators and operands Prefix, Postfix, Infix, Mixfix write as expression: (-b+sqrt(b*b-4.0*a*c)/(2.0*a)

7 Operator Expression1 Expression2
Prefix Notation Operator Expression1 Expression2 is meaning summation of 3 and 5 Example 2.1 * = * = 3000 * = * = 1800 Try to do this * = ?

8 Expression1 Expression2 Operator
Postfix Notation Expression1 Expression2 Operator is meaning summation of 3 and 5 Example 2.2 * = * = 3000 * = * = 1800 Try to do this * = ?

9 Infix Notation Precedence and Associativity
Expression1 Operator Expression2 is meaning summation of 3 and 5 Example 4 – 2 – 1 = ( 4 – 2 ) – 1 = 2 – 1 = 1 Or = 4 – ( 2 – 1 ) = 4 – 1 = 3 Try to do this b*b – 4*a*c = ?

10 Mixfix Notation Combination of symbols do not fit neatly into the prefix, infix, postfix classification. Example the keywords if, then and else if a>b then a else b

11 ABSTRACT SYNTAX TREES Identify the meaningful components of each construct in the language. Prefix : + a b Postfix : a b + Infix : a + b Corresponding tree representation is + a b

12 Tree Representation of Expression
A tree consists of nodes, k>0 A node with no children is called a leaf A node with no parent is called root A diagram for the tree is Op denoted operator Ek denoted operand Op E1 E2

13 Example A tree of expression b*b – 4*a*c (b*b)-(4*a*c)  E1 – E2
1. b*b  E1 * E2 2. (4*a)*c  E1 * E2 * b 4 a c - Prefix : - * b b * * 4 a c Infix: b * b – 4 * a * c Postfix: b b * 4 a * c * -

14 LEXICAL SYNTAX A lexical syntax for a language specified the correspondence between the written representation of the language and the tokens or terminals in a grammar for the language Alphabetic sequences as units in a language, called keywords Occurrences of a token; using token name for names and token number for integers White space: blank not effect meaning

15 CONTEXT-FREE GRAMMARS
The concrete syntax of a language describes its written representation. Context-free grammars are a notation for specifying concrete syntax. BNF from: Backus-Naur Form A way of writing grammars.

16 Grammars Parse tree 3 . 1 4 Real-number Integer-part fraction digit

17 Context-free grammar Terminals are the atomic symbols
Non-terminals are the variables Production: A set of rules for identifying the components of a construct A starting non-terminal represent in Main construct

18 BNF:Backus-Naur Form Terminals
Non-terminals enclosed between < and > Production in form non-terminal ::= terminal | <non-terminal> ::= “can be” | “or” A starting non-terminal

19 Example of BNF A BNF grammar for real numbers
Terminals : 0,1,2,3,4,5,6,7 Non-terminals : <real-number>,<integer-part>,<fraction>, and <digit> Productions <real-number> ::=<integer-part>.<fraction> <integer-part> ::=<digit>|<integer-part><digit> <fraction> ::=<digit>|<digit><fraction> <digit> ::=0|1|2|3|4|5|6|7|8|9

20 Parse Trees Depict Concrete Syntax
Each leaf is labeled with a terminal or <empty> Each non-leaf node is labeled with a non-terminal The root is labeled with the starting non-terminal Children of node, from left to right of right side

21 Example Real-number Integer-part fraction . Integer-part digit
1 2 3 7 8 9

22 Ambiguity Syntactic Ambiguity Dangling-Else Ambiguity


Download ppt "LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE"

Similar presentations


Ads by Google