language translation principles - california state …cputnam/comp516/overheads/pdfchap07.pdfa →...
TRANSCRIPT
![Page 1: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/1.jpg)
Chapter
Language TranslationPrinciples
7
![Page 2: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/2.jpg)
• The fundamental question of computer science:
“What can be automated?”
• One answer—Translation from one programming language to another.
![Page 3: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/3.jpg)
• Alphabet—A nonempty set of characters.
• Concatenation—joining characters to form a string.
• The empty string—The identity element for concatenation.
![Page 4: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/4.jpg)
{ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, *, /, =, <, >, [, ], (, ), {, }, ., ,, :, ;, &, !, %, ', " _, \, #, ?, }, |, ~ }
The C++ alphabet
![Page 5: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/5.jpg)
The alphabet for Pep/8 assembly language
{ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \, ., ,, :, ;, ‘, “ }
![Page 6: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/6.jpg)
The alphabet for real numbers
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, . }
![Page 7: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/7.jpg)
Concatenation• Joining two or more characters to make a
string
• Applies to strings concatenated to construct longer strings
![Page 8: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/8.jpg)
The empty string•
• Concatenation property
7.1 LANGUAGES, GRAMMARS, AND PARSING
!
"
!x = x! = x
All nonassociative binary infix operators associate from left to right except !!, #, and", which associate from right to left.The operators on lines (j), (l), and (m) may have a slash / through them to denote
negation—e.g. b #$ c is an abbreviation for ¬(b $ c).
SOME BASIC TYPES
Name Symbol Type (set of values)
integer Z integers: . . . ,%3,%2,%1, 0, 1, 2, 3, . . .
nat N natural numbers: 0, 1, 2, . . .
positive Z+ positive integers: 1, 2, 3, . . .
negative Z! negative integers: %1,%2,%3, . . .
rational Q rational numbers: i/j for i, j integers, j #= 0reals R real numbers
positive reals R+ positive real numbers
bool B booleans: true, false
THEOREMS OF THE PROPOSITIONAL CALCULUS
Equivalence and true.
(3.1) Axiom, Associativity of $ : ((p $ q) $ r) $ (p $ (q $ r))(3.2) Axiom, Symmetry of $ : p $ q $ q $ p
(3.3) Axiom, Identity of $ : true $ q $ q
(3.4) true
(3.5) Reflexivity of $ : p $ p
Negation, inequivalence, and false.
(3.8) Axiom, Definition of false : false $ ¬true
(3.9) Axiom, Distributivity of ¬ over $ : ¬(p $ q) $ ¬p $ q
(3.10) Axiom, Definition of #$ : (p #$ q) $ ¬(p $ q)(3.11) ¬p $ q $ p $ ¬q
(3.12) Double negation: ¬¬p $ p
(3.13) Negation of false: ¬false $ true
(3.14) (p #$ q) $ ¬p $ q
(3.15) ¬p $ p $ false
(3.16) Symmetry of #$ : (p #$ q) $ (q #$ p)(3.17) Associativity of #$ : ((p #$ q) #$ r) $ (p #$ (q #$ r))(3.18) Mutual associativity: ((p #$ q) $ r) $ (p #$ (q $ r))(3.19) Mutual interchangeability: p #$ q $ r $ p $ q #$ r
1
7.1 LANGUAGES, GRAMMARS, AND PARSING
!
"
!x = x! = x
All nonassociative binary infix operators associate from left to right except !!, #, and", which associate from right to left.The operators on lines (j), (l), and (m) may have a slash / through them to denote
negation—e.g. b #$ c is an abbreviation for ¬(b $ c).
SOME BASIC TYPES
Name Symbol Type (set of values)
integer Z integers: . . . ,%3,%2,%1, 0, 1, 2, 3, . . .
nat N natural numbers: 0, 1, 2, . . .
positive Z+ positive integers: 1, 2, 3, . . .
negative Z! negative integers: %1,%2,%3, . . .
rational Q rational numbers: i/j for i, j integers, j #= 0reals R real numbers
positive reals R+ positive real numbers
bool B booleans: true, false
THEOREMS OF THE PROPOSITIONAL CALCULUS
Equivalence and true.
(3.1) Axiom, Associativity of $ : ((p $ q) $ r) $ (p $ (q $ r))(3.2) Axiom, Symmetry of $ : p $ q $ q $ p
(3.3) Axiom, Identity of $ : true $ q $ q
(3.4) true
(3.5) Reflexivity of $ : p $ p
Negation, inequivalence, and false.
(3.8) Axiom, Definition of false : false $ ¬true
(3.9) Axiom, Distributivity of ¬ over $ : ¬(p $ q) $ ¬p $ q
(3.10) Axiom, Definition of #$ : (p #$ q) $ ¬(p $ q)(3.11) ¬p $ q $ p $ ¬q
(3.12) Double negation: ¬¬p $ p
(3.13) Negation of false: ¬false $ true
(3.14) (p #$ q) $ ¬p $ q
(3.15) ¬p $ p $ false
(3.16) Symmetry of #$ : (p #$ q) $ (q #$ p)(3.17) Associativity of #$ : ((p #$ q) #$ r) $ (p #$ (q #$ r))(3.18) Mutual associativity: ((p #$ q) $ r) $ (p #$ (q $ r))(3.19) Mutual interchangeability: p #$ q $ r $ p $ q #$ r
1
![Page 9: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/9.jpg)
Languages• The closure T* of alphabet T
‣ The set of all possible strings formed by concatenating elements from T
• Language
‣ A subset of the closure of its alphabet
![Page 10: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/10.jpg)
• Grammars
• Finite state machines
• Regular expressions
Techniques to specify syntax
![Page 11: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/11.jpg)
• N, a nonterminal alphabet
• T, a terminal alphabet
• P, a set of rules of production
• S, the start symbol, an element of N
The four parts of a grammar
![Page 12: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/12.jpg)
7.1 LANGUAGES, GRAMMARS, AND PARSING
!
!x = x! = x
N = {< identifier >,< letter >,< digit >}T = {a,b,c,1,2,3}P = the productions
1. < identifier >! < letter >
2. < identifier >! < identifier > < letter >
3. < identifier >! < identifier > < digit >
4. < letter >! a
5. < letter >! b
6. < letter >! c
7. < digit >! 1
8. < digit >! 2
9. < digit >! 3
S = < identifier >
All nonassociative binary infix operators associate from left to right except "", ", and#, which associate from right to left.The operators on lines (j), (l), and (m) may have a slash / through them to denote
negation—e.g. b $% c is an abbreviation for ¬(b % c).
SOME BASIC TYPES
Name Symbol Type (set of values)
integer Z integers: . . . ,&3,&2,&1, 0, 1, 2, 3, . . .
nat N natural numbers: 0, 1, 2, . . .
positive Z+ positive integers: 1, 2, 3, . . .
negative Z! negative integers: &1,&2,&3, . . .
rational Q rational numbers: i/j for i, j integers, j $= 0reals R real numbers
positive reals R+ positive real numbers
bool B booleans: true, false
THEOREMS OF THE PROPOSITIONAL CALCULUS
Equivalence and true.
(3.1) Axiom, Associativity of % : ((p % q) % r) % (p % (q % r))(3.2) Axiom, Symmetry of % : p % q % q % p
(3.3) Axiom, Identity of % : true % q % q
(3.4) true
(3.5) Reflexivity of % : p % p
1
Figure 7.1
![Page 13: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/13.jpg)
7.1 LANGUAGES, GRAMMARS, AND PARSING
!
!x = x! = x
N = {< identifier >,< letter >,< digit >}T = {a,b,c,1,2,3}P = the productions
1. < identifier >! < letter >
2. < identifier >! < identifier > < letter >
3. < identifier >! < identifier > < digit >
4. < letter >! a
5. < letter >! b
6. < letter >! c
7. < digit >! 1
8. < digit >! 2
9. < digit >! 3
S = < identifier >
N = {I,F,M}T = {+,-,d}P = the productions
1. I ! FM
2. F ! +
3. F ! -
4. F ! !
5. M ! dM
6. M ! d
S = I
1
Figure 7.2
![Page 14: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/14.jpg)
Grammars• Context-free
‣ A single nonterminal on the left side of every production rule
• Context-sensitive
‣ Not context-free
![Page 15: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/15.jpg)
7.1 LANGUAGES, GRAMMARS, AND PARSING
!
!x = x! = x
N = {< identifier >,< letter >,< digit >}T = {a,b,c,1,2,3}P = the productions
1. < identifier >! < letter >
2. < identifier >! < identifier > < letter >
3. < identifier >! < identifier > < digit >
4. < letter >! a
5. < letter >! b
6. < letter >! c
7. < digit >! 1
8. < digit >! 2
9. < digit >! 3
S = < identifier >
N = {I,F,M}T = {+,-,d}P = the productions
1. I ! FM
2. F ! +
3. F ! -
4. F ! !
5. M ! dM
6. M ! d
S = I
N = {A,B,C}T = {a,b,c}P = the productions
1. A ! aABC
2. A ! abC
3. CB ! BC
4. bB ! bb
5. bC ! bc
6. cC ! cc
S = A
1
Figure 7.3
![Page 16: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/16.jpg)
Valid sentenceDerivation
Proposed
sentenceGrammar
Grammar
(a) Deriving a valid sentence.
(b) The parsing problem.
Derivation
or
“not valid “
Figure 7.4
![Page 17: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/17.jpg)
2
N = {E,T,F}T = {+,*,(,),a}P = the productions
1. E ! E + T
2. E ! T
3. T ! T * F
4. T ! F
5. F ! ( E )
6. F ! a
S = E
Figure 7.5
![Page 18: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/18.jpg)
+E T
T F
F
E( )
T
*T F
F
E
Figure 7.6
![Page 19: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/19.jpg)
F M
M
I
Figure 7.7
![Page 20: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/20.jpg)
342 Chapter 7 Language Translation Principles
<translation-unit> !<external-declaration>! <translation-unit> <external-declaration>
<external-declaration> !<function-definition>! <declaration>
<function-definition> !<type-specifier> <identifier> ( <parameter-list> ) <compound-statement>! <identifier> ( <parameter-list> ) <compound-statement>
<declaration> ! <type-specifier> <declarator-list> ;
<type-specifier> ! void ! char ! int
<declarator-list> !<identifier>! <declarator-list> , <identifier>
<parameter-list> !!! <parameter-declaration>! <parameter-list> , <parameter-declaration>
<parameter-declaration> ! <type-specifier> <identifier>
<compound-statement> ! { <declaration-list> <statement-list> }
<declaration-list> !!! <declaration>! <declaration-list> <declaration>
<statement-list> !!! <statement>! <statement-list> <statement>
<statement> !<compound-statement>! <expression-statement>! <selection-statement>! <iteration-statement>
<expression-statement> ! <expression> ;
<selection-statement> !if ( <expression> ) <statement>! if ( <expression> ) <statement> else <statement>
<iteration-statement> !while ( <expression> ) <statement>! do <statement> while ( <expression> ) ;
<expression> !<relational-expression>! <identifier> " <expression>
Figure 7.8A grammar for a subset of the C++language.
32397_CH07_331_388.qxd 1/14/09 5:01 PM Page 342
Figure 7.8
![Page 21: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/21.jpg)
342 Chapter 7 Language Translation Principles
<translation-unit> !<external-declaration>! <translation-unit> <external-declaration>
<external-declaration> !<function-definition>! <declaration>
<function-definition> !<type-specifier> <identifier> ( <parameter-list> ) <compound-statement>! <identifier> ( <parameter-list> ) <compound-statement>
<declaration> ! <type-specifier> <declarator-list> ;
<type-specifier> ! void ! char ! int
<declarator-list> !<identifier>! <declarator-list> , <identifier>
<parameter-list> !!! <parameter-declaration>! <parameter-list> , <parameter-declaration>
<parameter-declaration> ! <type-specifier> <identifier>
<compound-statement> ! { <declaration-list> <statement-list> }
<declaration-list> !!! <declaration>! <declaration-list> <declaration>
<statement-list> !!! <statement>! <statement-list> <statement>
<statement> !<compound-statement>! <expression-statement>! <selection-statement>! <iteration-statement>
<expression-statement> ! <expression> ;
<selection-statement> !if ( <expression> ) <statement>! if ( <expression> ) <statement> else <statement>
<iteration-statement> !while ( <expression> ) <statement>! do <statement> while ( <expression> ) ;
<expression> !<relational-expression>! <identifier> " <expression>
Figure 7.8A grammar for a subset of the C++language.
32397_CH07_331_388.qxd 1/14/09 5:01 PM Page 342
Figure 7.8
![Page 22: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/22.jpg)
342 Chapter 7 Language Translation Principles
<translation-unit> !<external-declaration>! <translation-unit> <external-declaration>
<external-declaration> !<function-definition>! <declaration>
<function-definition> !<type-specifier> <identifier> ( <parameter-list> ) <compound-statement>! <identifier> ( <parameter-list> ) <compound-statement>
<declaration> ! <type-specifier> <declarator-list> ;
<type-specifier> ! void ! char ! int
<declarator-list> !<identifier>! <declarator-list> , <identifier>
<parameter-list> !!! <parameter-declaration>! <parameter-list> , <parameter-declaration>
<parameter-declaration> ! <type-specifier> <identifier>
<compound-statement> ! { <declaration-list> <statement-list> }
<declaration-list> !!! <declaration>! <declaration-list> <declaration>
<statement-list> !!! <statement>! <statement-list> <statement>
<statement> !<compound-statement>! <expression-statement>! <selection-statement>! <iteration-statement>
<expression-statement> ! <expression> ;
<selection-statement> !if ( <expression> ) <statement>! if ( <expression> ) <statement> else <statement>
<iteration-statement> !while ( <expression> ) <statement>! do <statement> while ( <expression> ) ;
<expression> !<relational-expression>! <identifier> " <expression>
Figure 7.8A grammar for a subset of the C++language.
32397_CH07_331_388.qxd 1/14/09 5:01 PM Page 342
Figure 7.8
![Page 23: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/23.jpg)
7.1 Languages, Grammars, and Parsing 343
<relational-expression> !<additive-expression>! <relational-expression> < <additive-expression>! <relational-expression> > <additive-expression>! <relational-expression> <= <additive-expression>! <relational-expression> >= <additive-expression>
<additive-expression> !<multiplicative-expression>! <additive-expression> + <multiplicative-expression>! <additive-expression> - <multiplicative-expression>
<multiplicative-expression> !<unary-expression>! <multiplicative-expression> * <unary-expression>! <multiplicative-expression> / <unary-expression>
<unary-expression> !<primary-expression>! <identifier> ( <argument-expression-list> )
<primary-expression> !<identifier>! <constant>! ( <expression> )
<argument-expression-list> !<expression>! <argument-expression-list> , <expression>
<constant> !<integer-constant>! <character-constant>
<integer-constant> !<digit>! <integer-constant> <digit>
<character-constant> ! ' <letter> '
<identifier> !<letter>! <identifier> <letter>! <identifier> <digit>
<letter> !a ! b ! c ! d ! e ! f ! g ! h ! i ! j ! k ! l ! m !n ! o ! p ! q ! r ! s ! t ! u ! v ! w ! x ! y ! z !A ! B ! C ! D ! E ! F ! G ! H ! I ! J ! K ! L ! M !N ! O ! P ! Q ! R ! S ! T ! U ! V ! W ! X ! Y ! Z
<digit> !0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! 9
Figure 7.8(Continued)
32397_CH07_331_388.qxd 1/14/09 5:01 PM Page 343
Figure 7.8
![Page 24: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/24.jpg)
7.1 Languages, Grammars, and Parsing 343
<relational-expression> !<additive-expression>! <relational-expression> < <additive-expression>! <relational-expression> > <additive-expression>! <relational-expression> <= <additive-expression>! <relational-expression> >= <additive-expression>
<additive-expression> !<multiplicative-expression>! <additive-expression> + <multiplicative-expression>! <additive-expression> - <multiplicative-expression>
<multiplicative-expression> !<unary-expression>! <multiplicative-expression> * <unary-expression>! <multiplicative-expression> / <unary-expression>
<unary-expression> !<primary-expression>! <identifier> ( <argument-expression-list> )
<primary-expression> !<identifier>! <constant>! ( <expression> )
<argument-expression-list> !<expression>! <argument-expression-list> , <expression>
<constant> !<integer-constant>! <character-constant>
<integer-constant> !<digit>! <integer-constant> <digit>
<character-constant> ! ' <letter> '
<identifier> !<letter>! <identifier> <letter>! <identifier> <digit>
<letter> !a ! b ! c ! d ! e ! f ! g ! h ! i ! j ! k ! l ! m !n ! o ! p ! q ! r ! s ! t ! u ! v ! w ! x ! y ! z !A ! B ! C ! D ! E ! F ! G ! H ! I ! J ! K ! L ! M !N ! O ! P ! Q ! R ! S ! T ! U ! V ! W ! X ! Y ! Z
<digit> !0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! 9
Figure 7.8(Continued)
32397_CH07_331_388.qxd 1/14/09 5:01 PM Page 343
Figure 7.8
![Page 25: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/25.jpg)
7.1 Languages, Grammars, and Parsing 343
<relational-expression> !<additive-expression>! <relational-expression> < <additive-expression>! <relational-expression> > <additive-expression>! <relational-expression> <= <additive-expression>! <relational-expression> >= <additive-expression>
<additive-expression> !<multiplicative-expression>! <additive-expression> + <multiplicative-expression>! <additive-expression> - <multiplicative-expression>
<multiplicative-expression> !<unary-expression>! <multiplicative-expression> * <unary-expression>! <multiplicative-expression> / <unary-expression>
<unary-expression> !<primary-expression>! <identifier> ( <argument-expression-list> )
<primary-expression> !<identifier>! <constant>! ( <expression> )
<argument-expression-list> !<expression>! <argument-expression-list> , <expression>
<constant> !<integer-constant>! <character-constant>
<integer-constant> !<digit>! <integer-constant> <digit>
<character-constant> ! ' <letter> '
<identifier> !<letter>! <identifier> <letter>! <identifier> <digit>
<letter> !a ! b ! c ! d ! e ! f ! g ! h ! i ! j ! k ! l ! m !n ! o ! p ! q ! r ! s ! t ! u ! v ! w ! x ! y ! z !A ! B ! C ! D ! E ! F ! G ! H ! I ! J ! K ! L ! M !N ! O ! P ! Q ! R ! S ! T ! U ! V ! W ! X ! Y ! Z
<digit> !0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! 9
Figure 7.8(Continued)
32397_CH07_331_388.qxd 1/14/09 5:01 PM Page 343
Figure 7.8
![Page 26: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/26.jpg)
<statement>
<expression>
<relational-expression>
<relational-expression>
<additive-expression>
<additive-expression>
<expression-statement>
<expression>
<multiplicative-expression>
<multiplicative-expression>
<unary-expression>
<unary-expression>
<primary-expression>
<primary-expression>
<constant>
<identifier> <integer-constant>
<letter> <digit>
a 9
<statement>while
<iteration-statement>
( )
<= ;
S1
Figure 7.9
![Page 27: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/27.jpg)
Finite state machines• Finite set of states called nodes represented
by circles
• Transitions between states represented by directed arcs
• Each arc labeled by a terminal character
• One state designated the start state
• A nonempty set of states designated final states
![Page 28: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/28.jpg)
B
C
letter
digit
digit
letter
digit
A
letterStart
Figure 7.10
![Page 29: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/29.jpg)
Parsing rules• Start at the start state
• Scan the string from left to right
• For each terminal scanned, make a transition to the next state in the FSM
• After the last terminal scanned, if you are in a final state the string is in the language
• Otherwise, it is not
![Page 30: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/30.jpg)
CurrentState
ABC
Next State
BB
C
Digit
CBC
Letter
Figure 7.11
![Page 31: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/31.jpg)
Simplified FSM• Not all states have transitions on all terminal
symbols
• Two ways to detect an illegal string
‣ You may run out of input, and not be in a final state
‣ You may be in some state, and the next input character does not correspond to any of the transitions from that state
![Page 32: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/32.jpg)
B
letter
digit
AStart letter
Figure 7.12
![Page 33: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/33.jpg)
CurrentState
AB
Next State
BB
Digit
B
Letter
Figure 7.13
![Page 34: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/34.jpg)
Nondeterministic FSM• At least one state has more than one
transition from it on the same character
• If you scan the last character and you are in a final state, the string is valid
• If you scan the last character and you are not in a final state, the string might be invalid
• To prove invalid, you must try all possibilities with backtracking
![Page 35: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/35.jpg)
B
digit
digit
AStart
+
C
digit
digit
-
Figure 7.14
![Page 36: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/36.jpg)
CurrentState
ABC
Next State
B
Digit
B, CB, C
+ –
B
Figure 7.15
![Page 37: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/37.jpg)
Empty transitions• An empty transition allows you to go from
one state to another state without scanning a terminal character
• All finite state machines with empty transitions are considered nondeterministic
![Page 38: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/38.jpg)
F
digit
IStart
+
M
!
digit
-
Figure 7.17
![Page 39: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/39.jpg)
CurrentState
IFM
Next State
FMM
+ –
F F
Figure 7.16
![Page 40: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/40.jpg)
Removing empty transitions
• Given a transition from p to q on , for every transition from q to r on a, add a transition from p to r on a.
• If q is a final state, make p a final state
7.1 LANGUAGES, GRAMMARS, AND PARSING
!
"
!x = x! = x
All nonassociative binary infix operators associate from left to right except !!, #, and", which associate from right to left.The operators on lines (j), (l), and (m) may have a slash / through them to denote
negation—e.g. b #$ c is an abbreviation for ¬(b $ c).
SOME BASIC TYPES
Name Symbol Type (set of values)
integer Z integers: . . . ,%3,%2,%1, 0, 1, 2, 3, . . .
nat N natural numbers: 0, 1, 2, . . .
positive Z+ positive integers: 1, 2, 3, . . .
negative Z! negative integers: %1,%2,%3, . . .
rational Q rational numbers: i/j for i, j integers, j #= 0reals R real numbers
positive reals R+ positive real numbers
bool B booleans: true, false
THEOREMS OF THE PROPOSITIONAL CALCULUS
Equivalence and true.
(3.1) Axiom, Associativity of $ : ((p $ q) $ r) $ (p $ (q $ r))(3.2) Axiom, Symmetry of $ : p $ q $ q $ p
(3.3) Axiom, Identity of $ : true $ q $ q
(3.4) true
(3.5) Reflexivity of $ : p $ p
Negation, inequivalence, and false.
(3.8) Axiom, Definition of false : false $ ¬true
(3.9) Axiom, Distributivity of ¬ over $ : ¬(p $ q) $ ¬p $ q
(3.10) Axiom, Definition of #$ : (p #$ q) $ ¬(p $ q)(3.11) ¬p $ q $ p $ ¬q
(3.12) Double negation: ¬¬p $ p
(3.13) Negation of false: ¬false $ true
(3.14) (p #$ q) $ ¬p $ q
(3.15) ¬p $ p $ false
(3.16) Symmetry of #$ : (p #$ q) $ (q #$ p)(3.17) Associativity of #$ : ((p #$ q) #$ r) $ (p #$ (q #$ r))(3.18) Mutual associativity: ((p #$ q) $ r) $ (p #$ (q $ r))(3.19) Mutual interchangeability: p #$ q $ r $ p $ q #$ r
1
![Page 41: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/41.jpg)
The equivalent FSM without anempty transition.
ZYXStart
c
!
( )a The original FSM.
aZYX
Start
c
a
( )b
a
Figure 7.18
The equivalent FSM without anempty transition.
ZYXStart
c
!
( )a The original FSM.
aZYX
Start
c
a
( )b
a
![Page 42: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/42.jpg)
digit
The empty transition removed.
F
digit
IStart
+
M
-
digit
( )a The original FSM.
F
digit
IStart
+
M
-
digit
( )b
Figure 7.19
![Page 43: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/43.jpg)
Z
WStart
a
bc
!
(a) The original FSM.
Start
Y
Z
Y
a
bb
ac
(b) The equivalent FSM without an empty transition.
WX X
Figure 7.20
![Page 44: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/44.jpg)
Multiple token recognizers
• Token
‣ A set of terminal characters that has meaning as a group
• FSM with multiple final states
• The final state determines the token that is recognized
![Page 45: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/45.jpg)
0 X
1..9
CBAStart
DStart
CBA
D
FStart
0 X
1..9
(a) Separate machines for the 0X and unsigned integer tokens.
(b) One nondeterministic FSM that recognizes the 0X or unsigned integer token.
0..9E0..9E
Figure 7.21
![Page 46: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/46.jpg)
Removing the empty transitions.( )a Removing the inaccessible states.
CBX
0..9E
FStart
1..9
0
( )b
CX
BA0
D1..9
FStart
0
1..9E 0..9
Figure 7.22
![Page 47: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/47.jpg)
CB
letter
digit
AStart :letter
Figure 7.23
![Page 48: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/48.jpg)
Sourceprogram
Lexical analyzer(deterministic FSM)
Parser(grammar)
Codegenerator
Objectprogram
Syntax Semantics
Figure 7.24
![Page 49: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/49.jpg)
FSM implementation techniques
• Table-lookup
• Direct-code
![Page 50: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/50.jpg)
CurrentState
ABC
Next State
BB
C
Digit
CBC
Letter
Figure 7.11
![Page 51: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/51.jpg)
Interactive Input/Output
Enter a string of letters and digits: cab3The string is a valid identifier.
Interactive Input/Output
Enter a string of letters and digits: 3cabInvalid identifier.
Figure 7.25Input/Output
![Page 52: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/52.jpg)
Figure 7.25
#include <iostream>#include <cctype> // isalpha#include <string> // stringusing namespace std;
enum State {eA, eB, eC};enum Alphabet {eLETTER, eDIGIT};State FSM[eC + 1][eDIGIT + 1] = { // Three rows, two columns eB, eC, // The state transition table of Figure 7.11 eB, eB, eC, eC};
![Page 53: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/53.jpg)
Figure 7.25(Continued)
int main () { char ch; string line; Alphabet FSMChar; State state = eA; cout << "Enter a string of letters and digits: "; cin >> line; for (int i = 0; i < line.size (); i++) { ch = line[i]; if (isalpha (ch)) { FSMChar = eLETTER; } else { FSMChar = eDIGIT; } state = FSM[state][FSMChar]; } if (state == eB) { cout << line << " is a valid identifier." << endl; } else { cout << line << " is not a valid identifier." << endl; } return 0;}
![Page 54: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/54.jpg)
digit
The empty transition removed.
F
digit
IStart
M digit
( )a The original FSM.
F
digit
IStart
M digit
( )b
Figure 7.19(b)
![Page 55: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/55.jpg)
Figure 7.26Input/Output
Interactive Input/Output
Enter a number: qInvalid entry.
Interactive Input/Output
Enter a number: -58Num = -58
![Page 56: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/56.jpg)
#include <iostream>#include <cctype> // isdigit#include <string> // string, getlineusing namespace std;
// Global bufferstring line;int lineIndex;
void getLine () { // Get a line of characters. // Install a newline character as sentinel. getline (cin, line); line.push_back ('\n'); lineIndex = 0;}
Figure 7.26
![Page 57: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/57.jpg)
int main () { bool valid; int num; cout << "Enter number: "; getLine (); parseNum (valid, num); if (valid) { cout << "Number = " << num << endl; } else { cout << "Invalid entry." << endl; } return 0;}
Figure 7.26(Continued)
![Page 58: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/58.jpg)
enum State {eI, eF, eM, eSTOP};
void parseNum (bool& v, int& n) { int sign; State state; char nextChar; v = true; state = eI; do { nextChar = line[lineIndex++]; switch (state) {
Figure 7.26(Continued)
![Page 59: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/59.jpg)
case eI: if (nextChar == '+') { sign = +1; state = eF; } else if (nextChar == '-') { sign = -1; state = eF; } else if (isdigit (nextChar)) { sign = +1; n = nextChar - '0'; state = eM; } else { v = false; } break; case eF: if (isdigit (nextChar)) { n = nextChar - '0'; state = eM; } else { v = false; } break;
Figure 7.26(Continued)
![Page 60: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/60.jpg)
case eM: if (isdigit (nextChar)) { n = 10 * n + nextChar - '0'; } else if (nextChar == '\n') { n = sign * n; state = eSTOP; } else { v = false; } break; } } while ((state != eSTOP) && v);}
Figure 7.26(Continued)
![Page 61: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/61.jpg)
An input buffer• Used to process one character at a time
from a C++ string as if from an input stream
• Provides a special feature needed by multiple-token parsers
• Ability to back up a character into the input stream after being scanned
![Page 62: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/62.jpg)
#include <string> // string, getline
class InBuffer {private: string line; int lineIndex;
public: void getLine () { getline (cin, line); line.push_back ('\n'); lineIndex = 0; }
void advanceInput (char& ch) { ch = line[lineIndex++]; }
void backUpInput () { lineIndex--; }};
Figure 7.27
![Page 63: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/63.jpg)
digit
space +
-
letter
Start
Ident
Sign
Int
digit
letter
digit
digit
Figure 7.28
![Page 64: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/64.jpg)
Figure 7.29Input/Output
Input
Here is A47 48B C-49 ALongIdentifier +50 D16-51
Output
Identifier = HereIdentifier = isIdentifier = A47Integer = 48Identifier = BEmpty tokenIdentifier = CInteger = -49Identifier = ALongIdentifierInteger = 50Identifier = D16Integer = -51Empty token
![Page 65: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/65.jpg)
Figure 7.29Input/Output
Input
Here is A47+ 48B C+49
ALongIdentifier
Output
Identifier = HereIdentifier = isIdentifier = A47Syntax errorIdentifier = CInteger = 49Empty tokenEmpty tokenIdentifier = ALongIdentifierEmpty token
![Page 66: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/66.jpg)
TEmpty TInvalid
+ tokenType(): Token + printToken ()
AToken
TIdentifier– identValue: int
+ TInteger (str: string)
TInteger – intValue: int
+ TInteger (i: int)
Figure 7.30
![Page 67: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/67.jpg)
Figure 7.31
#include <iostream>#include <cctype> // isalpha, isdigit#include <string> // string, getlineusing namespace std;
#include "inBuffer.hpp" // InBufferInBuffer inBuffer;
enum Token {eT_IDENTIFIER, eT_INTEGER, eT_EMPTY, eT_INVALID};
class AToken {public: virtual Token tokenType () = 0; virtual void printToken () = 0;};
class TEmpty : public AToken {public: Token tokenType () { return eT_EMPTY; } void printToken () { cout << "Empty token" << endl; }};
![Page 68: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/68.jpg)
Figure 7.31(Continued)
class TInvalid : public AToken {public: Token tokenType () { return eT_INVALID; } void printToken () { cout << "Syntax error" << endl; }};
class TInteger : public AToken {private: int intValue;public: TInteger (int i) { intValue = i; } Token tokenType () { return eT_INTEGER; } void printToken () { cout << "Integer = " << intValue << endl; }};
class TIdentifier : public AToken {private: string identValue;public: TIdentifier (string str) { identValue = str; } Token tokenType () { return eT_IDENTIFIER; } void printToken () { cout << "Identifier = " << identValue << endl; }};
![Page 69: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/69.jpg)
Figure 7.32
enum State {eS_START, eS_IDENT, eS_SIGN, eS_INTEGER, eS_STOP};
void getToken (AToken*& pAT) { // Pre: pAT is set to a token object. // Post: pAT is set to a token object that corresponds to // the next token in the input buffer. State state; char nextChar; int sign; int localIntValue; string localIdentValue; delete pAT; pAT = new TEmpty; state = eS_START; do { inBuffer.advanceInput (nextChar); switch (state) {
![Page 70: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/70.jpg)
Figure 7.32(Continued)
case eS_START: if (isalpha(nextChar)) { localIdentValue = nextChar; state = eS_IDENT; } else if (nextChar == '-') { sign = -1; state = eS_SIGN; } else if (nextChar == '+') { sign = +1; state = eS_SIGN; } else if (isdigit(nextChar)) { localIntValue = nextChar - '0'; sign = +1; state = eS_INTEGER; } else if (nextChar == '\n') { state = eS_STOP; } else if (nextChar != ' ') { delete pAT; pAT = new TInvalid; } break;
![Page 71: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/71.jpg)
Figure 7.32(Continued)
case eS_IDENT: if (isalpha (nextChar) || isdigit (nextChar)) { localIdentValue.push_back (nextChar); } else { inBuffer.backUpInput (); delete pAT; pAT = new TIdentifier (localIdentValue); state = eS_STOP; } break; case eS_SIGN: if (isdigit (nextChar)) { localIntValue = nextChar - '0'; state = eS_INTEGER; } else { delete pAT; pAT = new TInvalid; } break;
![Page 72: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/72.jpg)
Figure 7.32(Continued)
case eS_INTEGER: if (isdigit (nextChar)) { localIntValue = 10 * localIntValue + nextChar - '0'; } else { inBuffer.backUpInput (); delete pAT; pAT = new TInteger (sign * localIntValue); state = eS_STOP; } break; } } while ((state != eS_STOP) && (pAT->tokenType () != eT_INVALID));}
![Page 73: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/73.jpg)
Figure 7.33
int main () { AToken* pAToken = new TEmpty; inBuffer.getLine (); while (!cin.eof ()) { do { getToken (pAToken); pAToken->printToken (); } while ((pAToken->tokenType () != eT_EMPTY) && (pAToken->tokenType () != eT_INVALID)); inBuffer.getLine (); } delete pAToken; return 0;}
![Page 74: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/74.jpg)
You can never fail once you reach a final state. Instead, if the final state does not have a transition from it on the terminal just input, you have recognized a token and should back up the input. The character will then be available as the first terminal for the next token.
A design principle for multiple-token
recognizers
![Page 75: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/75.jpg)
Figure 7.34Input/Output
Input
set (Time, 15)set (Accel, 3)set (TSquared, Time)MUL (TSquared, Time)set (Position, TSquared)mul (Position, Accel)div (Position, 2)end
Output
Time := 15Accel := 3TSquared := TimeTSquared := TSquared * TimePosition := TSquaredPosition := Position * AccelPosition := Position / 2
![Page 76: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/76.jpg)
Figure 7.34Input/Output
Input
set (Alpha,, 123)set (Alpha)sit (Alpha, 123)set, (Alpha)mul (Alpha, Betaset (123, Alpha)neg (Alpha, Beta)set (Alpha, 123) x
Output
Error: Second argument not an identifier or integer.Error: Comma expected after first argument.Error: Line must begin with function identifier.Error: Left parenthesis expected after function.Error: Right parenthesis expected after argument.Error: First argument not an identifier.Error: The argument of "neg" is malformed.Error: Illegal trailing character.Error: Missing "end" sentinel.
![Page 77: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/77.jpg)
Figure 7.35
// ======== Global tablesenum Mnemon { eM_ADD, eM_SUB, eM_MUL, eM_DIV, eM_NEG, eM_SET, eM_END, eM_EMPTY};
map <Mnemon, string> operatorTable;map <string, Mnemon> mnemonTable;
void initGlobalTables () { operatorTable [eM_ADD] = "+"; operatorTable [eM_SUB] = "-"; operatorTable [eM_MUL] = "*"; operatorTable [eM_DIV] = "/"; mnemonTable ["add"] = eM_ADD; mnemonTable ["sub"] = eM_SUB; mnemonTable ["mul"] = eM_MUL; mnemonTable ["div"] = eM_DIV; mnemonTable ["neg"] = eM_NEG; mnemonTable ["set"] = eM_SET; mnemonTable ["end"] = eM_END;}
![Page 78: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/78.jpg)
Figure 7.35(Continued)
void lookUpMnemon (string id, Mnemon& mn, bool& fnd) { string lowerId = ""; for (int i = 0; i < id.size (); i++) { lowerId.push_back (tolower (id[i])); } if (mnemonTable.count (lowerId) == 1) { mn = mnemonTable[lowerId]; fnd = true; } else { fnd = false; }}
![Page 79: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/79.jpg)
+ putArg ()
AArg
IdentArg– identValue: string
+ IdentArg (str: string)
IntArg– intValue: int
+ IntArg (i: int)
Figure 7.36
![Page 80: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/80.jpg)
Figure 7.37
// ======== The argument classesclass AArg {public: virtual void putArg () = 0;};
class IdentArg : public AArg {private: string identValue;public: IdentArg (string str) { identValue = str; } void putArg () { cout << identValue; }};
class IntArg : public AArg {private: int intValue;public: IntArg (int i) { intValue = i; } void putArg () { cout << intValue; }};
![Page 81: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/81.jpg)
TComma TRightParen
TLeftParen TEmpty
TInvalid
+ tokenType ( ): Token+ getArg ( ); AArg*+ getIdentValue ( ): string
AToken
TIdentifier– identValue: string
+ TIdentifier (str: string)
TInteger– intValue: int
+ TInteger (i: int)
Figure 7.38
![Page 82: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/82.jpg)
Figure 7.39
// ======== The token classesenum Token { eT_IDENTIFIER, eT_INTEGER, eT_COMMA, eT_LEFT_PAREN, eT_RIGHT_PAREN, eT_EMPTY, eT_INVALID};
class AToken {public: virtual Token tokenType () = 0; virtual AArg* getArg () { return 0; } virtual string getIdentValue () { return ""; }};
class TIdentifier : public AToken {private: string identValue;public: TIdentifier (string str) { identValue = str; } AArg* getArg () { AArg* pArg = new IdentArg (identValue); return pArg; } Token tokenType () { return eT_IDENTIFIER; } string getIdentValue () { return identValue; }};
![Page 83: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/83.jpg)
Figure 7.39(Continued)
class TInteger : public AToken {private: int intValue;public: TInteger (int i) { intValue = i; } AArg* getArg () { IntArg* pArg = new IntArg (intValue); return pArg; } Token tokenType () { return eT_INTEGER; }};
class TComma : public AToken {public: Token tokenType () { return eT_COMMA; }};
![Page 84: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/84.jpg)
Figure 7.39(Continued)
class TLeftParen : public AToken {public: Token tokenType () { return eT_LEFT_PAREN; }};
class TRightParen : public AToken {public: Token tokenType () { return eT_RIGHT_PAREN; }};
class TEmpty : public AToken {public: Token tokenType () { return eT_EMPTY; }};
class TInvalid : public AToken {public: Token tokenType () { return eT_INVALID; }};
![Page 85: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/85.jpg)
+ putArg ()
AArg
IdentArg– identValue: string
+ IdentArg (str: string)
IntArg– intValue: int
+ IntArg (i: int)
TwoArgs– pFirstArg: AArg* – pSecondArg: AArg*
+ TwoArgs (mn: Mnemon, pFArg: AArg*, pSArg: AArg*)
+ ~TwoArgs ( )
OneArg– pFirstArg: AArg*
+ OneArg (mn: Mnemon, pFArg: AArg*) + ~OneArg ( )
Valid# mnemonic: Mnemonic
ZeroArg
EExtraChars
ENoRight
EBad2nd
ENoComma
EBadNeg
EBad1st
ENoLeft
ENoFunct
ENoEnd
Error
+ isError (): bool
+ generateCode ()
ACode
Figure 7.40
![Page 86: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/86.jpg)
Figure 7.41
// ======== The code classesclass ACode {public: virtual bool isError () = 0; virtual void generateCode () = 0;};
// ======== The error code classesclass Error : public ACode {public: bool isError () { return true; }};
class ENoEnd : public Error {public: void generateCode () { cout << "Error: Missing \"end\" sentinel."; }};
![Page 87: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/87.jpg)
Figure 7.41(Continued)
class ENoFunct : public Error {public: void generateCode () { cout << "Error: Line must begin with function identifier."; }};
class ENoLeft : public Error {public: void generateCode () { cout << "Error: Left parenthesis expected after function."; }};
class EBad1st : public Error {public: void generateCode () { cout << "Error: First argument not an identifier."; }};
![Page 88: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/88.jpg)
Figure 7.41(Continued)
class EBadNeg : public Error {public: void generateCode () { cout << "Error: The argument of \"neg\" is malformed."; }};
class ENoComma : public Error {public: void generateCode () { cout << "Error: Comma expected after first argument."; }};
class EBad2nd : public Error {public: void generateCode () { cout << "Error: Second argument not an identifier or integer."; }};
![Page 89: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/89.jpg)
Figure 7.41(Continued)
class ENoRight : public Error {public: void generateCode () { cout << "Error: Right parenthesis expected after argument."; }};
class EExtraChars : public Error {public: void generateCode () { cout << "Error: Illegal trailing character."; }};
![Page 90: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/90.jpg)
Figure 7.41(Continued)
// ======== The valid code classesclass Valid : public ACode {protected: Mnemon mnemonic;public: bool isError () { return false; }};
class ZeroArg : public Valid {public: ZeroArg (Mnemon mn) { mnemonic = mn; } void generateCode () { } // cout nothing};
![Page 91: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/91.jpg)
Figure 7.41(Continued)
class OneArg : public Valid {private: AArg *pFirstArg;public: OneArg (Mnemon mn, AArg* pFArg) { mnemonic = mn; pFirstArg = pFArg; } ~OneArg () { delete pFirstArg; } void generateCode () { pFirstArg->putArg (); cout << " := -"; pFirstArg->putArg (); }};
![Page 92: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/92.jpg)
Figure 7.41(Continued)
class TwoArgs : public Valid {private: AArg *pFirstArg, *pSecondArg;public: TwoArgs (Mnemon mn, AArg* pFArg, AArg* pSArg) { mnemonic = mn; pFirstArg = pFArg; pSecondArg = pSArg; } ~TwoArgs () { delete pFirstArg; delete pSecondArg; }
![Page 93: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/93.jpg)
Figure 7.41(Continued)
void generateCode () { switch (mnemonic) { case eM_SET: pFirstArg->putArg (); cout << " := "; pSecondArg->putArg (); break; case eM_ADD: case eM_SUB: case eM_MUL: case eM_DIV: pFirstArg->putArg (); cout << " := "; pFirstArg->putArg (); cout << " " << operatorTable[mnemonic] << " "; pSecondArg->putArg (); break; } }};
![Page 94: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/94.jpg)
Figure 7.42
// ======== The lexical analyzerenum LexState { eLS_START, eLS_IDENT, eLS_SIGN, eLS_INTEGER, eLS_STOP};
void getToken (AToken*& pAT) { // Pre: pAT is allocated to an irrelevant value. char nextChar; string localIdentValue; int localIntValue; int sign; delete pAT; pAT = new TEmpty; LexState state = eLS_START; do { inBuffer.advanceInput (nextChar); switch (state) {
![Page 95: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/95.jpg)
Figure 7.42(Continued)
case eLS_START: if (isalpha (nextChar)) { localIdentValue = nextChar; state = eLS_IDENT; } else if (nextChar == '-') { sign = -1; state = eLS_SIGN; } else if (nextChar == '+') { sign = +1; state = eLS_SIGN; } else if (isdigit (nextChar)) { localIntValue = nextChar - '0'; sign = +1; state = eLS_INTEGER; } else if (nextChar == ',') { delete pAT; pAT = new TComma; state = eLS_STOP; }
![Page 96: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/96.jpg)
Figure 7.42(Continued)
else if (nextChar == '(') { delete pAT; pAT = new TLeftParen; state = eLS_STOP; } else if (nextChar == ')') { delete pAT; pAT = new TRightParen; state = eLS_STOP; } else if (nextChar == '\n') { // pAT is initialized to TEmpty. state = eLS_STOP; } else if (nextChar != ' ') { delete pAT; pAT = new TInvalid; } break;
![Page 97: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/97.jpg)
Figure 7.42(Continued)
case eLS_IDENT: if (isalpha (nextChar) || isdigit (nextChar)) { localIdentValue.push_back (nextChar); } else { inBuffer.backUpInput(); delete pAT; pAT = new TIdentifier (localIdentValue); state = eLS_STOP; } break; case eLS_SIGN: if (isdigit (nextChar)) { localIntValue = nextChar - '0'; state = eLS_INTEGER; } else { delete pAT; pAT = new TInvalid; } break;
![Page 98: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/98.jpg)
Figure 7.42(Continued)
case eLS_INTEGER: if (isdigit (nextChar)) { localIntValue = 10 * localIntValue + nextChar - '0'; } else { inBuffer.backUpInput(); delete pAT; pAT = new TInteger (sign * localIntValue); state = eLS_STOP; } break; } } while ((state != eLS_STOP) && (pAT->tokenType () != eT_INVALID));}
![Page 99: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/99.jpg)
ePS_START
ePS_UNARY
ePS_END
ePS_FUNCTION
ePS_OPEN
ePS_1ST_OPRND
ePS_COMMA
ePS_2ND_OPRND
eT_IDENTIFIER2
eT_LEFT_PAREN
eT_RIGHT_PAREN3
eT_IDENTIFIER1
ePS_FINISHeT_EMPTY
eT_RIGHT_PAREN
eT_EMPTY eT_EMPTY
eT_IDENTIFIER
eT_IDENTIFIER
eT_COMMA4
eT_INTEGER
Note 1: Only the identifier end.Note 2: Only the identifiers set, add, sub, mul, div, and neg.Note 3: Only for mnemonic eM_NEG.Note 4: Only for mnemonics eM_SET, eM_ADD, eM_SUB, eM_MUL, and eM_DIV.
eT_EMPTYePS_NON_UNARY
Figure 7.43
![Page 100: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/100.jpg)
Figure 7.44
// ======== The parserenum ParseState { ePS_START, ePS_END, ePS_FUNCTION, ePS_OPEN, ePS_1ST_OPRND, ePS_UNARY, ePS_COMMA, ePS_2ND_OPRND, ePS_NON_UNARY, ePS_FINISH};
void processSourceLine (ACode*& pAC, bool& term) { // Pre: term is false. // A source line is in inBuffer ready for processing. // pAC is allocated to an irrelevant value. AArg* pLocalFirstArg; AArg* pLocalSecondArg; Mnemon mnemon; delete pAC; pAC = new ZeroArg (eM_EMPTY); AToken* pAToken = new TEmpty; ParseState state = ePS_START; do { getToken (pAToken); switch (state) {
![Page 101: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/101.jpg)
Figure 7.44(Continued)
case ePS_START: if (pAToken->tokenType () == eT_IDENTIFIER) { string tempStr = pAToken->getIdentValue (); bool found; lookUpMnemon (tempStr, mnemon, found); if (found) { if (mnemon == eM_END) { delete pAC; pAC = new ZeroArg (eM_END); term = true; state = ePS_END; } else { state = ePS_FUNCTION; } } else { delete pAC; pAC = new ENoFunct; } }
![Page 102: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/102.jpg)
Figure 7.44(Continued)
else if (pAToken->tokenType () == eT_EMPTY) { // pAC initialized to ZeroArg (eM_EMPTY) state = ePS_FINISH; } else { delete pAC; pAC = new ENoFunct; } break;
![Page 103: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/103.jpg)
Figure 7.44(Continued)
case ePS_END: if (pAToken->tokenType () == eT_EMPTY) { state = ePS_FINISH; } else { delete pAC; pAC = new EExtraChars; } break; case ePS_FUNCTION: if (pAToken->tokenType () == eT_LEFT_PAREN) { state = ePS_OPEN; } else { delete pAC; pAC = new ENoLeft; } break;
![Page 104: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/104.jpg)
Figure 7.44(Continued)
case ePS_OPEN: if (pAToken->tokenType () == eT_IDENTIFIER) { pLocalFirstArg = pAToken->getArg (); state = ePS_1ST_OPRND; } else { delete pAC; pAC = new EBad1st; } break;
![Page 105: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/105.jpg)
Figure 7.44(Continued)
case ePS_1ST_OPRND: if (mnemon == eM_NEG) { if (pAToken->tokenType () == eT_RIGHT_PAREN) { delete pAC; pAC = new OneArg (mnemon, pLocalFirstArg); state = ePS_UNARY; } else { delete pAC; pAC = new EBadNeg; } } else if (pAToken->tokenType () == eT_COMMA) { state = ePS_COMMA; } else { delete pAC; pAC = new ENoComma; } break;
![Page 106: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/106.jpg)
Figure 7.44(Continued)
case ePS_UNARY: if (pAToken->tokenType () == eT_EMPTY) { state = ePS_FINISH; } else { delete pAC; pAC = new EExtraChars; } break; case ePS_COMMA: if ((pAToken->tokenType () == eT_IDENTIFIER) || (pAToken->tokenType () == eT_INTEGER)) { pLocalSecondArg = pAToken->getArg (); delete pAC; pAC = new TwoArgs (mnemon, pLocalFirstArg, pLocalSecondArg); state = ePS_2ND_OPRND; } else { delete pAC; pAC = new EBad2nd; } break;
![Page 107: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/107.jpg)
Figure 7.44(Continued)
case ePS_2ND_OPRND: if (pAToken->tokenType () == eT_RIGHT_PAREN) { state = ePS_NON_UNARY; } else { delete pAC; pAC = new ENoRight; } break; case ePS_NON_UNARY: if (pAToken->tokenType () == eT_EMPTY) { state = ePS_FINISH; } else { delete pAC; pAC = new EExtraChars; } break; } } while ((state != ePS_FINISH) && (!pAC->isError ())); delete pAToken;}
![Page 108: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/108.jpg)
Figure 7.45
// ======== The main program
int main () { bool terminateWithEnd = false; ACode* pACode = new ZeroArg (eM_EMPTY); initGlobalTables (); inBuffer.getLine (); while (! (cin.eof() || terminateWithEnd)) { processSourceLine (pACode, terminateWithEnd); pACode->generateCode (); cout << endl; inBuffer.getLine (); } if (!terminateWithEnd) { delete pACode; pACode = new ENoEnd; pACode->generateCode (); cout << endl; } delete pACode; return 0;}
![Page 109: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/109.jpg)
2
N = {E,T,F}T = {+,*,(,),a}P = the productions
1. E ! E + T
2. E ! T
3. T ! T * F
4. T ! F
5. F ! ( E )
6. F ! a
S = E
N = {A,B}T = {0,1}P = the productions
1. A ! 0B
2. B ! 10B
3. B ! !
S = A
Figure 7.46, 7.47
2
N = {E,T,F}T = {+,*,(,),a}P = the productions
1. E ! E + T
2. E ! T
3. T ! T * F
4. T ! F
5. F ! ( E )
6. F ! a
S = E
N = {A,B}T = {0,1}P = the productions
1. A ! 0B
2. B ! 10B
3. B ! !
S = A
N = {C}T = {0,1}P = the productions
1. C ! C10
2. C ! 0
S = C
![Page 110: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/110.jpg)
XW
Y
a
a
*(a)
ac b
Z
XW
Y
a
c
(b)
b
Z
XW
Y
a
a
(c)
a
Z
XW
Y
a
b
a
a c
(d)
b
cb
Z
c
Figure 7.48
XW
Y
a
a
*(a)
ac b
Z
XW
Y
a
c
(b)
b
Z
XW
Y
a
a
(c)
a
Z
XW
Y
a
b
a
a c
(d)
b
cb
Z
c
![Page 111: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/111.jpg)
aX
Z
Wb
cb
a
c
( )a
bX
Z
Wa
c
( )b
Yb
c
a
Y
Figure 7.49
![Page 112: Language Translation Principles - California State …cputnam/Comp516/Overheads/PDFChap07.pdfA → aABC 2. A → abC 3. CB → BC 4. bB → bb 5. bC → bc 6. cC → cc S = A 1 Figure](https://reader035.vdocuments.us/reader035/viewer/2022070610/5af2ce207f8b9a95468babaa/html5/thumbnails/112.jpg)
Figure 7.50
eLS_ADDR1 eLS_ADDR2letter
eLS_IDENT
eLS_START
eLS_DOT1
eLS_INT2
eLS_INT1
eLS_SIGN
eLS_HEX2
eLS_DOT2letter
letter
.
,
digit
digit
letterdigit
letterdigit
digit
space
letterdigit
hexdigit
hexdigit
space
0
1..9
xX
+–
eLS_HEX1