PPL Unit 2 - Unit 2 Syntax and Semantics 1 Topics General...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Unit 2 Syntax and Semantics
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Topics General problem of describing syntax Formal methods of describing syntax / Language generation mechanisms Attribute grammars Describing meanings of programs / Dynamic Semantics
Background image of page 2
3 Syntax: Syntax of a programming language is the set of rules that govern the formation of its expressions, statements, and programs units. Semantics: is the meaning of the expressions, statements, and program units. General problem of describing syntax • A language is a set of strings of characters from some alphabet. • The strings of a language are called sentences or statements . • Syntax rules of a language specify which strings of characters from the language’s alphabet are in the language. • The lowest-level syntactic units are called lexemes that include identifiers, literals, operators, and special words. • A program is a collection of lexemes governed by the syntax of the language. • A token is a category of its lexemes. Ex: a = 2 * count + 17; Lexemes Tokens a identifier = operator 2 literal * operator count identifier + operator 17 literal ; special character
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Languages can be defined in two distinct ways: by recognition by generation Language Recognizer It is a device determines whether a given program is in the language or not. For a language L that uses an alphabet A of characters a recognition mechanism R reads strings of characters from A and indicates that a given string is or is not in L i.e. R either accepts or rejects the given string. Ex: syntax analyzer of a compiler Language Generator It is a device that generates the sentences of a language. Ex: grammars Formal methods of describing syntax Syntax of a language is described by language generators called grammars. Backus-Naur Form, Context-Free Grammars, Regular grammars CFG and Regular grammars are notations developed by Chomsky for describing syntax of a programming language. Regular grammar describes the tokens of the language. CFG describes the whole program of a language. BNF is a revised version of CFG modified by John Backus and Peter Naur.
Background image of page 4
5 Fundamentals of BNF Metalanguage is a language that is used to describe another language Ex:BNF BNF uses abstractions for syntactic structures Ex : <assign> <var>=<expression> LHS of arrow is the abstraction and RHS is its definition that consists of mixture of tokens, lexemes and references to other abstractions. LHS and RHS together forms a rule or production. A sentence that follows the above rule is total = sub1 + sub2 abstractions in the rule are called nonterminals. lexemes and tokens in the rule are called terminals . A grammar is a collection of rules that generates a language. Multiple definitions can be written as a single rule using logical OR symbol ‘|’
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

PPL Unit 2 - Unit 2 Syntax and Semantics 1 Topics General...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online