Download presentation
Presentation is loading. Please wait.
1
Slide 1 Chapter 2-b Syntax, Semantics
2
Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements and programming units. The semantics is the meaning of these expressions, statements and programming units. A grammar is a formal set of rules that describes a valid syntax of a language.
3
Slide 3 Syntax, Semantics - Examples Syntax of Date: DD/DD/DDDD where D represents a digit. The semantics describes which parts stand for the date, month and year. Syntax of “if” statement: if ( ) Semantics: will be executed only if evaluates to “true”
4
Slide 4 Lexemes Lexemes are the lowest level syntactic units. Example: val = (int)(xdot + y*0.3) ; In the above statement, the lexemes are val, =, (, int, ), (, xdot, +, y, *, 0.3, ), ;
5
Slide 5 Tokens The category of lexemes are tokens. Identifiers: Names chosen by the programmer. Eg. val, xdot, y Keywords: Names chosen by the language designer to help syntax and structure. Eg. int, return, void. (Keywords that cannot be used as identifiers are known as reserved words )
6
Slide 6 Tokens (Contd.) Operators: Identify actions. Eg. +, &&, ! Literals: Denote values directly. Eg. 3.14, -10, ‘a’, true, null Punctuation Symbols: Supports syntactic structure. Eg. (, ), ;, {, }
7
Slide 7 Backus Naur Form (BNF) Useful for describing the syntax of progr. languages. Eg. Pascal “if”: Terminals if then Production LHS Non-terminals Non-terminals are abstractions for syntactic structures. Terminals are lexemes or tokens.
8
Slide 8 Logical OR in BNF Logical OR in BNF is denoted by | Eg. 0|1|2|3|4|5|6|7|8|9 if then | if then else + |
9
Slide 9 Recursive rules in BNF A BNF rule is recursive if LHS appears on RHS. Eg: |, |
10
Slide 10 Extended BNF [ ] Optional element: if then [ else ] [ ]. { } Unspecified number of repititions: Repeated infefinitely or left out altogether. {, }
11
Slide 11 EBNF (Contd.) ( …| …) Multiple choice options. A single element must be chosen from a group. “for” loop in Pascal: for := (to|downto) do EBNF enhances the readability and writability of BNF.
12
Slide 12 Syntax Graphs BNFSyntax Graph LHS if_stmt Non-terminal Terminal if stmt if
13
Slide 13 Syntax Graph - Example BNF: if then Syntax Graph: if_stmt if logic_expr then stmt
14
Slide 14 Syntax Graph Constructs Alternatives: | Optional [ ] stmt funct expr
15
Slide 15 Syntax Graph Constructs (Contd.) Unspecified repetitions: { } Repetition with minimum one occurrence { } digit
16
Slide 16 A simple grammar = A|B|C + | * | ( ) |
17
Slide 17 Sentences A sentence is got by replacing the non terminals by strings of symbols according to the rules in the grammar. Egs. (Based on the grammar on previous slide) A = B*(A+C) C = A+B*A B = A
18
Slide 18 Parse Trees Parse trees describe the hierarchical structure of sentences. It has the following properties: The root is labeled by LHS. Every non-leaf node (internal node) is a non- terminal. Each leaf is labeled with a terminal.
19
Slide 19 Parse tree for A=B*C = * A B C
20
Slide 20 Ambiguous Grammar A grammar that generates a sentence which has two or more distinct parse trees is said to be an ambiguous grammar. Eg. If we rewrite the grammar on slide 15 as below, = A|B|C + | * | ( ) | then the sentence A = B*C+A would have two distinct parse trees, and therefore the above grammar is ambiguous.
21
Slide 21 Derivation Derivation is a mechanism by which the rules of a grammar can be repeatedly applied to generate a sentence. At each stage, a non-terminal is replaced by the right-hand side of a rule, till finally the whole sentence is generated.
22
Slide 22 Derivation - Example. = A = A = * A = B * A = B * C
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.