fundamentals - Fundamantals (1) Syntax of Programming...

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

View Full Document Right Arrow Icon
cs3723 1 Fundamantals (1) Syntax of Programming Languages
Background image of page 1

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

View Full DocumentRight Arrow Icon
cs3723 2 Syntax and Semantics Syntax The symbols and rules to write legal programs Semantics The meaning of legal programs Programming language implementation Syntax > semantics Translate program syntax into machine actions Example: date specification Syntax date ::= dd/dd/dddd d = 0|1|2|3|4|5|6|7|8|9 Semantics 01/02/2005 => Jan 02, 2005 (or Feb 01,2005) ?
Background image of page 2
cs3723 3 Describing Language Syntax Lexical grammar Spelling of words (tokens/terminals) Numbers, strings, names, … Keywords/reserved words (if, while, for, else) Formal description: regular expressions Describe the composition of words [a-zA-Z_][a-zA-Z0-9_]*, [0-9]+, “while” Context-free grammar Rules to compose programs from tokens forStmt: “for” “(“ exp “;” exp “;” exp“)” stmt Formal description: BNF (Backus-Naur Form) More powerful than regular expressions
Background image of page 3

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

View Full DocumentRight Arrow Icon
cs3723 4 Why Describing Syntax? A translator/compiler needs to understand programs via syntax analysis Needs to implement syntax analysis in C/C++/Java etc. Why does the syntax need to be formally defined? Support communications between programmers and translators/compilers Support automated generation and validation of syntax analyzers Every automation requires an interface language Regular expressions and BNFs are themselves languages for describing language syntax
Background image of page 4
cs3723 5 BNF: Expressing Context-free Grammars Each BNF includes A set of terminals: the words/tokens of the language A set of non-terminals: variables that could be replaced with different sequences of terminals A set of productions Rules identifying the structure of each non-terminal Each production has format A ::= B where A is a single non-terminal B is a sequence of terminals and non-terminals A start non-terminal: the top-level syntax of the language Example: BNF for expressions e ::= n | e+e | e e | e * e | e / e n ::= d | nd d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Non-terminals: e, n, d; start non-terminal: e Terminals: 0,1,2,3,4,5,6,7,8,9 What language does the grammar describe?
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.

This note was uploaded on 10/13/2009 for the course CS 3773 taught by Professor Staff during the Spring '08 term at The University of Texas at San Antonio- San Antonio.

Page1 / 17

fundamentals - Fundamantals (1) Syntax of Programming...

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