Download presentation
Presentation is loading. Please wait.
Published byRalf Shepherd Modified over 9 years ago
1
D Goforth COSC 31271 Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126
2
D Goforth COSC 31272 Stages of translation Lexical analysis - the lexer or scanner Syntactic analysis - the parser 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 From characters to lexemes yVal = x + 450 – min ( 100, 4xVal ));
5
D Goforth COSC 31275 Examples: tokens and lexemes Some token categories contain only one lexeme: semi-colon ; Some tokens categorize many lexemes: identifier count, maxCost,… based on a rule for legal identifier strings
6
D Goforth COSC 31276 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
7
D Goforth COSC 31277 Syntax or Grammar of Language rules for generating (used by programmer) or Recognizing (used by parser) a valid sequence of lexemes
8
D Goforth COSC 31278 Grammars 4 categories of grammars (Chomsky) Two categories are important in computing: Regular expressions (pattern matching) Context-free grammars (programming languages)
9
D Goforth COSC 31279 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) or EBNF Syntax graphs
10
D Goforth COSC 312710 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,
11
D Goforth COSC 312711 BNF rules Non-terminal and terminal symbols: Non-terminals are defined by at least one rule = Terminals are tokens (or lexemes)
12
D Goforth COSC 312712 Simple sample grammar(p.123) = A | B | C // lexical + | * | ( ) | terminals terminals
13
D Goforth COSC 312713 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 ) = A | B | C + | * | ( ) | = A | B | C + | * | ( ) |
14
D Goforth COSC 312714 Sample parse tree = + * B A ( ) C C Leaves represent the sentence of lexemes Rule application = A | B | C + | * | ( ) | = A | B | C + | * | ( ) |
15
D Goforth COSC 312715 extended sample grammar | if ( ) then | if ( ) then else | = | == | ~= How to add compound condition?
16
D Goforth COSC 312716 Ambiguous grammar Different parse trees for same sentence Different translations for same sentence Different machine code for same source code!
17
D Goforth COSC 312717 Grammars for ‘human’ conventions without ambiguity Putting features of languages into grammars: expression any length: lists, p. 121 precedence - an extra non-terminal: p. 125 associativity - order in recursive rules: p. 128 nested if statements - “dangling else” problem: p. 130
18
D Goforth COSC 312718 Forms for grammars Backus-Naur form (BNF) Extended Backus-Naur form (EBNF) -shortens set of rules Syntax graphs -easier to read for learning language
19
D Goforth COSC 312719 EBNF optional zero or one occurrence [..] -> [ + ] optional zero or more occurrences {..} -> { + } ‘or’ choice of alternative symbols | -> [ (*|/) ]
20
Syntax Graph - basic structures expr term factor * / expr term + - factor * / term
21
BNF (p. 121)EBNF Syntax Graph -> + | - | -> * | / | -> [ (+|-)] -> [ (*|/)] -> {(+|-) } -> {(*|/) } expr term + - factor * /
22
D Goforth COSC 312722 Attribute grammars Problem: context-free grammars cannot describe some features needed in programming - “static semantics” e.g.: rules for using data types *Can’t assign real to integer (clumsy in BNF) *Can’t access variable before assigning (impossible in BNF)
23
D Goforth COSC 312723 Attributes Symbols in the grammar can have attributes (properties) Productions can have functions of some of the attributes of their symbols that compute the attributes of other symbols Predicates (boolean functions) inspect the attributes of non- terminals to see if they are legitimate
24
D Goforth COSC 312724 Using attributes 1)Apply productions to create parse tree (symbols have some intrinsic attributes) 2)Apply functions to determine remaining attributes 3)Apply predicates to test correctness of parse tree
25
D Goforth COSC 312725 Sebesta’s example = + | A | B | C Add attributes for type checking Expected_type Actual_type
26
D Goforth COSC 312726 Sebesta’s example = + | A | B | C expected_type actual_type expected_type actual_type expected_type actual_type expected_type actual_type
27
D Goforth COSC 312727 Sebesta’s example = + | A | B | C actual_type Determined from string (A,B,C) Which has been declared actual_type Determined from string (A,B,C) Which has been declared
28
D Goforth COSC 312728 Sebesta’s example = + | A | B | C actual_type Determined from Actual types actual_type Determined from Actual types
29
D Goforth COSC 312729 Sebesta’s example = + | A | B | C expected type Determined from Actual types expected type Determined from Actual types
30
D Goforth COSC 312730 Sebesta’s type rules p.138
31
D Goforth COSC 312731 Sebesta’s example
32
D Goforth COSC 312732 Sebesta’s example
33
D Goforth COSC 312733 Axiomatic semantics Assertions about statements Preconditions Postconditions like JUnit testing Purpose Define meaning of statement Test for validity of computation (does it do what it is supposed to do?)
34
D Goforth COSC 312734 Example for assignment What the statement should do is expressed as a postcondition Based on the syntax of the assignment, a precondition is inferred When statement is executed, conditions can be verified before and after
35
D Goforth COSC 312735 Example assignment statement y = 25 + x * 2 postcondition: y>40 y>40 25+x*2>40 x*2>15 x>7.5precondition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.