Download presentation
Presentation is loading. Please wait.
Published byIsaac Andrews Modified over 9 years ago
1
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6
2
Syntax is the expected form or structure of the expressions, statements, and program units of a programming language Syntax of a Java while statement: while ( ) Partial syntax of an if statement: if ( )
3
Semantics is the meaning of the expressions, statements, and program units of a given programming language Semantics of a Java while statement while ( ) Execute zero or more times as long as evaluates to true
4
Together, syntax and semantics define a programming language Syntax errors are detected and reported by a compiler Errors related to semantics are defects in program logic that cause incorrect results or program crashes
5
Terminology to describe syntax: A sentence is a string of characters over an alphabet of symbols A language is a set of sentences A lexeme is the lowest-level syntactic unit of a language (e.g. +, *, sum, while ) ▪ One step above individual characters A token is a set of lexemes ▪ e.g. identifier, equal_sign, integer_literal, etc.
6
A language recognizer reads an input string and determines whether it belongs to the given language This is the syntax analysis part of a compiler or interpreter input strings (source code) language recognizer language recognizer accept or reject each input string
7
A language generator produces syntactically acceptable strings of a given language Not practical to generate all valid strings Instead, inspect generator rules (the grammar) to determine if a sentence is acceptable for a given language language generator language generator valid strings of the language
8
In the mid-1950s, linguist Noam Chomsky (born 1928) developed four classes of generative grammars Context-free grammars (CFGs) are useful for describing programming language syntax Regular grammars are useful for describing valid tokens of a programming language
9
In 1960, John Backus and Peter Naur developed a formal notation for specifying programming language syntax Backus-Naur Form (BNF) is nearly identical to Chomsky’s context-free grammars Syntax of an assignment statement in BNF: ▪ = ;
10
Syntax of an assignment statement in BNF: BNF rule or production defining : = ; abstraction being defined definition of The definition consists of other abstractions, as well as lexemes and tokens
11
begin end | ; = a | b | c | d | e + | - | literal-integer-value a vertical bar indicates an OR a token, which is simply a grouping of lexemes Write a sentence that conforms to this grammar
12
A derivation is a repeated application of rules Start with a start symbol and end with a sentence => begin end => begin = end => begin b = end => begin b = + end => begin b = c + end => begin b = c + 123 end Many possible (often infinite) derivations begin end | ; = a | b | c | d | e + | - | literal-integer-value
13
A leftmost derivation is one in which the leftmost abstraction is always the next one expanded Write both a leftmost and rightmost derivation to obtain this sentence: begin d = 10 - a end Why is the leftmost derivation important? begin end | ; = a | b | c | d | e + | - | literal-integer-value
14
Given this simple grammar: Which of the following sentences are generated by this grammar? ▪ baaabbccc ▪ abc ▪ bbaabbaabbaabbaac ▪ aabbbbccccccccccccccccccccc a | a b | b c | c
15
Write BNF for the following constructs from your favorite programming language: Assignment statement ▪ Include operators +, -, *, /, %, ++, -- Complete while and if statements Class header for Java/C++/C# etc.
16
Given this grammar: Show both leftmost and rightmost derivations for the following sentences: ▪ A = A * ( B + ( C * A ) ) ▪ B = B * ( (D) + C ) ▪ C = A + B + C * D + A = A | B | C | D + | * | ( ) |
17
Use BNF to write a grammar for reverse Polish notation Use as your start symbol Valid sentences include: ▪ 5 8 19 + * ▪ 2 3 + 5 7 * / ▪ 2 3 + 5 7 * / 3 4 + * 1 – ▪ 9 9 * 8 7 * * 5 5 * * 4 -
18
Read and study Chapter 3 Do Exercises at the end of Chapter 3
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.