{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

fundamentals - Fundamantals(1 Syntax of Programming...

Info icon This 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
Image of page 1

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

View Full Document Right 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) ?
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
Image of page 3

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

View Full Document Right 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
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?
Image of page 5

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern