LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
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.
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
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.
Over View EXPRESSION NOTATIONS ABSTRACT SYNTAX TREES LEXICAL SYNTAX CONTEXT-FREE GRAMMARS GRAMMARS FOR EXPRESSIONS VARIANTS OF GRAMMARS
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)
Operator Expression1 Expression2 Prefix Notation Operator Expression1 Expression2 + 3 5 is meaning summation of 3 and 5 Example 2.1 * + 20 30 60 = * 50 60 = 3000 * 20 + 30 60 = * 20 90 = 1800 Try to do this * 2 - 4 + 5 7 = ?
Expression1 Expression2 Operator Postfix Notation Expression1 Expression2 Operator 3 5 + is meaning summation of 3 and 5 Example 2.2 20 30 + 60 * = 50 60 * = 3000 20 30 60 + * = 20 90 * = 1800 Try to do this 45 37 - 69 * = ?
Infix Notation Precedence and Associativity Expression1 Operator Expression2 3 + 5 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 = ?
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
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
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
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 * -
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
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.
Grammars Parse tree 3 . 1 4 Real-number Integer-part fraction digit
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
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
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
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
Example Real-number Integer-part fraction . Integer-part digit 1 2 3 7 8 9
Ambiguity Syntactic Ambiguity Dangling-Else Ambiguity