3-Describing%20Syntax%20and%20Semantics

3-Describing%20Syntax%20and%20Semantics - 3. Describing...

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

View Full Document Right Arrow Icon
ICS 313 - Fundamentals of Programming Languages 1 3. Describing Syntax and Semantics 3.1 Introduction ± Who must use language definitions? ` Other language designers ` Implementors ` Programmers (the users of the language) ± Syntax ` the form or structure of the expressions, statements, and program units ± Semantics ` the meaning of the expressions, statements, and program units
Background image of page 1

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

View Full DocumentRight Arrow Icon
ICS 313 - Fundamentals of Programming Languages 2 3.2 Describing Syntax ± A sentence is a string of characters over some alphabet ± A language is a set of sentences ± A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) ± A token is a category of lexemes (e.g., identifier) ± Formal approaches to describing syntax ` Recognizers - used in compilers (see Ch. 4) ` Generators - what we'll study in this chapter 3.3 Formal Methods of Describing Syntax ± Context-Free Grammars ` Developed by Noam Chomsky in the mid-1950s ` Language generators, meant to describe the syntax of natural languages ` Define a class of languages called context-free languages ± Backus-Naur Form (1959) ` Invented by John Backus to describe Algol 58 ` BNF is equivalent to context-free grammars ` A metalanguage is a language used to describe another language. ` In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols) <while_stmt> while ( <logic_expr> ) <stmt> This is a rule; it describes the structure of a while statement
Background image of page 2
ICS 313 - Fundamentals of Programming Languages 3 3.3 Formal Methods of Describing Syntax (co.) ± A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal and nonterminal symbols ± A grammar is a finite nonempty set of rules ± An abstraction (or nonterminal symbol) can have more than one RHS ± Syntactic lists are described using recursion ± A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) 3.3 Formal Methods of Describing Syntax (co.) ± An example grammar ± An example derivation ± Every string of symbols in the derivation is a sentential form ± A sentence is a sentential form that has only terminal symbols ± A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded ± A derivation may be neither leftmost nor rightmost
Background image of page 3

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

View Full DocumentRight Arrow Icon
ICS 313 - Fundamentals of Programming Languages 4 3.3 Formal Methods of Describing Syntax (co.) ± A parse tree is a hierarchical representation of a derivation ± A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees 3.3 Formal Methods of Describing Syntax (co.) ± An ambiguous expression grammar ± If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity
Background image of page 4
ICS 313 - Fundamentals of Programming Languages 5 3.3 Formal Methods of Describing Syntax (co.) ± An unambiguous expression grammar 3.3 Formal Methods of Describing Syntax (co.)
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 / 14

3-Describing%20Syntax%20and%20Semantics - 3. Describing...

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