330 likes | 475 Views
Chapter 07. The four parts of a grammar. N , a nonterminal alphabet T , a terminal alphabet P , a set of rules of production S , the start symbol, an element of N. A grammar for C++ identifiers. N = {<identifier>, <letter>,<digit>} T = {a, b, c, 1, 2, 3} P = the productions
E N D
The four parts of a grammar • N, a nonterminal alphabet • T, a terminal alphabet • P, a set of rules of production • S, the start symbol, an element of N
A grammar for C++ identifiers • N = {<identifier>, <letter>,<digit>} • T = {a, b, c, 1, 2, 3} • P = the productions • <identifier> <letter> • <identifier> <identifier> <letter> • <identifier> <identifier> <digit> • <letter> a • <letter> b • <letter> c • <letter> 1 • <digit> 2 • <digit> 3 • S = <identifier>
A grammar for signed integers • N = {I, F, M} • T = {+, -, d} • P = the productions • I FM • F + • F – • F • M dM • M d • S = I
A context-sensitive grammar • N = {A, B, C} • T = {a, b, c} • P = the productions • A aABC • A abC • CB BC • bB bb • bC bc • cC cc • S = A
The difference between deriving an arbitrary sentence and parsing a proposed sentence
A grammar for expressions • N = {E, T, F} • T = {+, *, (, ), a} • P = the productions • E E + T • E T • T T * F • T F • F (E) • F a • S = E
A grammar for a subset of the C++ language • Please click on the following link Programs\c07g05.gif to view the appropriate program.
A finite state machine (FSM) to parse an identifier (Cont’d)
An FSM with an empty transition to parse a signed integer (Cont’d)
Combining two machines to construct one FSM that recognizes both tokens
An FSM to parse a Pep/7 assembly language identifier or symbol definition
The two FSM implementation techniques • Table lookup • Direct code
Implementation of the FSM of Figure 7.5 with the table lookup technique • Please click on the following link Programs\c07p01_prog.GIF to view the appropriate program.
A programmer-designed parse of an integer string • Please click on the following link Programs\c07p02_prog.gif to view the appropriate program.
A finite machine that recognizes multiple tokens (Cont’d) • Please click on the following links, Programs\c07p03_prog.GIF and Programs\c07p03_io.GIF, to view the appropriate programs.
A language translator • Please click on the following links, Programs\c07p04_prog.GIF and Programs\c07p04_io.GIF, to view the appropriate programs.
The three translation phases of Program 7.4 • Lexical analyzer: getToken • Parser: processSourceLine • Code generator: generateCode