Download presentation
Presentation is loading. Please wait.
Published byJessie Oliver Jacobs Modified over 9 years ago
1
D Goforth COSC 31271 Translating High Level Languages
2
D Goforth COSC 31272 Stages of translation Lexical analysis Syntactic analysis Code generation Linking Before Execution
3
D Goforth COSC 31273 Lexical analysis Translate stream of characters into lexemes Lexemes belong to categories called tokens Token identity of lexemes is used at the next stage of syntactic analysis
4
D Goforth COSC 31274 Examples: tokens and lexemes Some token categories contain only one lexeme: semi-colon ; Some tokens categorize many lexemes: identifier count, maxCost,…
5
D Goforth COSC 31275 Tokens and Lexemes yVal = x + 450 – min ( 100, 4xVal )); Lexical analysis identifies lexemes and their token type recognizes illegal lexemes (4xVal) does NOT identify syntax error: ) ) identifier illegal lexeme left_paren equal_sign
6
D Goforth COSC 31276 Syntax or Grammar of Language rules for generating (used by programmer) or recognizing (used by syntactic analyzer in translation a valid sequence of lexemes
7
D Goforth COSC 31277 Grammars 4 categories of grammars (Chomsky) Two categories are important in computing: Regular expressions (pattern matching) Context-free grammars (programming languages)
8
D Goforth COSC 31278 Context-free grammar Meta-language for describing languages States rules or productions for what lexeme sequences are correct in the language Written in Backus-Naur Form (BNF)
9
D Goforth COSC 31279 Example of BNF rule PROBLEM: how to recognize all these as correct? y = x f = rVec.length + 1 button[4].label = “Exit” RULE for defining assignment statement: = Assumes other rules for,
10
D Goforth COSC 312710 BNF rules Non-terminal and terminal symbols: Non-terminals are defined by at least one rule Terminals are tokens or lexemes =
11
D Goforth COSC 312711 Simple sample grammar(p.113) = A | B | C // lexical + | * | ( ) | Assumes other rules for,
12
D Goforth COSC 312712 Simple sample production = <- apply one rule at each step B = to leftmost non-terminal B = * B = A * B = A * ( ) B = A * ( + ) B = A * ( C + ) B = A * ( C + C )
13
D Goforth COSC 312713 Sample parse tree = + * B A ( ) C C Leaves represent the sentence of lexemes
14
D Goforth COSC 312714 Ambiguous grammar Different parse trees for same sentence Different translations for same sentence Different machine code for same source code!
15
D Goforth COSC 312715 Grammars for ‘human’ conventions Putting features of languages into grammars: expression any length precedence - an extra non-terminal associativity - order in recursive rules nested if statements - “dangling else” problem: p. 119
16
D Goforth COSC 312716 Forms for grammars Backus-Naur form (BNF) Extended Backus-Naur fomr (EBNF) -shortens set of rules Syntax graphs -easier to read for learning language
17
D Goforth COSC 312717 EBNF optional zero or one occurrence -> [ + ] optional zero or more occurrences -> { + } ‘or’ choice of alternative symbols -> [ (*|/) ]
18
Syntax Graph - basic structures expr term factor * / expr term + - factor * / term
19
BNF (p. 121)EBNF Syntax Graph -> + | - | -> * | / | -> [ (+|-)] -> [ (*\/)] -> {(+|-) } -> {(*|/) } expr term + - factor * /
20
D Goforth COSC 312720 Attribute grammars Problem: context-free grammars cannot describe some features needed in programming e.g.: rules for using data types
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.