+ All Categories
Home > Documents > Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis...

Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis...

Date post: 29-Dec-2015
Category:
Upload: ashlyn-patrick
View: 235 times
Download: 6 times
Share this document with a friend
38
Chapter 4 Lexical and Syntax Analysis
Transcript
Page 1: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

Chapter 4

Lexical and Syntax Analysis

Page 2: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

4-2

Chapter 4 Topics

4.1 Introduction4.2 Lexical Analysis4.3 The Parsing Problem4.4 Recursive-Descent Parsing4.5 Bottom-Up Parsing

Page 3: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

4-3

4.1 Introduction

• Language implementation systems must analyze source code, regardless of the specific implementation approach

• Nearly all syntax analysis is based on a formal description of the syntax of the source language (BNF)

Page 4: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-4

Syntax Analysis

• The syntax analysis portion of a language processor nearly always consists of two parts:– A low-level part called a lexical analyzer

(mathematically, a finite automaton based on a regular grammar)

– A high-level part called a syntax analyzer, or parser (mathematically, a push-down automaton based on a context-free grammar, or BNF)

Page 5: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-5

Advantages of Using BNF to Describe Syntax

• BNF provides a clear and concise syntax description, both for human and for software system that uses them.

• BNF description can be used as the direct basis for the syntax analyzer.

• Implementations based on BNF are relatively easy to maintain because of their modularity.

Page 6: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-6

Reasons to Separate Lexical and Syntax Analysis

• Simplicity - less complex approaches can be used for lexical analysis; separating them simplifies the parser

• Efficiency - separation allows optimization of the lexical analyzer

• Portability - parts of the lexical analyzer may not be portable, but the parser always is portable

Page 7: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-7

4.2 Lexical Analysis

• A lexical analyzer is a pattern matcher for character strings.

• A lexical analyzer is a “front-end” for the parser. It is a part of syntax analyzer, and performs syntax analysis at the lowest level of program structure.

An input program appears to a compiler as a single string of character. It collects characters into logical groupings and assigned internal codes to the groupings.

• Logical groupings are named lexemes. Internal codes for categories of these groupings are named tokens.

Page 8: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

Example: result = oldsum – value / 100;

Token Lexeme

IDENT Result

ASSIGN_OP =

IDENT oldsum

SUB_OP –

IDENT value

DIV_OP /

INT_LIT 100

SEMICOLON ;

4-8

Page 9: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-9

Lexical Analysis (continued)

• The lexical analyzer is usually a function that is called by the parser when it needs the next token.

• Three approaches to building a lexical analyzer:– Write a formal description of the tokens and use a

software tool that constructs table-driven lexical analyzers given such a description

– Design a state diagram that describes the tokens and write a program that implements the state diagram

– Design a state diagram that describes the tokens and hand-construct a table-driven implementation of the state diagram

Page 10: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-10

Lexical Analysis (cont’d): State Diagram Design

– A naïve state diagram would have a transition from every state on every character in the source language - such a diagram would be very large!

Page 11: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-11

Lexical Analysis (cont.)

• In many cases, transitions can be combined to simplify the state diagram– When recognizing an identifier, all uppercase

and lowercase letters are equivalent• Use a character class that includes all 52 letters

– When recognizing an integer literal, all digits are equivalent - use a digit class for 10 integral literal.

Page 12: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-12

Lexical Analysis (cont.)

• Reserved words and identifiers can be recognized together (rather than having a part of the diagram for each reserved word)– Use a table lookup to determine whether a

possible identifier is in fact a reserved word

Page 13: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-13

Lexical Analysis (cont.)

• Convenient utility subprograms:– getChar - gets the next character of input,

puts it in nextChar, determines its class and puts the class in charClass

– addChar - puts the character from nextChar into the place the lexeme is being accumulated, lexeme

– lookup - determines whether the string in lexeme is a reserved word (returns a code)

Page 14: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-14

State Diagram for Recognizing Arithmetic Expressions

Page 15: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-15

Lexical Analyzer

Implementation: SHOW front.c (pp. 176-181)

- Following is the output of the lexical analyzer of

front.c when used on (sum + 47) / totalNext token is: 25 Next lexeme is (

Next token is: 11 Next lexeme is sum

Next token is: 21 Next lexeme is +

Next token is: 10 Next lexeme is 47

Next token is: 26 Next lexeme is )

Next token is: 24 Next lexeme is /

Next token is: 11 Next lexeme is total

Next token is: -1 Next lexeme is EOF

Page 16: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-16

4.3 The Parsing Problem

• Goals of the parser, given an input program:– Find all syntax errors; for each, produce an

appropriate diagnostic message and recover quickly

– Produce the parse tree, or at least a trace of the parse tree, for the program

Page 17: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-17

4.3.1 Introducing to Parsing

• Two categories of parsers– Top down - produce the parse tree, beginning

at the root• Order is that of a leftmost derivation• Traces or builds the parse tree in preorder

– Bottom up - produce the parse tree, beginning at the leaves

• Order is that of the reverse of a rightmost derivation

• Useful parsers look only one token ahead in the input

Page 18: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

A set of notational conventions for grammar symbols1. Terminals : lowercase letters at the beginning of the

alphabet (a, b, …)2. Nonterminals: uppercase letters at the beginning of the

alphabet (A, B, …) 3. Terminals or nonterminals: uppercase letters at the end of

the alphabet (W, X, Y, Z)4. Strings of terminals: lowercase letters at the end of the

alphabet (w, x, y, z)5. Mixed strings (terminals and/or nonterminals):lowercase

Greek letters (α, β, δ, γ)

4-18

Page 19: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-19

4.3.2 Top-Down Parsers

• Top-down Parsers– Top-down parsing is a type of parsing strategy

wherein one first looks at the highest level of the parse tree and works down the parse tree by using the rewriting rules of a formal grammar.

For example, if the current sentential form is:xAα

and the A-rules are AbB, AcBb, and Aa, a top-down parser must choose among three rules to get the next sentential form, which could be xbBα, xcBbα, or xaα

Page 20: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-20

Top-Down Parsers (cont’d)

• The most common top-down parsing algorithms:– Recursive descent parser - a coded

implementation based directly on BNF description of syntax language.

– Most common alternative to recursive descent parser is to use parsing table to implement BNF rules.

Both are LL parsers (left-to-right leftmost derivation)

Page 21: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-21

4.3.3 Bottom-Up Parsers

• Bottom-up parsers– A bottom-up parser constructs a parse tree by beginning

at the leaves and processing toward the root.

In term of derivation, it can described as follows: Given a right sentential form, , determine what substring of is the right-hand side of the rule in the grammar that must be reduced to produce the previous sentential form in the right derivation

– The most common bottom-up parsing algorithms are in the LR (left-to-right scan, rightmost derivation) family (such as LALR, canonical LR parser (LR(1) parser) , LR(0) parser)

Page 22: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-22

4.3.4 The Complexity of Parsing

• The Complexity of Parsing– Parsers that work for any unambiguous

grammar are complex and inefficient ( O(n3), where n is the length of the input )

– All algorithms used for the syntax analyzers of commercial compilers have complexity O(n).

Page 23: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-23

4.4 Recursive-Descent Parsing4.4.1 Recursive-Descent Parsing Process

• A recursive-descent parsing is so named because it consists of collection of subprograms, many of which are recursive, and it produce a parse tree in top-down order.

• EBNF is ideally suited for being the basis for a recursive-descent parser, because EBNF minimizes the number of nonterminals

Page 24: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-24

Recursive-Descent Parsing (cont.)

• A grammar for simple arithmetic expressions:

<expr> <term> {(+ | -) <term>}<term> <factor> {(* | /) <factor>}<factor> id | int_constant | ( <expr> )

Note: Inf EBNF, additional metacharacters– { } for a series of zero or more– ( ) for a list, must pick one– [ ] for an optional list; pick none or one

Page 25: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-25

Recursive-Descent Parsing (cont.)

• Assume we have a lexical analyzer named lex, which puts the next token code in nextToken

• The coding process:– For each terminal symbol in the RHS, compare

it with the nextToken. If they match, continue, else there is a syntax error

– For each nonterminal symbol in the RHS, call its associated parsing subprogram

Page 26: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-26

Recursive-Descent Parsing (cont.)/* Function expr Parses strings in the language generated by the rule: <expr> → <term> {(+ | -) <term>} */

void expr() {

/* Parse the first term */   term(); /* As long as the next token is + or -, call lex to get the next token and parse the next term */   while (nextToken = = ADD_OP || nextToken = = SUB_OP){    lex();    term();   }}

Page 27: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-27

Recursive-Descent Parsing (cont.)

• This particular routine does not detect errors• Convention: Every parsing routine leaves the

next token in nextToken

Page 28: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-28

Recursive-Descent Parsing (cont.)

• A nonterminal that has more than one RHS requires an initial process to determine which RHS it is to parse.– The correct RHS is chosen on the basis of the next token

of input (the lookahead)– The next token is compared with the first token that can

be generated by each RHS until a match is found– If no match is found, it is a syntax error

Page 29: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

Recursive-Descent Parsing (cont.)

/* term

Parses strings in the language generated by the rule:

<term> -> <factor> {(* | /) <factor>)

*/

void term() {

printf("Enter <term>\n");

/* Parse the first factor */

factor();

/* As long as the next token is * or /,

next token and parse the next factor */

while (nextToken == MULT_OP || nextToken == DIV_OP) {

lex();

factor();

}

printf("Exit <term>\n");

} /* End of function term */

1-29

Page 30: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-30

Recursive-Descent Parsing (cont.)

/* Function factor Parses strings in the language generated by the rule: <factor> -> id | (<expr>) */

void factor() {

/* Determine which RHS */   if (nextToken) == ID_CODE || nextToken == INT_CODE)

/* For the RHS id, just call lex */     lex();

/* If the RHS is (<expr>) – call lex to pass over the left parenthesis, call expr, and check for the right parenthesis */    else if (nextToken == LP_CODE) {      lex(); expr();    if (nextToken == RP_CODE) lex(); else error(); } /* End of else if (nextToken == ... */

else error(); /* Neither RHS matches */}

Page 31: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-31

Recursive-Descent Parsing (cont.)

- Trace of the lexical and syntax analyzers on (sum + 47) / total

Next token is: 25 Next lexeme is ( Next token is: 11 Next lexeme is total

Enter <expr> Enter <factor>

Enter <term> Next token is: -1 Next lexeme is EOF

Enter <factor> Exit <factor>

Next token is: 11 Next lexeme is sum Exit <term>

Enter <expr> Exit <expr>

Enter <term>

Enter <factor>

Next token is: 21 Next lexeme is +

Exit <factor>

Exit <term>

Next token is: 10 Next lexeme is 47

Enter <term>

Enter <factor>

Next token is: 26 Next lexeme is )

Exit <factor>

Exit <term>

Exit <expr>

Next token is: 24 Next lexeme is /

Exit <factor>

Page 32: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

4-32

<expr>

<term>

<factor>

( <expr> )

<factor>/

<term> + <term>

<factor>

<id>

sum

<factor>

int_constant

47

<id>

total

<expr> <term>{(+|-)<term>}<term> <factor>{(*|/)<factor>}<factor> <id> | int_constant | (<expr>)

Page 33: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

1-33

4.4.2 The LL Grammar Class

The Left Recursion Problem• If a grammar has left recursion, either direct or

indirect, it causes a catastrophic problem for LL parsers.e.g., A A + B

• A grammar can be modified to remove left recursionFor each nonterminal, A, 1. Group the A-rules as A → Aα1 | … | Aαm | β1 | β2 | … | βn

where none of the β’s begins with A2. Replace the original A-rules with A → β1A’ | β2A’ | … | βnA’

A’ → α1A’ | α2A’ | … | αmA’ | ε

(Note : ε specifies the empty string)

Page 34: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

• Example Grammar with Left RecursionE E + T | TT T*F | F Grammar 1F (E) | id

• Complete Replacement GrammarE T E’E’ + T E’|ε T F T’ Grammar 2T’ *F T’| εF (E) | id

Grammar 2 generates the same language as Grammar 1, but it is not left recursive.

4-34

E-rulesα1=+T, β=T, (m=1,n=1)

E-rulesα1=*F, β=F, (m=1,n=1)

Page 35: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

• Indirect Left Recursion Probleme.g., A B a A

B A b

To remove indirect left recursion => Paper (Aho et al., 2006)

4-35

Page 36: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

Left Recursion Problem (cont’d)

Two issues:• Left recursion disallow top-down parsing;• Whether the parser can always choose the correct RHS on

the basis of the next token of input, using only the first token generated by the leftmost nonterminal in the current sentential form.

Solution: To conduct a Pairwise Disjointness Test• Pairwise Disjointness Test: a test of non-left recursive

grammar.

4-36

Page 37: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

Pairwise disjointness test

• Define: FIRST(α) = { a | α =>* a β} (If α =>* ε, ε is in

FIRST(α))

• For each nonterminal, A, that has more than one RHS, for

each pair of rules, A αi and A αj,

if FIRST(αi) FIRST(αj) = φ, this pair passes

the test; otherwise it fails the test.

4-37

Page 38: Chapter 4 Lexical and Syntax Analysis. 4-2 Chapter 4 Topics 4.1 Introduction 4.2 Lexical Analysis 4.3 The Parsing Problem 4.4 Recursive-Descent Parsing.

Pairwise disjointness test

Example: Perform the pairwise disjointness test for the following rules:

A aB | bAb | BbB cB | d

Sol: FIRST(aB)={a}, FIRST(bAb)={b}, FIRST(Bb)={c,d}

FIRST(aB) FIRST(bAb)= φ FIRST(aB) FIRST(Bb)= φFIRST(bAb) FIRST(Bb)= φ

4-38


Recommended