Lecture 3 Syntactic Analysis Part I Joey Paquet

  • Slides: 34
Download presentation
Lecture 3 Syntactic Analysis Part I Joey Paquet, 2000, 2002, 2008 1

Lecture 3 Syntactic Analysis Part I Joey Paquet, 2000, 2002, 2008 1

Syntactic Analyzer • Roles – Analyze the structure of the program and its component

Syntactic Analyzer • Roles – Analyze the structure of the program and its component statements and expressions – Check for (and recover from) syntax errors – Control the front-end’s execution Joey Paquet, 2000, 2002, 2008 2

Historic • Historically based on formal natural language grammatical analysis (Chomsky [1950’s]) • A

Historic • Historically based on formal natural language grammatical analysis (Chomsky [1950’s]) • A generative grammar is used – builds sentences in a series of steps – starts from abstract concepts defined by a set of grammatical rules (often called productions) – refines the analysis down to actual words • Analyzing (parsing) consists in reconstructing the way in which the sentences were constructed • Valid sentences can be represented as a parse tree Joey Paquet, 2000, 2002, 2008 3

Example <sentence> : : = <noun phrase><verb phrase> <noun phrase> : : = article

Example <sentence> : : = <noun phrase><verb phrase> <noun phrase> : : = article noun <verb phrase> : : = verb <noun phrase> sentence noun phrase article the noun dog verb phrase verb gnawed noun phrase article noun the bone Joey Paquet, 2000, 2002, 2008 4

Syntax and Semantics • Syntax: defines how valid sentences are formed • Semantics: defines

Syntax and Semantics • Syntax: defines how valid sentences are formed • Semantics: defines the meaning of valid sentences • Some grammatically correct sentences can have no meaning – The bone walked the dog • It is impossible to automatically validate the full meaning of all English sentences • In programming languages, semantics is about giving a meaning by translating programs into executables Joey Paquet, 2000, 2002, 2008 5

Grammars • A grammar is a quadruple (T, N, S, R) – – T:

Grammars • A grammar is a quadruple (T, N, S, R) – – T: a finite set of terminal symbols N: a finite set of non-terminal symbols S: a unique starting symbol (S N) R: a finite set of productions • | ( , (T N) ) • Context free grammars have productions of the form: – A | (A N) ( (T N) ) Joey Paquet, 2000, 2002, 2008 6

Backus-Naur Form • J. W. Backus: main designer of the first FORTRAN compiler •

Backus-Naur Form • J. W. Backus: main designer of the first FORTRAN compiler • P. Naur: main designer of the Algol-60 programming language – – – non-terminals are placed in angle brackets the symbol : : = is used instead of an arrow a vertical bar can be used to signify alternatives curly braces are used to signify an indefinite number of repetitions square brackets are used to signify optionality • Widely used to define programming languages’ syntax Joey Paquet, 2000, 2002, 2008 7

Example • Grammar for expressions: G = (T, N, S, R), T = {id,

Example • Grammar for expressions: G = (T, N, S, R), T = {id, +, , , /, (, )}, N = {E}, S = E, R = { E E + E, E E / E, E ( E ), E id} Joey Paquet, 2000, 2002, 2008 8

Example • Parse the sequence: (a+b)/(a b) • The lexical analyzer tokenizes the sequence

Example • Parse the sequence: (a+b)/(a b) • The lexical analyzer tokenizes the sequence as: (id+id)/(id id) • Construct a parse tree for the expression: – – start symbol non-terminal production = = root internal node leaf subtree Joey Paquet, 2000, 2002, 2008 9

Top-Down Parsing • Starts at the root (starting symbol) • Builds the tree downwards

Top-Down Parsing • Starts at the root (starting symbol) • Builds the tree downwards from: – the sequence of tokens in input (from left to right) – the rules in the grammar Joey Paquet, 2000, 2002, 2008 10

Example 1 - Using: E E / E E E / 3 - Using:

Example 1 - Using: E E / E E E / 3 - Using: E E + E E E id E E 2 - Using: E ( E ) E E E ( E / ) E ( E ) / E ( E ) E + E E E id id id Joey Paquet, 2000, 2002, 2008 id 11

Derivations • The application of grammar rules towards the recognition of a grammatically valid

Derivations • The application of grammar rules towards the recognition of a grammatically valid sequence of terminals can be represented with a derivation • Noted as a series of transformations: – { [ ] | ( , (T N) ) ( R)} – where production is used to transform into . Joey Paquet, 2000, 2002, 2008 12

Example E E / E E/(E) E/(E E) E / (E id ) E

Example E E / E E/(E) E/(E E) E / (E id ) E / ( id ) ( E ) / ( id id ) ( E + id ) / ( id ) ( id + id ) / ( id ) [E [E [E E / E] ( E )] E E] id] ( E )] E + E] id] E E / E ( E ) E + E E E id id (id+id)/(id id) • In this case, we say that E G • The language generated by the grammar can be defined as: L(G) = { | S } G Joey Paquet, 2000, 2002, 2008 13

Left and Rightmost Derivations E E/E E/(E) E/(E E) E / (E id )

Left and Rightmost Derivations E E/E E/(E) E/(E E) E / (E id ) E / ( id ) ( E ) / ( id id ) ( E + id ) / ( id ) ( id + id ) / ( id ) [E [E [E E / E] ( E )] E E] id] ( E )] E + E] id] E E/E (E)/E (E+E)/E ( id + E ) / E ( id + id ) / ( E ) ( id + id ) / ( E E ) ( id + id ) / ( id id ) [E [E [E E / E] ( E )] E + E] id] ( E )] E E] id] Rightmost Derivation Leftmost Derivation Joey Paquet, 2000, 2002, 2008 14

Top-Down & Bottom-up Parsing • A top-down parser builds a parse tree starting at

Top-Down & Bottom-up Parsing • A top-down parser builds a parse tree starting at the root down to the leafs – It builds leftmost derivations • A bottom-up parser builds a parse tree starting from the leafs up to the root – They build rightmost derivations Joey Paquet, 2000, 2002, 2008 15

Ambiguous Grammars • Which of these trees is the right one for the expression

Ambiguous Grammars • Which of these trees is the right one for the expression “id + id * id” ? E E id E E * E E + E id id E * id id E id • According to the grammar, both are right • The language defined by this grammar is ambiguous. That is not acceptable in a compiler. Joey Paquet, 2000, 2002, 2008 16

Ambiguous Grammars • Solutions: – incorporate operation precedence in the parser (complicates the compiler,

Ambiguous Grammars • Solutions: – incorporate operation precedence in the parser (complicates the compiler, rarely done) – rewrite the grammar to remove ambiguities E E E E+E E E E/E (E) id E E E+T|E T|T T T F|T/F|F F ( E ) | id E + T F T * id F F id id Joey Paquet, 2000, 2002, 2008 17

Left Recursion • The aim is to design a parser that has no arbitrary

Left Recursion • The aim is to design a parser that has no arbitrary choices to make between rules (predictive parsing) • The first rule that can apply is applied • In this case, productions of the form A A will be applied forever • Example: id + id E E . . . Joey Paquet, 2000, 2002, 2008 E + T T 18

Non-immediate Left Recursions • Non-immediate left recursions are sets of productions of the form:

Non-immediate Left Recursions • Non-immediate left recursions are sets of productions of the form: A B | … B A | … A B A . . . Joey Paquet, 2000, 2002, 2008 19

Left Recursion • This problem afflicts all top-down parsers • Solution: apply a transformation

Left Recursion • This problem afflicts all top-down parsers • Solution: apply a transformation to the grammar to remove the left recursions 1 - Isolate each set of productions of the form: A A 1 | A 2 | A 3 | … (left recursive) A 1 | 2 | 3 | … (non-left-recursive) 2 - Introduce a new non-terminal A 3 - Change all the non-recursive productions on A to: A 1 A | 2 A | 3 A | … 4 - Remove the left-recursive production on A and substitute: A | 1 A | 2 A | 3 A |. . . Joey Paquet, 2000, 2002, 2008 20

Example E E+T|E T|T T T F|T/F|F F ( E ) | id (i)

Example E E+T|E T|T T T F|T/F|F F ( E ) | id (i) 1234 - E E+T|E T|T E E+T|E T E E TE E | +TE | TE (A A 1 | A 2) (A 1) (A 1 A ) (A | 1 A | 2 A ) E TE E | +TE | TE T T F|T/F|F F ( E ) | id Joey Paquet, 2000, 2002, 2008 21

Example E TE E | +TE | TE T T F|T/F|F F ( E

Example E TE E | +TE | TE T T F|T/F|F F ( E ) | id (ii) 1234 - T T F|T/F|F T T F|T/F T T FT T | FT | /FT (A A 1 | A 2) (A 1) (A 1 A ) (A | 1 A | 2 A ) E TE E | +TE | TE T FT T | FT | /FT F ( E ) | id Joey Paquet, 2000, 2002, 2008 22

Ambiguity • Sets of rules of the form: A 1 | 2 | 3

Ambiguity • Sets of rules of the form: A 1 | 2 | 3 | … can be eliminated using a factorization technique 1 - Isolate a set of productions of the form: A 1 | 2 | 3 | … 2 - Introduce a new non-terminal A 3 - Replace all the ambiguous set of productions on A by: A A 4 - Add a set of factorized productions on A : A 1 | 2 | 3 | … Joey Paquet, 2000, 2002, 2008 23

Backtracking • It is possible to write a parser that implements an ambiguous grammar

Backtracking • It is possible to write a parser that implements an ambiguous grammar • In this case, when there is an arbitrary alternative, the parser explores them one after the other • If an alternative does not result in a valid parse tree, the parser backtracks to the last arbitrary alternative and selects another righthand-side • The parse fails only when there are no more alternatives left • This is often called a “brute-force” method Joey Paquet, 2000, 2002, 2008 24

Example S ee | b. Ac | b. Ae A d | c. A

Example S ee | b. Ac | b. Ae A d | c. A Seeking for : bcde S S b. Ac bcdc ? ? ? b. Ae bcde OK S [S b. Ac] • [A c. A] [A d] [S b. Ae] [A c. A] [A d] Joey Paquet, 2000, 2002, 2008 b A c c A d S b A c e A d 25

Backtracking • Backtracking is tricky and inefficient to implement • Normally, code is generated

Backtracking • Backtracking is tricky and inefficient to implement • Normally, code is generated after rules are applied; backtracking involves retraction of the generated code! • Parsing with backtracking is almost never used • The solution is to eliminate the ambiguities Joey Paquet, 2000, 2002, 2008 26

Predictive Parsing • Restriction: the parser must always be able to determine which of

Predictive Parsing • Restriction: the parser must always be able to determine which of the right-hand sides to follow, only with its knowledge of the next token in input. • Top-down parsing without backtracking • Deterministic parsing • No backtracking is possible Joey Paquet, 2000, 2002, 2008 27

Predictive Parsing • Recursive descent predictive parser – a function is defined for each

Predictive Parsing • Recursive descent predictive parser – a function is defined for each non-terminal symbol – its predictive nature allows it to choose the righthand-side – it recognizes terminal symbols and calls other functions to recognize non-terminal symbols in the chosen right hand side – the parse tree is actually constructed by the nest of function calls – very easy to implement – hard to maintain Joey Paquet, 2000, 2002, 2008 28

Predictive Parsing • Table-driven predictive parser – – – table tells the parser which

Predictive Parsing • Table-driven predictive parser – – – table tells the parser which right-hand-side to choose the driver algorithm is standard to all parsers only the table changes easy to maintain table is hard to build for most languages will be covered in next lecture Joey Paquet, 2000, 2002, 2008 29

FIRST and FOLLOW sets • Predictive parsers need to know what right hand side

FIRST and FOLLOW sets • Predictive parsers need to know what right hand side to choose • The only information we have is the next token in input • If all the right hand sides begin with terminal symbols, the choice is straightforward • If some right hand sides begin with non-terminals, the parser must know what token can begin the sequence generated by this non-terminal (i. e. the FIRST set) • If a FIRST set contains , it must know what follows this nonterminal (i. e. the FOLLOW set) in order to chose the production. Joey Paquet, 2000, 2002, 2008 30

Example E E’ T T’ F FIRST(E) FIRST(E’) FIRST(T’) FIRST(F) = = = TE’

Example E E’ T T’ F FIRST(E) FIRST(E’) FIRST(T’) FIRST(F) = = = TE’ +TE’ | FT’ *FT’ | 0 | 1 | (E) {0, 1, (} {+, } {0, 1, (} {*, } {0, 1, (} FOLLOW(E) = {$, )} FOLLOW(E’)= {$, )} FOLLOW(T) = {+, $, )} FOLLOW(T’)= {+, $, )} FOLLOW(F) = {*, +, $, )} Joey Paquet, 2000, 2002, 2008 31

Example E E' T T' F --> --> --> TE' +TE' | epsilon FT'

Example E E' T T' F --> --> --> TE' +TE' | epsilon FT' *FT' | epsilon 0 | 1 | (E) E E’ T FIRST 0, 1, ( epsilon, + 0, 1, ( FOLLOW $, ) +, $, ) T’ epsilon, * +, $, ) F 0, 1, ( *, +, $, ) Joey Paquet, 2000, 2002, 2008 32

Example Parse(){ lookahead = Next. Token() skip. Errors([0, 1, (], [$]) if (E(); match('$'))

Example Parse(){ lookahead = Next. Token() skip. Errors([0, 1, (], [$]) if (E(); match('$')) else error = true} E(){ if (lookahead is in [0, 1, (]) //FIRST(TE') if (T(); E'(); ) write(E->TE') else error = true} E'(){ if (lookahead is in [+]) //FIRST[+TE'] if (match('+'); T(); E'()) write(E'->TE') else error = true else if (lookahead is in [$, )] //FOLLOW[E'](epsilon) write(E'->epsilon); else error = true} T(){ if (lookahead is in [0, 1, (]) //FIRST[FT'] if (F(); T'(); ) write(T->FT') else error = true} Joey Paquet, 2000, 2002, 2008 33

Example T'(){ if (lookahead is in [*]) //FIRST[*FT'] if (match('*'); F(); T'()) write(T'->*FT') else

Example T'(){ if (lookahead is in [*]) //FIRST[*FT'] if (match('*'); F(); T'()) write(T'->*FT') else error = true else if (lookahead is in [+, ), $] //FOLLOW[T'] (epsilon) write(T'->epsilon) else error = true} F(){ if (lookahead is in [0]) //FIRST[0] match('0') write(F->0) else if (lookahead is in [1]) //FIRST[1] match('1') write(F->1) else if (lookahead is in [(]) //FIRST[(E)] if (match('('); E(); match(')')) write(F->(E)); else error = true} } Joey Paquet, 2000, 2002, 2008 34