Download presentation
Presentation is loading. Please wait.
Published byTracey Bridges 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
lexical analysis groups characters into lexical units syntax analysis transforms lexical units into parse trees a parse tree represents the syntactic structure of the program analyzes parse trees to produce intermediate code transforms intermediate code into executable machine code
3
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
4
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
5
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
6
A parse tree represents the structure of a derivation Every internal node is a non-terminal abstraction Every leaf node is a terminal symbol Draw a parse tree for ▪ B = A * C + D = A | B | C | D + | * | ( ) | = = * * C C B B A A
7
= = * * B B A A + + C C D D = A | B | C | D + | * | ( ) | B = A * C + D
8
= = + + B B * * A A C C D D = A | B | C | D + | * | ( ) |
9
A grammar that generates a sentential form for which there are two or more distinct parse trees is an ambiguous grammar Ambiguity in a grammar leads to problems... ...because compilers often base semantics on parse trees ▪ operator precedence ▪ if-else parse trees are analyzed to produce intermediate code
10
Ambiguous grammar Unambiguous grammar = A | B | C | D + | * | ( ) | = A | B | C | D + | * | ( ) | An unambiguous grammar has exactly one derivation and parse tree for each unique sentential form
11
Read and study Chapter 3 Do Exercises at the end of Chapter 3 Do the What next? exercises from the Week 3 lecture notes DUE NEXT CLASS!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.