Book

390
Lexical Analysis and Parsing using C ++

Transcript of Book

Page 1: Book

Lex i ca l Ana ly s i s and Pars ing

using C++

Page 2: Book

ii

Page 3: Book

Lex i ca l Ana ly s i s and Pars ing

using C++

Bruno R. PreissB.A.Sc., M.A.Sc., Ph.D., P.Eng.

MMIV

Page 4: Book

Copyright c© 2004 by Bruno R. Preiss.

All rights reserved. No part of this publication may be reproduced, stored in a re-

trieval system, or transmitted, in any form or by any means, electronic, mechanical,

photocopying, recording, or otherwise, without the prior written permission of the

author.

This book was prepared with LATEX and reproduced from camera-ready copy sup-

plied by the author. The book is typeset using the Computer Modern fonts designed

by Donald E. Knuth with various additional glyphs designed by the author and im-

plemented using METAFONT.

METAFONT is a trademark of Addison Wesley Publishing Company.

TEX is a trademark of the American Mathematical Society.

UNIX is a registered trademark of AT&T Bell Laboratories.

Page 5: Book

To Someone

Page 6: Book

vi

Page 7: Book

Contents

Preface xiii

I Lexis—The Program 1

1 Lexis Input Specifications 3

1.1 Lexis Token Specification . . . . . . . . . . . . . . . . . . . . . . . . 3

1.1.1 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2 Lexis Grammar Specification . . . . . . . . . . . . . . . . . . . . . . 4

1.2.1 ParserData Class . . . . . . . . . . . . . . . . . . . . . . . . 8

1.2.2 ItemAttributes Class . . . . . . . . . . . . . . . . . . . . . . 9

1.2.3 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.2.4 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2 State Class 17

2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

3 NFAState Class 21

3.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

4 NFA Class 27

4.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

4.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

5 DFAState Class 37

5.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

5.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

5.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

6 DFA State 41

6.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

6.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

6.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

Page 8: Book

viii Contents

7 CompressedDFA Class 51

7.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

7.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

7.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

8 Lexical Analyzer Classes 63

8.1 Common Types and Constants . . . . . . . . . . . . . . . . . . . . . 63

8.1.1 Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . 63

8.1.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

8.1.3 Anchor Enumeration . . . . . . . . . . . . . . . . . . . . . . . 64

8.1.4 Action Enumeration . . . . . . . . . . . . . . . . . . . . . . . 64

8.2 Input Class Template . . . . . . . . . . . . . . . . . . . . . . . . . . 65

8.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

8.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

8.3 Token Class Template . . . . . . . . . . . . . . . . . . . . . . . . . . 71

8.3.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

8.3.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

8.4 TokenStream Class Template . . . . . . . . . . . . . . . . . . . . . . 74

8.4.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

8.4.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

8.5 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

9 Lexis Main Program 81

9.1 main Method Definition . . . . . . . . . . . . . . . . . . . . . . . . . 82

9.2 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

II Gramatika—The Program 85

10 Gramatika Input Specifications 87

10.1 Gramatika Token Specifications . . . . . . . . . . . . . . . . . . . . . 87

10.1.1 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

10.2 Gramatika Grammar Specification . . . . . . . . . . . . . . . . . . . 88

10.2.1 ParserData Class . . . . . . . . . . . . . . . . . . . . . . . . 92

10.2.2 ItemAttributes Class . . . . . . . . . . . . . . . . . . . . . . 93

10.2.3 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

10.2.4 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

11 Symbol Class 99

11.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

11.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

11.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

12 Production Class 105

12.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

12.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

12.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Page 9: Book

Contents ix

13 SymbolTable Class 113

13.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

13.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

13.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

14 Item Class 125

14.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

14.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

14.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

15 ItemSet Class and its Iterator 133

15.1 ItemSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

15.1.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

15.1.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

15.2 ItemSet Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

15.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

15.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

15.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

16 Partition Class and its Iterator 143

16.1 Partition Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

16.1.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

16.1.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

16.2 Partition Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

16.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

16.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

16.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

17 ActionRecord Class 149

17.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

17.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

17.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

18 GotoRecord Class 153

18.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

18.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

18.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

19 State class 157

19.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

19.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

19.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

20 Automaton Class 165

20.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

20.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

20.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Page 10: Book

x Contents

21 ParseTable Class 171

21.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

21.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

21.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

22 Parser Classes 181

22.1 Common Types and Constants . . . . . . . . . . . . . . . . . . . . . 181

22.1.1 Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . 181

22.1.2 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

22.1.3 TableEntry Struct . . . . . . . . . . . . . . . . . . . . . . . . 182

22.1.4 MapEntry Struct . . . . . . . . . . . . . . . . . . . . . . . . . 182

22.1.5 SymbolType Enumeration . . . . . . . . . . . . . . . . . . . . 183

22.1.6 Action Enumeration . . . . . . . . . . . . . . . . . . . . . . . 183

22.2 Value Class Template . . . . . . . . . . . . . . . . . . . . . . . . . . 184

22.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

22.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

22.3 StackItem Class Template . . . . . . . . . . . . . . . . . . . . . . . 186

22.3.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

22.3.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

22.4 Parser Class Template . . . . . . . . . . . . . . . . . . . . . . . . . . 188

22.4.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

22.4.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

22.5 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

23 Gramatika Main Program 195

23.1 main Method Definition . . . . . . . . . . . . . . . . . . . . . . . . . 195

23.2 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

III The Toolbox 199

24 Character Utilities 201

24.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

24.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

24.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

25 String Utilities 205

25.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

25.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

25.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

26 EscapeSequence Utilities 209

26.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

26.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

26.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

27 PathName Class 215

27.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

27.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

27.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

Page 11: Book

Contents xi

28 Stack Class Template 219

28.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

28.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

28.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

29 Set Class and its Iterator 223

29.1 Set Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

29.1.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

29.1.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

29.2 Set Iterator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

29.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

29.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

29.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

30 CharacterSet Class 237

30.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

30.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

30.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

31 List Utilities 241

31.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

31.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

31.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

32 HashTable Class and its Iterator 245

32.1 HashTable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

32.1.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

32.1.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

32.2 HashTable Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

32.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

32.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

32.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

IV Loma—The Program 255

33 Loma Input Specifications 257

33.1 Loma Token Specifications . . . . . . . . . . . . . . . . . . . . . . . . 257

33.1.1 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

33.2 Loma Grammar Specification . . . . . . . . . . . . . . . . . . . . . . . 258

33.2.1 ParserData Class . . . . . . . . . . . . . . . . . . . . . . . . 264

33.2.2 ItemAttributes Class . . . . . . . . . . . . . . . . . . . . . . 264

33.2.3 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

33.2.4 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

34 Fragment Class and its Descendants 273

34.1 Fragment Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

34.1.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

34.1.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

34.2 TextFragment Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

Page 12: Book

xii Contents

34.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

34.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

34.3 CodeFragment Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

34.3.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

34.3.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

34.4 IncludeFragment Class . . . . . . . . . . . . . . . . . . . . . . . . . 278

34.4.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

34.4.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

34.5 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

35 Module Class and its Descendants 283

35.1 Module Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

35.1.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

35.1.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

35.2 TextModule Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

35.2.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

35.2.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

35.3 FileModule Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

35.3.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

35.3.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

35.4 CodeModule Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

35.4.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

35.4.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

35.5 ExampleModule Class . . . . . . . . . . . . . . . . . . . . . . . . . . 292

35.5.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

35.5.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

35.6 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

36 Group Class 295

36.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

36.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

36.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

37 File Class 301

37.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

37.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

37.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

38 Loma Output Routines 307

38.1 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

38.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

38.3 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

39 Loma Main Program 311

39.1 File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

List of Modules 315

List of Symbols 349

List of Files 373

Page 13: Book

Preface

The preface goes here.

Bruno R. Preiss

Waterloo, Canada

March 7, 2004

Page 14: Book

xiv Preface

Page 15: Book

Part I

Lexis—The Program

Page 16: Book
Page 17: Book

Chapter 1

Lexis Input Specifications

�1.1 Lexis Token Specification

〈Lexis auxiliary definitions〉1 ≡1 blank = ’ ’.

2 tab = ’\t’.

3 newline = ’\n’.

4 whitespace = blank | tab | newline.

5 letter = /A-Za-z/.

6 octaldigit = /0-7/.

7 hexdigit = /0-9A-Fa-f/.

8 singleton = /ntvbrfa\\?’"\//.

9 octalcode = octaldigit [ octaldigit [ octaldigit ] ].

10 hexcode = ’x’ hexdigit { hexdigit }.

11 escapesequence = ’\\’ ( singleton | octalcode | hexcode ).

This code is used in ¶3.

¶1

Page 18: Book

4 Chapter 1. Lexis Input Specifications

〈Lexis token definitions〉2 ≡1 circumflex = ’^’.

2 colon = ’:’.

3 dollar = ’$’.

4 equal = ’=’.

5 leftBrace = ’{’.

6 leftBracket = ’[’.

7 leftParenthesis = ’(’.

8 period = ’.’.

9 questionMark = ’?’.

10 rightBrace = ’}’.

11 rightBracket = ’]’.

12 rightParenthesis = ’)’.

13 verticalBar = ’|’.

14 stringT = ’\’’ { /!’\\/ | escapesequence } ’\’’.

15 characterClass = ’/’ { /!\/\\/ | escapesequence } ’/’.

16 definitions = ’Definitions’.

17 tokens = ’Tokens’.

18 language = ’Language’.

19 identifier = letter { letter }.

20 = whitespace { whitespace }.

21 = ’#’ { ? } $.

This code is used in ¶3.

¶2

1.1.1 File

〈File: Lexis/Lexis.lex〉3 ≡1 Language:

2 LexisTokens.

3 Definitions:

4 〈Lexis auxiliary definitions〉15 Tokens:

6 〈Lexis token definitions〉2

¶3

�1.2 Lexis Grammar Specification

〈Lexis non-terminal definitions〉4 ≡1 alternation (nfa), alternative (nfa), auxiliaryDefinition,

2 auxiliaryDefinitionList, auxiliaryDefinitionsPart,

3 expression (nfa), file (nfa), item (nfa),

4 languageDefinitionPart (language), tokenDefinition (nfa),

5 tokenDefinitionList (nfa), tokenDefinitionsPart (nfa).

This code is used in ¶54.

¶4

Page 19: Book

1.2. Lexis Grammar Specification 5

〈Lexis start symbol definition〉5 ≡1 file.

This code is used in ¶54.

¶5

〈Lexis grammar productions〉6 ≡1 file =

2 languageDefinitionPart auxiliaryDefinitionsPart tokenDefinitionsPart

3 ‘‘

4 〈file actions〉295 ’’.

This code is used in ¶54.

¶6

〈Lexis grammar productions〉7 ≡1 languageDefinitionPart =

2 ‘‘

3 〈languageDefinitionPart case 1 actions〉304 ’’

5 | language colon identifier period

6 ‘‘

7 〈languageDefinitionPart case 2 actions〉318 ’’

9 .

This code is used in ¶54.

¶7

〈Lexis grammar productions〉8 ≡1 auxiliaryDefinitionsPart =

2 | definitions colon auxiliaryDefinitionList.

This code is used in ¶54.

¶8

〈Lexis grammar productions〉9 ≡1 auxiliaryDefinitionList =

2 | auxiliaryDefinitionList auxiliaryDefinition.

This code is used in ¶54.

¶9

〈Lexis grammar productions〉10 ≡1 auxiliaryDefinition =

2 identifier equal expression period

3 ‘‘

4 〈auxiliaryDefinition actions〉325 ’’.

This code is used in ¶54.

¶10

Page 20: Book

6 Chapter 1. Lexis Input Specifications

〈Lexis grammar productions〉11 ≡1 tokenDefinitionsPart =

2 ‘‘

3 〈tokenDefinitionsPart case 1 actions〉334 ’’

5 | tokens colon tokenDefinitionList

6 ‘‘

7 〈tokenDefinitionsPart case 2 actions〉348 ’’.

This code is used in ¶54.

¶11

〈Lexis grammar productions〉12 ≡1 tokenDefinitionList =

2 ‘‘

3 〈tokenDefinitionList case 1 actions〉354 ’’

5 | tokenDefinitionList tokenDefinition

6 ‘‘

7 〈tokenDefinitionList case 2 actions〉368 ’’.

This code is used in ¶54.

¶12

〈Lexis grammar productions〉13 ≡1 tokenDefinition =

2 equal expression period

3 ‘‘

4 〈tokenDefinition case 1 actions〉375 ’’

6 | identifier equal expression period

7 ‘‘

8 〈tokenDefinition case 2 actions〉389 ’’.

This code is used in ¶54.

¶13

Page 21: Book

1.2. Lexis Grammar Specification 7

〈Lexis grammar productions〉14 ≡1 expression =

2 alternation

3 ‘‘

4 〈expression case 1 actions〉395 ’’

6 | circumflex alternation

7 ‘‘

8 〈expression case 2 actions〉409 ’’

10 | alternation dollar

11 ‘‘

12 〈expression case 3 actions〉4113 ’’

14 | circumflex alternation dollar

15 ‘‘

16 〈expression case 4 actions〉4217 ’’.

This code is used in ¶54.

¶14

〈Lexis grammar productions〉15 ≡1 alternation =

2 alternative

3 ‘‘

4 〈alternation case 1 actions〉435 ’’

6 | alternation verticalBar alternative

7 ‘‘

8 〈alternation case 2 actions〉449 ’’.

This code is used in ¶54.

¶15

〈Lexis grammar productions〉16 ≡1 alternative =

2 ‘‘

3 〈alternative case 1 actions〉454 ’’

5 | alternative item

6 ‘‘

7 〈alternative case 2 actions〉468 ’’.

This code is used in ¶54.

¶16

Page 22: Book

8 Chapter 1. Lexis Input Specifications

〈Lexis grammar productions〉17 ≡1 item =

2 questionMark

3 ‘‘

4 〈item case 1 actions〉475 ’’

6 | identifier

7 ‘‘

8 〈item case 2 actions〉489 ’’

10 | stringT

11 ‘‘

12 〈item case 3 actions〉4913 ’’

14 | characterClass

15 ‘‘

16 〈item case 4 actions〉5017 ’’

18 | leftParenthesis expression rightParenthesis

19 ‘‘

20 〈item case 5 actions〉5121 ’’

22 | leftBracket expression rightBracket

23 ‘‘

24 〈item case 6 actions〉5225 ’’

26 | leftBrace expression rightBrace

27 ‘‘

28 〈item case 7 actions〉5329 ’’.

This code is used in ¶54.

¶17

1.2.1 ParserData Class

〈ParserData class declaration〉18 ≡1 class ParserData

2 {

3 〈ParserData field declarations〉194 〈ParserData method declarations〉205 };

This code is used in ¶22.

¶18

〈ParserData field declarations〉19 ≡1 protected: HashTable<string,NFA*> definitionsTable;

This code is used in ¶18.

¶19

Page 23: Book

1.2. Lexis Grammar Specification 9

〈ParserData method declarations〉20 ≡1 protected: ParserData();

This code is used in ¶18.

¶20

〈ParserData class inline method definitions〉21 ≡1 inline ParserData::ParserData() :

2 definitionsTable()

3 {}

This code is used in ¶22.

¶21

〈File: Lexis/ParserData.h〉22 ≡1 #if !defined(_Lexis_ParserData_h)

2 #define _Lexis_ParserData_h

3

4 #include <string>

5 #include "Lexis/NFA.h"

6 #include "Toolbox/HashTable.h"

7 #include "Toolbox/String.h"

8

9 using namespace Toolbox;

10 using namespace Toolbox::String;

11

12 namespace Lexis

13 {

14 〈ParserData class declaration〉1815 〈ParserData class inline method definitions〉2116 }

17

18 #endif

¶22

1.2.2 ItemAttributes Class

〈ItemAttributes class declaration〉23 ≡1 class ItemAttributes

2 {

3 〈ItemAttributes field declarations〉244 〈ItemAttributes method declarations〉255 };

This code is used in ¶27.

¶23

〈ItemAttributes field declarations〉24 ≡1 public: NFA* nfa;

2 public: string language;

This code is used in ¶23.

¶24

Page 24: Book

10 Chapter 1. Lexis Input Specifications

〈ItemAttributes method declarations〉25 ≡1 public: ItemAttributes();

This code is used in ¶23.

¶25

〈ItemAttributes class inline method definitions〉26 ≡1 inline ItemAttributes::ItemAttributes() :

2 nfa(0),

3 language()

4 {}

This code is used in ¶27.

¶26

〈File: Lexis/ItemAttributes.h〉27 ≡1 #if !defined(_Lexis_ItemAttributes_h)

2 #define _Lexis_ItemAttributes_h

3

4 #include <string>

5 #include "Lexis/NFA.h"

6

7 using namespace Toolbox;

8 using namespace Toolbox::String;

9

10 namespace Lexis

11 {

12 〈ItemAttributes class declaration〉2313 〈ItemAttributes class inline method definitions〉2614 }

15

16 #endif

¶27

1.2.3 Actions

〈Lexis actions preamble〉28 ≡1 #include <stdint.h>

2 #include "Lexis/ParserData.h"

3 #include "Lexis/ItemAttributes.h"

4 #include "Toolbox/CharacterSet.h"

5 #include "Toolbox/EscapeSequence.h"

6

7 using Lexis::NFA;

This code is used in ¶54.

¶28

〈file actions〉29 ≡1 NFA* nfa = $tokenDefinitionsPart;

2 nfa->setLanguage($languageDefinitionPart);

3 $$ = nfa;

This code is used in ¶6.

¶29

Page 25: Book

1.2. Lexis Grammar Specification 11

〈languageDefinitionPart case 1 actions〉30 ≡1 $$ = "UserDefined";

This code is used in ¶7.

¶30

〈languageDefinitionPart case 2 actions〉31 ≡1 $$ = $identifier.getLexeme();

This code is used in ¶7.

¶31

〈auxiliaryDefinition actions〉32 ≡1 HashTable<string,NFA*>::const_iterator ptr =

2 definitionsTable.find($identifier.getLexeme()), null;

3 if (ptr != null)

4 cerr << "Error: " << $identifier.getLexeme() <<

5 " multiply defined.\n";

6 definitionsTable.add(

7 $identifier.getLexeme(), $expression);

This code is used in ¶10.

¶32

〈tokenDefinitionsPart case 1 actions〉33 ≡1 $$ = 0;

This code is used in ¶11.

¶33

〈tokenDefinitionsPart case 2 actions〉34 ≡1 $$ = $tokenDefinitionList;

This code is used in ¶11.

¶34

〈tokenDefinitionList case 1 actions〉35 ≡1 $$ = 0;

This code is used in ¶12.

¶35

〈tokenDefinitionList case 2 actions〉36 ≡1 NFA* result = 0;

2 if ($tokenDefinitionList == 0)

3 {

4 result = $tokenDefinition;

5 }

6 else

7 {

8 assert($tokenDefinition != 0);

9 result = new NFA(

10 *$tokenDefinitionList | *$tokenDefinition);

11 delete $tokenDefinitionList;

12 delete $tokenDefinition;

13 }

14 $$ = result;

This code is used in ¶12.

¶36

Page 26: Book

12 Chapter 1. Lexis Input Specifications

〈tokenDefinition case 1 actions〉37 ≡1 $expression->setAction(Lexis::discard);

2 $$ = $expression;

This code is used in ¶13.

¶37

〈tokenDefinition case 2 actions〉38 ≡1 $expression->setAction(Lexis::accept);

2 $expression->setToken($identifier.getLexeme());

3 $$ = $expression;

This code is used in ¶13.

¶38

〈expression case 1 actions〉39 ≡1 $$ = $alternation;

This code is used in ¶14.

¶39

〈expression case 2 actions〉40 ≡1 NFA nl("\n");

2 NFA* result = new NFA(nl * *$alternation);

3 result->setAnchor(Lexis::head);

4 delete $alternation;

5 $$ = result;

This code is used in ¶14.

¶40

〈expression case 3 actions〉41 ≡1 NFA nl("\n");

2 NFA* result = new NFA(*$alternation * nl);

3 result->setAnchor(Lexis::tail);

4 delete $alternation;

5 $$ = result;

This code is used in ¶14.

¶41

〈expression case 4 actions〉42 ≡1 NFA nl("\n");

2 NFA* result = new NFA(nl * *$alternation * nl);

3 result->setAnchor(Lexis::both);

4 delete $alternation;

5 $$ = result;

This code is used in ¶14.

¶42

〈alternation case 1 actions〉43 ≡1 $$ = $alternative;

This code is used in ¶15.

¶43

Page 27: Book

1.2. Lexis Grammar Specification 13

〈alternation case 2 actions〉44 ≡1 NFA* result = new NFA(*$alternation | *$alternative);

2 delete $alternation;

3 delete $alternative;

4 $$ = result;

This code is used in ¶15.

¶44

〈alternative case 1 actions〉45 ≡1 $$ = new NFA();

This code is used in ¶16.

¶45

〈alternative case 2 actions〉46 ≡1 NFA* result = new NFA(*$alternative * *$item);

2 delete $alternative;

3 delete $item;

4 $$ = result;

This code is used in ¶16.

¶46

〈item case 1 actions〉47 ≡1 CharacterSet set;

2 set += ’\n’;

3 set = ~set;

4 $$ = new NFA(set);

This code is used in ¶17.

¶47

〈item case 2 actions〉48 ≡1 HashTable<string,NFA*>::const_iterator ptr =

2 definitionsTable.find($identifier.getLexeme()), null;

3 NFA* result = 0;

4 if (ptr == null)

5 {

6 cerr << "Error: " << $identifier.getLexeme() <<

7 " not defined.\n";

8 result = new NFA();

9 }

10 else

11 result = new NFA(**ptr);

12 $$ = result;

This code is used in ¶17.

¶48

〈item case 3 actions〉49 ≡1 string src($stringT.getLexeme());

2

3 $$ = new NFA(EscapeSequence::decode(

4 src.substr(1, src.size()-2)));

This code is used in ¶17.

¶49

Page 28: Book

14 Chapter 1. Lexis Input Specifications

〈item case 4 actions〉50 ≡1 string src($characterClass.getLexeme());

2 bool complement = false;

3

4 assert(src.size() >= 2);

5 if (src[1U] == ’!’)

6 {

7 complement = true;

8 src = EscapeSequence::decode(src.substr(2, src.size()-3));

9 }

10 else

11 src = EscapeSequence::decode(src.substr(1, src.size()-2));

12

13 CharacterSet dst;

14 int16_t lastc = -1;

15 string::iterator p(src.begin()), lim(src.end());

16 while (p != lim)

17 {

18 char c = *p++;

19 if (c == ’-’ && lastc >= 0)

20 {

21 if (p != lim)

22 {

23 c = *p++;

24 for (uint8_t i = lastc + 1U;

25 i <= static_cast<uint8_t>(c); i++)

26 dst += i;

27 lastc = -1;

28 }

29 else

30 {

31 dst += ’-’;

32 lastc = ’-’;

33 }

34 }

35 else

36 {

37 dst += c;

38 lastc = static_cast<uint8_t>(c);

39 }

40 }

41 if (complement)

42 dst = ~dst;

43 $$ = new NFA(dst);

This code is used in ¶17.

¶50

〈item case 5 actions〉51 ≡1 $$ = $expression;

This code is used in ¶17.

¶51

Page 29: Book

1.2. Lexis Grammar Specification 15

〈item case 6 actions〉52 ≡1 NFA* result = new NFA(!*$expression);

2 delete $expression;

3 $$ = result;

This code is used in ¶17.

¶52

〈item case 7 actions〉53 ≡1 NFA* result = new NFA(~*$expression);

2 delete $expression;

3 $$ = result;

This code is used in ¶17.

¶53

1.2.4 File

〈File: Lexis/Lexis.gtk〉54 ≡1 Language:

2 LexisGrammar.

3 Preamble:

4 ‘‘

5 〈Lexis actions preamble〉28

6 ’’.

7 Terminals:

8 "lexistab.h".

9 Nonterminals:

10 〈Lexis non-terminal definitions〉411 Start:

12 〈Lexis start symbol definition〉513 Productions:

14 〈Lexis grammar productions〉6–17

¶54

Page 30: Book

16 Chapter 1. Lexis Input Specifications

Page 31: Book

Chapter 2

State Class

�2.1 Declarations

〈State class declaration〉55 ≡1 class State

2 {

3 〈State class field declarations〉564 〈State class method declarations〉575 };

This code is used in ¶71.

¶55

〈State class field declarations〉56 ≡1 protected: StateNumber number;

2 protected: Anchor anchor;

3 protected: Action action;

4 protected: string token;

This code is used in ¶55.

¶56

〈State class method declarations〉57 ≡1 public: State();

2 public: State(StateNumber, Anchor, Action, string const&);

3 public: State& operator=(State const&);

4 public: void setNumber(StateNumber);

5 public: void setAnchor(Anchor);

6 public: void setAction(Action);

7 public: void setToken(string const&);

8 public: StateNumber getNumber() const;

9 public: Anchor getAnchor() const;

10 public: Action getAction() const;

11 public: string const& getToken() const;

12 public: ostream& put(ostream&) const;

This code is used in ¶55.

¶57

Page 32: Book

18 Chapter 2. State Class

�2.2 Definitions

〈State class inline method definitions〉58 ≡1 inline State::State() :

2 number(0),

3 anchor(none),

4 action(advance),

5 token()

6 {}

This code is used in ¶71.

¶58

〈State class inline method definitions〉59 ≡1 inline State::State(StateNumber n, Anchor ancr,

2 Action actn, string const& s) :

3 number(n),

4 anchor(ancr),

5 action(actn),

6 token(s)

7 {}

This code is used in ¶71.

¶59

〈State class inline method definitions〉60 ≡1 inline StateNumber State::getNumber() const

2 { return number; }

This code is used in ¶71.

¶60

〈State class inline method definitions〉61 ≡1 inline Anchor State::getAnchor() const

2 { return anchor; }

This code is used in ¶71.

¶61

〈State class inline method definitions〉62 ≡1 inline Action State::getAction() const

2 { return action; }

This code is used in ¶71.

¶62

〈State class inline method definitions〉63 ≡1 inline string const& State::getToken() const

2 { return token; }

This code is used in ¶71.

¶63

〈State class inline method definitions〉64 ≡1 inline ostream& operator<<(ostream& stream, State const& state)

2 { return state.put(stream); }

This code is used in ¶71.

¶64

Page 33: Book

2.2. Definitions 19

〈State method definitions〉65 ≡1 State& State::operator=(State const& s)

2 {

3 anchor = s.anchor;

4 action = s.action;

5 token = s.token;

6 return *this;

7 }

This code is used in ¶72.

¶65

〈State method definitions〉66 ≡1 void State::setNumber(StateNumber n)

2 { number = n; }

This code is used in ¶72.

¶66

〈State method definitions〉67 ≡1 void State::setAnchor(Anchor a)

2 { anchor = a; }

This code is used in ¶72.

¶67

〈State method definitions〉68 ≡1 void State::setAction(Action a)

2 { action = a; }

This code is used in ¶72.

¶68

〈State method definitions〉69 ≡1 void State::setToken(string const& s)

2 { token = s; }

This code is used in ¶72.

¶69

〈State method definitions〉70 ≡1 ostream& State::put(ostream& s) const

2 {

3 s << "number = " << number;

4 s << ", anchor = " << anchor;

5 s << ", action = " << action;

6 return s;

7 }

This code is used in ¶72.

¶70

Page 34: Book

20 Chapter 2. State Class

�2.3 Files

〈File: Lexis/State.h〉71 ≡1 #if !defined(_Lexis_State_h)

2 #define _Lexis_State_h

3

4 #include <iostream>

5 #include <string>

6 #include <stdint.h>

7 #include "Lexis/Lexis.h"

8

9 namespace Lexis

10 {

11 〈State class declaration〉5512 〈State class inline method definitions〉58–6413 }

14

15 #endif

¶71

〈File: Lexis/State.cc〉72 ≡1 #include <iostream>

2 #include "Lexis/State.h"

3

4 namespace Lexis

5 {

6 〈State method definitions〉65–707 }

¶72

Page 35: Book

Chapter 3

NFAState Class

�3.1 Declarations

〈NFAState class declaration〉73 ≡1 class NFAState : public State

2 {

3 〈LabelType declaration〉744 〈NFAState constants〉75

5 〈NFAState field declarations〉766 〈NFAState method declarations〉777 };

This code is used in ¶90.

¶73

〈LabelType declaration〉74 ≡1 public: enum LabelType

2 {

3 epsilon,

4 character,

5 characterSet

6 };

This code is used in ¶73.

¶74

〈NFAState constants〉75 ≡1 private: enum Constants

2 {

3 maximumFanout = 2

4 };

This code is used in ¶73.

¶75

Page 36: Book

22 Chapter 3. NFAState Class

〈NFAState field declarations〉76 ≡1 private: uint8_t fanout;

2 private: int16_t next[maximumFanout];

3 private: LabelType labelType;

4 private: char labelChar;

5 private: CharacterSet labelSet;

This code is used in ¶73.

¶76

〈NFAState method declarations〉77 ≡1 public: NFAState();

2 public: NFAState& operator=(NFAState const&);

3 public: void connect(StateNumber);

4 public: void connect(StateNumber, StateNumber);

5 public: void connect(char, StateNumber);

6 public: void connect(CharacterSet const&, StateNumber);

7 public: uint8_t getFanout() const;

8 public: LabelType getLabelType() const;

9 public: bool isTransitionLabelled(char) const;

10 public: StateNumber getNextState(uint8_t) const;

11 public: ostream& put(ostream&) const;

This code is used in ¶73.

¶77

�3.2 Definitions

〈NFAState class inline method definitions〉78 ≡1 inline NFAState::NFAState() :

2 State(),

3 fanout(0),

4 labelType(epsilon),

5 labelChar(0),

6 labelSet()

7 {}

This code is used in ¶90.

¶78

〈NFAState class inline method definitions〉79 ≡1 inline uint8_t NFAState::getFanout() const

2 { return fanout; }

This code is used in ¶90.

¶79

〈NFAState class inline method definitions〉80 ≡1 inline NFAState::LabelType NFAState::getLabelType() const

2 { return labelType; }

This code is used in ¶90.

¶80

Page 37: Book

3.2. Definitions 23

〈NFAState class inline method definitions〉81 ≡1 inline ostream& operator<<(ostream& stream, NFAState const& state)

2 { return state.put(stream); }

This code is used in ¶90.

¶81

〈NFAState method definitions〉82 ≡1 NFAState& NFAState::operator=(NFAState const& s)

2 {

3 State::operator=(s);

4 fanout = s.fanout;

5 for (uint8_t i = 0; i < fanout; ++i)

6 next[i] = s.next[i];

7 labelType = s.labelType;

8 labelChar = s.labelChar;

9 labelSet = s.labelSet;

10 return *this;

11 }

This code is used in ¶91.

¶82

〈NFAState method definitions〉83 ≡1 void NFAState::connect(StateNumber nxt)

2 {

3 fanout = 1;

4 next[0] = nxt - number;

5 labelType = epsilon;

6 labelChar = 0;

7 labelSet = CharacterSet(0);

8 }

This code is used in ¶91.

¶83

〈NFAState method definitions〉84 ≡1 void NFAState::connect(StateNumber nxt0, StateNumber nxt1)

2 {

3 fanout = 2;

4 next[0] = nxt0 - number;

5 next[1] = nxt1 - number;

6 labelType = epsilon;

7 labelChar = 0;

8 labelSet = CharacterSet(0);

9 }

This code is used in ¶91.

¶84

Page 38: Book

24 Chapter 3. NFAState Class

〈NFAState method definitions〉85 ≡1 void NFAState::connect(char lab, StateNumber nxt)

2 {

3 assert(lab != 0);

4 fanout = 1;

5 next[0] = nxt - number;

6 labelType = character;

7 labelChar = lab;

8 labelSet = CharacterSet(0);

9 }

This code is used in ¶91.

¶85

〈NFAState method definitions〉86 ≡1 void NFAState::connect(CharacterSet const& set, StateNumber nxt)

2 {

3 fanout = 1;

4 next[0] = nxt - number;

5 labelType = characterSet;

6 labelChar = 0;

7 labelSet = set;

8 }

This code is used in ¶91.

¶86

〈NFAState method definitions〉87 ≡1 bool NFAState::isTransitionLabelled(char c) const

2 {

3 switch (labelType)

4 {

5 case epsilon: return false;

6 case character: return labelChar == c;

7 case characterSet: return labelSet.contains(c);

8 }

9 assert(false);

10 return false;

11 }

This code is used in ¶91.

¶87

〈NFAState method definitions〉88 ≡1 StateNumber NFAState::getNextState(uint8_t i) const

2 {

3 assert(i < fanout);

4 return number + next[i];

5 }

This code is used in ¶91.

¶88

Page 39: Book

3.2. Definitions 25

〈NFAState method definitions〉89 ≡1 ostream& NFAState::put(ostream& s) const

2 {

3 s << "NFAState {";

4 State::put(s);

5 s << "\n";

6 if (fanout > 0)

7 {

8 s << " goto ";

9 uint8_t i = 0;

10 for (;;)

11 {

12 s << (number + next[i]);

13 i += 1;

14 if (i == fanout)

15 break;

16 s << ", ";

17 }

18 s << " on ";

19 switch (labelType)

20 {

21 case epsilon:

22 s << "epsilon"; break;

23 case character:

24 s << "’" << EscapeSequence::encode(

25 labelChar, ’\’’) << "’";

26 break;

27 case characterSet:

28 s << labelSet; break;

29 }

30 s << "\n";

31 }

32 return s << "}\n";

33 }

This code is used in ¶91.

¶89

Page 40: Book

26 Chapter 3. NFAState Class

�3.3 Files

〈File: Lexis/NFAState.h〉90 ≡1 #if !defined(_Lexis_NFAState_h)

2 #define _Lexis_NFAState_h

3

4 #include <iostream>

5 #include <vector>

6 #include <string>

7 #include "Lexis/State.h"

8 #include "Toolbox/CharacterSet.h"

9

10 using namespace Toolbox;

11

12 namespace Lexis

13 {

14 〈NFAState class declaration〉7315 〈NFAState class inline method definitions〉78–8116 }

17

18 #endif

¶90

〈File: Lexis/NFAState.cc〉91 ≡1 #include <iostream>

2 #include "Lexis/NFAState.h"

3 #include "Toolbox/EscapeSequence.h"

4 #include "Toolbox/Stack.h"

5

6 using namespace Toolbox;

7

8 namespace Lexis

9 {

10 〈NFAState method definitions〉82–8911 }

¶91

Page 41: Book

Chapter 4

NFA Class

�4.1 Declarations

〈NFA class declaration〉92 ≡1 class NFA

2 {

3 〈NFA class field declarations〉934 〈NFA class method declarations〉945 〈NFA class friends〉95

6 };

This code is used in ¶120.

¶92

〈NFA class field declarations〉93 ≡1 public: string language;

2 public: StateNumber const numberOfStates;

3 public: vector<NFAState> state;

This code is used in ¶92.

¶93

Page 42: Book

28 Chapter 4. NFA Class

〈NFA class method declarations〉94 ≡1 public: explicit NFA(StateNumber = 1);

2 public: explicit NFA(string const&);

3 public: explicit NFA(CharacterSet const&);

4 public: NFA(NFA const&);

5 public: ~NFA();

6 public: NFAState const& operator[](StateNumber) const;

7 public: NFAState& operator[](StateNumber);

8 public: StateNumber getNumberOfStates() const;

9 public: void setAnchor(Anchor);

10 public: void setAction(Action);

11 public: void setToken(string const&);

12 public: void setLanguage(string const&);

13 public: ostream& put(ostream&) const;

14 public: Set getEpsilonClosure(Set const&) const;

15 public: Set getMoveSet(Set const&, char) const;

16 public: Action getDominantAction(Set const&) const;

17 public: Anchor getDominantAnchor(Set const&) const;

18 public: string getDominantToken(Set const&) const;

19 public: string const& getLanguage() const;

This code is used in ¶92.

¶94

〈NFA class friends〉95 ≡1 friend NFA operator*(NFA const&, NFA const&);

2 friend NFA operator|(NFA const&, NFA const&);

3 friend NFA operator!(NFA const&);

4 friend NFA operator~(NFA const&);

This code is used in ¶92.

¶95

�4.2 Definitions

〈NFA class inline method definitions〉96 ≡1 inline void NFA::setLanguage(string const& s)

2 { language = s; }

This code is used in ¶120.

¶96

〈NFA class inline method definitions〉97 ≡1 inline string const& NFA::getLanguage() const

2 { return language; }

This code is used in ¶120.

¶97

〈NFA class inline method definitions〉98 ≡1 inline StateNumber NFA::getNumberOfStates() const

2 { return numberOfStates; }

This code is used in ¶120.

¶98

Page 43: Book

4.2. Definitions 29

〈NFA class inline method definitions〉99 ≡1 inline ostream& operator<<(ostream& stream, NFA const& nfa)

2 { return nfa.put(stream); }

This code is used in ¶120.

¶99

〈NFA class method definitions〉100 ≡1 NFA::NFA(StateNumber states) :

2 numberOfStates(states),

3 state(numberOfStates)

4 {

5 for (StateNumber n = 0; n < numberOfStates; ++n)

6 state[n].setNumber(n);

7 }

This code is used in ¶121.

¶100

〈NFA class method definitions〉101 ≡1 NFA::NFA(string const& s) :

2 numberOfStates(s.size() + 1),

3 state(numberOfStates)

4 {

5 for (StateNumber n = 0; n < numberOfStates; ++n)

6 state[n].setNumber(n);

7 for (StateNumber n = 0; n < numberOfStates - 1U; ++n)

8 state[n].connect(s[n], n + 1);

9 }

This code is used in ¶121.

¶101

〈NFA class method definitions〉102 ≡1 NFA::NFA(CharacterSet const& s) :

2 numberOfStates(2),

3 state(2)

4 {

5 for (StateNumber n = 0; n < numberOfStates; ++n)

6 state[n].setNumber(n);

7 state[0].connect(s, 1);

8 }

This code is used in ¶121.

¶102

Page 44: Book

30 Chapter 4. NFA Class

〈NFA class method definitions〉103 ≡1 NFA::NFA(NFA const& nfa) :

2 numberOfStates(nfa.numberOfStates),

3 state(numberOfStates)

4 {

5 for (StateNumber n = 0; n < numberOfStates; ++n)

6 {

7 state[n].setNumber(n);

8 state[n] = nfa.state[n];

9 }

10 }

This code is used in ¶121.

¶103

〈NFA class method definitions〉104 ≡1 NFA::~NFA()

2 {}

This code is used in ¶121.

¶104

〈NFA class method definitions〉105 ≡1 NFAState const& NFA::operator[](StateNumber n) const

2 {

3 assert(n < numberOfStates);

4 return state[n];

5 }

This code is used in ¶121.

¶105

〈NFA class method definitions〉106 ≡1 NFAState& NFA::operator[](StateNumber n)

2 {

3 assert(n < numberOfStates);

4 return state[n];

5 }

This code is used in ¶121.

¶106

〈NFA class method definitions〉107 ≡1 void NFA::setAnchor(Anchor a)

2 {

3 assert(numberOfStates > 0);

4 state[numberOfStates - 1].setAnchor(a);

5 }

This code is used in ¶121.

¶107

Page 45: Book

4.2. Definitions 31

〈NFA class method definitions〉108 ≡1 void NFA::setAction(Action a)

2 {

3 assert(numberOfStates > 0);

4 state[numberOfStates - 1].setAction(a);

5 }

This code is used in ¶121.

¶108

〈NFA class method definitions〉109 ≡1 void NFA::setToken(string const& s)

2 {

3 assert(numberOfStates > 0);

4 state[numberOfStates - 1].setToken(s);

5 }

This code is used in ¶121.

¶109

〈NFA class method definitions〉110 ≡1 ostream& NFA::put(ostream& s) const

2 {

3 for (StateNumber n = 0; n < numberOfStates; ++n)

4 s << state[n];

5 return s;

6 }

This code is used in ¶121.

¶110

Page 46: Book

32 Chapter 4. NFA Class

〈NFA class method definitions〉111 ≡1 Set NFA::getEpsilonClosure(Set const& inputSet) const

2 {

3 Stack<StateNumber> stack;

4

5 Set result(inputSet);

6

7 for (Set::const_iterator p(inputSet.begin()), null; p != null; ++p)

8 {

9 stack.push(*p);

10 }

11

12 while (!stack.isEmpty())

13 {

14 StateNumber const s = stack.pop();

15 if (state[s].getLabelType() == NFAState::epsilon)

16 {

17 for (uint8_t i = 0;

18 i < state[s].getFanout(); ++i)

19 {

20 StateNumber const next =

21 state[s].getNextState(i);

22 if (!result.contains(next))

23 {

24 result += next;

25 stack.push(next);

26 }

27 }

28 }

29 }

30 return result;

31 }

This code is used in ¶121.

¶111

Page 47: Book

4.2. Definitions 33

〈NFA class method definitions〉112 ≡1 Set NFA::getMoveSet(Set const& inputSet, char c) const

2 {

3 Set result;

4

5 for (Set::const_iterator p(inputSet.begin()), null; p != null; ++p)

6 {

7 StateNumber const s = *p;

8 if (state[s].isTransitionLabelled(c))

9 {

10 assert(state[s].getFanout() == 1);

11 result += state[s].getNextState(0);

12 }

13 }

14 return result;

15 }

This code is used in ¶121.

¶112

〈NFA class method definitions〉113 ≡1 Action NFA::getDominantAction(Set const& inputSet) const

2 {

3 for (Set::const_iterator p(inputSet.begin()), null; p != null; ++p)

4 {

5 Action const a = state[*p].getAction();

6 if (a != advance)

7 return a;

8 }

9 return advance;

10 }

This code is used in ¶121.

¶113

〈NFA class method definitions〉114 ≡1 Anchor NFA::getDominantAnchor(Set const& inputSet) const

2 {

3 for (Set::const_iterator p(inputSet.begin()), null; p != null; ++p)

4 {

5 Action const a = state[*p].getAction();

6 if (a != advance)

7 return state[*p].getAnchor();

8 }

9 return none;

10 }

This code is used in ¶121.

¶114

Page 48: Book

34 Chapter 4. NFA Class

〈NFA class method definitions〉115 ≡1 string NFA::getDominantToken(Set const& inputSet) const

2 {

3 for (Set::const_iterator p(inputSet.begin()), null; p != null; ++p)

4 {

5 Action const a = state[*p].getAction();

6 if (a != advance)

7 return state[*p].getToken();

8 }

9 return "";

10 }

This code is used in ¶121.

¶115

〈NFA class method definitions〉116 ≡1 NFA operator*(NFA const& nfa0, NFA const& nfa1)

2 {

3 StateNumber const size0 = nfa0.getNumberOfStates();

4 StateNumber const size1 = nfa1.getNumberOfStates();

5 StateNumber const resultSize = size0 + size1 - 1;

6 NFA result(resultSize);

7 for (StateNumber n = 0; n < size0 - 1U; ++n)

8 result[n] = nfa0[n];

9 for (StateNumber n = 0; n < size1; ++n)

10 result[size0 - 1 + n] = nfa1[n];

11 return result;

12 }

This code is used in ¶121.

¶116

〈NFA class method definitions〉117 ≡1 NFA operator|(NFA const& nfa0, NFA const& nfa1)

2 {

3 StateNumber const size0 = nfa0.getNumberOfStates();

4 StateNumber const size1 = nfa1.getNumberOfStates();

5 StateNumber const resultSize = size0 + size1 + 2;

6 NFA result(resultSize);

7 result[0].connect(static_cast<StateNumber>(1), 1 + size0);

8 for (StateNumber n = 0; n < size0; ++n)

9 result[1 + n] = nfa0[n];

10 for (StateNumber n = 0; n < size1; ++n)

11 result[1 + size0 + n] = nfa1[n];

12 result[size0].connect(resultSize - 1);

13 result[size0 + size1].connect(resultSize - 1);

14 return result;

15 }

This code is used in ¶121.

¶117

Page 49: Book

4.3. Files 35

〈NFA class method definitions〉118 ≡1 NFA operator!(NFA const& nfa)

2 {

3 StateNumber const size = nfa.getNumberOfStates();

4 StateNumber const resultSize = size + 1;

5 NFA result(resultSize);

6 result[0].connect(static_cast<StateNumber>(1), resultSize - 1);

7 for (StateNumber n = 0; n < size; ++n)

8 result[1 + n] = nfa[n];

9 return result;

10 }

This code is used in ¶121.

¶118

〈NFA class method definitions〉119 ≡1 NFA operator~(NFA const& nfa)

2 {

3 StateNumber const size = nfa.getNumberOfStates();

4 StateNumber const resultSize = size + 2;

5 NFA result(resultSize);

6 result[0].connect(static_cast<StateNumber>(1), resultSize - 1);

7 for (StateNumber n = 0; n < size - 1U; ++n)

8 result[1 + n] = nfa[n];

9 result[size].connect(static_cast<StateNumber>(1), resultSize - 1);

10 return result;

11 }

This code is used in ¶121.

¶119

�4.3 Files

〈File: Lexis/NFA.h〉120 ≡1 #if !defined(_Lexis_NFA_h)

2 #define _Lexis_NFA_h

3

4 #include <iostream>

5 #include <vector>

6 #include <string>

7 #include "Lexis/NFAState.h"

8 #include "Toolbox/CharacterSet.h"

9

10 using namespace Toolbox;

11

12 namespace Lexis

13 {

14 〈NFA class declaration〉92

15 〈NFA class inline method definitions〉96–9916 }

17

18 #endif

¶120

Page 50: Book

36 Chapter 4. NFA Class

〈File: Lexis/NFA.cc〉121 ≡1 #include <iostream>

2 #include "Lexis/NFA.h"

3 #include "Lexis/NFAState.h"

4 #include "Toolbox/EscapeSequence.h"

5 #include "Toolbox/Stack.h"

6

7 namespace Lexis

8 {

9 〈NFA class method definitions〉100–11910 }

¶121

Page 51: Book

Chapter 5

DFAState Class

�5.1 Declarations

〈DFAState class declaration〉122 ≡1 class DFAState : public State

2 {

3 〈DFAState class field declarations〉1234 〈DFAState class method declarations〉1245 };

This code is used in ¶132.

¶122

〈DFAState class field declarations〉123 ≡1 private: Set const nfaStateSet;

2 private: vector<StateNumber> transition;

This code is used in ¶122.

¶123

〈DFAState class method declarations〉124 ≡1 public: DFAState(StateNumber, NFA const&, Set const&);

2 public: ~DFAState();

3 public: DFAState& addTransition(char, StateNumber);

4 public: StateNumber getNextState(char) const;

5 public: Set getNFAStateSet() const;

6 public: ostream& put(ostream&) const;

This code is used in ¶122.

¶124

Page 52: Book

38 Chapter 5. DFAState Class

�5.2 Definitions

〈DFAState class inline method definitions〉125 ≡1 inline Set DFAState::getNFAStateSet() const

2 { return nfaStateSet; }

This code is used in ¶132.

¶125

〈DFAState class inline method definitions〉126 ≡1 inline ostream& operator<<(ostream& stream, DFAState const& state)

2 { return state.put(stream); }

This code is used in ¶132.

¶126

〈DFAState class method definitions〉127 ≡1 DFAState::DFAState(StateNumber n, NFA const& nfa, Set const& s) :

2 State(n, nfa.getDominantAnchor(s),

3 nfa.getDominantAction(s), nfa.getDominantToken(s)),

4 nfaStateSet(s),

5 transition(maximumCharacter)

6 {

7 for (uint8_t i = 0; i < maximumCharacter; ++i)

8 transition[i] = errorState;

9 }

This code is used in ¶133.

¶127

〈DFAState class method definitions〉128 ≡1 DFAState::~DFAState()

2 {}

This code is used in ¶133.

¶128

〈DFAState class method definitions〉129 ≡1 DFAState& DFAState::addTransition(char c, StateNumber to)

2 {

3 assert(static_cast<uint8_t>(c) < maximumCharacter);

4 transition[c] = to;

5 return *this;

6 }

This code is used in ¶133.

¶129

〈DFAState class method definitions〉130 ≡1 StateNumber DFAState::getNextState(char c) const

2 {

3 assert(static_cast<uint8_t>(c) < maximumCharacter);

4 return transition[c];

5 }

This code is used in ¶133.

¶130

Page 53: Book

5.3. Files 39

〈DFAState class method definitions〉131 ≡1 ostream& DFAState::put(ostream& s) const

2 {

3 s << "DFAState {";

4 State::put(s);

5 s << "\n";

6 for (StateNumber n = 0; n < DFA::maximumNumberOfStates; ++n)

7 {

8 CharacterSet set;

9 for (uint8_t c = 0; c < maximumCharacter; ++c)

10 if (transition[c] == n)

11 set += c;

12 if (set != 0)

13 {

14 cout << " goto " << n;

15 cout << " on " << set << "\n";

16 }

17 }

18 return s << "}\n";

19 }

This code is used in ¶133.

¶131

�5.3 Files

〈File: Lexis/DFAState.h〉132 ≡1 #if !defined(_Lexis_DFAState_h)

2 #define _Lexis_DFAState_h

3

4 #include <iostream>

5 #include <string>

6 #include "Lexis/State.h"

7 #include "Lexis/NFA.h"

8 #include "Toolbox/Set.h"

9

10 using namespace Toolbox;

11

12 namespace Lexis

13 {

14 〈DFAState class declaration〉12215 〈DFAState class inline method definitions〉125,12616 }

17

18 #endif

¶132

Page 54: Book

40 Chapter 5. DFAState Class

〈File: Lexis/DFAState.cc〉133 ≡1 #include <iostream>

2 #include "Lexis/DFAState.h"

3 #include "Lexis/DFA.h"

4 #include "Toolbox/EscapeSequence.h"

5 #include "Toolbox/CharacterSet.h"

6

7 using namespace Toolbox;

8

9 namespace Lexis

10 {

11 〈DFAState class method definitions〉127–13112 }

¶133

Page 55: Book

Chapter 6

DFA State

�6.1 Declarations

〈DFA class declaration〉134 ≡1 class DFA

2 {

3 〈DFA constants〉135

4 〈DFA class field declarations〉1365 〈DFA class method declarations〉137,1386 };

This code is used in ¶152.

¶134

〈DFA constants〉135 ≡1 public: enum Constants

2 {

3 maximumNumberOfStates = errorState - 1

4 };

This code is used in ¶134.

¶135

〈DFA class field declarations〉136 ≡1 private: StateNumber numberOfStates;

2 private: vector<DFAState*> state;

3 private: string language;

This code is used in ¶134.

¶136

〈DFA class method declarations〉137 ≡1 private: StateNumber getStateNumber(Set const&) const;

2 private: StateNumber initializeGroups(

3 vector<Set>&, vector<StateNumber>&) const;

4 private: DFA& rebuild(

5 StateNumber, vector<Set> const&, vector<StateNumber> const&);

This code is used in ¶134.

¶137

Page 56: Book

42 Chapter 6. DFA State

〈DFA class method declarations〉138 ≡1 public: explicit DFA(NFA const&);

2 public: ~DFA();

3 public: void minimize();

4 public: StateNumber getNumberOfStates() const;

5 public: DFAState const& operator[](StateNumber) const;

6 public: bool isColumnEquivalent(char, char) const;

7 public: bool isRowEquivalent(StateNumber, StateNumber) const;

8 public: string const& getLanguage() const;

9 public: ostream& put(ostream&) const;

This code is used in ¶134.

¶138

�6.2 Definitions

〈DFA class inline method definitions〉139 ≡1 inline StateNumber DFA::getNumberOfStates() const

2 { return numberOfStates; }

This code is used in ¶152.

¶139

〈DFA class inline method definitions〉140 ≡1 inline string const& DFA::getLanguage() const

2 { return language; }

This code is used in ¶152.

¶140

〈DFA class inline method definitions〉141 ≡1 inline ostream& operator<<(ostream& stream, DFA const& dfa)

2 { return dfa.put(stream); }

This code is used in ¶152.

¶141

〈DFA class method definitions〉142 ≡1 StateNumber DFA::getStateNumber(Set const& s) const

2 {

3 for (StateNumber i = 0; i < numberOfStates; ++i)

4 if (state[i]->getNFAStateSet() == s)

5 return i;

6 return errorState;

7 }

This code is used in ¶153.

¶142

Page 57: Book

6.2. Definitions 43

〈DFA class method definitions〉143 ≡1 DFA::DFA(NFA const& nfa) :

2 numberOfStates(0),

3 state(maximumNumberOfStates),

4 language(nfa.getLanguage())

5 {

6 Set nfaStates;

7 nfaStates += 0;

8 nfaStates = nfa.getEpsilonClosure(nfaStates);

9

10 state[0] = new DFAState(0, nfa, nfaStates);

11

12 numberOfStates = 1;

13

14 for (StateNumber i = 0; i < numberOfStates; ++i)

15 {

16 cerr << ".";

17 DFAState* const current = state[i];

18 assert(current != 0);

19

20 for (uint8_t c = 0; c < maximumCharacter; ++c)

21 {

22 Set moveSet;

23 moveSet = nfa.getMoveSet(current->getNFAStateSet(), c);

24 moveSet = nfa.getEpsilonClosure(moveSet);

25

26 StateNumber nextState = errorState;

27 if (moveSet != 0)

28 {

29 nextState = getStateNumber(moveSet);

30 if (nextState == errorState)

31 {

32 assert(numberOfStates < maximumNumberOfStates);

33 state[numberOfStates] =

34 new DFAState(numberOfStates, nfa, moveSet);

35 nextState = numberOfStates;

36 numberOfStates += 1;

37 }

38 current->addTransition(c, nextState);

39 }

40 }

41 }

42 }

This code is used in ¶153.

¶143

Page 58: Book

44 Chapter 6. DFA State

〈DFA class method definitions〉144 ≡1 DFA::~DFA()

2 {

3 for (StateNumber i = 0; i < numberOfStates; ++i)

4 delete state[i];

5 }

This code is used in ¶153.

¶144

〈DFA class method definitions〉145 ≡1 DFAState const& DFA::operator[](StateNumber s) const

2 {

3 assert(s < numberOfStates);

4 return *state[s];

5 }

This code is used in ¶153.

¶145

〈DFA class method definitions〉146 ≡1 ostream& DFA::put(ostream& s) const

2 {

3 for (StateNumber i = 0; i < numberOfStates; ++i)

4 cout << *state[i];

5 return s;

6 }

This code is used in ¶153.

¶146

Page 59: Book

6.2. Definitions 45

〈DFA class method definitions〉147 ≡1 StateNumber DFA::initializeGroups(

2 vector<Set>& group,

3 vector<StateNumber>& inGroup) const

4 {

5 StateNumber numberOfGroups = 0;

6 for (StateNumber i = 0; i < numberOfStates; ++i)

7 {

8 bool found = false;

9 StateNumber j;

10 for (j = 0; j < i; ++j)

11 {

12 if (state[i]->getAction() == state[j]->getAction()

13 && state[i]->getToken() == state[j]->getToken())

14 {

15 found = true;

16 break;

17 }

18 }

19 if (found)

20 {

21 group[inGroup[j]] += i;

22 inGroup[i] = inGroup[j];

23 }

24 else

25 {

26 assert(numberOfGroups < numberOfStates);

27 group[numberOfGroups] += i;

28 inGroup[i] = numberOfGroups;

29 numberOfGroups += 1;

30 }

31 }

32 return numberOfGroups;

33 }

This code is used in ¶153.

¶147

Page 60: Book

46 Chapter 6. DFA State

〈DFA class method definitions〉148 ≡1 DFA& DFA::rebuild(StateNumber numberOfGroups,

2 vector<Set> const& group,

3 vector<StateNumber> const& inGroup)

4 {

5 vector<DFAState*> oldState(state);

6 state.resize(numberOfGroups);

7

8 for (StateNumber i = 0; i < numberOfGroups; ++i)

9 {

10 assert(group[i] != 0);

11 Set::const_iterator p(group[i].begin()), null;

12 state[i] = oldState[*p];

13 state[i]->setNumber(i);

14 for (uint8_t c = 0; c < maximumCharacter; ++c)

15 {

16 StateNumber const next = state[i]->getNextState(c);

17 if (next != errorState)

18 state[i]->addTransition(c, inGroup[next]);

19 }

20 for (++p; p != null; ++p)

21 delete oldState[*p];

22 }

23 numberOfStates = numberOfGroups;

24 return *this;

25 }

This code is used in ¶153.

¶148

Page 61: Book

6.2. Definitions 47

〈DFA class method definitions〉149 ≡1 void DFA::minimize()

2 {

3 vector<Set> group(numberOfStates);

4 vector<StateNumber> inGroup(numberOfStates);

5

6 StateNumber numberOfGroups = initializeGroups(group, inGroup);

7

8 bool done = false;

9 while (!done)

10 {

11 done = true;

12 for (StateNumber i = 0; i < numberOfGroups; ++i)

13 {

14 if (group[i].getSize() > 1)

15 {

16 Set newGroup;

17 Set::const_iterator p(group[i].begin()), null;

18 StateNumber const first = *p;

19 for (++p; p != null; ++p)

20 {

21 StateNumber const next = *p;

22 for (uint8_t c = 0; c < maximumCharacter; ++c)

23 {

24 StateNumber const firstGoto =

25 state[first]->getNextState(c);

26 StateNumber const nextGoto =

27 state[next]->getNextState(c);

28 if (firstGoto != nextGoto &&

29 (firstGoto == errorState ||

30 nextGoto == errorState ||

31 inGroup[firstGoto] != inGroup[nextGoto]))

32 {

33 group[i] -= next;

34 newGroup += next;

35 inGroup[next] = numberOfGroups;

36 break;

37 }

38 }

39 }

40 if (newGroup != 0)

41 {

42 assert(numberOfGroups < numberOfStates);

43 group[numberOfGroups] = newGroup;

44 numberOfGroups += 1;

45 done = false;

46 }

47 }

48 }

49 }

50 rebuild(numberOfGroups, group, inGroup);

51 }

This code is used in ¶153.

¶149

Page 62: Book

48 Chapter 6. DFA State

〈DFA class method definitions〉150 ≡1 bool DFA::isColumnEquivalent(char c1, char c2) const

2 {

3 for (StateNumber s = 0; s < numberOfStates; ++s)

4 if (state[s]->getNextState(c1) !=

5 state[s]->getNextState(c2))

6 return false;

7 return true;

8 }

This code is used in ¶153.

¶150

〈DFA class method definitions〉151 ≡1 bool DFA::isRowEquivalent(StateNumber s1, StateNumber s2) const

2 {

3 for (uint8_t c = 0; c < maximumCharacter; ++c)

4 if (state[s1]->getNextState(c) !=

5 state[s2]->getNextState(c))

6 return false;

7 return true;

8 }

This code is used in ¶153.

¶151

�6.3 Files

〈File: Lexis/DFA.h〉152 ≡1 #if !defined(_Lexis_DFA_h)

2 #define _Lexis_DFA_h

3

4 #include <iostream>

5 #include <string>

6 #include "Lexis/State.h"

7 #include "Lexis/NFA.h"

8 #include "Lexis/DFAState.h"

9 #include "Toolbox/Set.h"

10

11 using namespace Toolbox;

12

13 namespace Lexis

14 {

15 〈DFA class declaration〉13416 〈DFA class inline method definitions〉139–14117 }

18

19 #endif

¶152

Page 63: Book

6.3. Files 49

〈File: Lexis/DFA.cc〉153 ≡1 #include <iostream>

2 #include "Lexis/DFA.h"

3 #include "Toolbox/EscapeSequence.h"

4

5 namespace Lexis

6 {

7 〈DFA class method definitions〉142–1518 }

¶153

Page 64: Book

50 Chapter 6. DFA State

Page 65: Book

Chapter 7

CompressedDFA Class

�7.1 Declarations

〈CompressedDFA class declaration〉154 ≡1 class CompressedDFA

2 {

3 〈CompressedDFA class field declarations〉1554 〈CompressedDFA class method declarations〉156,1575 };

This code is used in ¶173.

¶154

〈CompressedDFA class field declarations〉155 ≡1 private: TableEntry numberOfStates;

2 private: TableEntry numberOfRows;

3 private: TableEntry numberOfColumns;

4 private: TableEntry numberOfTokens;

5 private: vector<TableEntry> rowMap;

6 private: vector<TableEntry> columnMap;

7 private: vector<Anchor> anchorTable;

8 private: vector<Action> actionTable;

9 private: vector<TableEntry> transitionTable;

10 private: vector<string> tokenTable;

11 private: vector<string> tokenList;

12 private: string language;

This code is used in ¶154.

¶155

〈CompressedDFA class method declarations〉156 ≡1 private: ostream& putMap(ostream&, char const*, TableEntry,

2 vector<TableEntry> const&) const;

3 private: ostream& putTransitionTable(ostream&) const;

4 private: ostream& putAnchorTable(ostream&) const;

5 private: ostream& putActionTable(ostream&) const;

6 private: ostream& putTokenList(ostream&) const;

7 private: ostream& putTokenTable(ostream&) const;

This code is used in ¶154.

¶156

Page 66: Book

52 Chapter 7. CompressedDFA Class

〈CompressedDFA class method declarations〉157 ≡1 public: explicit CompressedDFA(DFA const&);

2 public: ~CompressedDFA();

3 public: ostream& putTokens(ostream&) const;

4 public: ostream& putTables(ostream&) const;

This code is used in ¶154.

¶157

�7.2 Definitions

〈CompressedDFA class method definitions〉158 ≡1 CompressedDFA::CompressedDFA(DFA const& dfa) :

2 numberOfStates(dfa.getNumberOfStates()),

3 numberOfRows(0),

4 numberOfColumns(0),

5 numberOfTokens(0),

6 rowMap(dfa.getNumberOfStates()),

7 columnMap(maximumCharacter),

8 anchorTable(dfa.getNumberOfStates()),

9 actionTable(dfa.getNumberOfStates()),

10 transitionTable(0),

11 tokenTable(dfa.getNumberOfStates()),

12 tokenList(dfa.getNumberOfStates()),

13 language(dfa.getLanguage())

14 {

15 for (uint8_t c = 0; c < maximumCharacter; ++c)

16 columnMap[c] = maximumCharacter;

17

18 〈construct columnTable and columnMap〉15919 〈construct rowTable and rowMap〉16020 〈construct anchorTable, actionTable and tokenTable〉16121 〈construct tokenList〉16222 〈construct transitionTable〉16323 }

This code is used in ¶174.

¶158

Page 67: Book

7.2. Definitions 53

〈construct columnTable and columnMap〉159 ≡1 vector<TableEntry> columnTable(maximumCharacter);

2 for (uint8_t c = 0; c < maximumCharacter; ++c)

3 {

4 if (columnMap[c] == maximumCharacter)

5 {

6 columnTable[numberOfColumns] = c;

7 columnMap[c] = numberOfColumns;

8 for (uint8_t i = c + 1; i < maximumCharacter; ++i)

9 {

10 if (columnMap[i] == maximumCharacter &&

11 dfa.isColumnEquivalent(c, i))

12 columnMap[i] = numberOfColumns;

13 }

14 numberOfColumns += 1;

15 }

16 }

This code is used in ¶158.

¶159

〈construct rowTable and rowMap〉160 ≡1 vector<TableEntry> rowTable(numberOfStates);

2 for (StateNumber s = 0; s < numberOfStates; ++s)

3 rowMap[s] = numberOfStates;

4 for (StateNumber s = 0; s < numberOfStates; ++s)

5 {

6 if (rowMap[s] == numberOfStates)

7 {

8 rowTable[numberOfRows] = s;

9 rowMap[s] = numberOfRows;

10 for (StateNumber i = s + 1;

11 i < numberOfStates; ++i)

12 {

13 if (rowMap[i] == numberOfStates &&

14 dfa.isRowEquivalent(s, i))

15 rowMap[i] = numberOfRows;

16 }

17 numberOfRows += 1;

18 }

19 }

This code is used in ¶158.

¶160

Page 68: Book

54 Chapter 7. CompressedDFA Class

〈construct anchorTable, actionTable and tokenTable〉161 ≡1 for (StateNumber s = 0; s < numberOfStates; ++s)

2 {

3 anchorTable[s] = dfa[s].getAnchor();

4 actionTable[s] = dfa[s].getAction();

5 if (actionTable[s] == accept)

6 tokenTable[s] = dfa[s].getToken();

7 else

8 tokenTable[s] = "end_of_file";

9 }

This code is used in ¶158.

¶161

〈construct tokenList〉162 ≡1 tokenList[numberOfTokens++] = "_epsilon";

2 tokenList[numberOfTokens++] = "end_of_file";

3 for (StateNumber s = 0; s < numberOfStates; ++s)

4 {

5 TableEntry i;

6 for (i = 0; i < numberOfTokens; ++i)

7 if (tokenList[i] == tokenTable[s])

8 break;

9 if (i == numberOfTokens)

10 {

11 tokenList[numberOfTokens] = tokenTable[s];

12 numberOfTokens += 1;

13 }

14 }

This code is used in ¶158.

¶162

〈construct transitionTable〉163 ≡1 transitionTable.resize(numberOfRows * numberOfColumns);

2 for (TableEntry row = 0; row < numberOfRows; ++row)

3 for (TableEntry col = 0;

4 col < numberOfColumns; ++col)

5 transitionTable[row * numberOfColumns + col] =

6 dfa[rowTable[row]].getNextState(columnTable[col]);

This code is used in ¶158.

¶163

〈CompressedDFA class method definitions〉164 ≡1 CompressedDFA::~CompressedDFA()

2 {}

This code is used in ¶174.

¶164

Page 69: Book

7.2. Definitions 55

〈CompressedDFA class method definitions〉165 ≡1 ostream& CompressedDFA::putMap(ostream& stream,

2 char const* label, TableEntry size,

3 vector<TableEntry> const& map) const

4 {

5 stream << "TableEntry const LexisTables::"

6 << label << "[" << static_cast<uint16_t>(size) << "] =\n";

7 stream << "{\n";

8 stream << " ";

9 for (uint16_t i = 0; i < size; ++i)

10 {

11 stream << static_cast<uint16_t>(map[i]);

12 if (i < size - 1U)

13 stream << ", ";

14 if (i % 10 == 9)

15 {

16 stream << "\n";

17 stream << " ";

18 }

19 }

20 stream << "\n";

21 stream << "};\n";

22 return stream;

23 }

This code is used in ¶174.

¶165

〈CompressedDFA class method definitions〉166 ≡1 ostream& CompressedDFA::putActionTable(ostream& stream) const

2 {

3 stream << "Action const LexisTables::actionTable["

4 << static_cast<uint16_t>(numberOfStates) << "] =\n";

5 stream << "{\n";

6 stream << " ";

7 for (uint16_t i = 0; i < numberOfStates; ++i)

8 {

9 stream << actionTable[i];

10 if (i < numberOfStates - 1U)

11 stream << ", ";

12 if (i % 4 == 3)

13 {

14 stream << "\n";

15 stream << " ";

16 }

17 }

18 stream << "\n";

19 stream << "};\n";

20 return stream;

21 }

This code is used in ¶174.

¶166

Page 70: Book

56 Chapter 7. CompressedDFA Class

〈CompressedDFA class method definitions〉167 ≡1 ostream& CompressedDFA::putAnchorTable(ostream& stream) const

2 {

3 stream << "Anchor const LexisTables::anchorTable["

4 << static_cast<uint16_t>(numberOfStates) << "] =\n";

5 stream << "{\n";

6 stream << " ";

7 for (uint16_t i = 0; i < numberOfStates; ++i)

8 {

9 stream << anchorTable[i];

10 if (i < numberOfStates - 1U)

11 stream << ", ";

12 if (i % 4 == 3)

13 {

14 stream << "\n";

15 stream << " ";

16 }

17 }

18 stream << "\n";

19 stream << "};\n";

20 return stream;

21 }

This code is used in ¶174.

¶167

〈CompressedDFA class method definitions〉168 ≡1 ostream& CompressedDFA::putTokenList(ostream& stream) const

2 {

3 stream << "TableEntry const LexisTables::numberOfTokens = "

4 << static_cast<uint16_t>(numberOfTokens) << ";\n";

5 stream << "LexisTables::TokenName const LexisTables::nameTable["

6 << static_cast<uint16_t>(numberOfTokens) << "] =\n";

7 stream << "{\n";

8 for (TableEntry i = 0; i < numberOfTokens; ++i)

9 {

10 stream << " { " << tokenList[i] << ","

11 << " \"" << tokenList[i] << "\" }";

12 if (i < numberOfTokens - 1U)

13 stream << ",\n";

14 }

15 stream << "\n";

16 stream << "};\n";

17 return stream;

18 }

This code is used in ¶174.

¶168

Page 71: Book

7.2. Definitions 57

〈CompressedDFA class method definitions〉169 ≡1 ostream& CompressedDFA::putTokenTable(ostream& stream) const

2 {

3 stream << "LexisTables::TokenType const LexisTables::tokenTable["

4 << static_cast<uint16_t>(numberOfStates) << "] =\n";

5 stream << "{\n";

6 stream << " ";

7 for (uint16_t i = 0; i < numberOfStates; ++i)

8 {

9 stream << tokenTable[i];

10 if (i < numberOfStates - 1U)

11 stream << ", ";

12 if (i % 2 == 1)

13 {

14 stream << "\n";

15 stream << " ";

16 }

17 }

18 stream << "\n";

19 stream << "};\n";

20 return stream;

21 }

This code is used in ¶174.

¶169

Page 72: Book

58 Chapter 7. CompressedDFA Class

〈CompressedDFA class method definitions〉170 ≡1 ostream& CompressedDFA::putTransitionTable(ostream& stream) const

2 {

3 stream << "TableEntry const LexisTables::transitionTable["

4 << static_cast<uint16_t>(numberOfRows * numberOfColumns)

5 << "] =\n";

6 stream << "{\n";

7 for (TableEntry row = 0; row < numberOfRows; ++row)

8 {

9 stream << " // row " << static_cast<uint16_t>(row) << "\n";

10 stream << " ";

11 for (TableEntry col = 0; col < numberOfColumns; ++col)

12 {

13 stream << static_cast<uint16_t>(transitionTable[

14 row * numberOfColumns + col]);

15 if (col < numberOfColumns - 1U)

16 stream << ", ";

17 if (col % 10 == 9)

18 {

19 stream << "\n";

20 stream << " ";

21 }

22 }

23 if (row < numberOfRows - 1U)

24 stream << ",";

25 stream << "\n";

26 }

27 stream << "\n";

28 stream << "};\n";

29 return stream;

30 }

This code is used in ¶174.

¶170

Page 73: Book

7.2. Definitions 59

〈CompressedDFA class method definitions〉171 ≡1 ostream& CompressedDFA::putTokens(ostream& stream) const

2 {

3 stream <<

4 "#if !defined(lexistab_h)\n"

5 "#define lexistab_h\n"

6 "\n"

7 "#include \"Lexis/Lexis.h\"\n"

8 "\n"

9 "namespace " << language << "\n"

10 "{\n"

11 "using namespace Lexis;\n"

12 "using Lexis::advance;\n"

13 "class LexisTables\n"

14 "{\n"

15 "public:\n"

16 " enum TokenType\n"

17 " {\n";

18 for (TableEntry i = 0; i < numberOfTokens; ++i)

19 {

20 stream << "\t" << tokenList[i]

21 << " = " << static_cast<uint16_t>(i);

22 if (i < numberOfTokens - 1U)

23 stream << ",\n";

24 }

25 stream <<

26 "\n"

27 " };\n"

28 " struct TokenName\n"

29 " {\n"

30 "\tTokenType type;\n"

31 "\tchar const* name;\n"

32 " };\n"

33 " static uint8_t const numberOfTokens;\n"

34 " static TokenName const nameTable[];\n"

35 " static TableEntry const numberOfStates;\n"

36 " static TableEntry const numberOfRows;\n"

37 " static TableEntry const numberOfColumns;\n"

38 " static TableEntry const rowMap[];\n"

39 " static TableEntry const columnMap[];\n"

40 " static TableEntry const transitionTable[];\n"

41 " static Anchor const anchorTable[];\n"

42 " static Action const actionTable[];\n"

43 " static TokenType const tokenTable[];\n"

44 "};\n"

45 "}\n"

46 "\n"

47 "#endif\n";

48 return stream;

49 }

This code is used in ¶174.

¶171

Page 74: Book

60 Chapter 7. CompressedDFA Class

〈CompressedDFA class method definitions〉172 ≡1 ostream& CompressedDFA::putTables(ostream& stream) const

2 {

3 stream <<

4 "#include \"lexistab.h\"\n"

5 "\n"

6 "namespace " << language << "\n"

7 "{\n";

8 putTokenList(stream);

9 stream << "TableEntry const LexisTables::numberOfStates = "

10 << static_cast<uint16_t>(numberOfStates) << ";\n";

11 stream << "TableEntry const LexisTables::numberOfColumns = "

12 << static_cast<uint16_t>(numberOfColumns) << ";\n";

13 putMap(stream, "columnMap", maximumCharacter, columnMap);

14 stream << "TableEntry const LexisTables::numberOfRows = "

15 << static_cast<uint16_t>(numberOfRows) << ";\n";

16 putMap(stream, "rowMap", numberOfStates, rowMap);

17 putTransitionTable(stream);

18 putAnchorTable(stream);

19 putActionTable(stream);

20 putTokenTable(stream);

21 stream << "}\n";

22 return stream;

23 }

This code is used in ¶174.

¶172

�7.3 Files

〈File: Lexis/CompressedDFA.h〉173 ≡1 #if !defined(_Lexis_Compressed_h)

2 #define _Lexis_Compressed_h

3

4 #include <iostream>

5 #include <vector>

6 #include <string>

7 #include "Lexis/DFA.h"

8

9 namespace Lexis

10 {

11 〈CompressedDFA class declaration〉15412 }

13

14 #endif

¶173

Page 75: Book

7.3. Files 61

〈File: Lexis/CompressedDFA.cc〉174 ≡1 #include <iostream>

2 #include "Lexis/CompressedDFA.h"

3

4 namespace Lexis

5 {

6 〈CompressedDFA class method definitions〉158,164–1727 }

¶174

Page 76: Book

62 Chapter 7. CompressedDFA Class

Page 77: Book

Chapter 8

Lexical Analyzer Classes

�8.1 Common Types and Constants

8.1.1 Type Declarations

〈Lexis type declarations〉175 ≡1 typedef uint16_t StateNumber;

This code is used in ¶235.

¶175

〈Lexis type declarations〉176 ≡1 typedef uint8_t TableEntry;

This code is used in ¶235.

¶176

8.1.2 Constants

〈Lexis constants〉177 ≡1 StateNumber const errorState = 255;

This code is used in ¶235.

¶177

〈Lexis constants〉178 ≡1 TableEntry const maximumCharacter = 128;

This code is used in ¶235.

¶178

Page 78: Book

64 Chapter 8. Lexical Analyzer Classes

8.1.3 Anchor Enumeration

〈Anchor enumeration declaration〉179 ≡1 enum Anchor

2 {

3 none = 0,

4 head = 1,

5 tail = 2,

6 both = 3

7 };

This code is used in ¶235.

¶179

〈Anchor enumeration inline method definitions〉180 ≡1 inline ostream& operator<<(ostream& s, Anchor a)

2 {

3 switch (a)

4 {

5 case none: s << "none"; break;

6 case head: s << "head"; break;

7 case tail: s << "tail"; break;

8 case both: s << "both"; break;

9 }

10 return s;

11 }

This code is used in ¶235.

¶180

8.1.4 Action Enumeration

〈Action enumeration declaration〉181 ≡1 enum Action

2 {

3 advance = 0,

4 accept = 1,

5 discard = 3 // We want (discard & accept) != 0.

6 };

This code is used in ¶235.

¶181

〈Action enumeration inline method definitions〉182 ≡1 inline ostream& operator<<(ostream& s, Action a)

2 {

3 switch (a)

4 {

5 case advance: s << "advance"; break;

6 case accept: s << "accept"; break;

7 case discard: s << "discard"; break;

8 }

9 return s;

10 }

This code is used in ¶235.

¶182

Page 79: Book

8.2. Input Class Template 65

�8.2 Input Class Template

8.2.1 Declarations

〈Input class template declaration〉183 ≡1 template<typename T>

2 class Input

3 {

4 〈Input class template type declarations〉1845 〈Input constants〉185

6 〈Input class template field declarations〉1867 〈Input class template method declarations〉187,1888 };

This code is used in ¶235.

¶183

〈Input class template type declarations〉184 ≡1 public: typedef T Token;

This code is used in ¶183.

¶184

〈Input constants〉185 ≡1 private: enum Constants

2 {

3 maximumLexemeLength = 1024,

4 bufferSize = 2 * maximumLexemeLength

5 };

This code is used in ¶183.

¶185

〈Input class template field declarations〉186 ≡1 private: istream& inputStream;

2 private: char buffer[bufferSize];

3 private: char* nextInput;

4 private: char* endOfInput;

5 private: char* const endOfBuffer;

6 private: char* startOfLexeme;

7 private: char* endOfLexeme;

8 private: bool endOfFileRead;

9 private: uint16_t currentLineNumber;

10 private: uint16_t startMarkLineNumber;

11 private: uint16_t endMarkLineNumber;

This code is used in ¶183.

¶186

〈Input class template method declarations〉187 ≡1 private: void checkAssertions() const;

2 private: int16_t advance();

This code is used in ¶183.

¶187

Page 80: Book

66 Chapter 8. Lexical Analyzer Classes

〈Input class template method declarations〉188 ≡1 public: Input(istream&);

2 public: operator bool();

3 public: char operator*();

4 public: Input& operator++();

5 public: Input& operator--();

6 public: void markStart();

7 public: void markEnd();

8 public: void returnToStartMark();

9 public: void returnToEndMark();

10 public: void advanceStartMark();

11 public: uint16_t getLineNumber() const;

12 public: string getLexeme() const;

This code is used in ¶183.

¶188

8.2.2 Definitions

〈Input class template inline method definitions〉189 ≡1 template <typename T>

2 inline uint16_t Input<T>::getLineNumber() const

3 { return startMarkLineNumber; }

This code is used in ¶235.

¶189

〈Input class template method definitions〉190 ≡1 template <typename T>

2 Input<T>::Input(istream& s) :

3 inputStream(s),

4 nextInput(buffer),

5 endOfInput(buffer),

6 endOfBuffer(buffer + bufferSize),

7 startOfLexeme(buffer),

8 endOfLexeme(buffer),

9 endOfFileRead(false),

10 currentLineNumber(0),

11 startMarkLineNumber(0),

12 endMarkLineNumber(0)

13 {

14 assert(endOfInput < endOfBuffer);

15 *endOfInput++ = ’\n’;

16 checkAssertions();

17 }

This code is used in ¶236.

¶190

Page 81: Book

8.2. Input Class Template 67

〈Input class template method definitions〉191 ≡1 template <typename T>

2 void Input<T>::checkAssertions() const

3 {

4 assert(buffer <= startOfLexeme);

5 assert(startOfLexeme <= endOfLexeme);

6 assert(endOfLexeme <= nextInput);

7 assert(nextInput <= endOfInput);

8 assert(endOfInput <= endOfBuffer);

9 assert(nextInput - startOfLexeme <= maximumLexemeLength);

10 }

This code is used in ¶236.

¶191

〈Input class template method definitions〉192 ≡1 template <typename T>

2 int16_t Input<T>::advance()

3 {

4 if (nextInput == endOfInput && !endOfFileRead)

5 {

6 if (endOfInput == endOfBuffer)

7 {

8 uint32_t const shift = startOfLexeme - buffer;

9 char* dst = buffer;

10 char* src = startOfLexeme;

11 while (src < endOfInput)

12 *dst++ = *src++;

13 startOfLexeme -= shift;

14 endOfLexeme -= shift;

15 endOfInput -= shift;

16 nextInput -= shift;

17 }

18 assert(endOfInput < endOfBuffer);

19 int const c = inputStream.get();

20 if (c == EOF)

21 endOfFileRead = true;

22 else

23 {

24 *endOfInput = c;

25 endOfInput += 1;

26 }

27 }

28 int16_t result = EOF;

29 if (nextInput < endOfInput)

30 result = *nextInput;

31 else

32 assert(endOfFileRead);

33 checkAssertions();

34 return result;

35 }

This code is used in ¶236.

¶192

Page 82: Book

68 Chapter 8. Lexical Analyzer Classes

〈Input class template method definitions〉193 ≡1 template <typename T>

2 Input<T>::operator bool()

3 { return advance() != EOF; }

This code is used in ¶236.

¶193

〈Input class template method definitions〉194 ≡1 template <typename T>

2 char Input<T>::operator*()

3 {

4 int16_t const c = advance();

5 if (c != EOF)

6 return c;

7 else

8 return 0;

9 }

This code is used in ¶236.

¶194

〈Input class template method definitions〉195 ≡1 template <typename T>

2 Input<T>& Input<T>::operator++()

3 {

4 if (nextInput == endOfInput && !endOfFileRead)

5 advance();

6 if (nextInput < endOfInput)

7 {

8 if (*nextInput == ’\n’)

9 currentLineNumber += 1;

10 nextInput += 1;

11 if (nextInput - startOfLexeme > maximumLexemeLength)

12 {

13 if (*startOfLexeme == ’\n’)

14 startMarkLineNumber += 1;

15 startOfLexeme += 1;

16 if (endOfLexeme < startOfLexeme)

17 {

18 if (*endOfLexeme == ’\n’)

19 endMarkLineNumber += 1;

20 endOfLexeme += 1;

21 }

22 }

23 }

24 else

25 assert(endOfFileRead);

26 checkAssertions();

27 return *this;

28 }

This code is used in ¶236.

¶195

Page 83: Book

8.2. Input Class Template 69

〈Input class template method definitions〉196 ≡1 template <typename T>

2 Input<T>& Input<T>::operator--()

3 {

4 if (nextInput > startOfLexeme)

5 {

6 nextInput -= 1;

7 if (*nextInput == ’\n’)

8 currentLineNumber -= 1;

9 if (endOfLexeme > nextInput)

10 {

11 endOfLexeme = nextInput;

12 endMarkLineNumber = currentLineNumber;

13 }

14 }

15 checkAssertions();

16 return *this;

17 }

This code is used in ¶236.

¶196

〈Input class template method definitions〉197 ≡1 template <typename T>

2 void Input<T>::markStart()

3 {

4 startOfLexeme = nextInput;

5 endOfLexeme = nextInput;

6 startMarkLineNumber = currentLineNumber;

7 endMarkLineNumber = currentLineNumber;

8 checkAssertions();

9 }

This code is used in ¶236.

¶197

〈Input class template method definitions〉198 ≡1 template <typename T>

2 void Input<T>::markEnd()

3 {

4 endOfLexeme = nextInput;

5 endMarkLineNumber = currentLineNumber;

6 checkAssertions();

7 }

This code is used in ¶236.

¶198

Page 84: Book

70 Chapter 8. Lexical Analyzer Classes

〈Input class template method definitions〉199 ≡1 template <typename T>

2 void Input<T>::returnToStartMark()

3 {

4 nextInput = startOfLexeme;

5 endOfLexeme = startOfLexeme;

6 currentLineNumber = startMarkLineNumber;

7 checkAssertions();

8 }

This code is used in ¶236.

¶199

〈Input class template method definitions〉200 ≡1 template <typename T>

2 void Input<T>::returnToEndMark()

3 {

4 nextInput = endOfLexeme;

5 currentLineNumber = endMarkLineNumber;

6 checkAssertions();

7 }

This code is used in ¶236.

¶200

〈Input class template method definitions〉201 ≡1 template <typename T>

2 void Input<T>::advanceStartMark()

3 {

4 if (startOfLexeme < endOfLexeme)

5 if (*startOfLexeme == ’\n’)

6 startMarkLineNumber += 1;

7 startOfLexeme += 1;

8 checkAssertions();

9 }

This code is used in ¶236.

¶201

〈Input class template method definitions〉202 ≡1 template <typename T>

2 string Input<T>::getLexeme() const

3 {

4 string result(startOfLexeme, endOfLexeme - startOfLexeme);

5 checkAssertions();

6 return result;

7 }

This code is used in ¶236.

¶202

Page 85: Book

8.3. Token Class Template 71

�8.3 Token Class Template

8.3.1 Declarations

〈Token class template declaration〉203 ≡1 template <typename LT>

2 class Token

3 {

4 〈Token class template type declarations〉2045 〈Token class template field declarations〉2056 〈Token class template method declarations〉2067 };

This code is used in ¶235.

¶203

〈Token class template type declarations〉204 ≡1 public: typedef LT Tables;

2 public: typedef TokenStream<Token> Stream;

3 public: typedef typename LT::TokenType Type;

This code is used in ¶203.

¶204

〈Token class template field declarations〉205 ≡1 private: Type type;

2 private: string lexeme;

3 private: uint16_t lineNumber;

This code is used in ¶203.

¶205

〈Token class template method declarations〉206 ≡1 public: Token();

2 public: Token(Type, string const&, uint16_t = 0);

3 public: Token(Token const&);

4 public: Token& operator=(Token const&);

5 public: Type getType() const;

6 public: string const& getLexeme() const;

7 public: uint16_t getLineNumber() const;

8 public: string getName() const;

9 public: ostream& put(ostream& s) const;

This code is used in ¶203.

¶206

Page 86: Book

72 Chapter 8. Lexical Analyzer Classes

8.3.2 Definitions

〈Token class template inline method definitions〉207 ≡1 template <typename LT>

2 inline Token<LT>::Type Token<LT>::getType() const

3 { return type; }

This code is used in ¶235.

¶207

〈Token class template inline method definitions〉208 ≡1 template <typename LT>

2 inline string const& Token<LT>::getLexeme() const

3 { return lexeme; }

This code is used in ¶235.

¶208

〈Token class template inline method definitions〉209 ≡1 template <typename LT>

2 inline uint16_t Token<LT>::getLineNumber() const

3 { return lineNumber; }

This code is used in ¶235.

¶209

〈Token class template inline method definitions〉210 ≡1 template <typename LT>

2 inline ostream& operator<<(ostream& stream, Token<LT> const& token)

3 { return token.put(stream); }

This code is used in ¶235.

¶210

〈Token class template method definitions〉211 ≡1 template <typename LT>

2 Token<LT>::Token() :

3 type(LT::end_of_file),

4 lexeme(""),

5 lineNumber(0)

6 {}

This code is used in ¶236.

¶211

〈Token class template method definitions〉212 ≡1 template <typename LT>

2 Token<LT>::Token(Token<LT>::Type t, string const& s, uint16_t l) :

3 type(t),

4 lexeme(s),

5 lineNumber(l)

6 { assert(0 <= type && type < LT::numberOfTokens); }

This code is used in ¶236.

¶212

Page 87: Book

8.3. Token Class Template 73

〈Token class template method definitions〉213 ≡1 template <typename LT>

2 Token<LT>::Token(Token<LT> const& t) :

3 type(t.type),

4 lexeme(t.lexeme),

5 lineNumber(t.lineNumber)

6 { assert(0 <= type && type < LT::numberOfTokens); }

This code is used in ¶236.

¶213

〈Token class template method definitions〉214 ≡1 template <typename LT>

2 Token<LT>& Token<LT>::operator=(Token<LT> const& t)

3 {

4 type = t.type;

5 lexeme = t.lexeme;

6 lineNumber = t.lineNumber;

7 return *this;

8 }

This code is used in ¶236.

¶214

〈Token class template method definitions〉215 ≡1 template <typename LT>

2 string Token<LT>::getName() const

3 {

4 uint8_t i;

5 for (i = 0; i < LT::numberOfTokens; ++i)

6 if (LT::nameTable[i].type == type)

7 break;

8 assert(i < LT::numberOfTokens);

9 return LT::nameTable[i].name;

10 }

This code is used in ¶236.

¶215

〈Token class template method definitions〉216 ≡1 template <typename LT>

2 ostream& Token<LT>::put(ostream& s) const

3 {

4 s << "Token {" << getName();

5 s << ", \"" << EscapeSequence::encode(lexeme, ’"’) << "\"";

6 return s << ", " << lineNumber << "}";

7 }

This code is used in ¶236.

¶216

Page 88: Book

74 Chapter 8. Lexical Analyzer Classes

�8.4 TokenStream Class Template

8.4.1 Declarations

〈TokenStream class template forward declaration〉217 ≡1 template <typename T>

2 class TokenStream;

This code is used in ¶235.

¶217

〈TokenStream class template declaration〉218 ≡1 template <typename T>

2 class TokenStream

3 {

4 〈TokenStream class template type declarations〉2195 〈TokenStream class template field declarations〉2206 〈TokenStream class template method declarations〉221,2227 };

This code is used in ¶235.

¶218

〈TokenStream class template type declarations〉219 ≡1 public: typedef T Token;

2 public: typedef typename T::Tables Tables;

This code is used in ¶218.

¶219

〈TokenStream class template field declarations〉220 ≡1 private: Input<Token> inputStream;

This code is used in ¶218.

¶220

〈TokenStream class template method declarations〉221 ≡1 private: TableEntry getTransition(TableEntry, char) const;

2 private: bool isAcceptState(TableEntry) const;

3 private: bool isAcceptOrDiscardState(TableEntry) const;

4 private: bool isTailAnchored(TableEntry) const;

5 private: bool isHeadAnchored(TableEntry) const;

6 private: typename Token::Type getType(TableEntry) const;

This code is used in ¶218.

¶221

〈TokenStream class template method declarations〉222 ≡1 public: TokenStream(istream&);

2 public: TokenStream& operator>>(Token&);

This code is used in ¶218.

¶222

Page 89: Book

8.4. TokenStream Class Template 75

8.4.2 Definitions

〈TokenStream class template inline method definitions〉223 ≡1 template <typename T>

2 inline TokenStream<T>::TokenStream(istream& s) :

3 inputStream(s)

4 {}

This code is used in ¶235.

¶223

〈TokenStream class template method definitions〉224 ≡1 template <typename T>

2 TableEntry TokenStream<T>::getTransition(TableEntry state, char c) const

3 {

4 assert(state < Tables::numberOfStates);

5 if (static_cast<uint8_t>(c) < maximumCharacter)

6 return Tables::transitionTable[

7 Tables::rowMap[state] * Tables::numberOfColumns

8 + Tables::columnMap[c]];

9 else

10 return errorState;

11 }

This code is used in ¶236.

¶224

〈TokenStream class template method definitions〉225 ≡1 template <typename T>

2 bool TokenStream<T>::isAcceptState(TableEntry state) const

3 {

4 assert(state < Tables::numberOfStates);

5 return Tables::actionTable[state] == accept;

6 }

This code is used in ¶236.

¶225

〈TokenStream class template method definitions〉226 ≡1 template <typename T>

2 bool TokenStream<T>::isAcceptOrDiscardState(TableEntry state) const

3 {

4 assert(state < Tables::numberOfStates);

5 return (Tables::actionTable[state] & accept) != 0;

6 }

This code is used in ¶236.

¶226

〈TokenStream class template method definitions〉227 ≡1 template <typename T>

2 bool TokenStream<T>::isTailAnchored(TableEntry state) const

3 {

4 assert(state < Tables::numberOfStates);

5 return (Tables::anchorTable[state] & tail) != 0;

6 }

This code is used in ¶236.

¶227

Page 90: Book

76 Chapter 8. Lexical Analyzer Classes

〈TokenStream class template method definitions〉228 ≡1 template <typename T>

2 bool TokenStream<T>::isHeadAnchored(TableEntry state) const

3 {

4 assert(state < Tables::numberOfStates);

5 return (Tables::anchorTable[state] & head) != 0;

6 }

This code is used in ¶236.

¶228

〈TokenStream class template method definitions〉229 ≡1 template <typename T>

2 typename T::Type TokenStream<T>::getType(TableEntry state) const

3 {

4 assert(state < Tables::numberOfStates);

5 return Tables::tokenTable[state];

6 }

This code is used in ¶236.

¶229

Page 91: Book

8.4. TokenStream Class Template 77

〈TokenStream class template method definitions〉230 ≡1 template <typename T>

2 TokenStream<T>& TokenStream<T>::operator>>(T& result)

3 {

4 TableEntry currentState = 0;

5 TableEntry nextState = 0;

6 TableEntry acceptState = 0;

7 inputStream.markStart();

8 for (;;)

9 {

10 〈get the next character and compute nextState〉23111 currentState = nextState;

12 if (currentState != errorState)

13 {

14 〈normal transition processing〉23215 }

16 else

17 {

18 if (acceptState != 0)

19 {

20 〈return the last acceptable token〉23321 }

22 else

23 {

24 〈lexical error recovery〉23425 }

26 currentState = 0;

27 acceptState = 0;

28 inputStream.markStart();

29 }

30 }

31 }

This code is used in ¶236.

¶230

〈get the next character and compute nextState〉231 ≡1 if (inputStream)

2 {

3 char const nextChar = *inputStream;

4 ++inputStream;

5 nextState = getTransition(currentState, nextChar);

6 }

7 else if (acceptState != 0)

8 nextState = errorState;

9 else

10 {

11 result = T(Tables::end_of_file, "",

12 inputStream.getLineNumber());

13 return *this;

14 }

This code is used in ¶230.

¶231

Page 92: Book

78 Chapter 8. Lexical Analyzer Classes

〈normal transition processing〉232 ≡1 if(isAcceptOrDiscardState(currentState))

2 {

3 acceptState = currentState;

4 inputStream.markEnd();

5 }

This code is used in ¶230.

¶232

〈return the last acceptable token〉233 ≡1 inputStream.returnToEndMark();

2 if (isTailAnchored(acceptState))

3 --inputStream;

4 if (isHeadAnchored(acceptState))

5 inputStream.advanceStartMark();

6 if (isAcceptState(acceptState))

7 {

8 result = T(getType(acceptState),

9 inputStream.getLexeme(),

10 inputStream.getLineNumber());

11 return *this;

12 }

This code is used in ¶230.

¶233

〈lexical error recovery〉234 ≡1 inputStream.returnToStartMark();

2 char const nextChar = *inputStream;

3 cerr << "Lexis: Input error on line " <<

4 inputStream.getLineNumber() << ".";

5 cerr << " Input character ’" <<

6 EscapeSequence::encode(nextChar, ’\’’)

7 << "’ ignored.\n";

8 ++inputStream;

This code is used in ¶230.

¶234

Page 93: Book

8.5. Files 79

�8.5 Files

〈File: Lexis/Lexis.h〉235 ≡1 #if !defined(_Lexis_Lexis_h)

2 #define _Lexis_Lexis_h

3

4 #include <iostream>

5 #include <string>

6 #include <stdint.h>

7

8 namespace Lexis

9 {

10 〈Lexis type declarations〉175,17611 〈Lexis constants〉177,178

12

13 〈Anchor enumeration declaration〉17914 〈Anchor enumeration inline method definitions〉18015

16 〈Action enumeration declaration〉18117 〈Action enumeration inline method definitions〉18218

19 〈Input class template declaration〉18320 〈Input class template inline method definitions〉18921

22 〈TokenStream class template forward declaration〉21723

24 〈Token class template declaration〉20325 〈Token class template inline method definitions〉207–21026

27 〈TokenStream class template declaration〉21828 〈TokenStream class template inline method definitions〉22329 }

30

31 #include "Lexis/Lexis.cc"

32

33 #endif

¶235

Page 94: Book

80 Chapter 8. Lexical Analyzer Classes

〈File: Lexis/Lexis.cc〉236 ≡1 #if !defined(_Lexis_Lexis_cc)

2 #define _Lexis_Lexis_cc

3

4 #include <iostream.h>

5 #include <string>

6 #include "Lexis/Lexis.h"

7 #include "Toolbox/EscapeSequence.h"

8

9 using namespace Toolbox;

10

11 namespace Lexis

12 {

13 〈Input class template method definitions〉190–20214 〈Token class template method definitions〉211–21615 〈TokenStream class template method definitions〉224–23016 }

17

18 #endif

¶236

Page 95: Book
Page 96: Book

82 Chapter 9. Lexis Main Program

Chapter 9

Lexis Main Program

�9.1 main Method Definition

〈Lexis main method definition〉237 ≡1 using Lexis::NFA;

2 using Lexis::DFA;

3 using Lexis::CompressedDFA;

4 using Lexis::Token;

5 using Lexis::TokenStream;

6 using Lexis::ParserData;

7 using Lexis::ItemAttributes;

8 using LexisTokens::LexisTables;

9 using LexisGrammar::GramatikaTables;

10 using LexisGrammar::Parser;

11 using Gramatika::Value;

12 using Toolbox::PathName;

13

14 string const lexExtension(".lex");

15 string const tokenFileName("lexistab.h");

16 string const tableFileName("lexistab.cc");

17

18 int main(int argc, char* argv[])

19 {

20 if (argc != 2)

21 {

22 cerr << "Usage: lexis file[.lex]" << endl;

23 return 1;

24 }

25 〈read the Lexis input file〉23826 NFA* nfa = result.nfa;

27 if (nfa == 0)

28 cerr << "Null automaton." << endl;

29 else

30 {

31 〈compute the Lexis tables〉23932 〈write the Lexis output files〉24033 }

34 return 0;

35 }

This code is used in ¶241.

¶237

Page 97: Book

9.1. main Method Definition 83

〈read the Lexis input file〉238 ≡1 cerr << "Reading input file:";

2 PathName argument(argv[1]);

3 string lexFile;

4

5 if (argument.getExtension() == lexExtension)

6 lexFile = argument;

7 else

8 lexFile = argument + lexExtension;

9

10 cerr << " " << lexFile;

11 ifstream input(lexFile.c_str());

12 if (!input)

13 {

14 cerr << "Can’t open " << lexFile << endl;

15 return 1;

16 }

17 TokenStream<Token<LexisTables> > tin(input);

18 Parser<ParserData,Token<LexisTables>,ItemAttributes,GramatikaTables> parser;

19 Value<Token<LexisTables>,ItemAttributes> const result = parser.parse(tin);

20 cerr << "." << endl;

This code is used in ¶237.

¶238

〈compute the Lexis tables〉239 ≡1 cerr << "Computing tables:";

2 //cout << *nfa;

3 DFA dfa(*nfa);

4 delete nfa;

5 //cout << dfa;

6 dfa.minimize();

7 //cout << dfa;

8 CompressedDFA cdfa(dfa);

9 cerr << "." << endl;

This code is used in ¶237.

¶239

Page 98: Book

84 Chapter 9. Lexis Main Program

〈write the Lexis output files〉240 ≡1 cerr << "Writing output files:";

2 string const tokenFile = argument.getHead() + tokenFileName;

3

4 cerr << " " << tokenFile;

5 ofstream lexisdef(tokenFile.c_str());

6 if (!lexisdef)

7 {

8 cerr << "Can’t open " << tokenFile << endl;

9 return 1;

10 }

11 cdfa.putTokens(lexisdef);

12 string const tableFile = argument.getHead() + tableFileName;

13

14 cerr << " " << tableFile;

15 ofstream lexistbl(tableFile.c_str());

16 if (!lexistbl)

17 {

18 cerr << "Can’t open " << tableFile << endl;

19 return 1;

20 }

21 cdfa.putTables(lexistbl);

22 cerr << "." << endl;

This code is used in ¶237.

¶240

�9.2 File

〈File: Lexis/main.cc〉241 ≡1 #include <iostream>

2 #include <fstream>

3 #include <string>

4 #include "Lexis/Lexis.h"

5 #include "Lexis/CompressedDFA.h"

6 #include "Lexis/NFA.h"

7 #include "Lexis/DFA.h"

8 #include "Lexis/ParserData.h"

9 #include "Lexis/ItemAttributes.h"

10 #include "lexistab.h"

11 #include "Gramatika/Gramatika.h"

12 #include "gramadef.h"

13 #include "Toolbox/PathName.h"

14

15 〈Lexis main method definition〉237

¶241

Page 99: Book

Part II

Gramatika—The Program

Page 100: Book
Page 101: Book

Chapter 10

Gramatika Input

Specifications

�10.1 Gramatika Token Specifications

〈Gramatika auxiliary definitions〉242 ≡1 blank = ’ ’.

2 tab = ’\t’.

3 newline = ’\n’.

4 whitespace = blank | tab | newline.

5 letter = /A-Za-z/.

6 digit = /0-9/.

7 escapesequence = ’\\’ ?.

8 actionChar = /!’\\/ | escapesequence | newline.

This code is used in ¶244.

¶242

Page 102: Book

88 Chapter 10. Gramatika Input Specifications

〈Gramatika token definitions〉243 ≡1 action = ’‘‘’ { actionChar | ’\’’ actionChar } [’\’’] ’\’\’’.

2 colon = ’:’.

3 comma = ’,’.

4 equal = ’=’.

5 field = ’(’ { whitespace } ( letter | ’_’ )

6 { letter | digit | ’_’ } { whitespace } ’)’.

7 period = ’.’.

8 verticalBar = ’|’.

9 preamble = ’Preamble’.

10 terminals = ’Terminals’.

11 nonterminals = ’Nonterminals’.

12 start = ’Start’.

13 productions = ’Productions’.

14 postamble = ’Postamble’.

15 language = ’Language’.

16 stringT = ’"’ { /!"\\/ | ’\\"’ } ’"’.

17 identifier = letter { letter | digit }.

18 = whitespace { whitespace }.

19 = ’#’ { ? } $.

This code is used in ¶244.

¶243

10.1.1 File

〈File: Gramatika/Gramatika.lex〉244 ≡1 Language:

2 GramatikaTokens.

3 Definitions:

4 〈Gramatika auxiliary definitions〉2425 Tokens:

6 〈Gramatika token definitions〉243

¶244

�10.2 Gramatika Grammar Specification

〈Gramatika non-terminal definitions〉245 ≡1 file (symbolTable), leftHandSide (symbol), languagePart (language),

2 nonterminalDeclaration, nonterminalIdentifierList,

3 nonterminalsPart, postamblePart, preamblePart,

4 production (production), productionsPart,

5 rightHandSide (productionList), rule, ruleList,

6 startPart, symbol (symbol), symbolList (production),

7 terminalDeclaration, terminalIdentifierList, terminalsPart.

This code is used in ¶293.

¶245

Page 103: Book

10.2. Gramatika Grammar Specification 89

〈Gramatika start symbol definition〉246 ≡1 file.

This code is used in ¶293.

¶246

〈Gramatika grammar productions〉247 ≡1 file = languagePart preamblePart terminalsPart nonterminalsPart startPart

2 productionsPart postamblePart

3 ‘‘

4 〈file actions〉2755 ’’.

This code is used in ¶293.

¶247

〈Gramatika grammar productions〉248 ≡1 languagePart =

2 ‘‘

3 〈languagePart case 1 actions〉2764 ’’

5 | language colon identifier period

6 ‘‘

7 〈languagePart case 2 actions〉2778 ’’

9 .

This code is used in ¶293.

¶248

〈Gramatika grammar productions〉249 ≡1 preamblePart =

2 | preamble colon action period

3 ‘‘

4 〈preamblePart actions〉2785 ’’.

This code is used in ¶293.

¶249

〈Gramatika grammar productions〉250 ≡1 terminalsPart =

2 terminals colon period

3 | terminals colon stringT period

4 ‘‘

5 〈terminalsPart case 2 actions〉2796 ’’

7 | terminals colon terminalIdentifierList period.

This code is used in ¶293.

¶250

〈Gramatika grammar productions〉251 ≡1 terminalIdentifierList =

2 terminalDeclaration

3 | terminalIdentifierList comma terminalDeclaration.

This code is used in ¶293.

¶251

Page 104: Book

90 Chapter 10. Gramatika Input Specifications

〈Gramatika grammar productions〉252 ≡1 terminalDeclaration =

2 identifier

3 ‘‘

4 〈terminalDeclaration actions〉2805 ’’.

6 nonterminalsPart = nonterminals colon

7 nonterminalIdentifierList period.

This code is used in ¶293.

¶252

〈Gramatika grammar productions〉253 ≡1 nonterminalIdentifierList =

2 nonterminalDeclaration

3 | nonterminalIdentifierList comma nonterminalDeclaration.

This code is used in ¶293.

¶253

〈Gramatika grammar productions〉254 ≡1 nonterminalDeclaration =

2 identifier

3 ‘‘

4 〈nonterminalDeclaration case 1 actions〉2815 ’’

6 | identifier field

7 ‘‘

8 〈nonterminalDeclaration case 2 actions〉2829 ’’.

This code is used in ¶293.

¶254

〈Gramatika grammar productions〉255 ≡1 startPart = start colon identifier period

2 ‘‘

3 〈startPart actions〉2834 ’’.

This code is used in ¶293.

¶255

〈Gramatika grammar productions〉256 ≡1 productionsPart = productions colon ruleList.

This code is used in ¶293.

¶256

〈Gramatika grammar productions〉257 ≡1 ruleList =

2 rule

3 | ruleList rule.

This code is used in ¶293.

¶257

Page 105: Book

10.2. Gramatika Grammar Specification 91

〈Gramatika grammar productions〉258 ≡1 rule =

2 identifier equal rightHandSide period

3 ‘‘

4 〈rule actions〉2845 ’’.

This code is used in ¶293.

¶258

〈Gramatika grammar productions〉259 ≡1 rightHandSide =

2 production

3 ‘‘

4 〈rightHandSide case 1 actions〉2855 ’’

6 | rightHandSide verticalBar production

7 ‘‘

8 〈rightHandSide case 2 actions〉2869 ’’.

This code is used in ¶293.

¶259

〈Gramatika grammar productions〉260 ≡1 production =

2 symbolList

3 ‘‘

4 〈production case 1 actions〉2875 ’’

6 | symbolList action

7 ‘‘

8 〈production case 2 actions〉2889 ’’.

This code is used in ¶293.

¶260

〈Gramatika grammar productions〉261 ≡1 symbolList =

2 ‘‘

3 〈symbolList case 1 actions〉2894 ’’

5 | symbolList symbol

6 ‘‘

7 〈symbolList case 2 actions〉2908 ’’.

This code is used in ¶293.

¶261

Page 106: Book

92 Chapter 10. Gramatika Input Specifications

〈Gramatika grammar productions〉262 ≡1 symbol =

2 identifier

3 ‘‘

4 〈symbol case 1 actions〉2915 ’’.

This code is used in ¶293.

¶262

〈Gramatika grammar productions〉263 ≡1 postamblePart =

2 | postamble colon action period

3 ‘‘

4 〈symbol case 2 actions〉2925 ’’.

This code is used in ¶293.

¶263

10.2.1 ParserData Class

〈ParserData class declaration〉264 ≡1 class ParserData

2 {

3 〈ParserData class field declarations〉2654 〈ParserData class method declarations〉2665 };

This code is used in ¶268.

¶264

〈ParserData class field declarations〉265 ≡1 protected: SymbolTable* symbolTable;

This code is used in ¶264.

¶265

〈ParserData class method declarations〉266 ≡1 protected: ParserData();

This code is used in ¶264.

¶266

〈ParserData class inline method definitions〉267 ≡1 inline ParserData::ParserData() :

2 symbolTable(new SymbolTable())

3 {}

This code is used in ¶268.

¶267

Page 107: Book

10.2. Gramatika Grammar Specification 93

〈File: Gramatika/ParserData.h〉268 ≡1 #if !defined(_Gramatika_ParserData_h)

2 #define _Gramatika_ParserData_h

3

4 #include "Gramatika/SymbolTable.h"

5

6 namespace Gramatika

7 {

8 〈ParserData class declaration〉2649 〈ParserData class inline method definitions〉267

10 }

11

12 #endif

¶268

10.2.2 ItemAttributes Class

〈ItemAttributes class declaration〉269 ≡1 class ItemAttributes

2 {

3 〈ItemAttributes field declarations〉2704 〈ItemAttributes method declarations〉2715 };

This code is used in ¶273.

¶269

〈ItemAttributes field declarations〉270 ≡1 public: union

2 {

3 Production* production;

4 list<Production*>* productionList;

5 Symbol* symbol;

6 SymbolTable* symbolTable;

7 string* language;

8 };

This code is used in ¶269.

¶270

〈ItemAttributes method declarations〉271 ≡1 public: ItemAttributes();

This code is used in ¶269.

¶271

〈ItemAttributes class inline method definitions〉272 ≡1 inline ItemAttributes::ItemAttributes()

2 { symbolTable = 0; }

This code is used in ¶273.

¶272

Page 108: Book

94 Chapter 10. Gramatika Input Specifications

〈File: Gramatika/ItemAttributes.h〉273 ≡1 #if !defined(_Gramatika_ItemAttributes_h)

2 #define _Gramatika_ItemAttributes_h

3

4 #include <list>

5 #include <string>

6 #include "Gramatika/Symbol.h"

7 #include "Gramatika/Production.h"

8 #include "Gramatika/SymbolTable.h"

9

10 namespace Gramatika

11 {

12 〈ItemAttributes class declaration〉26913 〈ItemAttributes class inline method definitions〉27214 }

15

16 #endif

¶273

10.2.3 Actions

〈Gramatika actions preamble〉274 ≡1 #include <fstream>

2 #include <list>

3 #include <string>

4 #include "Gramatika/ParserData.h"

5 #include "Gramatika/ItemAttributes.h"

6 #include "Toolbox/PathName.h"

7

8 using Toolbox::PathName;

9

10 inline string stripQuotes(string const& s)

11 { return s.substr(2, s.size()-4); }

12 inline string stripParentheses(string const& s)

13 { return s.substr(1, s.size()-2); }

This code is used in ¶293.

¶274

〈file actions〉275 ≡1 string* language = $languagePart;

2 symbolTable->setLanguage(*language);

3 delete language;

4 $$ = symbolTable;

This code is used in ¶247.

¶275

〈languagePart case 1 actions〉276 ≡1 $$ = new string("UserDefined");

This code is used in ¶248.

¶276

Page 109: Book

10.2. Gramatika Grammar Specification 95

〈languagePart case 2 actions〉277 ≡1 $$ = new string($identifier.getLexeme());

This code is used in ¶248.

¶277

〈preamblePart actions〉278 ≡1 symbolTable->setPreamble(

2 stripQuotes($action.getLexeme()));

This code is used in ¶249.

¶278

〈terminalsPart case 2 actions〉279 ≡1 PathName terminalsFileName($stringT.getLexeme());

2 terminalsFileName = terminalsFileName.substr(

3 1,terminalsFileName.size()-2);

4

5 cerr << " (Reading terminals file: " << terminalsFileName;

6 ifstream terminalsFile(terminalsFileName.c_str());

7

8 symbolTable->getTerminalSymbols(terminalsFile);

9 cerr << ".)";

This code is used in ¶250.

¶279

〈terminalDeclaration actions〉280 ≡1 symbolTable->declareTerminal($identifier.getLexeme());

This code is used in ¶252.

¶280

〈nonterminalDeclaration case 1 actions〉281 ≡1 symbolTable->declareNonterminal($identifier.getLexeme());

This code is used in ¶254.

¶281

〈nonterminalDeclaration case 2 actions〉282 ≡1 symbolTable->declareNonterminal($identifier.getLexeme(),

2 stripParentheses($field.getLexeme()));

This code is used in ¶254.

¶282

〈startPart actions〉283 ≡1 symbolTable->declareStartSymbol($identifier.getLexeme());

This code is used in ¶255.

¶283

〈rule actions〉284 ≡1 assert($rightHandSide != 0);

2 symbolTable->declareProductions($identifier.getLexeme(),

3 *$rightHandSide);

4 delete $rightHandSide;

This code is used in ¶258.

¶284

Page 110: Book

96 Chapter 10. Gramatika Input Specifications

〈rightHandSide case 1 actions〉285 ≡1 list<Production*>* result = new list<Production*>();

2 result->push_back($production);

3 $$ = result;

This code is used in ¶259.

¶285

〈rightHandSide case 2 actions〉286 ≡1 assert($rightHandSide != 0);

2 $rightHandSide->push_back($production);

3 $$ = $rightHandSide;

This code is used in ¶259.

¶286

〈production case 1 actions〉287 ≡1 $$ = $symbolList;

This code is used in ¶260.

¶287

〈production case 2 actions〉288 ≡1 assert($symbolList != 0);

2 $symbolList->setAction(stripQuotes($action.getLexeme()));

3 $$ = $symbolList;

This code is used in ¶260.

¶288

〈symbolList case 1 actions〉289 ≡1 Production* result = new Production();

2 $$ = result;

This code is used in ¶261.

¶289

〈symbolList case 2 actions〉290 ≡1 assert($symbolList != 0);

2 $symbolList->add($symbol);

3 $$ = $symbolList;

This code is used in ¶261.

¶290

〈symbol case 1 actions〉291 ≡1 Symbol* result = symbolTable->getSymbol(

2 $identifier.getLexeme());

3 $$ = result;

This code is used in ¶262.

¶291

〈symbol case 2 actions〉292 ≡1 symbolTable->setPostamble(

2 stripQuotes($action.getLexeme()));

This code is used in ¶263.

¶292

Page 111: Book

10.2. Gramatika Grammar Specification 97

10.2.4 File

〈File: Gramatika/Gramatika.gtk〉293 ≡1 Language:

2 GramatikaGrammar.

3 Preamble:

4 ‘‘

5 〈Gramatika actions preamble〉2746 ’’.

7 Terminals:

8 "lexistab.h".

9 Nonterminals:

10 〈Gramatika non-terminal definitions〉24511 Start:

12 〈Gramatika start symbol definition〉24613 Productions:

14 〈Gramatika grammar productions〉247–263

¶293

Page 112: Book

98 Chapter 10. Gramatika Input Specifications

Page 113: Book

Chapter 11

Symbol Class

�11.1 Declarations

〈Symbol class declaration〉294 ≡1 class Symbol

2 {

3 〈Symbol class field declarations〉2954 〈Symbol class method declarations〉2965 };

This code is used in ¶317.

¶294

〈Symbol class field declarations〉295 ≡1 private: string name;

2 private: SymbolNumber number;

3 private: SymbolType type;

4 private: string field;

5 private: bool used;

6 private: bool defined;

7 private: list<Production*> productionList;

8 private: Set firstSet;

This code is used in ¶294.

¶295

Page 114: Book

100 Chapter 11. Symbol Class

〈Symbol class method declarations〉296 ≡1 public: Symbol(string const&, SymbolNumber,

2 SymbolType, string const&);

3 public: Symbol& setDefined();

4 public: Symbol& setUsed();

5 public: Symbol& setFirstSet(Set const&);

6 public: Symbol& append(list<Production*> const&);

7 public: SymbolNumber getNumber() const;

8 public: SymbolType getType() const;

9 public: string const& getName() const;

10 public: string const& getField() const;

11 public: list<Production*> const& getProductionList() const;

12 public: Set const& getFirstSet();

13 public: bool isUsed() const;

14 public: bool isDefined() const;

15 public: bool isTerminal() const;

16 public: bool isNonterminal() const;

17 public: bool isNullable() const;

18 public: Set getFirstClosure() const;

19 public: ostream& put(ostream&) const;

20 public: ostream& putActions(ostream&) const;

This code is used in ¶294.

¶296

�11.2 Definitions

〈Symbol class inline method definitions〉297 ≡1 inline Symbol& Symbol::setDefined()

2 { defined = true; return *this; }

This code is used in ¶317.

¶297

〈Symbol class inline method definitions〉298 ≡1 inline Symbol& Symbol::setUsed()

2 { used = true; return *this; }

This code is used in ¶317.

¶298

〈Symbol class inline method definitions〉299 ≡1 inline SymbolNumber Symbol::getNumber() const

2 { return number; }

This code is used in ¶317.

¶299

〈Symbol class inline method definitions〉300 ≡1 inline SymbolType Symbol::getType() const

2 { return type; }

This code is used in ¶317.

¶300

Page 115: Book

11.2. Definitions 101

〈Symbol class inline method definitions〉301 ≡1 inline string const& Symbol::getName() const

2 { return name; }

This code is used in ¶317.

¶301

〈Symbol class inline method definitions〉302 ≡1 inline string const& Symbol::getField() const

2 { return field; }

This code is used in ¶317.

¶302

〈Symbol class inline method definitions〉303 ≡1 inline list<Production*> const& Symbol::getProductionList() const

2 { return productionList; }

This code is used in ¶317.

¶303

〈Symbol class inline method definitions〉304 ≡1 inline Set const& Symbol::getFirstSet()

2 { return firstSet; }

This code is used in ¶317.

¶304

〈Symbol class inline method definitions〉305 ≡1 inline bool Symbol::isUsed() const

2 { return used; }

This code is used in ¶317.

¶305

〈Symbol class inline method definitions〉306 ≡1 inline bool Symbol::isDefined() const

2 { return defined; }

This code is used in ¶317.

¶306

〈Symbol class inline method definitions〉307 ≡1 inline bool Symbol::isTerminal() const

2 { return type == terminal; }

This code is used in ¶317.

¶307

〈Symbol class inline method definitions〉308 ≡1 inline bool Symbol::isNonterminal() const

2 { return type == nonterminal; }

This code is used in ¶317.

¶308

〈Symbol class inline method definitions〉309 ≡1 inline ostream& operator<<(ostream& stream, Symbol const& symbol)

2 { return symbol.put(stream); }

This code is used in ¶317.

¶309

Page 116: Book

102 Chapter 11. Symbol Class

〈Symbol class method definitions〉310 ≡1 Symbol::Symbol(string const& nam, SymbolNumber num,

2 SymbolType typ, string const& fld) :

3 name(nam),

4 number(num),

5 type(typ),

6 field(fld),

7 used(false),

8 defined(false),

9 productionList(),

10 firstSet()

11 {

12 if (type == terminal)

13 defined = true;

14 }

This code is used in ¶318.

¶310

〈Symbol class method definitions〉311 ≡1 Symbol& Symbol::setFirstSet(Set const& set)

2 {

3 firstSet = set;

4 return *this;

5 }

This code is used in ¶318.

¶311

〈Symbol class method definitions〉312 ≡1 Symbol& Symbol::append(list<Production*> const& list)

2 {

3 List::appendTo(productionList, list);

4 return *this;

5 }

This code is used in ¶318.

¶312

〈Symbol class method definitions〉313 ≡1 bool Symbol::isNullable() const

2 {

3 return type == nonterminal &&

4 firstSet.contains(epsilon);

5 }

This code is used in ¶318.

¶313

Page 117: Book

11.2. Definitions 103

〈Symbol class method definitions〉314 ≡1 Set Symbol::getFirstClosure() const

2 {

3 Set result(firstSet);

4 if (type == terminal)

5 result += number;

6 else

7 {

8 for (list<Production*>::const_iterator p(productionList.begin()),

9 lim(productionList.end()); p != lim; ++p)

10 {

11 Production const* const prod = *p;

12 result |= prod->getFirstClosure();

13 }

14 }

15 return result;

16 }

This code is used in ¶318.

¶314

〈Symbol class method definitions〉315 ≡1 ostream& Symbol::put(ostream& s) const

2 {

3 s << "Symbol {" << name;

4 s << ", " << number;

5 s << ", " << type;

6 s << ", " << field;

7 s << ", used=" << used;

8 s << ",\n defined=" << defined;

9 s << ", firstSet=" << firstSet;

10 s << "\n";

11 for (list<Production*>::const_iterator p(productionList.begin()),

12 lim(productionList.end()); p != lim; ++p)

13 s << " " << **p << "\n";

14 s << "}\n";

15 return s;

16 }

This code is used in ¶318.

¶315

〈Symbol class method definitions〉316 ≡1 ostream& Symbol::putActions(ostream& s) const

2 {

3 for (list<Production*>::const_iterator p(productionList.begin()),

4 lim(productionList.end()); p != lim; ++p)

5 (*p)->putAction(s);

6 return s;

7 }

This code is used in ¶318.

¶316

Page 118: Book

104 Chapter 11. Symbol Class

�11.3 Files

〈File: Gramatika/Symbol.h〉317 ≡1 #if !defined(symbol_h)

2 #define symbol_h

3

4 #include <iostream>

5 #include <vector>

6 #include <list>

7 #include <string>

8 #include "Toolbox/Set.h"

9 #include "Gramatika/Production.h"

10

11 using namespace Toolbox;

12

13 namespace Gramatika

14 {

15 〈Symbol class declaration〉29416 〈Symbol class inline method definitions〉297–30917 }

18

19 #endif

¶317

〈File: Gramatika/Symbol.cc〉318 ≡1 #include <iostream>

2 #include <string>

3 #include "Gramatika/Symbol.h"

4 #include "Toolbox/EscapeSequence.h"

5 #include "Toolbox/List.h"

6

7 using namespace Toolbox;

8

9 namespace Gramatika

10 {

11 〈Symbol class method definitions〉310–31612 }

¶318

Page 119: Book

Chapter 12

Production Class

�12.1 Declarations

〈Production class declaration〉319 ≡1 class Production

2 {

3 〈Production class field declarations〉3204 〈Production class method declarations〉321,3225 };

This code is used in ¶340.

¶319

〈Production class field declarations〉320 ≡1 private: ProductionNumber number;

2 private: Symbol* leftHandSide;

3 private: vector<Symbol*> rightHandSide;

4 private: string action;

This code is used in ¶319.

¶320

〈Production class method declarations〉321 ≡1 private: ProductionIndex getPosition(

2 string const&, ProductionIndex) const;

This code is used in ¶319.

¶321

Page 120: Book

106 Chapter 12. Production Class

〈Production class method declarations〉322 ≡1 public: Production();

2 public: ~Production();

3 public: void setNumber(ProductionNumber);

4 public: void setLeftHandSide(Symbol*);

5 public: void setAction(string const&);

6 public: void add(Symbol*);

7 public: ProductionNumber getNumber() const;

8 public: Symbol* getLeftHandSide() const;

9 public: Symbol* getRightHandSide(ProductionIndex) const;

10 public: ProductionIndex getLength() const;

11 public: Set getFirstClosure() const;

12 public: ostream& put(ostream&) const;

13 public: ostream& putAction(ostream&) const;

This code is used in ¶319.

¶322

�12.2 Definitions

〈Production class inline method definitions〉323 ≡1 inline void Production::setNumber(ProductionNumber n)

2 { number = n; }

This code is used in ¶340.

¶323

〈Production class inline method definitions〉324 ≡1 inline void Production::setLeftHandSide(Symbol* symbol)

2 { leftHandSide = symbol; }

This code is used in ¶340.

¶324

〈Production class inline method definitions〉325 ≡1 inline void Production::setAction(string const& s)

2 { action = s; }

This code is used in ¶340.

¶325

〈Production class inline method definitions〉326 ≡1 inline ProductionNumber Production::getNumber() const

2 { return number; }

This code is used in ¶340.

¶326

〈Production class inline method definitions〉327 ≡1 inline Symbol* Production::getLeftHandSide() const

2 { return leftHandSide; }

This code is used in ¶340.

¶327

Page 121: Book

12.2. Definitions 107

〈Production class inline method definitions〉328 ≡1 inline ProductionIndex Production::getLength() const

2 { return rightHandSide.size(); }

This code is used in ¶340.

¶328

〈Production class inline method definitions〉329 ≡1 inline ostream& operator<<(ostream& stream, Production const& production)

2 { return production.put(stream); }

This code is used in ¶340.

¶329

〈Production class method definitions〉330 ≡1 Production::Production() :

2 number(0),

3 leftHandSide(0),

4 rightHandSide(0),

5 action()

6 {}

This code is used in ¶341.

¶330

〈Production class method definitions〉331 ≡1 Production::~Production()

2 {}

This code is used in ¶341.

¶331

〈Production class method definitions〉332 ≡1 ProductionIndex Production::getPosition(

2 string const& name, ProductionIndex instance) const

3 {

4 ProductionIndex count = 0;

5 for (ProductionIndex i = 0; i < rightHandSide.size(); ++i)

6 {

7 if (rightHandSide[i]->getName() == name)

8 {

9 count += 1;

10 if (count == instance)

11 return i;

12 }

13 }

14 cerr << "Gramatika: $" << name << "#" << static_cast<uint16_t>(instance)

15 << " not found.\n";

16 return rightHandSide.size();

17 }

This code is used in ¶341.

¶332

Page 122: Book

108 Chapter 12. Production Class

〈Production class method definitions〉333 ≡1 Symbol* Production::getRightHandSide(ProductionIndex i) const

2 {

3 assert(i < rightHandSide.size());

4 return rightHandSide[i];

5 }

This code is used in ¶341.

¶333

〈Production class method definitions〉334 ≡1 void Production::add(Symbol* symbol)

2 {

3 assert(symbol != 0);

4 rightHandSide.push_back(symbol);

5 symbol->setUsed();

6 }

This code is used in ¶341.

¶334

〈Production class method definitions〉335 ≡1 Set Production::getFirstClosure() const

2 {

3 Set result;

4 if (rightHandSide.size() == 0)

5 result += epsilon;

6 else

7 {

8 for (ProductionIndex i = 0; i < rightHandSide.size(); ++i)

9 {

10 result |= rightHandSide[i]->getFirstSet();

11 if (!rightHandSide[i]->isNullable())

12 break;

13 }

14 }

15 return result;

16 }

This code is used in ¶341.

¶335

〈Production class method definitions〉336 ≡1 ostream& Production::put(ostream& s) const

2 {

3 assert(leftHandSide != 0);

4 s << leftHandSide->getName() << " ->";

5 for (ProductionIndex i = 0; i < rightHandSide.size(); ++i)

6 s << " " << rightHandSide[i]->getName();

7 s << ". (" << number << ")";

8 return s;

9 }

This code is used in ¶341.

¶336

Page 123: Book

12.2. Definitions 109

〈Production class method definitions〉337 ≡1 ostream& Production::putAction(ostream& s) const

2 {

3 s << " case " << number << ":\n";

4 s << "\t{\n";

5 string::const_iterator ptr(action.begin()), lim(action.end());

6 while (ptr != lim)

7 {

8 if (*ptr != ’$’)

9 {

10 s << *ptr;

11 ++ptr;

12 }

13 else

14 {

15 ++ptr;

16 if (ptr == lim)

17 s << ’$’;

18 else if (*ptr == ’$’)

19 {

20 〈process $$〉33821 }

22 else

23 {

24 〈process $identifier〉33925 }

26 }

27 }

28 s << "\n";

29 s << "\t}\n";

30 s << "\tbreak;\n";

31 return s;

32 }

This code is used in ¶341.

¶337

〈process $$〉338 ≡1 s << "resultValue" << leftHandSide->getField();

2 ++ptr;

This code is used in ¶337.

¶338

Page 124: Book

110 Chapter 12. Production Class

〈process $identifier〉339 ≡1 string name;

2 for ( ; ptr != lim; ++ptr)

3 {

4 if (!isalpha(*ptr)) break;

5 name += *ptr;

6 }

7 ProductionIndex instance = 0;

8 if (ptr != lim && *ptr == ’#’)

9 {

10 ++ptr;

11 for ( ; ptr != lim; ++ptr)

12 {

13 if (!isdigit(*ptr)) break;

14 instance = 10 * instance + *ptr - ’0’;

15 }

16 }

17 else

18 instance = 1;

19 ProductionIndex const i =

20 getPosition(name, instance);

21 s << "stack["

22 << static_cast<uint16_t>(rightHandSide.size()

23 - (i + 1))

24 << "]";

25 s << rightHandSide[i]->getField();

This code is used in ¶337.

¶339

Page 125: Book

12.3. Files 111

�12.3 Files

〈File: Gramatika/Production.h〉340 ≡1 #if !defined(_Gramatika_Production_h)

2 #define _Gramatika_Production_h

3

4 #include <iostream>

5 #include <vector>

6 #include <string>

7 #include "Gramatika/Gramatika.h"

8 #include "Toolbox/Set.h"

9

10 using namespace Toolbox;

11

12 namespace Gramatika

13 {

14 class Symbol;

15 〈Production class declaration〉31916 〈Production class inline method definitions〉323–32917 }

18

19 #endif

¶340

〈File: Gramatika/Production.cc〉341 ≡1 #include <iostream>

2 #include <string>

3 #include <ctype.h>

4 #include "Gramatika/Production.h"

5 #include "Gramatika/Symbol.h"

6 #include "Toolbox/fixwarn.h"

7

8 namespace Gramatika

9 {

10 〈Production class method definitions〉330–33711 }

¶341

Page 126: Book

112 Chapter 12. Production Class

Page 127: Book

Chapter 13

SymbolTable Class

�13.1 Declarations

〈SymbolTable class declaration〉342 ≡1 class SymbolTable

2 {

3 〈SymbolTable class field declarations〉3434 〈SymbolTable class method declarations〉344,3455 };

This code is used in ¶371.

¶342

〈SymbolTable class field declarations〉343 ≡1 private: SymbolNumber numberOfTerminals;

2 private: SymbolNumber numberOfNonterminals;

3 private: HashTable<string,Symbol*> hashTable;

4 private: vector<Symbol*> symbolTable;

5 private: vector<Production*> productionTable;

6 private: string preamble;

7 private: string postamble;

8 private: Symbol* startSymbol;

9 private: string language;

This code is used in ¶342.

¶343

〈SymbolTable class method declarations〉344 ≡1 private: void declareSymbol(

2 SymbolType, string const&, string const&, SymbolNumber = 0);

This code is used in ¶342.

¶344

Page 128: Book

114 Chapter 13. SymbolTable Class

〈SymbolTable class method declarations〉345 ≡1 public: SymbolTable();

2 public: ~SymbolTable();

3 public: void setPreamble(string const&);

4 public: void setPostamble(string const&);

5 public: void setLanguage(string const&);

6 public: void declareStartSymbol(string const&);

7 public: void declareTerminal(string const&, SymbolNumber = 0);

8 public: void declareNonterminal(string const&);

9 public: void declareNonterminal(string const&, string const&);

10 public: void declareProductions(string const&, list<Production*> const&);

11 public: void getTerminalSymbols(istream&);

12 public: void computeFirstSets();

13 public: Symbol* getSymbol(string const&) const;

14 public: Symbol* getStartSymbol() const;

15 public: Symbol const& operator[](SymbolNumber) const;

16 public: SymbolNumber getNumberOfSymbols() const;

17 public: ProductionNumber getNumberOfProductions() const;

18 public: string const& getPreamble() const;

19 public: string const& getPostamble() const;

20 public: string const& getLanguage() const;

21 public: ostream& put(ostream&) const;

22 public: ostream& putActions(ostream&) const;

23 public: ostream& putPreamble(ostream&) const;

This code is used in ¶342.

¶345

�13.2 Definitions

〈SymbolTable class inline method definitions〉346 ≡1 inline void SymbolTable::setPreamble(string const& s)

2 { preamble = s; }

This code is used in ¶371.

¶346

〈SymbolTable class inline method definitions〉347 ≡1 inline void SymbolTable::setPostamble(string const& s)

2 { postamble = s; }

This code is used in ¶371.

¶347

〈SymbolTable class inline method definitions〉348 ≡1 inline void SymbolTable::setLanguage(string const& s)

2 { language = s; }

This code is used in ¶371.

¶348

〈SymbolTable class inline method definitions〉349 ≡1 inline Symbol* SymbolTable::getStartSymbol() const

2 { return startSymbol; }

This code is used in ¶371.

¶349

Page 129: Book

13.2. Definitions 115

〈SymbolTable class inline method definitions〉350 ≡1 inline SymbolNumber SymbolTable::getNumberOfSymbols() const

2 { return symbolTable.size(); }

This code is used in ¶371.

¶350

〈SymbolTable class inline method definitions〉351 ≡1 inline ProductionNumber SymbolTable::getNumberOfProductions() const

2 { return productionTable.size(); }

This code is used in ¶371.

¶351

〈SymbolTable class inline method definitions〉352 ≡1 inline string const& SymbolTable::getPreamble() const

2 { return preamble; }

This code is used in ¶371.

¶352

〈SymbolTable class inline method definitions〉353 ≡1 inline string const& SymbolTable::getPostamble() const

2 { return postamble; }

This code is used in ¶371.

¶353

〈SymbolTable class inline method definitions〉354 ≡1 inline string const& SymbolTable::getLanguage() const

2 { return language; }

This code is used in ¶371.

¶354

〈SymbolTable class inline method definitions〉355 ≡1 inline ostream& operator<<(ostream& stream, SymbolTable const& symtbl)

2 { return symtbl.put(stream); }

This code is used in ¶371.

¶355

〈SymbolTable class method definitions〉356 ≡1 SymbolTable::SymbolTable() :

2 numberOfTerminals(0),

3 numberOfNonterminals(0),

4 hashTable(127),

5 symbolTable(),

6 productionTable(),

7 preamble(),

8 postamble(),

9 startSymbol(0)

10 {

11 declareTerminal("_epsilon", epsilon);

12 declareTerminal("end_of_file", end_of_file);

13 }

This code is used in ¶372.

¶356

Page 130: Book

116 Chapter 13. SymbolTable Class

〈SymbolTable class method definitions〉357 ≡1 SymbolTable::~SymbolTable()

2 {

3 for (SymbolNumber s = 0; s < symbolTable.size(); ++s)

4 delete symbolTable[s];

5 for (ProductionNumber p = 0;

6 p < productionTable.size(); ++p)

7 delete productionTable[p];

8 }

This code is used in ¶372.

¶357

〈SymbolTable class method definitions〉358 ≡1 Symbol const& SymbolTable::operator[](SymbolNumber i) const

2 {

3 assert(i < symbolTable.size());

4 return *symbolTable[i];

5 }

This code is used in ¶372.

¶358

〈SymbolTable class method definitions〉359 ≡1 void SymbolTable::declareStartSymbol(string const& name)

2 {

3 HashTable<string,Symbol*>::const_iterator ptr(hashTable.find(name)), null;

4 if (ptr != null)

5 {

6 Production* const production = new Production();

7 production->add(*ptr);

8 list<Production*>* const productionList =

9 new list<Production*>();

10 productionList->push_back(production);

11 declareNonterminal("_start", "default");

12 ptr = hashTable.find("_start");

13 assert(ptr != null);

14 startSymbol = *ptr;

15 declareProductions("_start", *productionList);

16 delete productionList;

17 }

18 else

19 cerr << "Error: " << name << " not declared.\n";

20 }

This code is used in ¶372.

¶359

Page 131: Book

13.2. Definitions 117

〈SymbolTable class method definitions〉360 ≡1 void SymbolTable::declareSymbol(

2 SymbolType type, string const& name,

3 string const& field, SymbolNumber number)

4 {

5 HashTable<string,Symbol*>::const_iterator

6 ptr(hashTable.find(name)), null;

7 if (ptr != null)

8 cerr << "Error: " << name << " multiply defined.\n";

9 else

10 {

11 if (number != 0)

12 assert(number == symbolTable.size());

13 Symbol* symbol =

14 new Symbol(name, symbolTable.size(), type, field);

15 symbolTable.push_back(symbol);

16 hashTable.add(name, symbol);

17 }

18 }

This code is used in ¶372.

¶360

〈SymbolTable class method definitions〉361 ≡1 void SymbolTable::declareTerminal(string const& name, SymbolNumber number)

2 {

3 declareSymbol(terminal, name, ".terminal", number);

4 numberOfTerminals += 1;

5 }

This code is used in ¶372.

¶361

〈SymbolTable class method definitions〉362 ≡1 void SymbolTable::declareNonterminal(string const& name)

2 { declareNonterminal(name, "_default"); }

This code is used in ¶372.

¶362

〈SymbolTable class method definitions〉363 ≡1 void SymbolTable::declareNonterminal(string const& name, string const& fld)

2 {

3 string attributesField(".");

4 attributesField += fld;

5 declareSymbol(

6 nonterminal, name, attributesField);

7 numberOfNonterminals += 1;

8 }

This code is used in ¶372.

¶363

Page 132: Book

118 Chapter 13. SymbolTable Class

〈SymbolTable class method definitions〉364 ≡1 void SymbolTable::declareProductions(

2 string const& name, list<Production*> const& productionList)

3 {

4 HashTable<string,Symbol*>::const_iterator

5 ptr(hashTable.find(name)), null;

6 if (ptr == null)

7 cerr << "Error: " << name << " not declared.\n";

8 else

9 {

10 Symbol* const symbol = *ptr;

11 assert(symbol != 0);

12 assert(productionTable.size() != 0 ||

13 symbol == startSymbol);

14 for (list<Production*>::const_iterator p(productionList.begin()),

15 lim(productionList.end()); p != lim; ++p)

16 {

17 Production* const production = *p;

18 production->setNumber(productionTable.size());

19 production->setLeftHandSide(symbol);

20 productionTable.push_back(production);

21 }

22 symbol->append(productionList);

23 symbol->setDefined();

24 }

25 }

This code is used in ¶372.

¶364

Page 133: Book

13.2. Definitions 119

〈SymbolTable class method definitions〉365 ≡1 void SymbolTable::getTerminalSymbols(istream& s)

2 {

3 uint8_t count = 0;

4 for (;;)

5 {

6 string word;

7

8 s >> word;

9 if (word == string("{"))

10 {

11 if (++count == 3)

12 break;

13 }

14 }

15 for (;;)

16 {

17 string name;

18 string word;

19 SymbolNumber number;

20

21 s >> name >> word >> number;

22 if (name != string("_epsilon") && name != string("end_of_file"))

23 declareTerminal(name, number);

24 s >> word;

25 if (word == string("};"))

26 break;

27 }

28 }

This code is used in ¶372.

¶365

〈SymbolTable class method definitions〉366 ≡1 Symbol* SymbolTable::getSymbol(string const& name) const

2 {

3 HashTable<string,Symbol*>::const_iterator

4 ptr(hashTable.find(name)), null;

5 assert(ptr != null);

6 return *ptr;

7 }

This code is used in ¶372.

¶366

Page 134: Book

120 Chapter 13. SymbolTable Class

〈SymbolTable class method definitions〉367 ≡1 void SymbolTable::computeFirstSets()

2 {

3 bool modified = false;

4 do

5 {

6 modified = false;

7 for (SymbolNumber i = 0;

8 i < symbolTable.size(); ++i)

9 {

10 Set newValue;

11 newValue = symbolTable[i]->getFirstClosure();

12 if (symbolTable[i]->getFirstSet() != newValue)

13 {

14 symbolTable[i]->setFirstSet(newValue);

15 modified = true;

16 }

17 }

18 }

19 while (modified);

20 }

This code is used in ¶372.

¶367

〈SymbolTable class method definitions〉368 ≡1 ostream& SymbolTable::put(ostream& s) const

2 {

3 s << "preamble = \"" << EscapeSequence::encode(preamble, ’"’)

4 << "\"\n";

5 s << "postamble = \"" << EscapeSequence::encode(postamble, ’"’)

6 << "\"\n";

7 s << "startSymbol = " << startSymbol->getName() << "\n";

8 for (SymbolNumber i = 0; i < symbolTable.size(); ++i)

9 s << *symbolTable[i];

10 return s;

11 }

This code is used in ¶372.

¶368

Page 135: Book

13.2. Definitions 121

〈SymbolTable class method definitions〉369 ≡1 ostream& SymbolTable::putActions(ostream& s) const

2 {

3 s <<

4 "#if !defined(gramactn_c)\n"

5 "#define gramactn_c\n"

6 "\n"

7 "#include \"gramadef.h\"\n"

8 "\n"

9 "namespace " << language << "\n"

10 "{\n"

11 "template <typename B, typename T, typename A, typename GT>\n"

12 "bool Parser<B,T,A,GT>::performAction"

13 "(ProductionNumber production)\n"

14 "{\n"

15 " switch (production)\n"

16 " {\n";

17 for (ProductionNumber i = 0; i < productionTable.size(); ++i)

18 productionTable[i]->putAction(s);

19 s <<

20 " default: break;\n"

21 " }\n"

22 " return true;\n"

23 "}\n"

24 "\n"

25 "}\n";

26 s << postamble << "\n";

27 s << "#endif\n";

28 return s;

29 }

This code is used in ¶372.

¶369

Page 136: Book

122 Chapter 13. SymbolTable Class

〈SymbolTable class method definitions〉370 ≡1 ostream& SymbolTable::putPreamble(ostream& s) const

2 {

3 s <<

4 "#if !defined(gramadef_h)\n"

5 "#define gramadef_h\n"

6 "\n"

7 "#include \"Gramatika/Gramatika.h\"\n"

8 "\n";

9 s << preamble << "\n";

10 s <<

11 "namespace " << language << "\n"

12 "{\n"

13 " using namespace Gramatika;\n"

14 " class GramatikaTables\n"

15 " {\n"

16 " public:\n"

17 "\tstatic StateNumber const numberOfStates;\n"

18 "\tstatic MapEntry const actionMap[];\n"

19 "\tstatic TableEntry const actionTable[];\n"

20 "\tstatic MapEntry const gotoMap[];\n"

21 "\tstatic TableEntry const gotoTable[];\n"

22 "\tstatic ProductionNumber const numberOfProductions;\n"

23 "\tstatic SymbolNumber const leftHandSideTable[];\n"

24 "\tstatic ProductionIndex const rightHandSideTable[];\n"

25 " };\n\n"

26 " template <typename B, typename T, typename A, typename GT>\n"

27 " class Parser : public B, public Gramatika::Parser<T,A,GT>\n"

28 " {\n"

29 " protected:\n"

30 "\tbool performAction(ProductionNumber);\n"

31 " };\n"

32 "\n"

33 "}\n"

34 "\n"

35 "#include \"gramactn.cc\"\n"

36 "\n"

37 "#endif\n";

38 return s;

39 }

This code is used in ¶372.

¶370

Page 137: Book

13.3. Files 123

�13.3 Files

〈File: Gramatika/SymbolTable.h〉371 ≡1 #if !defined(_Gramatika_SymbolTable_h)

2 #define _Gramatika_SymbolTable_h

3

4 #include <iostream>

5 #include <vector>

6 #include <list>

7 #include <string>

8 #include "Gramatika/Symbol.h"

9 #include "Toolbox/HashTable.h"

10 #include "Toolbox/String.h"

11

12 using namespace Toolbox;

13 using namespace Toolbox::String;

14

15 namespace Gramatika

16 {

17 〈SymbolTable class declaration〉34218 〈SymbolTable class inline method definitions〉346–35519 }

20

21 #endif

¶371

〈File: Gramatika/SymbolTable.cc〉372 ≡1 #include <iostream>

2 #include <string>

3 #include "Gramatika/Gramatika.h"

4 #include "Gramatika/SymbolTable.h"

5 #include "Toolbox/EscapeSequence.h"

6

7 namespace Gramatika

8 {

9 〈SymbolTable class method definitions〉356–37010 }

¶372

Page 138: Book

124 Chapter 13. SymbolTable Class

Page 139: Book

Chapter 14

Item Class

�14.1 Declarations

〈Item class declaration〉373 ≡1 class Item

2 {

3 〈Item class field declarations〉3744 〈Item class method declarations〉3755 };

This code is used in ¶400.

¶373

〈Item class field declarations〉374 ≡1 private: Production const* production;

2 private: ProductionIndex dotPosition;

3 private: Set lookAheadSet;

This code is used in ¶373.

¶374

Page 140: Book

126 Chapter 14. Item Class

〈Item class method declarations〉375 ≡1 public: Item();

2 public: explicit Item(Production const&);

3 public: Item(Item const&);

4 public: Item& operator=(Item const&);

5 public: Production const* getProduction() const;

6 public: ProductionNumber getProductionNumber() const;

7 public: Symbol* getLeftHandSide() const;

8 public: Symbol* getRightHandSide(ProductionIndex) const;

9 public: ProductionIndex getDotPosition() const;

10 public: ProductionIndex getLength() const;

11 public: Set const& getLookAheadSet() const;

12 public: int compare(Item const&) const;

13 public: bool operator==(Item const&) const;

14 public: bool operator<=(Item const&) const;

15 public: bool isMoreToRightOfDot() const;

16 public: void advanceDot();

17 public: void addLookAhead(Set const&);

18 public: bool mergeLookAhead(Item const&);

19 public: Symbol* getDotSymbol() const;

20 public: SymbolNumber getDotSymbolNumber() const;

21 public: Set getFirstSet(ProductionIndex) const;

22 public: bool isNullable(ProductionIndex) const;

23 public: ostream& put(ostream&) const;

This code is used in ¶373.

¶375

�14.2 Definitions

〈Item class inline method definitions〉376 ≡1 inline Production const* Item::getProduction() const

2 { return production; }

This code is used in ¶400.

¶376

〈Item class inline method definitions〉377 ≡1 inline ProductionNumber Item::getProductionNumber() const

2 { return production->getNumber(); }

This code is used in ¶400.

¶377

〈Item class inline method definitions〉378 ≡1 inline Symbol* Item::getLeftHandSide() const

2 { return production->getLeftHandSide(); }

This code is used in ¶400.

¶378

〈Item class inline method definitions〉379 ≡1 inline Symbol* Item::getRightHandSide(ProductionIndex i) const

2 { return production->getRightHandSide(i); }

This code is used in ¶400.

¶379

Page 141: Book

14.2. Definitions 127

〈Item class inline method definitions〉380 ≡1 inline ProductionIndex Item::getDotPosition() const

2 { return dotPosition; }

This code is used in ¶400.

¶380

〈Item class inline method definitions〉381 ≡1 inline ProductionIndex Item::getLength() const

2 { return production->getLength(); }

This code is used in ¶400.

¶381

〈Item class inline method definitions〉382 ≡1 inline Set const& Item::getLookAheadSet() const

2 { return lookAheadSet; }

This code is used in ¶400.

¶382

〈Item class inline method definitions〉383 ≡1 inline bool Item::operator==(Item const& i) const

2 { return compare(i) == 0; }

This code is used in ¶400.

¶383

〈Item class inline method definitions〉384 ≡1 inline bool Item::operator<=(Item const& i) const

2 { return compare(i) <= 0; }

This code is used in ¶400.

¶384

〈Item class inline method definitions〉385 ≡1 inline bool Item::isMoreToRightOfDot() const

2 { return dotPosition < getLength(); }

This code is used in ¶400.

¶385

〈Item class inline method definitions〉386 ≡1 inline ostream& operator<<(ostream& stream, Item const& item)

2 { return item.put(stream); }

This code is used in ¶400.

¶386

〈Item class method definitions〉387 ≡1 Item::Item() :

2 production(0),

3 dotPosition(0),

4 lookAheadSet()

5 {}

This code is used in ¶401.

¶387

Page 142: Book

128 Chapter 14. Item Class

〈Item class method definitions〉388 ≡1 Item::Item(Production const& p) :

2 production(&p),

3 dotPosition(0),

4 lookAheadSet()

5 {}

This code is used in ¶401.

¶388

〈Item class method definitions〉389 ≡1 Item::Item(Item const& item) :

2 production(item.production),

3 dotPosition(item.dotPosition),

4 lookAheadSet(item.lookAheadSet)

5 {}

This code is used in ¶401.

¶389

〈Item class method definitions〉390 ≡1 Item& Item::operator=(Item const& item)

2 {

3 production = item.production;

4 dotPosition = item.dotPosition;

5 lookAheadSet = item.lookAheadSet;

6 return *this;

7 }

This code is used in ¶401.

¶390

〈Item class method definitions〉391 ≡1 int Item::compare(Item const& item) const

2 {

3 assert(production != 0);

4 int result =

5 getDotSymbolNumber() - item.getDotSymbolNumber();

6 if (result != 0)

7 return result;

8 result = getProductionNumber() -

9 item.getProductionNumber();

10 if (result != 0)

11 return result;

12 return dotPosition - item.dotPosition;

13 }

This code is used in ¶401.

¶391

Page 143: Book

14.2. Definitions 129

〈Item class method definitions〉392 ≡1 void Item::advanceDot()

2 {

3 assert(production != 0);

4 dotPosition += 1;

5 assert(dotPosition <= getLength());

6 }

This code is used in ¶401.

¶392

〈Item class method definitions〉393 ≡1 void Item::addLookAhead(Set const& set)

2 {

3 assert(production != 0);

4 lookAheadSet |= set;

5 }

This code is used in ¶401.

¶393

〈Item class method definitions〉394 ≡1 bool Item::mergeLookAhead(Item const& i)

2 {

3 assert(*this == i);

4 if (i.lookAheadSet <= lookAheadSet)

5 return false;

6 lookAheadSet |= i.lookAheadSet;

7 return true;

8 }

This code is used in ¶401.

¶394

〈Item class method definitions〉395 ≡1 Symbol* Item::getDotSymbol() const

2 {

3 assert(production != 0);

4 if (dotPosition < getLength())

5 return getRightHandSide(dotPosition);

6 else

7 return 0;

8 }

This code is used in ¶401.

¶395

Page 144: Book

130 Chapter 14. Item Class

〈Item class method definitions〉396 ≡1 SymbolNumber Item::getDotSymbolNumber() const

2 {

3 assert(production != 0);

4 Symbol* const symbol = getDotSymbol();

5 if (symbol != 0)

6 return symbol->getNumber();

7 else

8 return 0;

9 }

This code is used in ¶401.

¶396

〈Item class method definitions〉397 ≡1 Set Item::getFirstSet(ProductionIndex lookAhead) const

2 {

3 assert(production != 0);

4 ProductionIndex const position =

5 dotPosition + lookAhead;

6 Set result;

7 if (position >= getLength())

8 result += epsilon;

9 else

10 {

11 for (ProductionIndex i = position;

12 i < getLength(); ++i)

13 {

14 Symbol* const symbol = getRightHandSide(i);

15 if (symbol->isTerminal())

16 result += symbol->getNumber();

17 else

18 result |= symbol->getFirstSet();

19 if (!symbol->isNullable())

20 break;

21 }

22 }

23 return result;

24 }

This code is used in ¶401.

¶397

〈Item class method definitions〉398 ≡1 bool Item::isNullable(ProductionIndex lookAhead) const

2 {

3 assert(production != 0);

4 for (ProductionIndex i = dotPosition + lookAhead;

5 i < getLength(); ++i)

6 if (!getRightHandSide(i)->isNullable())

7 return false;

8 return true;

9 }

This code is used in ¶401.

¶398

Page 145: Book

14.3. Files 131

〈Item class method definitions〉399 ≡1 ostream& Item::put(ostream& s) const

2 {

3 assert(production != 0);

4 s << getLeftHandSide()->getName() << " ->";

5 ProductionIndex i;

6 for (i = 0; i < dotPosition; ++i)

7 s << " " << getRightHandSide(i)->getName();

8 s << " ^";

9 for ( ; i < getLength(); ++i)

10 s << " " << getRightHandSide(i)->getName();

11 s << ". ";

12 return s << lookAheadSet;

13 }

This code is used in ¶401.

¶399

�14.3 Files

〈File: Gramatika/Item.h〉400 ≡1 #if !defined(_Gramatik_Item_h)

2 #define _Gramatik_Item_h

3

4 #include <iostream>

5 #include "Gramatika/Gramatika.h"

6 #include "Gramatika/Symbol.h"

7

8 namespace Gramatika

9 {

10 〈Item class declaration〉37311 〈Item class inline method definitions〉376–38612 }

13

14 #endif

¶400

〈File: Gramatika/Item.cc〉401 ≡1 #include <iostream>

2 #include "Gramatika/Item.h"

3

4 namespace Gramatika

5 {

6 〈Item class method definitions〉387–3997 }

¶401

Page 146: Book

132 Chapter 14. Item Class

Page 147: Book

Chapter 15

ItemSet Class and its Iterator

�15.1 ItemSet Class

15.1.1 Declarations

〈ItemSet class declaration〉402 ≡1 class ItemSet

2 {

3 〈ItemSet iterator class declaration〉4264 〈ItemSet class field declarations〉4035 〈ItemSet class method declarations〉404,4056 〈ItemSet class friends〉406

7 };

This code is used in ¶436.

¶402

〈ItemSet class field declarations〉403 ≡1 protected: vector<Item> item;

This code is used in ¶402.

¶403

〈ItemSet class method declarations〉404 ≡1 protected: bool close(Item);

This code is used in ¶402.

¶404

Page 148: Book

134 Chapter 15. ItemSet Class and its Iterator

〈ItemSet class method declarations〉405 ≡1 public: ItemSet();

2 public: ~ItemSet();

3 public: ItemSet(ItemSet const&);

4 public: ItemSet& operator=(ItemSet const&);

5 public: int compare(ItemSet const&) const;

6 public: bool operator==(ItemSet const&) const;

7 public: void add(Item const&);

8 public: void add(ItemSet const&);

9 public: void addKernelItems(ItemSet const&);

10 public: void advanceDots();

11 public: bool mergeLookAhead(ItemSet const&);

12 public: Item* getItem(Production const*);

13 public: ItemSet getClosure() const;

14 public: uint16_t getHashValue() const;

15 public: ostream& put(ostream&) const;

16 public: const_iterator begin() const;

This code is used in ¶402.

¶405

〈ItemSet class friends〉406 ≡1 friend class const_iterator;

2 friend class Partition;

This code is used in ¶402.

¶406

15.1.2 Definitions

〈ItemSet class inline method definitions〉407 ≡1 inline bool ItemSet::operator==(ItemSet const& itemSet) const

2 { return compare(itemSet) == 0; }

This code is used in ¶436.

¶407

〈ItemSet class inline method definitions〉408 ≡1 inline ostream& operator<<(ostream& stream, ItemSet const& itemSet)

2 { return itemSet.put(stream); }

This code is used in ¶436.

¶408

〈ItemSet class inline method definitions〉409 ≡1 inline uint16_t hash(ItemSet const& itemSet)

2 { return itemSet.getHashValue(); }

This code is used in ¶436.

¶409

〈ItemSet class method definitions〉410 ≡1 ItemSet::ItemSet() :

2 item(0)

3 {}

This code is used in ¶437.

¶410

Page 149: Book

15.1. ItemSet Class 135

〈ItemSet class method definitions〉411 ≡1 ItemSet::~ItemSet()

2 {}

This code is used in ¶437.

¶411

〈ItemSet class method definitions〉412 ≡1 ItemSet::ItemSet(ItemSet const& itemSet) :

2 item(itemSet.item)

3 {}

This code is used in ¶437.

¶412

〈ItemSet class method definitions〉413 ≡1 ItemSet& ItemSet::operator=(ItemSet const& itemSet)

2 {

3 if (this != &itemSet)

4 {

5 item = itemSet.item;

6 }

7 return *this;

8 }

This code is used in ¶437.

¶413

〈ItemSet class method definitions〉414 ≡1 int ItemSet::compare(ItemSet const& itemSet) const

2 {

3 int result = static_cast<int>(item.size())

4 - static_cast<int>(itemSet.item.size());

5 if (result != 0)

6 return result;

7 for (uint8_t i = 0; i < item.size(); ++i)

8 {

9 result = item[i].compare(itemSet.item[i]);

10 if (result != 0)

11 return result;

12 }

13 return 0;

14 }

This code is used in ¶437.

¶414

Page 150: Book

136 Chapter 15. ItemSet Class and its Iterator

〈ItemSet class method definitions〉415 ≡1 void ItemSet::add(Item const& newItem)

2 {

3 uint8_t i;

4 for (i = 0; i < item.size(); ++i)

5 {

6 if (item[i] == newItem)

7 {

8 item[i].addLookAhead(newItem.getLookAheadSet());

9 return;

10 }

11 }

12 item.resize(item.size() + 1U);

13 for (i = item.size() - 1U; i > 0; i--)

14 {

15 if (item[i - 1] <= newItem)

16 break;

17 item[i] = item[i - 1];

18 }

19 item[i] = newItem;

20 }

This code is used in ¶437.

¶415

〈ItemSet class method definitions〉416 ≡1 void ItemSet::add(ItemSet const& itemSet)

2 {

3 if (this != &itemSet)

4 {

5 for (uint8_t i = 0;

6 i < itemSet.item.size(); ++i)

7 add(itemSet.item[i]);

8 }

9 }

This code is used in ¶437.

¶416

〈ItemSet class method definitions〉417 ≡1 void ItemSet::addKernelItems(ItemSet const& kernelSet)

2 {

3 if (this != &kernelSet)

4 {

5 for (uint8_t i = 0;

6 i < kernelSet.item.size(); ++i)

7 if (kernelSet.item[i].isMoreToRightOfDot())

8 add(kernelSet.item[i]);

9 }

10 }

This code is used in ¶437.

¶417

Page 151: Book

15.1. ItemSet Class 137

〈ItemSet class method definitions〉418 ≡1 void ItemSet::advanceDots()

2 {

3 for (uint8_t i = 0; i < item.size(); ++i)

4 item[i].advanceDot();

5 }

This code is used in ¶437.

¶418

〈ItemSet class method definitions〉419 ≡1 bool ItemSet::mergeLookAhead(ItemSet const& itemSet)

2 {

3 assert(item.size() == itemSet.item.size());

4 bool modified = false;

5 for (uint8_t i = 0; i < item.size(); ++i)

6 modified |= item[i].mergeLookAhead(

7 itemSet.item[i]);

8 return modified;

9 }

This code is used in ¶437.

¶419

Page 152: Book

138 Chapter 15. ItemSet Class and its Iterator

〈ItemSet class method definitions〉420 ≡1 bool ItemSet::close(Item inputItem)

2 {

3 Symbol* const symbol = inputItem.getDotSymbol();

4

5 if (symbol == 0 || symbol->isTerminal())

6 return false;

7

8 bool modified = false;

9 list<Production*> const& prodList = symbol->getProductionList();

10 for (list<Production*>::const_iterator p(prodList.begin()),

11 lim(prodList.end()); p != lim; ++p)

12 {

13 Production const* const prod = *p;

14 Item* closureItem = getItem(prod);

15 if (closureItem == 0)

16 {

17 add(Item(*prod));

18 modified = true;

19 closureItem = getItem(prod);

20 assert(closureItem != 0);

21 }

22 Set closureSet = inputItem.getFirstSet(1);

23 closureSet -= epsilon;

24 if (inputItem.isNullable(1))

25 closureSet |= inputItem.getLookAheadSet();

26 if (!(closureSet <= closureItem->getLookAheadSet()))

27 {

28 closureItem->addLookAhead(closureSet);

29 modified = true;

30 }

31 }

32 return modified;

33 }

This code is used in ¶437.

¶420

Page 153: Book

15.1. ItemSet Class 139

〈ItemSet class method definitions〉421 ≡1 ItemSet ItemSet::getClosure() const

2 {

3 ItemSet result;

4

5 bool modified = false;

6 for (uint8_t i = 0; i < item.size(); ++i)

7 modified |= result.close(item[i]);

8

9 while (modified)

10 {

11 modified = false;

12 for (uint8_t i = 0;

13 i < result.item.size(); ++i)

14 modified |= result.close(result.item[i]);

15 }

16

17 return result;

18 }

This code is used in ¶437.

¶421

〈ItemSet class method definitions〉422 ≡1 Item* ItemSet::getItem(Production const* p)

2 {

3 for (uint8_t i = 0; i < item.size(); ++i)

4 if (item[i].getProduction() == p)

5 return &item[i];

6 return 0;

7 }

This code is used in ¶437.

¶422

〈ItemSet class method definitions〉423 ≡1 uint16_t ItemSet::getHashValue() const

2 {

3 uint16_t result = 0;

4 for (uint8_t i = 0; i < item.size(); ++i)

5 result += item[i].getProductionNumber() +

6 item[i].getDotPosition();

7 return result;

8 }

This code is used in ¶437.

¶423

Page 154: Book

140 Chapter 15. ItemSet Class and its Iterator

〈ItemSet class method definitions〉424 ≡1 ostream& ItemSet::put(ostream& s) const

2 {

3 for (uint8_t i = 0; i < item.size(); ++i)

4 s << item[i] << "\n";

5 return s;

6 }

This code is used in ¶437.

¶424

〈ItemSet class method definitions〉425 ≡1 ItemSet::const_iterator ItemSet::begin() const

2 {

3 if (item.size() > 0)

4 return const_iterator(this, 0);

5 else

6 return const_iterator();

7 }

This code is used in ¶437.

¶425

�15.2 ItemSet Iterator

15.2.1 Declarations

〈ItemSet iterator class declaration〉426 ≡1 public: class const_iterator

2 {

3 〈ItemSet iterator class field declarations〉4274 〈ItemSet iterator class method declarations〉428,4295 friend ItemSet;

6 };

This code is used in ¶402.

¶426

〈ItemSet iterator class field declarations〉427 ≡1 private: ItemSet const* itemSet;

2 private: uint8_t referent;

This code is used in ¶426.

¶427

〈ItemSet iterator class method declarations〉428 ≡1 private: const_iterator(ItemSet const*, uint8_t referent);

This code is used in ¶426.

¶428

〈ItemSet iterator class method declarations〉429 ≡1 public: const_iterator();

2 public: const_iterator(const_iterator const&);

3 public: Item const& operator*() const;

4 public: const_iterator& operator++();

5 public: bool operator!=(const_iterator const&) const;

This code is used in ¶426.

¶429

Page 155: Book

15.2. ItemSet Iterator 141

15.2.2 Definitions

〈ItemSet iterator class method definitions〉430 ≡1 ItemSet::const_iterator::const_iterator() :

2 itemSet(0),

3 referent(0)

4 {}

This code is used in ¶437.

¶430

〈ItemSet iterator class method definitions〉431 ≡1 ItemSet::const_iterator::const_iterator(ItemSet const* i, uint8_t r) :

2 itemSet(i),

3 referent(r)

4 {}

This code is used in ¶437.

¶431

〈ItemSet iterator class method definitions〉432 ≡1 ItemSet::const_iterator::const_iterator(ItemSet::const_iterator const& i) :

2 itemSet(i.itemSet),

3 referent(i.referent)

4 {}

This code is used in ¶437.

¶432

〈ItemSet iterator class method definitions〉433 ≡1 Item const& ItemSet::const_iterator::operator*() const

2 {

3 assert(itemSet != 0 && referent < itemSet->item.size());

4 return itemSet->item[referent];

5 }

This code is used in ¶437.

¶433

〈ItemSet iterator class method definitions〉434 ≡1 ItemSet::const_iterator& ItemSet::const_iterator::operator++()

2 {

3 assert(itemSet != 0);

4 if (++referent == itemSet->item.size())

5 {

6 itemSet = 0;

7 referent = 0;

8 }

9 return *this;

10 }

This code is used in ¶437.

¶434

〈ItemSet iterator class method definitions〉435 ≡1 bool ItemSet::const_iterator::operator!=(

2 ItemSet::const_iterator const& i) const

3 { return itemSet != i.itemSet || referent != i.referent; }

This code is used in ¶437.

¶435

Page 156: Book

142 Chapter 15. ItemSet Class and its Iterator

�15.3 Files

〈File: Gramatika/ItemSet.h〉436 ≡1 #if !defined(_Gramatika_ItemSet_h)

2 #define _Gramatika_ItemSet_h

3

4 #include <iostream>

5 #include <vector>

6 #include "Gramatika/Gramatika.h"

7 #include "Gramatika/Symbol.h"

8 #include "Gramatika/Item.h"

9

10 namespace Gramatika

11 {

12 〈ItemSet class declaration〉40213 〈ItemSet class inline method definitions〉407–40914 }

15

16 #endif

¶436

〈File: Gramatika/ItemSet.cc〉437 ≡1 #include <iostream>

2 #include <list>

3 #include "Gramatika/ItemSet.h"

4

5 namespace Gramatika

6 {

7 〈ItemSet class method definitions〉410–4258 〈ItemSet iterator class method definitions〉430–4359 }

¶437

Page 157: Book

Chapter 16

Partition Class and its

Iterator

�16.1 Partition Class

16.1.1 Declarations

〈Partition class declaration〉438 ≡1 class Partition : public ItemSet

2 {

3 〈Partition class iterator declaration〉4464 〈Partition class field declarations〉4395 〈Partition class method declarations〉4406 friend class const_iterator;

7 };

This code is used in ¶457.

¶438

〈Partition class field declarations〉439 ≡1 protected: uint8_t numberOfElements;

2 protected: vector<uint8_t> startOfElement;

3 protected: vector<uint8_t> sizeOfElement;

4 protected: vector<Symbol const*> elementSymbol;

This code is used in ¶438.

¶439

〈Partition class method declarations〉440 ≡1 public: explicit Partition(ItemSet const&);

2 public: ~Partition();

3 public: ostream& put(ostream&) const;

4 public: const_iterator begin() const;

This code is used in ¶438.

¶440

Page 158: Book

144 Chapter 16. Partition Class and its Iterator

16.1.2 Definitions

〈Partition class inline method definitions〉441 ≡1 inline ostream& operator<<(ostream& stream, Partition const& partition)

2 { return partition.put(stream); }

This code is used in ¶457.

¶441

〈Partition class method definitions〉442 ≡1 Partition::Partition(ItemSet const& itemSet) :

2 ItemSet(itemSet),

3 numberOfElements(0),

4 startOfElement(itemSet.item.size() + 1),

5 sizeOfElement(itemSet.item.size() + 1),

6 elementSymbol(itemSet.item.size() + 1)

7 {

8 uint8_t i = 0;

9 elementSymbol[numberOfElements] = 0;

10 startOfElement[numberOfElements] = i;

11 for ( ; i < item.size(); ++i)

12 if (item[i].getLength() > 0)

13 break;

14 sizeOfElement[numberOfElements] =

15 i - startOfElement[numberOfElements];

16 numberOfElements += 1;

17 while (i < item.size())

18 {

19 startOfElement[numberOfElements] = i;

20 Symbol* const symbol = item[i].getDotSymbol();

21 elementSymbol[numberOfElements] = symbol;

22 for ( ; i < item.size(); ++i)

23 if (item[i].getDotSymbol() != symbol)

24 break;

25 sizeOfElement[numberOfElements] =

26 i - startOfElement[numberOfElements];

27 numberOfElements += 1;

28 }

29 }

This code is used in ¶458.

¶442

〈Partition class method definitions〉443 ≡1 Partition::~Partition()

2 {}

This code is used in ¶458.

¶443

Page 159: Book

16.2. Partition Iterator 145

〈Partition class method definitions〉444 ≡1 ostream& Partition::put(ostream& s) const

2 {

3 s << "Partition {\n";

4 assert(numberOfElements > 0);

5 uint8_t i = 0;

6 for (;;)

7 {

8 uint8_t const start = startOfElement[i];

9 uint8_t const size = sizeOfElement[i];

10 for (uint8_t j = 0; j < size; ++j)

11 s << item[start + j] << "\n";

12 i += 1;

13 if (i == numberOfElements)

14 break;

15 s << "--------\n";

16 }

17 return s << "}\n";

18 }

This code is used in ¶458.

¶444

〈Partition class method definitions〉445 ≡1 Partition::const_iterator Partition::begin() const

2 {

3 if (numberOfElements > 0)

4 return const_iterator(this, 0);

5 else

6 return const_iterator();

7 }

This code is used in ¶458.

¶445

�16.2 Partition Iterator

16.2.1 Declarations

〈Partition class iterator declaration〉446 ≡1 public: class const_iterator

2 {

3 〈Partition iterator field declarations〉4474 〈Partition iterator method declarations〉448,4495 friend class Partition;

6 };

This code is used in ¶438.

¶446

〈Partition iterator field declarations〉447 ≡1 private: Partition const* partition;

2 private: uint8_t referent;

This code is used in ¶446.

¶447

Page 160: Book

146 Chapter 16. Partition Class and its Iterator

〈Partition iterator method declarations〉448 ≡1 private: const_iterator(Partition const*, uint8_t);

This code is used in ¶446.

¶448

〈Partition iterator method declarations〉449 ≡1 public: const_iterator();

2 public: const_iterator(const_iterator const&);

3 public: ItemSet operator*() const;

4 public: Symbol const* getSymbol() const;

5 public: const_iterator& operator++();

6 public: bool operator!=(const_iterator const&) const;

This code is used in ¶446.

¶449

16.2.2 Definitions

〈Partition iterator method definitions〉450 ≡1 Partition::const_iterator::const_iterator() :

2 partition(0),

3 referent(0)

4 {}

This code is used in ¶458.

¶450

〈Partition iterator method definitions〉451 ≡1 Partition::const_iterator::const_iterator(Partition const* p, uint8_t r) :

2 partition(p),

3 referent(r)

4 {}

This code is used in ¶458.

¶451

〈Partition iterator method definitions〉452 ≡1 Partition::const_iterator::const_iterator(

2 Partition::const_iterator const& i) :

3 partition(i.partition),

4 referent(i.referent)

5 {}

This code is used in ¶458.

¶452

Page 161: Book

16.2. Partition Iterator 147

〈Partition iterator method definitions〉453 ≡1 ItemSet Partition::const_iterator::operator*() const

2 {

3 assert(partition != 0 && referent < partition->numberOfElements);

4 ItemSet result;

5 uint8_t const start =

6 partition->startOfElement[referent];

7 uint8_t const size =

8 partition->sizeOfElement[referent];

9 for (uint8_t i = 0; i < size; ++i)

10 result.add(partition->item[start + i]);

11 return result;

12 }

This code is used in ¶458.

¶453

〈Partition iterator method definitions〉454 ≡1 Symbol const* Partition::const_iterator::getSymbol() const

2 {

3 assert(partition != 0 && referent < partition->numberOfElements);

4 return partition->elementSymbol[referent];

5 }

This code is used in ¶458.

¶454

〈Partition iterator method definitions〉455 ≡1 Partition::const_iterator& Partition::const_iterator::operator++()

2 {

3 assert(partition != 0);

4 if (++referent == partition->numberOfElements)

5 {

6 partition = 0;

7 referent = 0;

8 }

9 return *this;

10 }

This code is used in ¶458.

¶455

〈Partition iterator method definitions〉456 ≡1 bool Partition::const_iterator::operator!=(

2 Partition::const_iterator const& i) const

3 {

4 return partition != i.partition || referent != i.referent;

5 }

This code is used in ¶458.

¶456

Page 162: Book

148 Chapter 16. Partition Class and its Iterator

�16.3 Files

〈File: Gramatika/Partition.h〉457 ≡1 #if !defined(_Gramatika_Partition_h)

2 #define _Gramatika_Partition_h

3

4 #include <iostream>

5 #include <vector>

6 #include "Gramatika/Gramatika.h"

7 #include "Gramatika/Symbol.h"

8 #include "Gramatika/Item.h"

9 #include "Gramatika/ItemSet.h"

10

11 namespace Gramatika

12 {

13 〈Partition class declaration〉43814 〈Partition class inline method definitions〉44115 }

16

17 #endif

¶457

〈File: Gramatika/Partition.cc〉458 ≡1 #include <iostream>

2 #include "Gramatika/Partition.h"

3

4 namespace Gramatika

5 {

6 〈Partition class method definitions〉442–4457 〈Partition iterator method definitions〉450–4568 }

¶458

Page 163: Book

Chapter 17

ActionRecord Class

�17.1 Declarations

〈ActionRecord class declaration〉459 ≡1 class ActionRecord : public TableEntry

2 {

3 〈ActionRecord method declarations〉460,4614 };

This code is used in ¶471.

¶459

〈ActionRecord method declarations〉460 ≡1 private: int compare(ActionRecord const&) const;

This code is used in ¶459.

¶460

〈ActionRecord method declarations〉461 ≡1 public: ActionRecord(SymbolNumber, Action, uint16_t);

2 public: SymbolNumber getSymbolNumber() const;

3 public: Action getAction() const;

4 public: uint16_t getNumber() const;

5 public: bool operator==(ActionRecord const&) const;

6 public: bool operator<=(ActionRecord const&) const;

7 public: ostream& put(ostream&) const;

This code is used in ¶459.

¶461

Page 164: Book

150 Chapter 17. ActionRecord Class

�17.2 Definitions

〈ActionRecord class inline method definitions〉462 ≡1 inline SymbolNumber ActionRecord::getSymbolNumber() const

2 { return symbol; }

This code is used in ¶471.

¶462

〈ActionRecord class inline method definitions〉463 ≡1 inline Action ActionRecord::getAction() const

2 { return static_cast<Action>(action); }

This code is used in ¶471.

¶463

〈ActionRecord class inline method definitions〉464 ≡1 inline uint16_t ActionRecord::getNumber() const

2 { return number; }

This code is used in ¶471.

¶464

〈ActionRecord class inline method definitions〉465 ≡1 inline bool ActionRecord::operator==(ActionRecord const& arg) const

2 { return compare(arg) == 0; }

This code is used in ¶471.

¶465

〈ActionRecord class inline method definitions〉466 ≡1 inline bool ActionRecord::operator<=(ActionRecord const& arg) const

2 { return compare(arg) <= 0; }

This code is used in ¶471.

¶466

〈ActionRecord class inline method definitions〉467 ≡1 inline ostream& operator<<(ostream& stream, ActionRecord const& action)

2 { return action.put(stream); }

This code is used in ¶471.

¶467

〈ActionRecord method definitions〉468 ≡1 ActionRecord::ActionRecord(SymbolNumber sym, Action act, uint16_t num)

2 {

3 symbol = sym;

4 action = act;

5 number = num;

6 }

This code is used in ¶472.

¶468

Page 165: Book

17.3. Files 151

〈ActionRecord method definitions〉469 ≡1 int ActionRecord::compare(ActionRecord const& arg) const

2 {

3 int result = symbol - arg.symbol;

4 if (result != 0)

5 return result;

6 result = action - arg.action;

7 if (result != 0)

8 return result;

9 return number - arg.number;

10 }

This code is used in ¶472.

¶469

〈ActionRecord method definitions〉470 ≡1 ostream& ActionRecord::put(ostream& s) const

2 {

3 s << "Action { symbol = " << static_cast<uint16_t>(symbol);

4 s << ", action = " << static_cast<uint16_t>(action);

5 switch (action)

6 {

7 case shiftAction:

8 s << ", state = " << number; break;

9 case reduceAction:

10 s << ", production = " << number; break;

11 }

12 return s << " }";

13 }

This code is used in ¶472.

¶470

�17.3 Files

〈File: Gramatika/ActionRecord.h〉471 ≡1 #if !defined(_Gramatika_ActionRecord_h)

2 #define _Gramatika_ActionRecord_h

3

4 #include <iostream>

5 #include <list>

6 #include "Gramatika/Gramatika.h"

7

8 namespace Gramatika

9 {

10 〈ActionRecord class declaration〉45911 〈ActionRecord class inline method definitions〉462–46712 }

13 #endif

¶471

Page 166: Book

152 Chapter 17. ActionRecord Class

〈File: Gramatika/ActionRecord.cc〉472 ≡1 #include <iostream>

2 #include "Gramatika/ActionRecord.h"

3

4 namespace Gramatika

5 {

6 〈ActionRecord method definitions〉468–4707 }

¶472

Page 167: Book

Chapter 18

GotoRecord Class

�18.1 Declarations

〈GotoRecord class declaration〉473 ≡1 class GotoRecord : public TableEntry

2 {

3 〈GotoRecord method declarations〉474,4754 };

This code is used in ¶484.

¶473

〈GotoRecord method declarations〉474 ≡1 private: int compare(GotoRecord const&) const;

This code is used in ¶473.

¶474

〈GotoRecord method declarations〉475 ≡1 public: GotoRecord(SymbolNumber, StateNumber);

2 public: SymbolNumber getSymbolNumber() const;

3 public: StateNumber getStateNumber() const;

4 public: bool operator==(GotoRecord const&) const;

5 public: bool operator<=(GotoRecord const&) const;

6 public: ostream& put(ostream&) const;

This code is used in ¶473.

¶475

Page 168: Book

154 Chapter 18. GotoRecord Class

�18.2 Definitions

〈GotoRecord inline method definitions〉476 ≡1 inline SymbolNumber GotoRecord::getSymbolNumber() const

2 { return symbol; }

This code is used in ¶484.

¶476

〈GotoRecord inline method definitions〉477 ≡1 inline StateNumber GotoRecord::getStateNumber() const

2 { return number; }

This code is used in ¶484.

¶477

〈GotoRecord inline method definitions〉478 ≡1 inline bool GotoRecord::operator==(GotoRecord const& arg) const

2 { return compare(arg) == 0; }

This code is used in ¶484.

¶478

〈GotoRecord inline method definitions〉479 ≡1 inline bool GotoRecord::operator<=(GotoRecord const& arg) const

2 { return compare(arg) <= 0; }

This code is used in ¶484.

¶479

〈GotoRecord inline method definitions〉480 ≡1 inline ostream& operator<<(ostream& stream, GotoRecord const& g)

2 { return g.put(stream); }

This code is used in ¶484.

¶480

〈GotoRecord method definitions〉481 ≡1 GotoRecord::GotoRecord(SymbolNumber sym, StateNumber state)

2 {

3 symbol = sym;

4 action = 0;

5 number = state;

6 }

This code is used in ¶485.

¶481

〈GotoRecord method definitions〉482 ≡1 int GotoRecord::compare(GotoRecord const& arg) const

2 {

3 int result = symbol - arg.symbol;

4 if (result != 0)

5 return result;

6 return number - arg.number;

7 }

This code is used in ¶485.

¶482

Page 169: Book

18.3. Files 155

〈GotoRecord method definitions〉483 ≡1 ostream& GotoRecord::put(ostream& s) const

2 {

3 s << "Goto { symbol = " << static_cast<uint16_t>(symbol);

4 s << ", state = " << static_cast<uint16_t>(number);

5 return s << " }";

6 }

This code is used in ¶485.

¶483

�18.3 Files

〈File: Gramatika/GotoRecord.h〉484 ≡1 #if !defined(_Gramatika_GotoRecord_h)

2 #define _Gramatika_GotoRecord_h

3

4 #include <iostream>

5 #include <list>

6 #include "Gramatika/Gramatika.h"

7

8 namespace Gramatika

9 {

10 〈GotoRecord class declaration〉47311 〈GotoRecord inline method definitions〉476–48012 }

13

14 #endif

¶484

〈File: Gramatika/GotoRecord.cc〉485 ≡1 #include <iostream>

2 #include "Gramatika/GotoRecord.h"

3

4 namespace Gramatika

5 {

6 〈GotoRecord method definitions〉481–4837 }

¶485

Page 170: Book

156 Chapter 18. GotoRecord Class

Page 171: Book

Chapter 19

State class

�19.1 Declarations

〈State class declaration〉486 ≡1 class State

2 {

3 〈State class field declarations〉4874 〈State class method declarations〉488,4895 };

This code is used in ¶510.

¶486

〈State class field declarations〉487 ≡1 private: StateNumber const number;

2 private: ItemSet kernelItems;

3 private: ItemSet epsilonItems;

4 private: list<ActionRecord> actions;

5 private: list<GotoRecord> gotos;

6 private: bool closed;

7 private: uint8_t reduceReduceConflicts;

8 private: uint8_t shiftReduceConflicts;

This code is used in ¶486.

¶487

〈State class method declarations〉488 ≡1 private: ActionRecord const* getAction(SymbolNumber) const;

2 private: void reduce(Item const&);

This code is used in ¶486.

¶488

Page 172: Book

158 Chapter 19. State class

〈State class method declarations〉489 ≡1 public: State(StateNumber, ItemSet const&);

2 public: StateNumber getNumber() const;

3 public: ItemSet const& getKernelItems() const;

4 public: ItemSet const& getEpsilonItems() const;

5 public: bool isClosed() const;

6 public: int compare(State const&) const;

7 public: bool operator==(State const&) const;

8 public: void setClosed();

9 public: void addEpsilonItems(ItemSet const&);

10 public: void addShift(SymbolNumber, StateNumber);

11 public: void addGoto(SymbolNumber, StateNumber);

12 public: void addReduction(SymbolNumber, ProductionNumber);

13 public: void addReductions();

14 public: bool mergeLookAhead(ItemSet const&);

15 public: list<ActionRecord> const& getActionList() const;

16 public: list<GotoRecord> const& getGotoList() const;

17 public: ostream& put(ostream&) const;

This code is used in ¶486.

¶489

�19.2 Definitions

〈State class inline method definitions〉490 ≡1 inline StateNumber State::getNumber() const

2 { return number; }

This code is used in ¶510.

¶490

〈State class inline method definitions〉491 ≡1 inline ItemSet const& State::getKernelItems() const

2 { return kernelItems; }

This code is used in ¶510.

¶491

〈State class inline method definitions〉492 ≡1 inline ItemSet const& State::getEpsilonItems() const

2 { return epsilonItems; }

This code is used in ¶510.

¶492

〈State class inline method definitions〉493 ≡1 inline bool State::isClosed() const

2 { return closed; }

This code is used in ¶510.

¶493

〈State class inline method definitions〉494 ≡1 inline bool State::operator==(State const& s) const

2 { return compare(s) == 0; }

This code is used in ¶510.

¶494

Page 173: Book

19.2. Definitions 159

〈State class inline method definitions〉495 ≡1 inline void State::setClosed()

2 { closed = true; }

This code is used in ¶510.

¶495

〈State class inline method definitions〉496 ≡1 inline list<ActionRecord> const& State::getActionList() const

2 { return actions; }

This code is used in ¶510.

¶496

〈State class inline method definitions〉497 ≡1 inline list<GotoRecord> const& State::getGotoList() const

2 { return gotos; }

This code is used in ¶510.

¶497

〈State class inline method definitions〉498 ≡1 inline ostream& operator<<(ostream& stream, State const& state)

2 { return state.put(stream); }

This code is used in ¶510.

¶498

〈State class method definitions〉499 ≡1 State::State(StateNumber num, ItemSet const& kernel) :

2 number(num),

3 kernelItems(kernel),

4 epsilonItems(),

5 actions(),

6 gotos(),

7 closed(false),

8 reduceReduceConflicts(0),

9 shiftReduceConflicts(0)

10 {}

This code is used in ¶511.

¶499

〈State class method definitions〉500 ≡1 int State::compare(State const& s) const

2 { return kernelItems.compare(s.kernelItems); }

This code is used in ¶511.

¶500

〈State class method definitions〉501 ≡1 void State::addEpsilonItems(ItemSet const& itemSet)

2 { epsilonItems.add(itemSet); }

This code is used in ¶511.

¶501

Page 174: Book

160 Chapter 19. State class

〈State class method definitions〉502 ≡1 void State::addShift(SymbolNumber sym, StateNumber stat)

2 {

3 List::sortedInsert(actions,

4 ActionRecord(sym, shiftAction, stat));

5 }

This code is used in ¶511.

¶502

〈State class method definitions〉503 ≡1 void State::addGoto(SymbolNumber sym, StateNumber stat)

2 {

3 List::sortedInsert(gotos, GotoRecord(sym, stat));

4 }

This code is used in ¶511.

¶503

〈State class method definitions〉504 ≡1 void State::addReduction(SymbolNumber sym, ProductionNumber prod)

2 {

3 List::sortedInsert(actions,

4 ActionRecord(sym, reduceAction, prod));

5 }

This code is used in ¶511.

¶504

〈State class method definitions〉505 ≡1 bool State::mergeLookAhead(ItemSet const& itemSet)

2 { return kernelItems.mergeLookAhead(itemSet); }

This code is used in ¶511.

¶505

〈State class method definitions〉506 ≡1 ActionRecord const* State::getAction(SymbolNumber symbol) const

2 {

3 for (list<ActionRecord>::const_iterator ptr(actions.begin()),

4 lim(actions.end()); ptr != lim; ++ptr)

5 if ((*ptr).getSymbolNumber() == symbol)

6 return &(*ptr);

7 return 0;

8 }

This code is used in ¶511.

¶506

Page 175: Book

19.2. Definitions 161

〈State class method definitions〉507 ≡1 void State::reduce(Item const& item)

2 {

3 if (item.getDotPosition() != item.getLength())

4 return;

5 for (Set::const_iterator ptr(item.getLookAheadSet().begin()), null;

6 ptr != null; ++ptr)

7 {

8 SymbolNumber symbol = *ptr;

9 ActionRecord const* action = getAction(symbol);

10 if (action != 0)

11 {

12 if (action->getAction() == shiftAction)

13 {

14 shiftReduceConflicts += 1;

15 cerr << "Shift/reduce conflict." << endl;

16 }

17 else

18 {

19 assert(action->getAction() == reduceAction);

20 reduceReduceConflicts += 1;

21 cerr << "Reduce/reduce conflict." << endl;

22 }

23 }

24 addReduction(symbol, item.getProductionNumber());

25 }

26 }

This code is used in ¶511.

¶507

〈State class method definitions〉508 ≡1 void State::addReductions()

2 {

3 for (ItemSet::const_iterator ptr(kernelItems.begin()), null;

4 ptr != null; ++ptr)

5 {

6 reduce(*ptr);

7 }

8 for (ItemSet::const_iterator ptr(epsilonItems.begin()), null;

9 ptr != null; ++ptr)

10 {

11 reduce(*ptr);

12 }

13 }

This code is used in ¶511.

¶508

Page 176: Book

162 Chapter 19. State class

〈State class method definitions〉509 ≡1 ostream& State::put(ostream& s) const

2 {

3 s << "State { number = " << number

4 << ", closed = " << closed << ",\n";

5 s << "reduce/reduce conflicts = " <<

6 static_cast<uint16_t>(reduceReduceConflicts)

7 << ", shift/reduce conflicts = " <<

8 static_cast<uint16_t>(shiftReduceConflicts) << "\n";

9 s << "Kernel Items:\n";

10 s << kernelItems;

11 s << "Epsilon Items:\n";

12 s << epsilonItems;

13 s << "ActionList:\n";

14 List::put(s, actions);

15 s << "GotoList:\n";

16 List::put(s, gotos);

17 return s << "}\n";

18 }

This code is used in ¶511.

¶509

�19.3 Files

〈File: Gramatika/State.h〉510 ≡1 #if !defined(_Gramatika_State_h)

2 #define _Gramatika_State_h

3

4 #include <iostream>

5 #include <list>

6 #include "Gramatika/Gramatika.h"

7 #include "Gramatika/ItemSet.h"

8 #include "Gramatika/ActionRecord.h"

9 #include "Gramatika/GotoRecord.h"

10 #include "Toolbox/List.h"

11

12 namespace Gramatika

13 {

14 〈State class declaration〉48615 〈State class inline method definitions〉490–49816 }

17

18 #endif

¶510

Page 177: Book

19.3. Files 163

〈File: Gramatika/State.cc〉511 ≡1 #include <iostream>

2 #include "Gramatika/State.h"

3 #include "Toolbox/List.h"

4

5 using namespace Toolbox;

6

7 namespace Gramatika

8 {

9 〈State class method definitions〉499–50910 }

¶511

Page 178: Book

164 Chapter 19. State class

Page 179: Book

Chapter 20

Automaton Class

�20.1 Declarations

〈Automaton class declaration〉512 ≡1 class Automaton

2 {

3 〈Automaton class field declarations〉5134 〈Automaton class method declarations〉514,5155 };

This code is used in ¶531.

¶512

〈Automaton class field declarations〉513 ≡1 private: SymbolTable const* const symbolTable;

2 private: vector<State*> state;

3 private: HashTable<ItemSet,State*> hashTable;

4 private: Set unfinishedStates;

This code is used in ¶512.

¶513

〈Automaton class method declarations〉514 ≡1 private: State* getUnfinishedState();

2 private: State* newState(ItemSet const&);

3 private: void createStartState();

4 private: void createStates();

5 private: void addReductions();

6 private: void addUnfinishedState(State const*);

This code is used in ¶512.

¶514

〈Automaton class method declarations〉515 ≡1 public: explicit Automaton(SymbolTable const&);

2 public: ~Automaton();

3 public: State const& operator[](StateNumber) const;

4 public: SymbolTable const& getSymbolTable() const;

5 public: StateNumber getNumberOfStates() const;

6 public: ostream& put(ostream&) const;

This code is used in ¶512.

¶515

Page 180: Book

166 Chapter 20. Automaton Class

�20.2 Definitions

〈Automaton class inline method definitions〉516 ≡1 inline SymbolTable const& Automaton::getSymbolTable() const

2 { return *symbolTable; }

This code is used in ¶531.

¶516

〈Automaton class inline method definitions〉517 ≡1 inline StateNumber Automaton::getNumberOfStates() const

2 { return state.size(); }

This code is used in ¶531.

¶517

〈Automaton class inline method definitions〉518 ≡1 inline ostream& operator<<(ostream& stream, Automaton const& tbl)

2 { return tbl.put(stream); }

This code is used in ¶531.

¶518

〈Automaton class method definitions〉519 ≡1 Automaton::Automaton(SymbolTable const& symTbl) :

2 symbolTable(&symTbl),

3 state(),

4 hashTable(127),

5 unfinishedStates()

6 {

7 createStates();

8 addReductions();

9 }

This code is used in ¶532.

¶519

〈Automaton class method definitions〉520 ≡1 Automaton::~Automaton()

2 {

3 for (StateNumber i = 0; i < state.size(); ++i)

4 delete state[i];

5 }

This code is used in ¶532.

¶520

〈Automaton class method definitions〉521 ≡1 State const& Automaton::operator[](StateNumber i) const

2 {

3 assert(i < state.size());

4 return *state[i];

5 }

This code is used in ¶532.

¶521

Page 181: Book

20.2. Definitions 167

〈Automaton class method definitions〉522 ≡1 State* Automaton::newState(ItemSet const& kernel)

2 {

3 HashTable<ItemSet,State*>::const_iterator

4 ptr(hashTable.find(kernel)), null;

5 if (ptr != null)

6 return *ptr;

7 State* const result = new State(state.size(), kernel);

8 state.push_back(result);

9 hashTable.add(kernel, result);

10 return result;

11 }

This code is used in ¶532.

¶522

〈Automaton class method definitions〉523 ≡1 State* Automaton::getUnfinishedState()

2 {

3 if (unfinishedStates == 0)

4 return 0;

5 Set::const_iterator ptr(unfinishedStates.begin());

6 StateNumber unfinished = *ptr;

7 assert(unfinished < state.size());

8 unfinishedStates -= unfinished;

9 return state[unfinished];

10 }

This code is used in ¶532.

¶523

〈Automaton class method definitions〉524 ≡1 void Automaton::addUnfinishedState(State const* state)

2 { unfinishedStates += state->getNumber(); }

This code is used in ¶532.

¶524

Page 182: Book

168 Chapter 20. Automaton Class

〈Automaton class method definitions〉525 ≡1 void Automaton::createStartState()

2 {

3 assert(state.size() == 0);

4 Symbol* const startSymbol = symbolTable->getStartSymbol();

5 assert(startSymbol != 0);

6 assert(startSymbol->getProductionList().size() == 1);

7 list<Production*>::const_iterator ptr(

8 startSymbol->getProductionList().begin());

9 assert(ptr != startSymbol->getProductionList().end());

10 Production const* const production = *ptr;

11 assert(production != 0);

12 Item startItem(*production);

13 Set startLookAhead;

14 startLookAhead += end_of_file;

15 startItem.addLookAhead(startLookAhead);

16 ItemSet startKernel;

17 startKernel.add(startItem);

18 State* const state = newState(startKernel);

19 addUnfinishedState(state);

20 }

This code is used in ¶532.

¶525

〈Automaton class method definitions〉526 ≡1 void Automaton::createStates()

2 {

3 createStartState();

4

5 for (;;)

6 {

7 State* const currentState = getUnfinishedState();

8 if (currentState == 0)

9 break;

10 cerr << ".";

11 ItemSet closureItems(

12 currentState->getKernelItems().getClosure());

13 closureItems.addKernelItems(

14 currentState->getKernelItems());

15 Partition partition(closureItems);

16 Partition::const_iterator part(partition.begin()), null;

17 assert(part != null);

18 currentState->addEpsilonItems(*part);

19 for (++part; part != null; ++part)

20 {

21 〈process the partition element *part〉52722 }

23 currentState->setClosed();

24 }

25 }

This code is used in ¶532.

¶526

Page 183: Book

20.2. Definitions 169

〈process the partition element *part〉527 ≡1 State* nextState;

2 ItemSet kernel = *part;

3 kernel.advanceDots();

4 HashTable<ItemSet,State*>::const_iterator ptr(

5 hashTable.find(kernel)), null;

6 if (ptr != null)

7 {

8 nextState = *ptr;

9 bool const modified = nextState->mergeLookAhead(kernel);

10 if (modified)

11 addUnfinishedState(nextState);

12 }

13 else

14 {

15 nextState = newState(kernel);

16 addUnfinishedState(nextState);

17 }

18 〈add shift/goto transitions〉528This code is used in ¶526.

¶527

〈add shift/goto transitions〉528 ≡1 if (!currentState->isClosed())

2 {

3 Symbol const* const symbol = part.getSymbol();

4 if (symbol->isTerminal())

5 currentState->addShift(symbol->getNumber(),

6 nextState->getNumber());

7 else

8 currentState->addGoto(symbol->getNumber(),

9 nextState->getNumber());

10 }

This code is used in ¶527.

¶528

〈Automaton class method definitions〉529 ≡1 void Automaton::addReductions()

2 {

3 for (StateNumber i = 0; i < state.size(); ++i)

4 state[i]->addReductions();

5 }

This code is used in ¶532.

¶529

Page 184: Book

170 Chapter 20. Automaton Class

〈Automaton class method definitions〉530 ≡1 ostream& Automaton::put(ostream& s) const

2 {

3 for (StateNumber i = 0; i < state.size(); ++i)

4 s << *state[i];

5 return s;

6 }

This code is used in ¶532.

¶530

�20.3 Files

〈File: Gramatika/Automaton.h〉531 ≡1 #if !defined(_Gramatika_Automaton_h)

2 #define _Gramatika_Automaton_h

3

4 #include <iostream>

5 #include <vector>

6 #include "Gramatika/SymbolTable.h"

7 #include "Gramatika/State.h"

8 #include "Toolbox/HashTable.h"

9 #include "Toolbox/String.h"

10

11 using namespace Toolbox;

12 using namespace Toolbox::String;

13

14 namespace Gramatika

15 {

16 〈Automaton class declaration〉51217 〈Automaton class inline method definitions〉516–51818 }

19

20 #endif

¶531

〈File: Gramatika/Automaton.cc〉532 ≡1 #include <iostream>

2 #include <list>

3 #include "Gramatika/Automaton.h"

4 #include "Gramatika/Partition.h"

5

6 namespace Gramatika

7 {

8 〈Automaton class method definitions〉519–526,529,5309 }

¶532

Page 185: Book

Chapter 21

ParseTable Class

�21.1 Declarations

〈ParseTable class declaration〉533 ≡1 class ParseTable

2 {

3 〈ParseTable constants〉5344 〈ParseTable field declarations〉5355 〈ParseTable method declarations〉536,5376 };

This code is used in ¶551.

¶533

〈ParseTable constants〉534 ≡1 public: enum Constants

2 {

3 invalid = UINT16_MAX >> 1

4 };

This code is used in ¶533.

¶534

〈ParseTable field declarations〉535 ≡1 private: Automaton const* const automaton;

2 private: SymbolTable const* const symbolTable;

3 private: SymbolNumber const numberOfStates;

4 private: ProductionNumber const numberOfProductions;

5 private: vector<MapEntry> actionMap;

6 private: vector<TableEntry> actionTable;

7 private: uint16_t actionTableSize;

8 private: Set actionStates;

9 private: vector<MapEntry> gotoMap;

10 private: vector<TableEntry> gotoTable;

11 private: uint16_t gotoTableSize;

12 private: Set gotoStates;

13 private: vector<SymbolNumber> leftHandSideTable;

14 private: vector<ProductionIndex> rightHandSideTable;

This code is used in ¶533.

¶535

Page 186: Book

172 Chapter 21. ParseTable Class

〈ParseTable method declarations〉536 ≡1 private: void makeProductionTables();

2 private: void makeActionMap();

3 private: void makeActionTable();

4 private: void makeGotoMap();

5 private: void makeGotoTable();

6 private: ostream& putMap(

7 ostream&, char const*, vector<MapEntry> const&) const;

8 private: ostream& putTable(ostream&, char const*, uint16_t,

9 vector<TableEntry> const&) const;

10 private: ostream& putLeftHandSideTable(ostream&) const;

11 private: ostream& putRightHandSideTable(ostream&) const;

This code is used in ¶533.

¶536

〈ParseTable method declarations〉537 ≡1 public: explicit ParseTable(Automaton const&);

2 public: ~ParseTable();

3 public: ostream& put(ostream&) const;

This code is used in ¶533.

¶537

�21.2 Definitions

〈ParseTable class inline method definitions〉538 ≡1 inline ostream& operator<<(ostream& stream, ParseTable const& table)

2 { return table.put(stream); }

This code is used in ¶551.

¶538

Page 187: Book

21.2. Definitions 173

〈ParseTable class method definitions〉539 ≡1 ParseTable::ParseTable(Automaton const& a) :

2 automaton(&a),

3 symbolTable(&a.getSymbolTable()),

4 numberOfStates(a.getNumberOfStates()),

5 numberOfProductions(

6 a.getSymbolTable().getNumberOfProductions()),

7 actionMap(numberOfStates),

8 actionTable(0),

9 actionTableSize(0),

10 actionStates(),

11 gotoMap(numberOfStates),

12 gotoTable(0), gotoTableSize(0), gotoStates(),

13 leftHandSideTable(numberOfProductions),

14 rightHandSideTable(numberOfProductions)

15 {

16 makeActionMap();

17 makeActionTable();

18 makeGotoMap();

19 makeGotoTable();

20 makeProductionTables();

21 }

This code is used in ¶552.

¶539

〈ParseTable class method definitions〉540 ≡1 ParseTable::~ParseTable()

2 {}

This code is used in ¶552.

¶540

Page 188: Book

174 Chapter 21. ParseTable Class

〈ParseTable class method definitions〉541 ≡1 void ParseTable::makeActionMap()

2 {

3 for (StateNumber i = 0; i < numberOfStates; ++i)

4 actionMap[i].start = invalid;

5 for (StateNumber i = 0; i < numberOfStates; ++i)

6 {

7 if (actionMap[i].start == invalid)

8 {

9 actionStates += i;

10 uint8_t const len =

11 (*automaton)[i].getActionList().size();

12 actionMap[i].start = actionTableSize;

13 actionMap[i].count = len;

14 actionTableSize += len;

15 for (StateNumber j = i + 1;

16 j < numberOfStates; ++j)

17 if (actionMap[j].start == invalid)

18 if (List::equals (

19 (*automaton)[i].getActionList(),

20 (*automaton)[j].getActionList()))

21 actionMap[j] = actionMap[i];

22 }

23 }

24 }

This code is used in ¶552.

¶541

〈ParseTable class method definitions〉542 ≡1 void ParseTable::makeActionTable()

2 {

3 actionTable.resize(actionTableSize);

4

5 uint16_t position = 0;

6 for (Set::const_iterator state(actionStates.begin()), lim;

7 state != lim; ++state)

8 {

9 StateNumber const i = *state;

10 list<ActionRecord> const& actionList =

11 (*automaton)[i].getActionList();

12 for (list<ActionRecord>::const_iterator ptr(actionList.begin()),

13 lim(actionList.end()); ptr != lim; ++ptr)

14 {

15 actionTable[position] = *ptr;

16 position += 1;

17 }

18 }

19 assert(position == actionTableSize);

20 }

This code is used in ¶552.

¶542

Page 189: Book

21.2. Definitions 175

〈ParseTable class method definitions〉543 ≡1 void ParseTable::makeGotoMap()

2 {

3 for (StateNumber i = 0; i < numberOfStates; ++i)

4 gotoMap[i].start = invalid;

5 for (StateNumber i = 0; i < numberOfStates; ++i)

6 {

7 if (gotoMap[i].start == invalid)

8 {

9 gotoStates += i;

10 uint8_t const len =

11 (*automaton)[i].getGotoList().size();

12 gotoMap[i].start = gotoTableSize;

13 gotoMap[i].count = len;

14 gotoTableSize += len;

15 for (StateNumber j = i + 1;

16 j < numberOfStates; ++j)

17 if (gotoMap[j].start == invalid)

18 if (List::equals(

19 (*automaton)[i].getGotoList(),

20 (*automaton)[j].getGotoList()))

21 gotoMap[j] = gotoMap[i];

22 }

23 }

24 }

This code is used in ¶552.

¶543

〈ParseTable class method definitions〉544 ≡1 void ParseTable::makeGotoTable()

2 {

3 gotoTable.resize(gotoTableSize);

4

5 uint16_t position = 0;

6 for (Set::const_iterator state(gotoStates.begin()), null;

7 state != null; ++state)

8 {

9 StateNumber const i = *state;

10 list<GotoRecord> const& gotoList =

11 (*automaton)[i].getGotoList();

12 for (list<GotoRecord>::const_iterator ptr(gotoList.begin()),

13 lim(gotoList.end()); ptr != lim; ++ptr)

14 {

15 gotoTable[position] = *ptr;

16 position += 1;

17 }

18 }

19 assert(position == gotoTableSize);

20 }

This code is used in ¶552.

¶544

Page 190: Book

176 Chapter 21. ParseTable Class

〈ParseTable class method definitions〉545 ≡1 void ParseTable::makeProductionTables()

2 {

3 for (SymbolNumber i = 0;

4 i < symbolTable->getNumberOfSymbols(); ++i)

5 {

6 list<Production*> const& prodList =

7 (*symbolTable)[i].getProductionList();

8 for (list<Production*>::const_iterator ptr(prodList.begin()),

9 lim(prodList.end()); ptr != lim; ++ptr)

10 {

11 Production const* const prod = *ptr;

12 leftHandSideTable[prod->getNumber()] =

13 prod->getLeftHandSide()->getNumber();

14 rightHandSideTable[prod->getNumber()] =

15 prod->getLength();

16 }

17 }

18 }

This code is used in ¶552.

¶545

〈ParseTable class method definitions〉546 ≡1 ostream& ParseTable::putMap(ostream& s, char const* label,

2 vector<MapEntry> const& map) const

3 {

4 s << "MapEntry const GramatikaTables::"

5 << label << "[" << numberOfStates << "] =\n";

6 s << "{\n";

7 s << " ";

8 for (StateNumber i = 0; i < numberOfStates; ++i)

9 {

10 s << map[i];

11 if (i < numberOfStates - 1U)

12 s << ", ";

13 if (i % 5 == 4)

14 {

15 s << "\n";

16 s << " ";

17 }

18 }

19 s << "\n";

20 s << "};\n";

21 return s;

22 }

This code is used in ¶552.

¶546

Page 191: Book

21.2. Definitions 177

〈ParseTable class method definitions〉547 ≡1 ostream& ParseTable::putTable(ostream& s, char const* label,

2 uint16_t size,

3 vector<TableEntry> const& table) const

4 {

5 s << "TableEntry const GramatikaTables::"

6 << label << "[" << size << "] =\n";

7 s << "{\n";

8 s << " ";

9 for (uint16_t i = 0; i < size; ++i)

10 {

11 s << table[i];

12 if (i < size - 1U)

13 s << ", ";

14 if (i % 5 == 4)

15 {

16 s << "\n";

17 s << " ";

18 }

19 }

20 s << "\n";

21 s << "};\n";

22 return s;

23 }

This code is used in ¶552.

¶547

〈ParseTable class method definitions〉548 ≡1 ostream& ParseTable::putLeftHandSideTable(ostream& s) const

2 {

3 s << "SymbolNumber const GramatikaTables::leftHandSideTable["

4 << numberOfProductions << "] =\n";

5 s << "{\n";

6 s << " ";

7 for (ProductionNumber i = 0;

8 i < numberOfProductions; ++i)

9 {

10 s << leftHandSideTable[i];

11 if (i < numberOfProductions - 1U)

12 s << ", ";

13 if (i % 10 == 9)

14 {

15 s << "\n";

16 s << " ";

17 }

18 }

19 s << "\n";

20 s << "};\n";

21 return s;

22 }

This code is used in ¶552.

¶548

Page 192: Book

178 Chapter 21. ParseTable Class

〈ParseTable class method definitions〉549 ≡1 ostream& ParseTable::putRightHandSideTable(ostream& s) const

2 {

3 s << "ProductionIndex const GramatikaTables::rightHandSideTable["

4 << numberOfProductions << "] =\n";

5 s << "{\n";

6 s << " ";

7 for (ProductionNumber i = 0;

8 i < numberOfProductions; ++i)

9 {

10 s << static_cast<uint16_t>(rightHandSideTable[i]);

11 if (i < numberOfProductions - 1U)

12 s << ", ";

13 if (i % 10 == 9)

14 {

15 s << "\n";

16 s << " ";

17 }

18 }

19 s << "\n";

20 s << "};\n";

21 return s;

22 }

This code is used in ¶552.

¶549

〈ParseTable class method definitions〉550 ≡1 ostream& ParseTable::put(ostream& s) const

2 {

3 s <<

4 "#include \"gramadef.h\"\n"

5 "\n"

6 "namespace " << symbolTable->getLanguage() << "\n"

7 "{\n";

8 s << "StateNumber const GramatikaTables::numberOfStates = "

9 << numberOfStates << ";\n";

10 putMap(s, "actionMap", actionMap);

11 putTable(s, "actionTable", actionTableSize, actionTable);

12 putMap(s, "gotoMap", gotoMap);

13 putTable(s, "gotoTable", gotoTableSize, gotoTable);

14 s << "ProductionNumber const GramatikaTables::numberOfProductions = "

15 << numberOfProductions << ";\n";

16 putLeftHandSideTable(s);

17 putRightHandSideTable(s);

18 s << "}\n";

19 return s;

20 }

This code is used in ¶552.

¶550

Page 193: Book

21.3. Files 179

�21.3 Files

〈File: Gramatika/ParseTable.h〉551 ≡1 #if !defined(_Gramatika_ParseTable_h)

2 #define _Gramatika_ParseTable_h

3

4 #include <iostream>

5 #include <vector>

6 #include <stdint.h>

7 #include "Gramatika/Gramatika.h"

8 #include "Gramatika/Automaton.h"

9

10 namespace Gramatika

11 {

12 〈ParseTable class declaration〉53313 〈ParseTable class inline method definitions〉53814 }

15

16 #endif

¶551

〈File: Gramatika/ParseTable.cc〉552 ≡1 #include <iostream>

2 #include <list>

3 #include "Gramatika/Gramatika.h"

4 #include "Gramatika/ParseTable.h"

5 #include "Toolbox/List.h"

6

7 using namespace Toolbox;

8

9 namespace Gramatika

10 {

11 〈ParseTable class method definitions〉539–55012 }

¶552

Page 194: Book

180 Chapter 21. ParseTable Class

Page 195: Book

Chapter 22

Parser Classes

�22.1 Common Types and Constants

22.1.1 Type Declarations

〈Gramatika type declarations〉553 ≡1 typedef uint16_t SymbolNumber;

This code is used in ¶598.

¶553

〈Gramatika type declarations〉554 ≡1 typedef uint16_t StateNumber;

This code is used in ¶598.

¶554

〈Gramatika type declarations〉555 ≡1 typedef uint16_t ProductionNumber;

This code is used in ¶598.

¶555

〈Gramatika type declarations〉556 ≡1 typedef uint8_t ProductionIndex;

This code is used in ¶598.

¶556

22.1.2 Constants

〈Gramatika constants〉557 ≡1 enum Constants

2 {

3 epsilon = 0,

4 end_of_file = 1

5 };

This code is used in ¶598.

¶557

Page 196: Book

182 Chapter 22. Parser Classes

22.1.3 TableEntry Struct

〈TableEntry struct declaration〉558 ≡1 struct TableEntry

2 {

3 〈TableEntry field declarations〉5594 };

This code is used in ¶598.

¶558

〈TableEntry field declarations〉559 ≡1 public: uint8_t symbol;

2 public: uint8_t action;

3 public: uint16_t number;

This code is used in ¶558.

¶559

〈TableEntry struct inline method definitions〉560 ≡1 inline ostream& operator<<(ostream& s, TableEntry entry)

2 {

3 s << "{" << static_cast<uint16_t>(entry.symbol);

4 s << "," << static_cast<uint16_t>(entry.action);

5 s << "," << entry.number << "}";

6 return s;

7 }

This code is used in ¶598.

¶560

22.1.4 MapEntry Struct

〈MapEntry struct declaration〉561 ≡1 struct MapEntry

2 {

3 〈MapEntry field declarations〉5624 };

This code is used in ¶598.

¶561

〈MapEntry field declarations〉562 ≡1 public: uint16_t start;

2 public: uint8_t count;

This code is used in ¶561.

¶562

〈MapEntry struct inline method definitions〉563 ≡1 inline ostream& operator<<(ostream& s, MapEntry entry)

2 {

3 s << "{" << entry.start;

4 s << "," << static_cast<uint16_t>(entry.count) << "}";

5 return s;

6 }

This code is used in ¶598.

¶563

Page 197: Book

22.1. Common Types and Constants 183

22.1.5 SymbolType Enumeration

〈SymbolType enumeration declaration〉564 ≡1 enum SymbolType

2 {

3 terminal,

4 nonterminal

5 };

This code is used in ¶598.

¶564

〈SymbolType enumeration inline method definitions〉565 ≡1 inline ostream& operator<<(ostream& s, SymbolType sym)

2 {

3 switch (sym)

4 {

5 case terminal: s << "terminal"; break;

6 case nonterminal: s << "nonterminal"; break;

7 }

8 return s;

9 }

This code is used in ¶598.

¶565

22.1.6 Action Enumeration

〈Action enumeration declaration〉566 ≡1 enum Action

2 {

3 shiftAction,

4 reduceAction,

5 error

6 };

This code is used in ¶598.

¶566

〈Action enumeration inline method definitions〉567 ≡1 inline ostream& operator<<(ostream& s, Action act)

2 {

3 switch (act)

4 {

5 case shiftAction: s << "shift"; break;

6 case reduceAction: s << "reduce"; break;

7 case error: s << "error"; break;

8 }

9 return s;

10 }

This code is used in ¶598.

¶567

Page 198: Book

184 Chapter 22. Parser Classes

�22.2 Value Class Template

22.2.1 Declarations

〈Value class template declaration〉568 ≡1 template <typename T, typename A>

2 class Value : public A

3 {

4 〈Value class template type declarations〉5695 〈Value class template field declarations〉5706 〈Value class template method declarations〉5717 };

This code is used in ¶598.

¶568

〈Value class template type declarations〉569 ≡1 public: typedef T Token;

2 public: typedef A Attributes;

This code is used in ¶568.

¶569

〈Value class template field declarations〉570 ≡1 public: Token terminal;

2 public: int _default;

This code is used in ¶568.

¶570

〈Value class template method declarations〉571 ≡1 public: Value();

2 public: Value(Token const&);

3 public: ostream& put(ostream&) const;

This code is used in ¶568.

¶571

Page 199: Book

22.3. StackItem Class Template 185

22.2.2 Definitions

〈Value class template inline method definitions〉572 ≡1 template <typename T, typename A>

2 inline Value<T,A>::Value() :

3 A(),

4 terminal(),

5 _default(0)

6 {}

This code is used in ¶598.

¶572

〈Value class template inline method definitions〉573 ≡1 template <typename T, typename A>

2 inline Value<T,A>::Value(T const& t) :

3 A(),

4 terminal(t),

5 _default(0)

6 {}

This code is used in ¶598.

¶573

〈Value class template inline method definitions〉574 ≡1 template <typename T, typename A>

2 inline ostream& operator<<(ostream& stream, Value<T,A> const& value)

3 { return value.put(stream); }

This code is used in ¶598.

¶574

〈Value class template method definitions〉575 ≡1 template <typename T, typename A>

2 ostream& Value<T,A>::put(ostream& s) const

3 { return s << terminal; }

This code is used in ¶599.

¶575

Page 200: Book

186 Chapter 22. Parser Classes

�22.3 StackItem Class Template

22.3.1 Declarations

〈StackItem class template declaration〉576 ≡1 template <typename T, typename A>

2 class StackItem : public Value<T,A>

3 {

4 public:

5 〈StackItem class template type declarations〉5776 〈StackItem class template field declarations〉5787 〈StackItem class template method declarations〉5798 };

This code is used in ¶598.

¶576

〈StackItem class template type declarations〉577 ≡1 typedef T Token;

2 typedef A Attributes;

This code is used in ¶576.

¶577

〈StackItem class template field declarations〉578 ≡1 public: StateNumber state;

This code is used in ¶576.

¶578

〈StackItem class template method declarations〉579 ≡1 public: StackItem();

2 public: StackItem(StateNumber, Value<T,A>);

3 public: ostream& put(ostream&) const;

This code is used in ¶576.

¶579

Page 201: Book

22.4. Parser Class Template 187

22.3.2 Definitions

〈StackItem class template inline method definitions〉580 ≡1 template <typename T, typename A>

2 inline StackItem<T,A>::StackItem() :

3 Value<T,A>(),

4 state(0)

5 {}

This code is used in ¶598.

¶580

〈StackItem class template inline method definitions〉581 ≡1 template <typename T, typename A>

2 inline StackItem<T,A>::StackItem(StateNumber s, Value<T,A> v) :

3 Value<T,A>(v),

4 state(s)

5 {}

This code is used in ¶598.

¶581

〈StackItem class template inline method definitions〉582 ≡1 template <typename A,typename T>

2 inline ostream& operator<<(

3 ostream& stream, StackItem<T,A> const& item)

4 { return item.put(stream); }

This code is used in ¶598.

¶582

〈StackItem class template method definitions〉583 ≡1 template <typename T, typename A>

2 ostream& StackItem<T,A>::put(ostream& s) const

3 {

4 s << "state = " << state << ", value = ";

5 return Value<T,A>::put(s);

6 }

This code is used in ¶599.

¶583

Page 202: Book

188 Chapter 22. Parser Classes

�22.4 Parser Class Template

22.4.1 Declarations

〈Parser class template declaration〉584 ≡1 template <typename T, typename A, typename GT>

2 class Parser

3 {

4 〈Parser class template type declarations〉5855 〈Parser class template field declarations〉5866 〈Parser class template method declarations〉587,5887 };

This code is used in ¶598.

¶584

〈Parser class template type declarations〉585 ≡1 public: typedef T Token;

2 public: typedef A Attributes;

3 public: typedef GT Tables;

4 public: typedef typename T::Stream TokenStream;

5 public: typedef typename T::Tables TokenTables;

This code is used in ¶584.

¶585

〈Parser class template field declarations〉586 ≡1 protected: Stack<StackItem<T,A> > stack;

2 protected: Value<T,A> resultValue;

This code is used in ¶584.

¶586

〈Parser class template method declarations〉587 ≡1 protected: TableEntry getEntry(StateNumber, SymbolNumber,

2 MapEntry const*, TableEntry const*) const;

3 protected: TableEntry getAction(StateNumber, SymbolNumber) const;

4 protected: TableEntry getGoto(StateNumber, SymbolNumber) const;

5 protected: void shift(StateNumber, Token const&);

6 protected: void reduce(ProductionNumber);

7 protected: bool recover(TokenStream&, Token&);

8 protected: virtual bool performAction(ProductionNumber) = 0;

This code is used in ¶584.

¶587

〈Parser class template method declarations〉588 ≡1 public: Parser();

2 public: virtual ~Parser();

3 public: Value<T,A> parse(TokenStream&);

This code is used in ¶584.

¶588

Page 203: Book

22.4. Parser Class Template 189

22.4.2 Definitions

〈Parser class template method definitions〉589 ≡1 template <typename T, typename A, typename GT>

2 TableEntry Parser<T,A,GT>::getEntry(

3 StateNumber state, SymbolNumber symbol,

4 MapEntry const* map, TableEntry const* table) const

5 {

6 uint16_t const start = map[state].start;

7 uint8_t const count = map[state].count;

8 for (uint8_t i = 0; i < count; ++i)

9 if (table[start + i].symbol == symbol)

10 return table[start + i];

11 TableEntry result;

12 result.symbol = symbol;

13 result.action = error;

14 result.number = 0;

15 return result;

16 }

This code is used in ¶599.

¶589

〈Parser class template method definitions〉590 ≡1 template <typename T, typename A, typename GT>

2 TableEntry Parser<T,A,GT>::getAction(

3 StateNumber state, SymbolNumber symbol) const

4 {

5 return getEntry(state, symbol, GT::actionMap, GT::actionTable);

6 }

This code is used in ¶599.

¶590

〈Parser class template method definitions〉591 ≡1 template <typename T, typename A, typename GT>

2 TableEntry Parser<T,A,GT>::getGoto(

3 StateNumber state, SymbolNumber symbol) const

4 {

5 return getEntry(state, symbol, GT::gotoMap, GT::gotoTable);

6 }

This code is used in ¶599.

¶591

〈Parser class template method definitions〉592 ≡1 template <typename T, typename A, typename GT>

2 Parser<T,A,GT>::Parser() :

3 stack(),

4 resultValue()

5 {}

This code is used in ¶599.

¶592

Page 204: Book

190 Chapter 22. Parser Classes

〈Parser class template method definitions〉593 ≡1 template <typename T, typename A, typename GT>

2 Parser<T,A,GT>::~Parser()

3 {}

This code is used in ¶599.

¶593

〈Parser class template method definitions〉594 ≡1 template <typename T, typename A, typename GT>

2 void Parser<T,A,GT>::shift(StateNumber state, T const& terminal)

3 {

4 stack.push(StackItem<T,A>(state, terminal));

5 }

This code is used in ¶599.

¶594

〈Parser class template method definitions〉595 ≡1 template <typename T, typename A, typename GT>

2 void Parser<T,A,GT>::reduce(ProductionNumber production)

3 {

4 ProductionIndex const length =

5 GT::rightHandSideTable[production];

6

7 stack.pop(length);

8

9 SymbolNumber const symbol =

10 GT::leftHandSideTable[production];

11 TableEntry const entry =

12 getGoto(stack.getTop().state, symbol);

13

14 stack.push(StackItem<T,A>(entry.number, resultValue));

15 }

This code is used in ¶599.

¶595

Page 205: Book

22.4. Parser Class Template 191

〈Parser class template method definitions〉596 ≡1 template <typename T, typename A, typename GT>

2 bool Parser<T,A,GT>::recover(TokenStream& tokenStream, T& nextToken)

3 {

4 uint16_t const depth = stack.getSize();

5 for (;;)

6 {

7 for (uint16_t i = 0; i < depth; ++i)

8 {

9 TableEntry const entry = getAction(

10 stack[i].state, nextToken.getType());

11 if (entry.action != error)

12 {

13 stack.pop(i);

14 return true;

15 }

16 }

17 if (nextToken.getType() == TokenTables::end_of_file)

18 return false;

19 cerr << "Gramatika: ignoring " << nextToken << "\n";

20 tokenStream >> nextToken;

21 }

22 }

This code is used in ¶599.

¶596

Page 206: Book

192 Chapter 22. Parser Classes

〈Parser class template method definitions〉597 ≡1 template <typename T, typename A, typename GT>

2 Value<T,A> Parser<T,A,GT>::parse(typename T::Stream& tokenStream)

3 {

4 stack.reset();

5

6 stack.push(StackItem<T,A>(0, Value<T,A>()));

7

8 T nextToken;

9 tokenStream >> nextToken;

10 for (;;)

11 {

12 TableEntry const entry = getAction(

13 stack.getTop().state, nextToken.getType());

14 if (entry.action == shiftAction)

15 {

16 shift(entry.number, nextToken);

17 tokenStream >> nextToken;

18 }

19 else if (entry.action == reduceAction)

20 {

21 resultValue = stack.getTop();

22 if (!performAction(entry.number))

23 break;

24 if (entry.number == 0)

25 break;

26 reduce(entry.number);

27 }

28 else

29 {

30 cerr << "Gramatika: Syntax error: unexpected " <<

31 nextToken << "\n";

32 if (!recover(tokenStream, nextToken))

33 break;

34 }

35 }

36 return resultValue;

37 }

This code is used in ¶599.

¶597

Page 207: Book

22.5. Files 193

�22.5 Files

〈File: Gramatika/Gramatika.h〉598 ≡1 #if !defined(_Gramatika_Gramatika_h)

2 #define _Gramatika_Gramatika_h

3

4 #include <stdint.h>

5 #include "Toolbox/Stack.h"

6

7 using namespace Toolbox;

8

9 namespace Gramatika

10 {

11 〈Gramatika type declarations〉553–55612 〈Gramatika constants〉557

13

14 〈TableEntry struct declaration〉55815 〈TableEntry struct inline method definitions〉56016

17 〈MapEntry struct declaration〉56118 〈MapEntry struct inline method definitions〉56319

20 〈SymbolType enumeration declaration〉56421 〈SymbolType enumeration inline method definitions〉56522

23 〈Action enumeration declaration〉56624 〈Action enumeration inline method definitions〉56725

26 〈Value class template declaration〉56827 〈Value class template inline method definitions〉572–57428

29 〈StackItem class template declaration〉57630 〈StackItem class template inline method definitions〉580–58231

32 〈Parser class template declaration〉58433 }

34

35 #include "Gramatika/Gramatika.cc"

36

37 #endif

¶598

Page 208: Book

194 Chapter 22. Parser Classes

〈File: Gramatika/Gramatika.cc〉599 ≡1 #if !defined(_Gramatika_Gramatika_cc)

2 #define _Gramatika_Gramatika_cc

3

4 #include <iostream>

5 #include "Gramatika/Gramatika.h"

6

7 namespace Gramatika

8 {

9 〈Value class template method definitions〉57510 〈StackItem class template method definitions〉58311 〈Parser class template method definitions〉589–59712 }

13

14 #endif

¶599

Page 209: Book

Chapter 23

Gramatika Main Program

�23.1 main Method Definition

〈Gramatika main method definition〉600 ≡1 using Lexis::Token;

2 using Lexis::TokenStream;

3 using GramatikaTokens::LexisTables;

4 using Gramatika::Value;

5 using Gramatika::SymbolTable;

6 using Gramatika::Automaton;

7 using Gramatika::ParseTable;

8 using Gramatika::ParserData;

9 using Gramatika::ItemAttributes;

10 using GramatikaGrammar::GramatikaTables;

11 using GramatikaGrammar::Parser;

12 using Toolbox::PathName;

13

14 string const gtkExtension(".gtk");

15 string const definitionFileName("gramadef.h");

16 string const actionFileName("gramactn.cc");

17 string const tableFileName("gramatbl.cc");

18

19 int main(int argc, char* argv[])

20 {

21 if (argc != 2)

22 {

23 cerr << "Usage: gramatik file[.lom]" << endl;

24 return 1;

25 }

26 〈read the Gramatika input file〉60127 〈construct the Gramatika tables〉60228 〈write the Gramatika output files〉60329 return 0;

30 }

This code is used in ¶607.

¶600

Page 210: Book

196 Chapter 23. Gramatika Main Program

〈read the Gramatika input file〉601 ≡1 cerr << "Reading input file:";

2 PathName const argument(argv[1]);

3 string gtkFile;

4

5 if (argument.getExtension() == gtkExtension)

6 gtkFile = argument;

7 else

8 gtkFile = argument + gtkExtension;

9

10 cerr << " " << gtkFile;

11 ifstream input(gtkFile.c_str());

12 if (!input)

13 {

14 cerr << " Can’t open " << gtkFile << endl;

15 return 1;

16 }

17 TokenStream<Token<LexisTables> > tin(input);

18 Parser<ParserData,Token<LexisTables>,ItemAttributes,GramatikaTables> parser;

19 Value<Token<LexisTables>,ItemAttributes> result = parser.parse(tin);

20 SymbolTable* symbolTable = result.symbolTable;

21 assert(symbolTable != 0);

22 cerr << "." << endl;

This code is used in ¶600.

¶601

〈construct the Gramatika tables〉602 ≡1 cerr << "Constructing tables:";

2 // cout << *symbolTable;

3 symbolTable->computeFirstSets();

4 Automaton automaton(*symbolTable);

5 //cout << automaton;

6 ParseTable parseTable(automaton);

7 cerr << "." << endl;

This code is used in ¶600.

¶602

〈write the Gramatika output files〉603 ≡1 cerr << "Writing output files:";

2 string const actionFile = argument.getHead() + actionFileName;

3 〈write the Gramatika action file〉6044 〈write the Gramatika definitions file〉6055 〈write the Gramatika table file〉6066 cerr << "." << endl;

This code is used in ¶600.

¶603

Page 211: Book

23.1. main Method Definition 197

〈write the Gramatika action file〉604 ≡1 cerr << " " << actionFile;

2 ofstream gramactn(actionFile.c_str());

3 if (!gramactn)

4 {

5 cerr << " Can’t open " << actionFile << endl;

6 return 1;

7 }

8 symbolTable->putActions(gramactn);

9 string const definitionFile =

10 argument.getHead() + definitionFileName;

This code is used in ¶603.

¶604

〈write the Gramatika definitions file〉605 ≡1 cerr << " " << definitionFile;

2 ofstream gramadef(definitionFile.c_str());

3 if (!gramadef)

4 {

5 cerr << " Can’t open " << definitionFile << endl;

6 return 1;

7 }

8 symbolTable->putPreamble(gramadef);

9 string const tableFile = argument.getHead() + tableFileName;

This code is used in ¶603.

¶605

〈write the Gramatika table file〉606 ≡1 cerr << " " << tableFile;

2 ofstream gramatbl(tableFile.c_str());

3 if (!gramatbl)

4 {

5 cerr << " Can’t open " << tableFile << endl;

6 return 1;

7 }

8 gramatbl << parseTable;

This code is used in ¶603.

¶606

Page 212: Book

198 Chapter 23. Gramatika Main Program

�23.2 File

〈File: Gramatika/main.cc〉607 ≡1 #include <iostream>

2 #include <fstream>

3 #include <string>

4 #include "Lexis/Lexis.h"

5 #include "lexistab.h"

6 #include "Gramatika/Gramatika.h"

7 #include "gramadef.h"

8 #include "Gramatika/Automaton.h"

9 #include "Gramatika/ParseTable.h"

10 #include "Toolbox/PathName.h"

11

12 〈Gramatika main method definition〉600

¶607

Page 213: Book

Part III

The Toolbox

Page 214: Book
Page 215: Book

Chapter 24

Character Utilities

�24.1 Declarations

〈Character utilities declaration〉608 ≡1 namespace Character

2 {

3 〈Character static method declarations〉6094 }

This code is used in ¶614.

¶608

〈Character static method declarations〉609 ≡1 bool isOctalDigit(char);

2 char octalToBinary(char);

3 char hexToBinary(char);

This code is used in ¶608.

¶609

�24.2 Definitions

〈Character utilities definition〉610 ≡1 namespace Character

2 {

3 〈Character static method definitions〉611–6134 }

This code is used in ¶615.

¶610

〈Character static method definitions〉611 ≡1 char octalToBinary(char c)

2 {

3 assert(isOctalDigit(c));

4 return c - ’0’;

5 }

This code is used in ¶610.

¶611

Page 216: Book

202 Chapter 24. Character Utilities

〈Character static method definitions〉612 ≡1 char hexToBinary(char c)

2 {

3 assert(isxdigit(c));

4 if (isdigit(c))

5 return c - ’0’;

6 else

7 return toupper(c) - ’A’ + 10;

8 }

This code is used in ¶610.

¶612

〈Character static method definitions〉613 ≡1 bool isOctalDigit(char c)

2 { return isdigit(c) && c < ’8’; }

This code is used in ¶610.

¶613

�24.3 Files

〈File: Toolbox/Character.h〉614 ≡1 #if !defined(_Toolbox_Character_h)

2 #define _Toolbox_Character_h

3

4 #include <string>

5

6 namespace Toolbox

7 {

8 〈Character utilities declaration〉6089 }

10

11 #endif

¶614

〈File: Toolbox/Character.cc〉615 ≡1 #include <ctype.h>

2 #include "Toolbox/Character.h"

3 #include "fixwarn.h"

4

5 namespace Toolbox

6 {

7 〈Character utilities definition〉6108 }

¶615

Page 217: Book

24.3. Files 203

〈File: Toolbox/fixwarn.h〉616 ≡1 #if !defined(_Toolbox_fixwarn_h)

2 #define _Toolbox_fixwarn_h

3

4 // Macro included from "/usr/include/ctype.h".

5 // Replace old-style casts with C++ casts.

6 #undef __isctype

7 #define __isctype(c, type) \

8 (__ctype_b[static_cast<int>(c)] & static_cast<unsigned short int>(type))

9

10 #endif

¶616

Page 218: Book

204 Chapter 24. Character Utilities

Page 219: Book

Chapter 25

String Utilities

�25.1 Declarations

〈String utilities declaration〉617 ≡1 namespace String

2 {

3 〈String static method declarations〉6184 }

This code is used in ¶623.

¶617

〈String static method declarations〉618 ≡1 uint16_t hash(string const& s);

2 uint16_t getWidth(string const& s);

This code is used in ¶617.

¶618

�25.2 Definitions

〈String utilities definition〉619 ≡1 namespace String

2 {

3 〈String constants〉620

4 〈String static method definitions〉621,6225 }

This code is used in ¶624.

¶619

〈String constants〉620 ≡1 uint16_t const UINT16_BITS = sizeof(uint16_t) * CHAR_BIT;

2 uint16_t const oneEighth = UINT16_BITS / 8;

3 uint16_t const threeQuarters = UINT16_BITS * 3 / 4;

4 uint16_t const highBits = (UINT16_MAX >> oneEighth);

This code is used in ¶619.

¶620

Page 220: Book

206 Chapter 25. String Utilities

〈String static method definitions〉621 ≡1 uint16_t hash(string const& s)

2 {

3 uint16_t hash = 0;

4

5 for (string::const_iterator ptr(s.begin()), lim(s.end());

6 ptr != lim; ++ptr)

7 {

8 hash = (hash << oneEighth) + *ptr;

9 uint16_t const top = hash & highBits;

10 if ((hash & highBits) != 0)

11 hash = (hash ^ (top >> threeQuarters)) & ~highBits;

12 }

13 return hash;

14 }

This code is used in ¶619.

¶621

〈String static method definitions〉622 ≡1 uint16_t getWidth(string const& s)

2 {

3 uint16_t result = 0;

4 for (string::const_iterator p(s.begin()), lim(s.end());

5 p != lim; ++p)

6 {

7 if (*p == ’\t’)

8 result = (result + 8U) & ~07U;

9 else if (isprint(*p))

10 result += 1U;

11 }

12 return result;

13 }

This code is used in ¶619.

¶622

�25.3 Files

〈File: Toolbox/String.h〉623 ≡1 #if !defined(_Toolbox_String_h)

2 #define _Toolbox_String_h

3

4 #include <string>

5 #include <stdint.h>

6

7 namespace Toolbox

8 {

9 〈String utilities declaration〉61710 }

11

12 #endif

¶623

Page 221: Book

25.3. Files 207

〈File: Toolbox/String.cc〉624 ≡1 #include <ctype.h>

2 #include <limits.h>

3 #include "Toolbox/String.h"

4 #include "Toolbox/fixwarn.h"

5

6 namespace Toolbox

7 {

8 〈String utilities definition〉6199 }

¶624

Page 222: Book

208 Chapter 25. String Utilities

Page 223: Book

Chapter 26

EscapeSequence Utilities

�26.1 Declarations

〈EscapeSequence utilities declaration〉625 ≡1 namespace EscapeSequence

2 {

3 〈EscapeSequence static method declarations〉6264 }

This code is used in ¶634.

¶625

〈EscapeSequence static method declarations〉626 ≡1 string encode(char, char = 0);

2 string encode(string const&, char = 0);

3 string decode(string const&);

This code is used in ¶625.

¶626

�26.2 Definitions

〈EscapeSequence utilities definition〉627 ≡1 namespace EscapeSequence

2 {

3 〈EscapeSequence static method definitions〉628,630,6314 }

This code is used in ¶635.

¶627

Page 224: Book

210 Chapter 26. EscapeSequence Utilities

〈EscapeSequence static method definitions〉628 ≡1 string encode(char c, char delimiter)

2 {

3 string result;

4

5 if (isprint(c))

6 {

7 if (c == ’\\’ || c == delimiter)

8 result += ’\\’;

9 result += c;

10 }

11 else

12 {

13 result += ’\\’;

14 switch (c)

15 {

16 case ’\n’: result += ’n’; break;

17 case ’\t’: result += ’t’; break;

18 case ’\b’: result += ’b’; break;

19 case ’\v’: result += ’v’; break;

20 case ’\r’: result += ’r’; break;

21 case ’\f’: result += ’f’; break;

22 case ’\a’: result += ’a’; break;

23 default:

24 〈encode octal escape sequence〉62925 break;

26 }

27 }

28 return result;

29 }

This code is used in ¶627.

¶628

〈encode octal escape sequence〉629 ≡1 {

2 char buffer[3];

3 int16_t count = 0;

4 do

5 {

6 assert(count < 3);

7 buffer[count++] = ’0’ + (c & 07);

8 c = static_cast<uint8_t>(c) >> 3;

9 }

10 while (c != 0);

11

12 do { result += buffer[--count]; }

13 while (count != 0);

14 }

This code is used in ¶628.

¶629

Page 225: Book

26.2. Definitions 211

〈EscapeSequence static method definitions〉630 ≡1 string encode(string const& s, char delimiter)

2 {

3 string result;

4 for(string::const_iterator p(s.begin()), lim(s.end());

5 p != lim; ++p)

6 result += encode(*p, delimiter);

7 return result;

8 }

This code is used in ¶627.

¶630

Page 226: Book

212 Chapter 26. EscapeSequence Utilities

〈EscapeSequence static method definitions〉631 ≡1 string decode(string const& s)

2 {

3 string result;

4 string::const_iterator ptr(s.begin()), lim(s.end());

5 while (ptr != lim)

6 {

7 char c = *ptr++;

8 if (c == ’\\’)

9 {

10 if (ptr == lim)

11 {

12 result += c;

13 break;

14 }

15 c = *ptr++;

16 switch (c)

17 {

18 case ’n’: c = ’\n’; break;

19 case ’t’: c = ’\t’; break;

20 case ’v’: c = ’\v’; break;

21 case ’b’: c = ’\b’; break;

22 case ’r’: c = ’\r’; break;

23 case ’f’: c = ’\f’; break;

24 case ’a’: c = ’\a’; break;

25 case ’0’: case ’1’: case ’2’: case ’3’:

26 case ’4’: case ’5’: case ’6’: case ’7’:

27 {

28 〈decode octal escape sequence〉63229 }

30 break;

31 case ’x’:

32 {

33 〈decode hex escape sequence〉63334 }

35 break;

36 default:

37 break;

38 }

39 }

40 result += c;

41 }

42 return result;

43 }

This code is used in ¶627.

¶631

Page 227: Book

26.3. Files 213

〈decode octal escape sequence〉632 ≡1 c = octalToBinary(c);

2 if (ptr != lim && isOctalDigit(*ptr))

3 {

4 c = (c << 3) + octalToBinary(*ptr++);

5 if (ptr != lim && isOctalDigit(*ptr))

6 c = (c << 3) + octalToBinary(*ptr++);

7 }

This code is used in ¶631.

¶632

〈decode hex escape sequence〉633 ≡1 if (ptr != lim && isxdigit(*ptr))

2 {

3 c = hexToBinary(*ptr++);

4 while (ptr != lim && isxdigit(*ptr))

5 {

6 c = (c << 4) + hexToBinary(*ptr++);

7 }

8 }

This code is used in ¶631.

¶633

�26.3 Files

〈File: Toolbox/EscapeSequence.h〉634 ≡1 #if !defined(_Toolbox_EscapeSequence_h)

2 #define _Toolbox_EscapeSequence_h

3

4 #include <string>

5 #include <ctype.h>

6 #include "fixwarn.h"

7

8 namespace Toolbox

9 {

10 〈EscapeSequence utilities declaration〉62511 }

12

13 #endif

¶634

Page 228: Book

214 Chapter 26. EscapeSequence Utilities

〈File: Toolbox/EscapeSequence.cc〉635 ≡1 #include <ctype.h>

2 #include <stdint.h>

3 #include "Toolbox/EscapeSequence.h"

4 #include "Toolbox/Character.h"

5 #include "fixwarn.h"

6

7 using namespace Toolbox::Character;

8

9 namespace Toolbox

10 {

11 〈EscapeSequence utilities definition〉62712 }

¶635

Page 229: Book

Chapter 27

PathName Class

�27.1 Declarations

〈PathName class declaration〉636 ≡1 class PathName : public string

2 {

3 〈PathName constants〉637

4 〈PathName method declarations〉6385 };

This code is used in ¶648.

¶636

〈PathName constants〉637 ≡1 private: enum Constants

2 {

3 dot = ’.’,

4

5 #if defined(unix)

6 separator = ’/’

7 #endif

8

9 #if defined(__MSDOS__)

10 separator = ’\\’

11 #endif

12 };

This code is used in ¶636.

¶637

Page 230: Book

216 Chapter 27. PathName Class

〈PathName method declarations〉638 ≡1 public: PathName();

2 public: PathName(string const&);

3 public: PathName(char const*);

4 public: PathName(char const*, uint16_t);

5 public: string getHead() const;

6 public: string getTail() const;

7 public: string getBase() const;

8 public: string getExtension() const;

9 public: string getRoot() const;

This code is used in ¶636.

¶638

�27.2 Definitions

〈PathName inline method definitions〉639 ≡1 inline PathName::PathName() :

2 string()

3 {}

This code is used in ¶648.

¶639

〈PathName inline method definitions〉640 ≡1 inline PathName::PathName(string const& _string) :

2 string(_string)

3 {}

This code is used in ¶648.

¶640

〈PathName inline method definitions〉641 ≡1 inline PathName::PathName(char const* _string) :

2 string(_string)

3 {}

This code is used in ¶648.

¶641

〈PathName inline method definitions〉642 ≡1 inline PathName::PathName(char const* _string, uint16_t _length) :

2 string(_string, _length)

3 {}

This code is used in ¶648.

¶642

Page 231: Book

27.2. Definitions 217

〈PathName method definitions〉643 ≡1 string PathName::getHead() const

2 {

3 int16_t pos = rfind(separator);

4 if (pos >= 0)

5 return substr(0, pos + 1);

6 else

7 return "";

8 }

This code is used in ¶649.

¶643

〈PathName method definitions〉644 ≡1 string PathName::getTail() const

2 {

3 int16_t pos = rfind(separator);

4 if (pos >= 0)

5 return substr(pos + 1);

6 else

7 return *this;

8 }

This code is used in ¶649.

¶644

〈PathName method definitions〉645 ≡1 string PathName::getBase() const

2 { return getHead() + getRoot(); }

This code is used in ¶649.

¶645

〈PathName method definitions〉646 ≡1 string PathName::getExtension() const

2 {

3 string const tail = getTail();

4 int16_t pos = rfind(dot);

5 if (pos >= 0)

6 return tail.substr(pos);

7 else

8 return "";

9 }

This code is used in ¶649.

¶646

Page 232: Book

218 Chapter 27. PathName Class

〈PathName method definitions〉647 ≡1 string PathName::getRoot() const

2 {

3 string const tail = getTail();

4 int16_t pos = rfind(dot);

5 if (pos >= 0)

6 return tail.substr(0, pos);

7 else

8 return tail;

9 }

This code is used in ¶649.

¶647

�27.3 Files

〈File: Toolbox/PathName.h〉648 ≡1 #if !defined(_Toolbox_PathName_h)

2 #define _Toolbox_PathName_h

3

4 #include <string>

5 #include <stdint.h>

6

7 namespace Toolbox

8 {

9 〈PathName class declaration〉63610 〈PathName inline method definitions〉639–64211 }

12

13 #endif

¶648

〈File: Toolbox/PathName.cc〉649 ≡1 #include <iostream.h>

2 #include <assert.h>

3 #include "Toolbox/PathName.h"

4

5 namespace Toolbox

6 {

7 〈PathName method definitions〉643–6478 }

¶649

Page 233: Book

Chapter 28

Stack Class Template

�28.1 Declarations

〈Stack class declaration〉650 ≡1 template <typename T>

2 class Stack

3 {

4 〈Stack type definitions〉6515 〈Stack field declarations〉6526 〈Stack method declarations〉6537 };

This code is used in ¶664.

¶650

〈Stack type definitions〉651 ≡1 public: typedef T ElementType;

This code is used in ¶650.

¶651

〈Stack field declarations〉652 ≡1 private: vector<T> data;

This code is used in ¶650.

¶652

〈Stack method declarations〉653 ≡1 public: Stack();

2 public: T pop(uint16_t = 1);

3 public: void push(T);

4 public: void reset();

5 public: bool isEmpty() const;

6 public: T const& operator[](uint16_t) const;

7 public: T const& getTop() const;

8 public: uint16_t getSize() const;

9 public: ostream& put(ostream&) const;

This code is used in ¶650.

¶653

Page 234: Book

220 Chapter 28. Stack Class Template

�28.2 Definitions

〈Stack method definitions〉654 ≡1 template <typename T>

2 Stack<T>::Stack() :

3 data()

4 {}

This code is used in ¶665.

¶654

〈Stack method definitions〉655 ≡1 template <typename T>

2 void Stack<T>::push(T item)

3 { data.push_back(item); }

This code is used in ¶665.

¶655

〈Stack method definitions〉656 ≡1 template <typename T>

2 T Stack<T>::pop(uint16_t count)

3 {

4 assert(data.size() >= count);

5 T result;

6 for (uint16_t i = 0; i < count; ++i)

7 {

8 result = data.back();

9 data.pop_back();

10 }

11 return result;

12 }

This code is used in ¶665.

¶656

〈Stack method definitions〉657 ≡1 template <typename T>

2 void Stack<T>::reset()

3 { data.resize(0); }

This code is used in ¶665.

¶657

〈Stack method definitions〉658 ≡1 template <typename T>

2 bool Stack<T>::isEmpty() const

3 { return data.empty(); }

This code is used in ¶665.

¶658

Page 235: Book

28.2. Definitions 221

〈Stack method definitions〉659 ≡1 template <typename T>

2 T const& Stack<T>::operator[](uint16_t i) const

3 {

4 assert(i < data.size());

5 return data[data.size() - (i + 1)];

6 }

This code is used in ¶665.

¶659

〈Stack method definitions〉660 ≡1 template <typename T>

2 ostream& Stack<T>::put(ostream& s) const

3 {

4 for (uint16_t i = data.size(); i > 0; --i)

5 s << data[i - 1] << endl;

6 return s;

7 }

This code is used in ¶665.

¶660

〈Stack method definitions〉661 ≡1 template <typename T>

2 T const& Stack<T>::getTop() const

3 {

4 assert(data.size() > 0);

5 return data.back();

6 }

This code is used in ¶665.

¶661

〈Stack method definitions〉662 ≡1 template <typename T>

2 uint16_t Stack<T>::getSize() const

3 { return data.size(); }

This code is used in ¶665.

¶662

〈Stack inline method definitions〉663 ≡1 template <typename T>

2 inline ostream& operator<<(

3 ostream& stream, Stack<T> const& stack)

4 { return stack.put(stream); }

This code is used in ¶664.

¶663

Page 236: Book

222 Chapter 28. Stack Class Template

�28.3 Files

〈File: Toolbox/Stack.h〉664 ≡1 #if !defined(_Toolbox_Stack_h)

2 #define _Toolbox_Stack_h

3

4 #include <iostream>

5 #include <vector>

6 #include <stdint.h>

7

8 namespace Toolbox

9 {

10 〈Stack class declaration〉65011 〈Stack inline method definitions〉66312 }

13

14 #include "Toolbox/Stack.cc"

15

16 #endif

¶664

〈File: Toolbox/Stack.cc〉665 ≡1 #if !defined(_Toolbox_Stack_cc)

2 #define _Toolbox_Stack_cc

3

4 #include <stdint.h>

5 #include "Toolbox/Stack.h"

6

7 namespace Toolbox

8 {

9 〈Stack method definitions〉654–66210 }

11

12 #endif

¶665

Page 237: Book

Chapter 29

Set Class and its Iterator

�29.1 Set Class

29.1.1 Declarations

〈Set class declaration〉666 ≡1 class Set

2 {

3 〈Set iterator declaration〉6964 〈Set constants〉667

5 〈Set field declarations〉6686 〈Set method declarations〉669,6707 friend class const_iterator;

8 };

This code is used in ¶706.

¶666

〈Set constants〉667 ≡1 protected: enum Constants

2 {

3 divShift = 3,

4 modMask = 0x07

5 };

This code is used in ¶666.

¶667

〈Set field declarations〉668 ≡1 protected: vector<uint8_t> data;

2 protected: uint8_t fill;

This code is used in ¶666.

¶668

〈Set method declarations〉669 ≡1 protected: Set& enlarge(uint16_t);

This code is used in ¶666.

¶669

Page 238: Book

224 Chapter 29. Set Class and its Iterator

〈Set method declarations〉670 ≡1 public: explicit Set(uint16_t = 1);

2 public: ~Set();

3 public: Set(Set const&);

4 public: Set& operator=(Set const&);

5 public: Set& operator=(uint16_t);

6 public: Set operator|(Set const&) const;

7 public: Set& operator|=(Set const&);

8 public: Set operator&(Set const&) const;

9 public: Set& operator&=(Set const&);

10 public: Set operator^(Set const&) const;

11 public: Set& operator^=(Set const&);

12 public: Set& operator+=(uint16_t);

13 public: Set& operator-=(uint16_t);

14 public: Set operator~() const;

15 public: bool operator==(Set const&) const;

16 public: bool operator!=(Set const&) const;

17 public: bool operator<=(Set const&) const;

18 public: bool operator==(int) const;

19 public: bool operator!=(int) const;

20 public: bool contains(uint16_t) const;

21 public: uint32_t getSize() const;

22 public: ostream& put(ostream&) const;

23 public: const_iterator begin() const;

This code is used in ¶666.

¶670

29.1.2 Definitions

〈Set inline method definitions〉671 ≡1 inline ostream& operator<<(ostream& stream, Set const& set)

2 { return set.put(stream); }

This code is used in ¶706.

¶671

〈Set method definitions〉672 ≡1 Set::Set(uint16_t size) :

2 data((size + CHAR_BIT - 1U) >> divShift, 0),

3 fill(0)

4 {}

This code is used in ¶707.

¶672

〈Set method definitions〉673 ≡1 Set::~Set()

2 {}

This code is used in ¶707.

¶673

Page 239: Book

29.1. Set Class 225

〈Set method definitions〉674 ≡1 Set::Set(Set const& s) :

2 data(s.data),

3 fill(s.fill)

4 {}

This code is used in ¶707.

¶674

〈Set method definitions〉675 ≡1 Set& Set::enlarge(uint16_t size)

2 {

3 if (data.size() < size)

4 {

5 data.resize(size, fill);

6 }

7 return *this;

8 }

This code is used in ¶707.

¶675

〈Set method definitions〉676 ≡1 Set& Set::operator=(Set const& s)

2 {

3 if (this != &s)

4 {

5 data = s.data;

6 fill = s.fill;

7 }

8 return *this;

9 }

This code is used in ¶707.

¶676

〈Set method definitions〉677 ≡1 Set& Set::operator=(uint16_t arg)

2 {

3 assert(arg == 0);

4 for (uint16_t i = 0; i < data.size(); ++i)

5 data[i] = 0;

6 fill = 0;

7 return *this;

8 }

This code is used in ¶707.

¶677

Page 240: Book

226 Chapter 29. Set Class and its Iterator

〈Set method definitions〉678 ≡1 Set Set::operator|(Set const& s) const

2 {

3 Set result(*this);

4 result |= s;

5 return result;

6 }

This code is used in ¶707.

¶678

〈Set method definitions〉679 ≡1 Set& Set::operator|=(Set const& s)

2 {

3 enlarge(s.data.size());

4 uint16_t i;

5 for (i = 0; i < s.data.size(); ++i)

6 data[i] |= s.data[i];

7 for ( ; i < data.size(); ++i)

8 data[i] |= s.fill;

9 fill |= s.fill;

10 return *this;

11 }

This code is used in ¶707.

¶679

〈Set method definitions〉680 ≡1 Set Set::operator&(Set const& s) const

2 {

3 Set result(*this);

4 result &= s;

5 return result;

6 }

This code is used in ¶707.

¶680

〈Set method definitions〉681 ≡1 Set& Set::operator&=(Set const& s)

2 {

3 enlarge(s.data.size());

4 uint16_t i;

5 for (i = 0; i < s.data.size(); ++i)

6 data[i] &= s.data[i];

7 for ( ; i < data.size(); ++i)

8 data[i] &= s.fill;

9 fill &= s.fill;

10 return *this;

11 }

This code is used in ¶707.

¶681

Page 241: Book

29.1. Set Class 227

〈Set method definitions〉682 ≡1 Set Set::operator^(Set const& s) const

2 {

3 Set result(*this);

4 result ^= s;

5 return result;

6 }

This code is used in ¶707.

¶682

〈Set method definitions〉683 ≡1 Set& Set::operator^=(Set const& s)

2 {

3 enlarge(s.data.size());

4 uint16_t i;

5 for (i = 0; i < s.data.size(); ++i)

6 data[i] ^= s.data[i];

7 for ( ; i < data.size(); ++i)

8 data[i] ^= s.fill;

9 fill ^= s.fill;

10 return *this;

11 }

This code is used in ¶707.

¶683

〈Set method definitions〉684 ≡1 Set& Set::operator+=(uint16_t i)

2 {

3 uint16_t const byte = i >> divShift;

4 uint16_t const bit = i & modMask;

5 enlarge(byte + 1);

6 data[byte] |= 1 << bit;

7 return *this;

8 }

This code is used in ¶707.

¶684

〈Set method definitions〉685 ≡1 Set& Set::operator-=(uint16_t i)

2 {

3 uint16_t const byte = i >> divShift;

4 uint16_t const bit = i & modMask;

5 enlarge(byte + 1);

6 data[byte] &= ~(1 << bit);

7 return *this;

8 }

This code is used in ¶707.

¶685

Page 242: Book

228 Chapter 29. Set Class and its Iterator

〈Set method definitions〉686 ≡1 Set Set::operator~() const

2 {

3 Set result(*this);

4 for (uint16_t i = 0; i < result.data.size(); ++i)

5 result.data[i] = ~result.data[i];

6 result.fill = ~result.fill;

7 return result;

8 }

This code is used in ¶707.

¶686

〈Set method definitions〉687 ≡1 bool Set::operator==(Set const& s) const

2 {

3 if (data.size() <= s.data.size())

4 {

5 uint16_t i;

6 for (i = 0; i < data.size(); ++i)

7 if (data[i] != s.data[i])

8 return false;

9 for ( ; i < s.data.size(); ++i)

10 if (fill != s.data[i])

11 return false;

12 }

13 else

14 {

15 uint16_t i;

16 for (i = 0; i < s.data.size(); ++i)

17 if (data[i] != s.data[i])

18 return false;

19 for ( ; i < data.size(); ++i)

20 if (data[i] != s.fill)

21 return false;

22 }

23 if (fill != s.fill)

24 return false;

25 return true;

26 }

This code is used in ¶707.

¶687

〈Set method definitions〉688 ≡1 bool Set::operator!=(Set const& s) const

2 { return !Set::operator==(s); }

This code is used in ¶707.

¶688

Page 243: Book

29.1. Set Class 229

〈Set method definitions〉689 ≡1 bool Set::operator<=(Set const& s) const

2 {

3 if (data.size() <= s.data.size())

4 {

5 uint16_t i;

6 for (i = 0; i < data.size(); ++i)

7 if ((data[i] & s.data[i]) != data[i])

8 return false;

9 for ( ; i < s.data.size(); ++i)

10 if ((fill & s.data[i]) != fill)

11 return false;

12 }

13 else

14 {

15 uint16_t i;

16 for (i = 0; i < s.data.size(); ++i)

17 if ((data[i] & s.data[i]) != data[i])

18 return false;

19 for ( ; i < data.size(); ++i)

20 if ((data[i] & s.fill) != data[i])

21 return false;

22 }

23 if ((fill & s.fill) != fill)

24 return false;

25 return true;

26 }

This code is used in ¶707.

¶689

〈Set method definitions〉690 ≡1 bool Set::operator==(int arg) const

2 {

3 assert(arg == 0);

4 for (uint16_t i = 0; i < data.size(); ++i)

5 if (data[i] != 0)

6 return false;

7 if (fill != 0)

8 return false;

9 return true;

10 }

This code is used in ¶707.

¶690

〈Set method definitions〉691 ≡1 bool Set::operator!=(int arg) const

2 { return !Set::operator==(arg); }

This code is used in ¶707.

¶691

Page 244: Book

230 Chapter 29. Set Class and its Iterator

〈Set method definitions〉692 ≡1 bool Set::contains(uint16_t i) const

2 {

3 uint16_t const byte = i >> divShift;

4 uint16_t const bit = i & modMask;

5 if (byte < data.size())

6 return (data[byte] & (1 << bit)) != 0;

7 else

8 return fill != 0;

9 }

This code is used in ¶707.

¶692

〈Set method definitions〉693 ≡1 uint32_t Set::getSize() const

2 {

3 uint32_t result = 0;

4 for (uint16_t i = 0; i < data.size(); ++i)

5 {

6 uint8_t c = data[i];

7 while (c != 0)

8 {

9 c = c & (c ^ ((~c) + 1));

10 result += 1;

11 }

12 }

13 if (fill != 0)

14 result += UINT16_MAX + 1L - (data.size() << divShift);

15 return result;

16 }

This code is used in ¶707.

¶693

Page 245: Book

29.1. Set Class 231

〈Set method definitions〉694 ≡1 ostream& Set::put(ostream& s) const

2 {

3 bool comma = false;

4

5 s << "{";

6 for (uint16_t i = 0; i < data.size(); ++i)

7 {

8 for (uint16_t bit = 0; bit < CHAR_BIT; ++bit)

9 {

10 if ((data[i] & (1 << bit)) != 0)

11 {

12 if (comma)

13 s << ",";

14 s << ((i << divShift) | bit);

15 comma = true;

16 }

17 }

18 }

19 if (fill != 0)

20 s << "...";

21 s << "}";

22 return s;

23 }

This code is used in ¶707.

¶694

Page 246: Book

232 Chapter 29. Set Class and its Iterator

〈Set method definitions〉695 ≡1 Set::const_iterator Set::begin() const

2 {

3 for (uint16_t i = 0; i < data.size(); ++i)

4 {

5 for (uint16_t bit = 0; bit < CHAR_BIT; ++bit)

6 {

7 if ((data[i] & (1 << bit)) != 0)

8 {

9 uint16_t referent = (i << divShift) | bit;

10 return const_iterator(this, referent);

11 }

12 }

13 }

14 if (fill != 0)

15 {

16 uint16_t referent = data.size() << divShift;

17 return const_iterator(this, referent);

18 }

19 else

20 {

21 return const_iterator(this, UINT16_MAX);

22 }

23 }

This code is used in ¶707.

¶695

�29.2 Set Iterator Class

29.2.1 Declarations

〈Set iterator declaration〉696 ≡1 public: class const_iterator

2 {

3 〈Set iterator field declarations〉6974 〈Set iterator method declarations〉6985 friend class Set;

6 };

This code is used in ¶666.

¶696

〈Set iterator field declarations〉697 ≡1 private: Set const* set;

2 private: uint16_t referent;

This code is used in ¶696.

¶697

Page 247: Book

29.2. Set Iterator Class 233

〈Set iterator method declarations〉698 ≡1 public: const_iterator();

2 public: const_iterator(Set const*, uint16_t);

3 public: const_iterator(const_iterator const& i);

4 public: uint16_t operator*() const;

5 public: const_iterator& operator++();

6 public: bool operator!=(const_iterator const&) const;

7 public: bool operator<(const_iterator const&) const;

This code is used in ¶696.

¶698

29.2.2 Definitions

〈Set iterator inline method definitions〉699 ≡1 inline Set::const_iterator::const_iterator() :

2 set(0),

3 referent(UINT16_MAX)

4 {}

This code is used in ¶706.

¶699

〈Set iterator inline method definitions〉700 ≡1 inline Set::const_iterator::const_iterator(const_iterator const& i) :

2 set(i.set),

3 referent(i.referent)

4 {}

This code is used in ¶706.

¶700

〈Set iterator inline method definitions〉701 ≡1 inline Set::const_iterator::const_iterator(

2 Set const* _set, uint16_t _referent) :

3 set(_set),

4 referent(_referent)

5 {}

This code is used in ¶706.

¶701

〈Set iterator inline method definitions〉702 ≡1 inline uint16_t Set::const_iterator::operator*() const

2 { return referent; }

This code is used in ¶706.

¶702

〈Set iterator inline method definitions〉703 ≡1 inline bool Set::const_iterator::operator!=(const_iterator const& i) const

2 { return referent != i.referent; }

This code is used in ¶706.

¶703

Page 248: Book

234 Chapter 29. Set Class and its Iterator

〈Set iterator inline method definitions〉704 ≡1 inline bool Set::const_iterator::operator<(const_iterator const& i) const

2 { return referent < i.referent; }

This code is used in ¶706.

¶704

〈Set iterator method definitions〉705 ≡1 Set::const_iterator& Set::const_iterator::operator++()

2 {

3 ++referent;

4 for (;;)

5 {

6 uint16_t const byte = referent >> Set::divShift;

7 if (byte < set->data.size())

8 {

9 uint16_t const bit = referent & Set::modMask;

10 if ((set->data[byte] & (1 << bit)) != 0)

11 {

12 break;

13 }

14 }

15 else

16 {

17 if (set->fill == 0)

18 {

19 referent = UINT16_MAX;

20 }

21 break;

22 }

23 ++referent;

24 }

25 return *this;

26 }

This code is used in ¶707.

¶705

Page 249: Book

29.3. Files 235

�29.3 Files

〈File: Toolbox/Set.h〉706 ≡1 #if !defined(_Toolbox_Set_h)

2 #define _Toolbox_Set_h

3

4 #include <iostream>

5 #include <vector>

6 #include <stdint.h>

7

8 namespace Toolbox

9 {

10 〈Set class declaration〉666

11 〈Set inline method definitions〉67112 〈Set iterator inline method definitions〉699–70413 }

14

15 #endif

¶706

〈File: Toolbox/Set.cc〉707 ≡1 #include <iostream.h>

2 #include <stdio.h>

3 #include <stdint.h>

4 #include "Toolbox/Set.h"

5

6 namespace Toolbox

7 {

8 〈Set method definitions〉672–6959 〈Set iterator method definitions〉705

10 }

¶707

Page 250: Book

236 Chapter 29. Set Class and its Iterator

Page 251: Book

Chapter 30

CharacterSet Class

�30.1 Declarations

〈CharacterSet class declaration〉708 ≡1 class CharacterSet : public Set

2 {

3 〈CharacterSet method declarations〉7094 };

This code is used in ¶715.

¶708

〈CharacterSet method declarations〉709 ≡1 public: explicit CharacterSet(uint16_t = 1);

2 public: CharacterSet(Set const&);

3 public: CharacterSet(CharacterSet const&);

4 public: ostream& put(ostream&) const;

This code is used in ¶708.

¶709

�30.2 Definitions

〈CharacterSet inline method definitions〉710 ≡1 inline CharacterSet::CharacterSet(uint16_t size) :

2 Set(size)

3 {}

This code is used in ¶715.

¶710

〈CharacterSet inline method definitions〉711 ≡1 inline CharacterSet::CharacterSet(Set const& s) :

2 Set(s)

3 {}

This code is used in ¶715.

¶711

Page 252: Book

238 Chapter 30. CharacterSet Class

〈CharacterSet inline method definitions〉712 ≡1 inline CharacterSet::CharacterSet(CharacterSet const& s) :

2 Set(s)

3 {}

This code is used in ¶715.

¶712

〈CharacterSet inline method definitions〉713 ≡1 inline ostream& operator<<(ostream& stream, CharacterSet const& set)

2 { return set.put(stream); }

This code is used in ¶715.

¶713

〈CharacterSet method definitions〉714 ≡1 ostream& CharacterSet::put(ostream& s) const

2 {

3 s << "{\"";

4 for (uint16_t i = 0; i < data.size(); ++i)

5 {

6 for (uint16_t bit = 0; bit < CHAR_BIT; ++bit)

7 {

8 if ((data[i] & (1 << bit)) != 0)

9 s << EscapeSequence::encode(

10 (i << divShift) | bit, ’"’);

11 }

12 }

13 s << "\"";

14 if (fill != 0)

15 s << "...";

16 s << "}";

17 return s;

18 }

This code is used in ¶716.

¶714

�30.3 Files

〈File: Toolbox/CharacterSet.h〉715 ≡1 #if !defined(_Toolbox_CharacterSet_h)

2 #define _Toolbox_CharacterSet_h

3

4 #include <stdint.h>

5 #include "Toolbox/Set.h"

6

7 namespace Toolbox

8 {

9 〈CharacterSet class declaration〉70810 〈CharacterSet inline method definitions〉710–71311 }

12

13 #endif

¶715

Page 253: Book

30.3. Files 239

〈File: Toolbox/CharacterSet.cc〉716 ≡1 #include <iostream.h>

2 #include <limits.h>

3 #include "Toolbox/CharacterSet.h"

4 #include "Toolbox/EscapeSequence.h"

5

6 using namespace Toolbox;

7

8 namespace Toolbox

9 {

10 〈CharacterSet method definitions〉71411 }

¶716

Page 254: Book

240 Chapter 30. CharacterSet Class

Page 255: Book

Chapter 31

List Utilities

�31.1 Declarations

〈List utilities declaration〉717 ≡1 namespace List

2 {

3 〈List static method declarations〉718–7214 }

This code is used in ¶727.

¶717

〈List static method declarations〉718 ≡1 template <typename T>

2 bool equals(list<T> const& left, list<T> const& right);

This code is used in ¶717.

¶718

〈List static method declarations〉719 ≡1 template <typename T>

2 void appendTo(list<T>& first, list<T> const& second);

This code is used in ¶717.

¶719

〈List static method declarations〉720 ≡1 template <typename T>

2 void sortedInsert(list<T>& lst, T i);

This code is used in ¶717.

¶720

〈List static method declarations〉721 ≡1 template <typename T>

2 ostream& put(ostream& s, list<T> const& l);

This code is used in ¶717.

¶721

Page 256: Book

242 Chapter 31. List Utilities

�31.2 Definitions

〈List utilities definition〉722 ≡1 namespace List

2 {

3 〈List static method definitions〉723–7264 }

This code is used in ¶728.

¶722

〈List static method definitions〉723 ≡1 template <typename T>

2 bool equals(list<T> const& left, list<T> const& right)

3 {

4 typename list<T>::const_iterator lptr(left.begin());

5 typename list<T>::const_iterator rptr(right.begin());

6 while (lptr != left.end() && rptr != right.end())

7 {

8 if (!(*lptr == *rptr))

9 return false;

10 ++lptr;

11 ++rptr;

12 }

13 return true;

14 }

This code is used in ¶722.

¶723

〈List static method definitions〉724 ≡1 template <typename T>

2 void appendTo(list<T>& first, list<T> const& second)

3 {

4 assert(&first != &second);

5 for (typename list<T>::const_iterator ptr(second.begin()),

6 lim(second.end()); ptr != lim; ++ptr)

7 first.push_back(*ptr);

8 }

This code is used in ¶722.

¶724

Page 257: Book

31.2. Definitions 243

〈List static method definitions〉725 ≡1 template <typename T>

2 void sortedInsert(list<T>& lst, T i)

3 {

4 typename list<T>::iterator ptr(lst.begin());

5 typename list<T>::iterator lim(lst.end());

6 while (ptr != lim)

7 {

8 if (i <= *ptr)

9 {

10 break;

11 }

12 ++ptr;

13 }

14 if (ptr == lim)

15 lst.push_back(i);

16 else

17 lst.insert(ptr, i);

18 }

This code is used in ¶722.

¶725

〈List static method definitions〉726 ≡1 template <typename T>

2 ostream& put(ostream& s, list<T> const& l)

3 {

4 for (typename list<T>::const_iterator ptr(l.begin()),

5 lim(l.end()); ptr != lim; ++ptr)

6 {

7 s << *ptr << endl;

8 }

9 return s;

10 }

This code is used in ¶722.

¶726

Page 258: Book

244 Chapter 31. List Utilities

�31.3 Files

〈File: Toolbox/List.h〉727 ≡1 #if !defined(_Toolbox_List_h)

2 #define _Toolbox_List_h

3

4 #include <iostream>

5 #include <list>

6

7 namespace Toolbox

8 {

9 〈List utilities declaration〉71710 }

11

12 #include "Toolbox/List.cc"

13

14 #endif

¶727

〈File: Toolbox/List.cc〉728 ≡1 #if !defined(_Toolbox_List_cc)

2 #define _Toolbox_List_cc

3

4 #include <iostream>

5 #include <list>

6 #include "Toolbox/List.h"

7

8 namespace Toolbox

9 {

10 〈List utilities definition〉72211 }

12

13 #endif

¶728

Page 259: Book

Chapter 32

HashTable Class and its

Iterator

�32.1 HashTable Class

32.1.1 Declarations

〈HashTable class declaration〉729 ≡1 template <typename K, typename V>

2 class HashTable

3 {

4 〈HashTable type definitions〉7305 〈HashTable iterator declaration〉7446 〈HashTable constants〉7317 〈HashTable field declarations〉7328 〈HashTable method declarations〉7339 friend class const_iterator;

10 };

This code is used in ¶754.

¶729

〈HashTable type definitions〉730 ≡1 public: typedef K Key;

2 public: typedef V Value;

3 public: typedef pair<Key,Value> Pair;

4 public: typedef list<Pair> List;

5 public: typedef vector<List> Table;

This code is used in ¶729.

¶730

〈HashTable constants〉731 ≡1 private: enum Constants

2 {

3 defaultSize = 127

4 };

This code is used in ¶729.

¶731

Page 260: Book

246 Chapter 32. HashTable Class and its Iterator

〈HashTable field declarations〉732 ≡1 private: Table table;

This code is used in ¶729.

¶732

〈HashTable method declarations〉733 ≡1 public: explicit HashTable(uint16_t = defaultSize);

2 public: ~HashTable();

3 public: void add(K const&, V);

4 public: void remove(K const&);

5 public: void remove(K const&, V);

6 public: ostream& put(ostream&) const;

7 public: const_iterator find(K const&) const;

8 public: const_iterator begin() const;

This code is used in ¶729.

¶733

32.1.2 Definitions

〈HashTable inline method definitions〉734 ≡1 template <typename K, typename V>

2 inline ostream& operator<<(ostream& s, pair<K,V> const& pair)

3 { return s << pair.first << ":" << pair.second; }

This code is used in ¶754.

¶734

〈HashTable inline method definitions〉735 ≡1 template <typename K, typename V>

2 inline ostream& operator<<(ostream& s, HashTable<K,V> const& table)

3 { return table.put(s); }

This code is used in ¶754.

¶735

〈HashTable method definitions〉736 ≡1 template <typename K, typename V>

2 HashTable<K,V>::HashTable(uint16_t size) :

3 table(size)

4 { assert(size > 0); }

This code is used in ¶755.

¶736

〈HashTable method definitions〉737 ≡1 template <typename K, typename V>

2 HashTable<K,V>::~HashTable()

3 {}

This code is used in ¶755.

¶737

Page 261: Book

32.1. HashTable Class 247

〈HashTable method definitions〉738 ≡1 template <typename K, typename V>

2 void HashTable<K,V>::add(K const& k, V i)

3 {

4 uint16_t const h = hash(k) % table.size();

5 table[h].push_back(pair<K,V>(k, i));

6 }

This code is used in ¶755.

¶738

〈HashTable method definitions〉739 ≡1 template <typename K, typename V>

2 void HashTable<K,V>::remove(K const& k)

3 {

4 uint16_t const h = hash(k) % table.size();

5

6 for (typename List::iterator ptr(table[h].begin()),

7 lim(table[h].end()); ptr != lim; ++ptr)

8 {

9 if (ptr->first == k)

10 {

11 table[h].erase(ptr);

12 break;

13 }

14 }

15 }

This code is used in ¶755.

¶739

〈HashTable method definitions〉740 ≡1 template <typename K, typename V>

2 void HashTable<K,V>::remove(K const& k, V i)

3 {

4 uint16_t const h = hash(k) % table.size();

5

6 for (typename List::iterator ptr(table[h].begin()),

7 lim(table[h].end()); ptr != lim; ++ptr)

8 {

9 if (ptr->first == k && ptr->second == i)

10 {

11 table[h].erase(ptr);

12 break;

13 }

14 }

15 }

This code is used in ¶755.

¶740

Page 262: Book

248 Chapter 32. HashTable Class and its Iterator

〈HashTable method definitions〉741 ≡1 template <typename K, typename V>

2 ostream& HashTable<K,V>::put(ostream& s) const

3 {

4 s << "HashTable {\n";

5 for (uint16_t i = 0; i < table.size(); ++i)

6 {

7 for (typename List::const_iterator ptr(table[i].begin()),

8 lim(table[i].end()); ptr != lim; ++ptr)

9 {

10 s << *ptr << endl;

11 }

12 }

13 return s << "}\n";

14 }

This code is used in ¶755.

¶741

〈HashTable method definitions〉742 ≡1 template <typename K, typename V>

2 HashTable<K,V>::const_iterator HashTable<K,V>::begin() const

3 {

4 for (uint16_t i = 0; i < table.size(); ++i)

5 {

6 for (typename List::const_iterator ptr(table[i].begin()),

7 lim(table[i].end()); ptr != lim; ++ptr)

8 {

9 return const_iterator(this, i, ptr);

10 }

11 }

12 return const_iterator();

13 }

This code is used in ¶755.

¶742

Page 263: Book

32.2. HashTable Iterator 249

〈HashTable method definitions〉743 ≡1 template <typename K, typename V>

2 HashTable<K,V>::const_iterator HashTable<K,V>::find(

3 K const& k) const

4 {

5 uint16_t const h = hash(k) % table.size();

6

7 for (typename List::const_iterator ptr(table[h].begin()),

8 lim(table[h].end()); ptr != lim; ++ptr)

9 {

10 if (ptr->first == k)

11 {

12 return const_iterator(this, h, ptr);

13 }

14 }

15 return const_iterator();

16 }

This code is used in ¶755.

¶743

�32.2 HashTable Iterator

32.2.1 Declarations

〈HashTable iterator declaration〉744 ≡1 public: class const_iterator

2 {

3 〈HashTable iterator field declarations〉7454 〈HashTable iterator method declarations〉7465 };

This code is used in ¶729.

¶744

〈HashTable iterator field declarations〉745 ≡1 private: HashTable<K,V> const* hashTable;

2 private: uint16_t position;

3 private: typename List::const_iterator ptr;

This code is used in ¶744.

¶745

〈HashTable iterator method declarations〉746 ≡1 public: const_iterator();

2 public: const_iterator(

3 HashTable<K,V> const*, uint16_t,

4 typename List::const_iterator const&);

5 public: const_iterator(const_iterator const&);

6 public: V operator*() const;

7 public: const_iterator& operator++();

8 public: bool operator==(const_iterator const&) const;

9 public: bool operator!=(const_iterator const&) const;

This code is used in ¶744.

¶746

Page 264: Book

250 Chapter 32. HashTable Class and its Iterator

32.2.2 Definitions

〈HashTable iterator inline method definitions〉747 ≡1 template <typename K, typename V>

2 inline HashTable<K,V>::const_iterator::const_iterator() :

3 hashTable(0),

4 position(UINT16_MAX),

5 ptr()

6 {}

This code is used in ¶754.

¶747

〈HashTable iterator inline method definitions〉748 ≡1 template <typename K, typename V>

2 inline HashTable<K,V>::const_iterator::const_iterator(

3 HashTable<K,V> const* _hashTable,

4 uint16_t _position,

5 typename List::const_iterator const& _ptr) :

6 hashTable(_hashTable),

7 position(_position),

8 ptr(_ptr)

9 {}

This code is used in ¶754.

¶748

〈HashTable iterator inline method definitions〉749 ≡1 template <typename K, typename V>

2 inline HashTable<K,V>::const_iterator::const_iterator(

3 HashTable<K,V>::const_iterator const& i) :

4 hashTable(i.hashTable),

5 position(i.position),

6 ptr(i.ptr)

7 {}

This code is used in ¶754.

¶749

〈HashTable iterator inline method definitions〉750 ≡1 template <typename K, typename V>

2 inline V HashTable<K,V>::const_iterator::operator*() const

3 { return ptr->second; }

This code is used in ¶754.

¶750

〈HashTable iterator inline method definitions〉751 ≡1 template <typename K, typename V>

2 inline bool HashTable<K,V>::const_iterator::operator!=(

3 HashTable<K,V>::const_iterator const& i) const

4 { return !(*this == i); }

This code is used in ¶754.

¶751

Page 265: Book

32.2. HashTable Iterator 251

〈HashTable iterator method definitions〉752 ≡1 template <typename K, typename V>

2 bool HashTable<K,V>::const_iterator::operator==(

3 HashTable<K,V>::const_iterator const& i) const

4 {

5 bool result = false;

6 if (hashTable == 0 || i.hashTable == 0)

7 {

8 return hashTable == i.hashTable;

9 }

10 else

11 {

12 if (position >= hashTable->table.size() ||

13 i.position >= hashTable->table.size())

14 {

15 result = (position == i.position);

16 }

17 else

18 {

19 result = (position == i.position && ptr == i.ptr);

20 }

21 }

22 return result;

23 }

This code is used in ¶755.

¶752

Page 266: Book

252 Chapter 32. HashTable Class and its Iterator

〈HashTable iterator method definitions〉753 ≡1 template <typename K, typename V>

2 HashTable<K,V>::const_iterator&

3 HashTable<K,V>::const_iterator::operator++()

4 {

5 if (position < hashTable->table.size())

6 {

7 if (ptr != hashTable->table[position].end())

8 {

9 ++ptr;

10 if (ptr != hashTable->table[position].end())

11 return *this;

12 }

13 for (++position; position < hashTable->table.size(); ++position)

14 {

15 ptr = hashTable->table[position].begin();

16 if (ptr != hashTable->table[position].end())

17 return *this;

18 }

19 hashTable = 0;

20 position = UINT16_MAX;

21 ptr = List::const_iterator();

22 }

23 return *this;

24 }

This code is used in ¶755.

¶753

�32.3 Files

〈File: Toolbox/HashTable.h〉754 ≡1 #if !defined(_Toolbox_HashTable_h)

2 #define _Toolbox_HashTable_h

3

4 #include <iostream>

5 #include <vector>

6 #include <list>

7 #include <stdint.h>

8

9 namespace Toolbox

10 {

11 〈HashTable class declaration〉72912 〈HashTable inline method definitions〉734,73513 〈HashTable iterator inline method definitions〉747–75114 }

15

16 #include "Toolbox/HashTable.cc"

17

18 #endif

¶754

Page 267: Book

32.3. Files 253

〈File: Toolbox/HashTable.cc〉755 ≡1 #if !defined(_Toolbox_HashTable_cc)

2 #define _Toolbox_HashTable_cc

3

4 #include <iostream>

5 #include <stdint.h>

6 #include "Toolbox/HashTable.h"

7

8 namespace Toolbox

9 {

10

11 〈HashTable method definitions〉736–74312 〈HashTable iterator method definitions〉752,75313 }

14

15 #endif

¶755

Page 268: Book

254 Chapter 32. HashTable Class and its Iterator

Page 269: Book

Part IV

Loma—The Program

Page 270: Book
Page 271: Book

Chapter 33

Loma Input Specifications

�33.1 Loma Token Specifications

〈Loma token definitions〉756 ≡1 atDefine = ’@define’.

2 atEnd = ’@end’.

3 atExample = ’@example’.

4 atFile = ’@file’.

5 atInclude = ’@include’.

6 atLabel = ’@label’.

7 atSign = ’@@’.

8 atSymbol = ’@symbol’.

9 atString = ’@"’ { /!"\n/ | ’\\"’ } ’"’.

10 quotedString = ’"’ { /!"\n/ | ’\\"’ } ’"’.

11 word = /! \t\n@/ { /! \t\n/ }.

12 blank = ’ ’.

13 tab = ’\t’.

14 endOfLine = ’\n’.

This code is used in ¶757.

¶756

33.1.1 File

〈File: Loma/Loma.lex〉757 ≡1 Language:

2 LomaTokens.

3 Tokens:

4 〈Loma token definitions〉756

¶757

Page 272: Book

258 Chapter 33. Loma Input Specifications

�33.2 Loma Grammar Specification

〈Loma non-terminal definitions〉758 ≡1 codeFragment (fragment), codeModule (module),

2 codeSequence (fragmentList),

3 endDirective, exampleDirective, exampleModule (module),

4 file (file), fileModule (module),

5 includeFragment (fragment), includeDirective (text),

6 module (module), moduleList (moduleList),

7 string (text), textFragment (fragment),

8 textModule (module), textSequence (fragmentList),

9 wordList (wordList), ws (text).

This code is used in ¶829.

¶758

〈Loma start symbol definition〉759 ≡1 file.

This code is used in ¶829.

¶759

〈Loma grammar productions〉760 ≡1 file =

2 endOfLine moduleList

3 ‘‘

4 〈file actions〉7885 ’’.

This code is used in ¶829.

¶760

〈Loma grammar productions〉761 ≡1 moduleList =

2 ‘‘

3 〈moduleList case 1 actions〉7894 ’’

5 | moduleList module

6 ‘‘

7 〈moduleList case 2 actions〉7908 ’’.

This code is used in ¶829.

¶761

Page 273: Book

33.2. Loma Grammar Specification 259

〈Loma grammar productions〉762 ≡1 module =

2 textModule

3 ‘‘

4 〈module case 1 actions〉7915 ’’

6 | fileModule

7 ‘‘

8 〈module case 2 actions〉7929 ’’

10 | codeModule

11 ‘‘

12 〈module case 3 actions〉79313 ’’

14 | exampleModule

15 ‘‘

16 〈module case 4 actions〉79417 ’’.

This code is used in ¶829.

¶762

〈Loma grammar productions〉763 ≡1 textModule =

2 textSequence

3 ‘‘

4 〈textModule actions〉7955 ’’.

This code is used in ¶829.

¶763

〈Loma grammar productions〉764 ≡1 textSequence =

2 textFragment

3 ‘‘

4 〈textSequence actions〉7965 ’’.

This code is used in ¶829.

¶764

〈Loma grammar productions〉765 ≡1 textFragment =

2 ws endOfLine

3 ‘‘

4 〈textFragment case 1 actions〉7975 ’’

6 | ws wordList endOfLine

7 ‘‘

8 〈textFragment case 2 actions〉7989 ’’.

This code is used in ¶829.

¶765

Page 274: Book

260 Chapter 33. Loma Input Specifications

〈Loma grammar productions〉766 ≡1 wordList =

2 word

3 ‘‘

4 〈wordList case 1 actions〉7995 ’’

6 | quotedString

7 ‘‘

8 〈wordList case 2 actions〉8009 ’’

10 | atSign

11 ‘‘

12 〈wordList case 3 actions〉80113 ’’

14 | atString

15 ‘‘

16 〈wordList case 4 actions〉80217 ’’

18 | wordList word

19 ‘‘

20 〈wordList case 5 actions〉80321 ’’

22 | wordList quotedString

23 ‘‘

24 〈wordList case 6 actions〉80425 ’’

26 | wordList atSign

27 ‘‘

28 〈wordList case 7 actions〉80529 ’’

30 | wordList atString

31 ‘‘

32 〈wordList case 8 actions〉80633 ’’

34 | wordList blank

35 ‘‘

36 〈wordList case 9 actions〉80737 ’’

38 | wordList tab

39 ‘‘

40 〈wordList case 10 actions〉80841 ’’.

This code is used in ¶829.

¶766

Page 275: Book

33.2. Loma Grammar Specification 261

〈Loma grammar productions〉767 ≡1 codeSequence =

2 ‘‘

3 〈codeSequence case 1 actions〉8094 ’’

5 | codeSequence codeFragment

6 ‘‘

7 〈codeSequence case 2 actions〉8108 ’’

9 | codeSequence includeFragment

10 ‘‘

11 〈codeSequence case 3 actions〉81112 ’’.

This code is used in ¶829.

¶767

〈Loma grammar productions〉768 ≡1 codeFragment =

2 ws endOfLine

3 ‘‘

4 〈codeFragment case 1 actions〉8125 ’’

6 | ws wordList endOfLine

7 ‘‘

8 〈codeFragment case 2 actions〉8139 ’’

10 | ws wordList atLabel ws string ws endOfLine

11 ‘‘

12 〈codeFragment case 3 actions〉81413 ’’

14 | ws wordList atSymbol ws string ws endOfLine

15 ‘‘

16 〈codeFragment case 4 actions〉81517 ’’

18 | ws wordList atLabel ws string

19 ws atSymbol ws string ws endOfLine

20 ‘‘

21 〈codeFragment case 5 actions〉81622 ’’

23 | ws wordList atSymbol ws string

24 ws atLabel ws string ws endOfLine

25 ‘‘

26 〈codeFragment case 6 actions〉81727 ’’.

This code is used in ¶829.

¶768

Page 276: Book

262 Chapter 33. Loma Input Specifications

〈Loma grammar productions〉769 ≡1 includeFragment =

2 ws includeDirective

3 ‘‘

4 〈includeFragment actions〉8185 ’’.

This code is used in ¶829.

¶769

〈Loma grammar productions〉770 ≡1 includeDirective =

2 atInclude ws string ws endOfLine

3 ‘‘

4 〈includeDirective actions〉8195 ’’.

This code is used in ¶829.

¶770

〈Loma grammar productions〉771 ≡1 fileModule =

2 ws atFile ws string ws endOfLine codeSequence endDirective

3 ‘‘

4 〈fileModule case 1 actions〉8205 ’’

6 | ws atFile ws string ws string ws endOfLine codeSequence endDirective

7 ‘‘

8 〈fileModule case 2 actions〉8219 ’’.

This code is used in ¶829.

¶771

〈Loma grammar productions〉772 ≡1 codeModule =

2 ws atDefine ws string ws endOfLine codeSequence endDirective

3 ‘‘

4 〈codeModule case 1 actions〉8225 ’’

6 | ws atDefine ws string ws string ws endOfLine codeSequence endDirective

7 ‘‘

8 〈codeModule case 2 actions〉8239 ’’.

This code is used in ¶829.

¶772

〈Loma grammar productions〉773 ≡1 exampleModule =

2 exampleDirective codeSequence endDirective

3 ‘‘

4 〈exampleModule actions〉8245 ’’.

This code is used in ¶829.

¶773

Page 277: Book

33.2. Loma Grammar Specification 263

〈Loma grammar productions〉774 ≡1 exampleDirective =

2 ws atExample ws endOfLine.

This code is used in ¶829.

¶774

〈Loma grammar productions〉775 ≡1 endDirective =

2 ws atEnd ws endOfLine.

This code is used in ¶829.

¶775

〈Loma grammar productions〉776 ≡1 string =

2 quotedString

3 ‘‘

4 〈quotedString actions〉8255 ’’.

This code is used in ¶829.

¶776

〈Loma grammar productions〉777 ≡1 ws =

2 ‘‘

3 〈ws case 1 actions〉8264 ’’

5 | ws blank

6 ‘‘

7 〈ws case 2 actions〉8278 ’’

9 | ws tab

10 ‘‘

11 〈ws case 3 actions〉82812 ’’.

This code is used in ¶829.

¶777

Page 278: Book

264 Chapter 33. Loma Input Specifications

33.2.1 ParserData Class

〈ParserData class declaration〉778 ≡1 class ParserData {};

This code is used in ¶779.

¶778

〈File: Loma/ParserData.h〉779 ≡1 #if !defined(_Loma_ParserData_h)

2 #define _Loma_ParserData_h

3

4 namespace Loma

5 {

6 〈ParserData class declaration〉7787 }

8

9 #endif

¶779

33.2.2 ItemAttributes Class

〈WordList struct declaration〉780 ≡1 struct WordList

2 {

3 〈WordList struct field declarations〉7814 };

This code is used in ¶786.

¶780

〈WordList struct field declarations〉781 ≡1 public: string text;

2 public: string tag;

This code is used in ¶780.

¶781

〈ItemAttributes class declaration〉782 ≡1 class ItemAttributes

2 {

3 〈ItemAttributes field declarations〉7834 〈ItemAttributes method declarations〉7845 };

This code is used in ¶786.

¶782

Page 279: Book

33.2. Loma Grammar Specification 265

〈ItemAttributes field declarations〉783 ≡1 public: string text;

2 public: WordList wordList;

3 public: union

4 {

5 Fragment* fragment;

6 FragmentList* fragmentList;

7 Module* module;

8 ModuleList* moduleList;

9 File* file;

10 };

This code is used in ¶782.

¶783

〈ItemAttributes method declarations〉784 ≡1 public: ItemAttributes();

This code is used in ¶782.

¶784

〈ItemAttributes class inline method definitions〉785 ≡1 inline ItemAttributes::ItemAttributes() :

2 text(),

3 wordList(),

4 fragment(0)

5 {}

This code is used in ¶786.

¶785

〈File: Loma/ItemAttributes.h〉786 ≡1 #if !defined(_Loma_ItemAttributes_h)

2 #define _Loma_ItemAttributes_h

3

4 #include <string>

5 #include "Loma/File.h"

6 #include "Loma/Fragment.h"

7 #include "Loma/Module.h"

8

9 namespace Loma

10 {

11 〈WordList struct declaration〉78012 〈ItemAttributes class declaration〉78213 〈ItemAttributes class inline method definitions〉78514 }

15

16 #endif

¶786

Page 280: Book

266 Chapter 33. Loma Input Specifications

33.2.3 Actions

〈Loma actions preamble〉787 ≡1 #include <string>

2 #include "Loma/ParserData.h"

3 #include "Loma/ItemAttributes.h"

4

5 using namespace Loma;

This code is used in ¶829.

¶787

〈file actions〉788 ≡1 File* result = new File($moduleList);

2 assert(result != 0);

3 result->crossReference();

4 $$ = result;

This code is used in ¶760.

¶788

〈moduleList case 1 actions〉789 ≡1 ModuleList* const result = new ModuleList();

2 assert(result != 0);

3 $$ = result;

This code is used in ¶761.

¶789

〈moduleList case 2 actions〉790 ≡1 $moduleList->push_back($module);

2 $$ = $moduleList;

This code is used in ¶761.

¶790

〈module case 1 actions〉791 ≡1 $$ = $textModule;

This code is used in ¶762.

¶791

〈module case 2 actions〉792 ≡1 $$ = $fileModule;

2 cerr << ".";

This code is used in ¶762.

¶792

〈module case 3 actions〉793 ≡1 $$ = $codeModule;

2 cerr << ".";

This code is used in ¶762.

¶793

〈module case 4 actions〉794 ≡1 $$ = $exampleModule;

This code is used in ¶762.

¶794

Page 281: Book

33.2. Loma Grammar Specification 267

〈textModule actions〉795 ≡1 Module* const result = new TextModule($textSequence);

2 assert(result != 0);

3 $$ = result;

This code is used in ¶763.

¶795

〈textSequence actions〉796 ≡1 FragmentList* const result = new FragmentList();

2 assert(result != 0);

3 result->push_back($textFragment);

4 $$ = result;

This code is used in ¶764.

¶796

〈textFragment case 1 actions〉797 ≡1 Fragment* const result = new TextFragment($ws);

2 assert(result != 0);

3 $$ = result;

This code is used in ¶765.

¶797

〈textFragment case 2 actions〉798 ≡1 Fragment* const result = new TextFragment($ws + $wordList.text);

2 assert(result != 0);

3 $$ = result;

This code is used in ¶765.

¶798

〈wordList case 1 actions〉799 ≡1 WordList result;

2 result.text = $word.getLexeme();

3 $$ = result;

This code is used in ¶766.

¶799

〈wordList case 2 actions〉800 ≡1 WordList result;

2 result.text = $quotedString.getLexeme();

3 $$ = result;

This code is used in ¶766.

¶800

〈wordList case 3 actions〉801 ≡1 WordList result;

2 result.text = "@";

3 $$ = result;

This code is used in ¶766.

¶801

Page 282: Book

268 Chapter 33. Loma Input Specifications

〈wordList case 4 actions〉802 ≡1 WordList result;

2 string tag = $atString.getLexeme();

3 tag = tag.substr(2, tag.size()-3);

4 result.text = tag;

5 result.tag = tag;

6 $$ = result;

This code is used in ¶766.

¶802

〈wordList case 5 actions〉803 ≡1 WordList result = $wordList;

2 result.text += $word.getLexeme();

3 $$ = result;

This code is used in ¶766.

¶803

〈wordList case 6 actions〉804 ≡1 WordList result = $wordList;

2 result.text += $quotedString.getLexeme();

3 $$ = result;

This code is used in ¶766.

¶804

〈wordList case 7 actions〉805 ≡1 WordList result = $wordList;

2 result.text += "@";

3 $$ = result;

This code is used in ¶766.

¶805

〈wordList case 8 actions〉806 ≡1 WordList result = $wordList;

2 string tag = $atString.getLexeme();

3 tag = tag.substr(2, tag.size()-3);

4 result.text += tag;

5 result.tag = tag;

6 $$ = result;

This code is used in ¶766.

¶806

〈wordList case 9 actions〉807 ≡1 WordList result = $wordList;

2 result.text += $blank.getLexeme();

3 $$ = result;

This code is used in ¶766.

¶807

Page 283: Book

33.2. Loma Grammar Specification 269

〈wordList case 10 actions〉808 ≡1 WordList result = $wordList;

2 result.text += $tab.getLexeme();

3 $$ = result;

This code is used in ¶766.

¶808

〈codeSequence case 1 actions〉809 ≡1 FragmentList* const result = new FragmentList();

2 assert(result != 0);

3 $$ = result;

This code is used in ¶767.

¶809

〈codeSequence case 2 actions〉810 ≡1 $codeSequence->push_back($codeFragment);

2 $$ = $codeSequence;

This code is used in ¶767.

¶810

〈codeSequence case 3 actions〉811 ≡1 $codeSequence->push_back($includeFragment);

2 $$ = $codeSequence;

This code is used in ¶767.

¶811

〈codeFragment case 1 actions〉812 ≡1 Fragment* const result =

2 new CodeFragment($endOfLine.getLineNumber(), getWidth($ws),

3 "", "", "");

4 assert(result != 0);

5 $$ = result;

This code is used in ¶768.

¶812

〈codeFragment case 2 actions〉813 ≡1 Fragment* const result =

2 new CodeFragment($endOfLine.getLineNumber(), getWidth($ws),

3 $wordList.text, "", $wordList.tag);

4 assert(result != 0);

5 $$ = result;

This code is used in ¶768.

¶813

〈codeFragment case 3 actions〉814 ≡1 Fragment* const result =

2 new CodeFragment($endOfLine.getLineNumber(), getWidth($ws),

3 $wordList.text, $string#1, $wordList.tag);

4 assert(result != 0);

5 $$ = result;

This code is used in ¶768.

¶814

Page 284: Book

270 Chapter 33. Loma Input Specifications

〈codeFragment case 4 actions〉815 ≡1 Fragment* const result =

2 new CodeFragment($endOfLine.getLineNumber(), getWidth($ws),

3 $wordList.text, "", $string#1);

4 assert(result != 0);

5 $$ = result;

This code is used in ¶768.

¶815

〈codeFragment case 5 actions〉816 ≡1 Fragment* const result =

2 new CodeFragment($endOfLine.getLineNumber(), getWidth($ws),

3 $wordList.text, $string#1, $string#2);

4 assert(result != 0);

5 $$ = result;

This code is used in ¶768.

¶816

〈codeFragment case 6 actions〉817 ≡1 Fragment* const result =

2 new CodeFragment($endOfLine.getLineNumber(), getWidth($ws),

3 $wordList.text, $string#2, $string#1);

4 assert(result != 0);

5 $$ = result;

This code is used in ¶768.

¶817

〈includeFragment actions〉818 ≡1 Fragment* const result =

2 new IncludeFragment(getWidth($ws), $includeDirective);

3 assert(result != 0);

4 $$ = result;

This code is used in ¶769.

¶818

〈includeDirective actions〉819 ≡1 $$ = $string;

This code is used in ¶770.

¶819

〈fileModule case 1 actions〉820 ≡1 Module* const result =

2 new FileModule($string, $codeSequence);

3 assert(result != 0);

4 $$ = result;

This code is used in ¶771.

¶820

Page 285: Book

33.2. Loma Grammar Specification 271

〈fileModule case 2 actions〉821 ≡1 Module* const result =

2 new FileModule($string#1, $string#2, $codeSequence);

3 assert(result != 0);

4 $$ = result;

This code is used in ¶771.

¶821

〈codeModule case 1 actions〉822 ≡1 Module* const result =

2 new CodeModule($string, $codeSequence);

3 assert(result != 0);

4 $$ = result;

This code is used in ¶772.

¶822

〈codeModule case 2 actions〉823 ≡1 Module* const result =

2 new CodeModule($string#1, $string#2, $codeSequence);

3 assert(result != 0);

4 $$ = result;

This code is used in ¶772.

¶823

〈exampleModule actions〉824 ≡1 Module* const result =

2 new ExampleModule($codeSequence);

3 assert(result != 0);

4 $$ = result;

This code is used in ¶773.

¶824

〈quotedString actions〉825 ≡1 string s = $quotedString.getLexeme();

2 $$ = s.substr(1, s.size()-2);

This code is used in ¶776.

¶825

〈ws case 1 actions〉826 ≡1 $$ = "";

This code is used in ¶777.

¶826

〈ws case 2 actions〉827 ≡1 $$ = $ws + " ";

This code is used in ¶777.

¶827

〈ws case 3 actions〉828 ≡1 $$ = $ws + "\t";

This code is used in ¶777.

¶828

asdf

Page 286: Book

272 Chapter 33. Loma Input Specifications

33.2.4 File

〈File: Loma/Loma.gtk〉829 ≡1 Language:

2 LomaGrammar.

3 Preamble:

4 ‘‘

5 〈Loma actions preamble〉7876 ’’ .

7 Terminals:

8 "lexistab.h".

9 Nonterminals:

10 〈Loma non-terminal definitions〉75811 Start:

12 〈Loma start symbol definition〉75913 Productions:

14 〈Loma grammar productions〉760–777

¶829

Page 287: Book

Chapter 34

Fragment Class and its

Descendants

�34.1 Fragment Class

34.1.1 Declarations

〈Fragment class declaration〉830 ≡1 class Fragment

2 {

3 〈Fragment class field declarations〉8324 〈Fragment class method declarations〉833,8345 };

This code is used in ¶862.

¶830

〈textttFragmentList declaration〉831 ≡1 typedef list<Fragment*> FragmentList;

This code is used in ¶862.

¶831

〈Fragment class field declarations〉832 ≡1 protected: Module* module;

This code is used in ¶830.

¶832

〈Fragment class method declarations〉833 ≡1 protected: Fragment();

This code is used in ¶830.

¶833

〈Fragment class method declarations〉834 ≡1 public: virtual ~Fragment();

2 public: void setModule(Module*);

3 public: virtual void crossReference(File&);

4 public: virtual ostream& putCode(

5 ostream&, string const&, uint16_t = 0) const = 0;

6 public: virtual ostream& put(ostream&) const = 0;

This code is used in ¶830.

¶834

Page 288: Book

274 Chapter 34. Fragment Class and its Descendants

34.1.2 Definitions

〈Fragment class inline method definitions〉835 ≡1 inline Fragment::Fragment() :

2 module(0)

3 {}

This code is used in ¶862.

¶835

〈Fragment class inline method definitions〉836 ≡1 inline ostream& operator<<(ostream& s, Fragment const& f)

2 { return f.put(s); }

This code is used in ¶862.

¶836

〈Fragment class method definitions〉837 ≡1 Fragment::~Fragment() {}

This code is used in ¶863.

¶837

〈Fragment class method definitions〉838 ≡1 void Fragment::setModule(Module* _module)

2 {

3 assert(module == 0);

4 module = _module;

5 }

This code is used in ¶863.

¶838

〈Fragment class method definitions〉839 ≡1 void Fragment::crossReference(File&)

2 {}

This code is used in ¶863.

¶839

Page 289: Book

34.2. TextFragment Class 275

�34.2 TextFragment Class

34.2.1 Declarations

〈TextFragment class declaration〉840 ≡1 class TextFragment : public Fragment

2 {

3 〈TextFragment class field declarations〉8414 〈TextFragment class method declarations〉8425 };

This code is used in ¶862.

¶840

〈TextFragment class field declarations〉841 ≡1 private: string const text;

This code is used in ¶840.

¶841

〈TextFragment class method declarations〉842 ≡1 public: TextFragment(string const&);

2 public: ~TextFragment();

3 public: ostream& putCode(ostream&, string const&, uint16_t = 0) const;

4 public: ostream& put(ostream&) const;

This code is used in ¶840.

¶842

34.2.2 Definitions

〈TextFragment class inline method definitions〉843 ≡1 inline TextFragment::TextFragment(string const& _text) :

2 text(_text)

3 {}

This code is used in ¶862.

¶843

〈TextFragment class method definitions〉844 ≡1 TextFragment::~TextFragment()

2 {}

This code is used in ¶863.

¶844

〈TextFragment class method definitions〉845 ≡1 ostream& TextFragment::putCode(ostream& s, string const&, uint16_t) const

2 { return s; }

This code is used in ¶863.

¶845

〈TextFragment class method definitions〉846 ≡1 ostream& TextFragment::put(ostream& s) const

2 { return s << text << endl; }

This code is used in ¶863.

¶846

Page 290: Book

276 Chapter 34. Fragment Class and its Descendants

�34.3 CodeFragment Class

34.3.1 Declarations

〈CodeFragment class declaration〉847 ≡1 class CodeFragment : public Fragment

2 {

3 〈CodeFragment class field declarations〉8484 〈CodeFragment class method declarations〉8495 };

This code is used in ¶862.

¶847

〈CodeFragment class field declarations〉848 ≡1 private: uint16_t const line;

2 private: uint16_t const indent;

3 private: string const code;

4 private: string const label;

5 private: string const symbol;

This code is used in ¶847.

¶848

〈CodeFragment class method declarations〉849 ≡1 public: CodeFragment(uint16_t, uint16_t, string const&,

2 string const&, string const&);

3 public: ~CodeFragment();

4 public: ostream& putCode(ostream&, string const&, uint16_t = 0) const;

5 public: ostream& put(ostream&) const;

This code is used in ¶847.

¶849

34.3.2 Definitions

〈CodeFragment class inline method definitions〉850 ≡1 inline CodeFragment::CodeFragment(

2 uint16_t _line, uint16_t _indent, string const& _code,

3 string const& _label, string const& _symbol) :

4 line(_line),

5 indent(_indent),

6 code(_code),

7 label(_label),

8 symbol(_symbol)

9 {}

This code is used in ¶862.

¶850

〈CodeFragment class method definitions〉851 ≡1 CodeFragment::~CodeFragment()

2 {}

This code is used in ¶863.

¶851

Page 291: Book

34.3. CodeFragment Class 277

〈CodeFragment class method definitions〉852 ≡1 ostream& CodeFragment::putCode(

2 ostream& s, string const& srcFile, uint16_t _indent) const

3 {

4 static int next = -1;

5 if (line != next)

6 {

7 s << "#line " << line << " \"" << srcFile << "\"" << endl;

8 }

9 next = line + 1;

10 s << textIndent(indent + _indent) << code << endl;

11 return s;

12 }

This code is used in ¶863.

¶852

〈CodeFragment class method definitions〉853 ≡1 ostream& CodeFragment::put(ostream& s) const

2 {

3 s << texIndent(indent) << texCode(code);

4 if (label.size() > 0)

5 s << "\\lomalabel{" << label << "}";

6 if (symbol.size() > 0)

7 {

8 string const fullName = module->getScope() + symbol;

9 string head;

10 string tail;

11 int16_t pos = fullName.rfind(’:’);

12 if (pos < 0)

13 {

14 s << "\\lomasymbola{" << noBlanks(clearText(fullName)) << "}";

15 s << "{" << texCode(fullName) << "}";

16 }

17 else

18 {

19 string head(fullName.substr(0, pos+1));

20 string tail(fullName.substr(pos+1));

21 s << "\\lomasymbolb{" << noBlanks(clearText(tail)) << "}";

22 s << "{" << texCode(tail) << "}";

23 s << "{" << noBlanks(clearText(head)) << "}";

24 s << "{" << texCode(head) << "}";

25 }

26 }

27 return s << "\\\\\n";

28 }

This code is used in ¶863.

¶853

Page 292: Book

278 Chapter 34. Fragment Class and its Descendants

�34.4 IncludeFragment Class

34.4.1 Declarations

〈IncludeFragment class declaration〉854 ≡1 class IncludeFragment : public Fragment

2 {

3 〈IncludeFragment class field declarations〉8554 〈IncludeFragment class method declarations〉8565 };

This code is used in ¶862.

¶854

〈IncludeFragment class field declarations〉855 ≡1 private: uint16_t const indent;

2 private: string const name;

3 private: Group* includeGroup;

This code is used in ¶854.

¶855

〈IncludeFragment class method declarations〉856 ≡1 public: IncludeFragment(uint16_t, string const&);

2 public: ~IncludeFragment();

3 public: void crossReference(File&);

4 public: ostream& putCode(ostream&, string const&, uint16_t = 0) const;

5 public: ostream& put(ostream&) const;

This code is used in ¶854.

¶856

34.4.2 Definitions

〈IncludeFragment class inline method definitions〉857 ≡1 inline IncludeFragment::IncludeFragment

2 (uint16_t _indent, string const& _name) :

3 indent(_indent),

4 name(_name),

5 includeGroup(0)

6 {}

This code is used in ¶862.

¶857

〈IncludeFragment class method definitions〉858 ≡1 IncludeFragment::~IncludeFragment()

2 {}

This code is used in ¶863.

¶858

Page 293: Book

34.4. IncludeFragment Class 279

〈IncludeFragment class method definitions〉859 ≡1 void IncludeFragment::crossReference(File& f)

2 {

3 includeGroup = f.find(name);

4 if (includeGroup == 0)

5 {

6 cerr << "Loma: \"" << name;

7 cerr << "\" is not defined." << endl;

8 }

9 else

10 includeGroup->setUsed(module);

11 }

This code is used in ¶863.

¶859

〈IncludeFragment class method definitions〉860 ≡1 ostream& IncludeFragment::putCode(

2 ostream& s, string const& srcFile, uint16_t _indent) const

3 {

4 if (includeGroup != 0)

5 includeGroup->putCode(s, srcFile, indent + _indent);

6 return s;

7 }

This code is used in ¶863.

¶860

〈IncludeFragment class method definitions〉861 ≡1 ostream& IncludeFragment::put(ostream& s) const

2 {

3 if (includeGroup != 0)

4 includeGroup->put(s, indent);

5 return s;

6 }

This code is used in ¶863.

¶861

Page 294: Book

280 Chapter 34. Fragment Class and its Descendants

�34.5 Files

〈File: Loma/Fragment.h〉862 ≡1 #if !defined(_Loma_Fragment_h)

2 #define _Loma_Fragment_h

3

4 #include <iostream>

5 #include <string>

6 #include <list>

7 #include <stdint.h>

8

9 namespace Loma

10 {

11 class Fragment;

12 class Module;

13 class Group;

14 class File;

15

16 〈textttFragmentList declaration〉83117

18 〈Fragment class declaration〉83019 〈Fragment class inline method definitions〉835,83620

21 〈TextFragment class declaration〉84022 〈TextFragment class inline method definitions〉84323

24 〈CodeFragment class declaration〉84725 〈CodeFragment class inline method definitions〉85026

27 〈IncludeFragment class declaration〉85428 〈IncludeFragment class inline method definitions〉85729 }

30

31 #endif

¶862

Page 295: Book

34.5. Files 281

〈File: Loma/Fragment.cc〉863 ≡1 #include "Loma/Fragment.h"

2 #include "Loma/Module.h"

3 #include "Loma/Group.h"

4 #include "Loma/File.h"

5 #include "Loma/Output.h"

6

7 namespace Loma

8 {

9 〈Fragment class method definitions〉837–83910 〈TextFragment class method definitions〉844–84611 〈CodeFragment class method definitions〉851–85312 〈IncludeFragment class method definitions〉858–86113 }

¶863

Page 296: Book

282 Chapter 34. Fragment Class and its Descendants

Page 297: Book

Chapter 35

Module Class and its

Descendants

�35.1 Module Class

35.1.1 Declarations

〈Module class declaration〉864 ≡1 class Module

2 {

3 〈Module class field declarations〉8664 〈Module class method declarations〉867,8685 };

This code is used in ¶913.

¶864

〈ModuleList type declaration〉865 ≡1 typedef list<Module*> ModuleList;

This code is used in ¶913.

¶865

〈Module class field declarations〉866 ≡1 protected: uint16_t number;

2 protected: string name;

3 protected: FragmentList* const fragmentList;

4 protected: static uint16_t numberOfModules;

This code is used in ¶864.

¶866

〈Module class method declarations〉867 ≡1 protected: Module(string const&, FragmentList*, uint16_t = 0);

This code is used in ¶864.

¶867

Page 298: Book

284 Chapter 35. Module Class and its Descendants

〈Module class method declarations〉868 ≡1 public: virtual ~Module();

2 public: uint16_t getNumber() const;

3 public: string const& getName() const;

4 public: virtual void registerIt(File&);

5 public: virtual void crossReference(File&);

6 public: virtual void checkused();

7 public: virtual void setUsed(Module*);

8 public: virtual string getScope() const;

9 public: virtual ostream& put(ostream&) const;

10 public: virtual ostream& putCode(

11 ostream&, string const&, uint16_t = 0) const;

This code is used in ¶864.

¶868

35.1.2 Definitions

〈Module class static field definitions〉869 ≡1 uint16_t Module::numberOfModules = 0;

This code is used in ¶914.

¶869

〈Module class inline method definitions〉870 ≡1 inline uint16_t Module::getNumber() const

2 { return number; }

This code is used in ¶913.

¶870

〈Module class inline method definitions〉871 ≡1 inline string const& Module::getName() const

2 { return name; }

This code is used in ¶913.

¶871

〈Module class inline method definitions〉872 ≡1 inline ostream& operator<<(ostream& s, Module const& m)

2 { return m.put(s); }

This code is used in ¶913.

¶872

Page 299: Book

35.1. Module Class 285

〈Module class method definitions〉873 ≡1 Module::Module(string const& _name,

2 FragmentList* _fragmentList, uint16_t _number) :

3 number(_number),

4 name(_name),

5 fragmentList(_fragmentList)

6 {

7 assert(fragmentList != 0);

8 for (list<Fragment*>::const_iterator p(fragmentList->begin()),

9 lim(fragmentList->end()); p != lim; ++p)

10 {

11 (*p)->setModule(this);

12 }

13 }

This code is used in ¶914.

¶873

〈Module class method definitions〉874 ≡1 Module::~Module()

2 {

3 for (list<Fragment*>::const_iterator p(fragmentList->begin()),

4 lim(fragmentList->end()); p != lim; ++p)

5 {

6 delete *p;

7 }

8 delete fragmentList;

9 }

This code is used in ¶914.

¶874

〈Module class method definitions〉875 ≡1 void Module::registerIt(File&)

2 {}

This code is used in ¶914.

¶875

〈Module class method definitions〉876 ≡1 void Module::checkused()

2 {}

This code is used in ¶914.

¶876

〈Module class method definitions〉877 ≡1 void Module::setUsed(Module*)

2 {}

This code is used in ¶914.

¶877

Page 300: Book

286 Chapter 35. Module Class and its Descendants

〈Module class method definitions〉878 ≡1 void Module::crossReference(File& f)

2 {

3 for (list<Fragment*>::const_iterator p(fragmentList->begin()),

4 lim(fragmentList->end()); p != lim; ++p)

5 {

6 (*p)->crossReference(f);

7 }

8 }

This code is used in ¶914.

¶878

〈Module class method definitions〉879 ≡1 ostream& Module::put(ostream& s) const

2 {

3 uint16_t lineNumber = 0;

4 for (list<Fragment*>::const_iterator p(fragmentList->begin()),

5 lim(fragmentList->end()); p != lim; ++p)

6 {

7 s << "\\lomaline{" << ++lineNumber << "}" << **p;

8 }

9 return s;

10 }

This code is used in ¶914.

¶879

〈Module class method definitions〉880 ≡1 ostream& Module::putCode(

2 ostream& s, string const& srcFile, uint16_t indent) const

3 {

4 for (list<Fragment*>::const_iterator p(fragmentList->begin()),

5 lim(fragmentList->end()); p != lim; ++p)

6 {

7 (*p)->putCode(s, srcFile, indent);

8 }

9 return s;

10 }

This code is used in ¶914.

¶880

〈Module class method definitions〉881 ≡1 string Module::getScope() const

2 { return ""; }

This code is used in ¶914.

¶881

Page 301: Book

35.2. TextModule Class 287

�35.2 TextModule Class

35.2.1 Declarations

〈TextModule class declaration〉882 ≡1 class TextModule : public Module

2 {

3 〈TextModule method declarations〉8834 };

This code is used in ¶913.

¶882

〈TextModule method declarations〉883 ≡1 public: TextModule(FragmentList*);

2 public: ~TextModule();

3 public: ostream& put(ostream&) const;

4 public: ostream& putCode(ostream&, string const&, uint16_t = 0) const;

This code is used in ¶882.

¶883

35.2.2 Definitions

〈TextModule class inline method definitions〉884 ≡1 inline TextModule::TextModule(FragmentList* _fragmentList) :

2 Module("", _fragmentList)

3 {}

This code is used in ¶913.

¶884

〈TextModule class method definitions〉885 ≡1 TextModule::~TextModule()

2 {}

This code is used in ¶914.

¶885

〈TextModule class method definitions〉886 ≡1 ostream& TextModule::put(ostream& s) const

2 {

3 for (list<Fragment*>::const_iterator p(fragmentList->begin()),

4 lim(fragmentList->end()); p != lim; ++p)

5 {

6 s << **p;

7 }

8 return s;

9 }

This code is used in ¶914.

¶886

〈TextModule class method definitions〉887 ≡1 ostream& TextModule::putCode(ostream& s, string const&, uint16_t) const

2 { return s; }

This code is used in ¶914.

¶887

Page 302: Book

288 Chapter 35. Module Class and its Descendants

�35.3 FileModule Class

35.3.1 Declarations

〈FileModule class declaration〉888 ≡1 class FileModule : public Module

2 {

3 〈FileModule class field declarations〉8894 〈FileModule class method declarations〉8905 };

This code is used in ¶913.

¶888

〈FileModule class field declarations〉889 ≡1 private: string scope;

This code is used in ¶888.

¶889

〈FileModule class method declarations〉890 ≡1 public: FileModule(string const&, FragmentList*);

2 public: FileModule(string const&, string const&, FragmentList*);

3 public: ~FileModule();

4 public: void registerIt(File&);

5 public: string getScope() const;

6 public: ostream& put(ostream&) const;

This code is used in ¶888.

¶890

35.3.2 Definitions

〈FileModule class inline method definitions〉891 ≡1 inline FileModule::FileModule(string const& _name,

2 FragmentList* _fragmentList) :

3 Module(_name, _fragmentList, ++numberOfModules),

4 scope()

5 {}

This code is used in ¶913.

¶891

〈FileModule class inline method definitions〉892 ≡1 inline FileModule::FileModule(string const& _name,

2 string const& _scope,

3 FragmentList* _fragmentList) :

4 Module(_name, _fragmentList, ++numberOfModules),

5 scope(_scope)

6 {}

This code is used in ¶913.

¶892

Page 303: Book

35.4. CodeModule Class 289

〈FileModule class method definitions〉893 ≡1 FileModule::~FileModule()

2 {}

This code is used in ¶914.

¶893

〈FileModule class method definitions〉894 ≡1 void FileModule::registerIt(File& _file)

2 { _file.registerIt(this); }

This code is used in ¶914.

¶894

〈FileModule class method definitions〉895 ≡1 string FileModule::getScope() const

2 { return scope; }

This code is used in ¶914.

¶895

〈FileModule class method definitions〉896 ≡1 ostream& FileModule::put(ostream& s) const

2 {

3 s << "\\begin{lomafile}{" << clearText(name) << "}";

4 s << "{" << name << "}";

5 s << "{" << number << "}\n";

6 Module::put(s);

7 return s << "\\end{lomafile}\n";

8 }

This code is used in ¶914.

¶896

�35.4 CodeModule Class

35.4.1 Declarations

〈CodeModule class declaration〉897 ≡1 class CodeModule : public Module

2 {

3 〈CodeModule class field declarations〉8984 〈CodeModule class method declarations〉8995 };

This code is used in ¶913.

¶897

〈CodeModule class field declarations〉898 ≡1 private: Module* used;

2 private: string scope;

This code is used in ¶897.

¶898

Page 304: Book

290 Chapter 35. Module Class and its Descendants

〈CodeModule class method declarations〉899 ≡1 public: CodeModule(string const&, FragmentList*);

2 public: CodeModule(string const&, string const&, FragmentList*);

3 public: ~CodeModule();

4 public: void registerIt(File&);

5 public: void checkused();

6 public: void setUsed(Module*);

7 public: string getScope() const;

8 public: ostream& put(ostream&) const;

This code is used in ¶897.

¶899

35.4.2 Definitions

〈CodeModule class inline method definitions〉900 ≡1 inline CodeModule::CodeModule(string const& _name,

2 FragmentList* _fragmentList) :

3 Module(_name, _fragmentList, ++numberOfModules),

4 used(0),

5 scope()

6 {}

This code is used in ¶913.

¶900

〈CodeModule class inline method definitions〉901 ≡1 inline CodeModule::CodeModule(string const& _name,

2 string const& _scope,

3 FragmentList* _fragmentList) :

4 Module(_name, _fragmentList, ++numberOfModules),

5 used(0),

6 scope(_scope)

7 {}

This code is used in ¶913.

¶901

〈CodeModule class inline method definitions〉902 ≡1 inline void CodeModule::setUsed(Module* _used)

2 { used = _used; }

This code is used in ¶913.

¶902

〈CodeModule class method definitions〉903 ≡1 CodeModule::~CodeModule()

2 {}

This code is used in ¶914.

¶903

〈CodeModule class method definitions〉904 ≡1 void CodeModule::registerIt(File& _file)

2 { _file.registerIt(this); }

This code is used in ¶914.

¶904

Page 305: Book

35.5. ExampleModule Class 291

〈CodeModule class method definitions〉905 ≡1 void CodeModule::checkused()

2 {

3 if (used == 0)

4 cerr << "Loma: \"" << name << "\" is not used." << endl;

5 }

This code is used in ¶914.

¶905

〈CodeModule class method definitions〉906 ≡1 string CodeModule::getScope() const

2 {

3 if (used != 0)

4 return used->getScope() + scope;

5 else

6 return scope;

7 }

8

This code is used in ¶914.

¶906

〈CodeModule class method definitions〉907 ≡1 ostream& CodeModule::put(ostream& s) const

2 {

3 s << "\\begin{lomacode}{" << clearText(name) << "}";

4 s << "{" << name << "}";

5 s << "{" << number << "}";

6 if (used != 0)

7 s << "{" << used->getNumber() << "}\n";

8 else

9 s << "{unused}\n";

10 Module::put(s);

11 return s << "\\end{lomacode}\n";

12 }

This code is used in ¶914.

¶907

Page 306: Book

292 Chapter 35. Module Class and its Descendants

�35.5 ExampleModule Class

35.5.1 Declarations

〈ExampleModule class declaration〉908 ≡1 class ExampleModule : public Module

2 {

3 〈ExampleModule class method declarations〉9094 };

This code is used in ¶913.

¶908

〈ExampleModule class method declarations〉909 ≡1 public: ExampleModule(FragmentList*);

2 public: ~ExampleModule();

3 public: ostream& put(ostream&) const;

This code is used in ¶908.

¶909

35.5.2 Definitions

〈ExampleModule class inline method definitions〉910 ≡1 inline ExampleModule::ExampleModule

2 (FragmentList* _fragmentList) :

3 Module("", _fragmentList)

4 {}

This code is used in ¶913.

¶910

〈ExampleModule class method definitions〉911 ≡1 ExampleModule::~ExampleModule()

2 {}

This code is used in ¶914.

¶911

〈ExampleModule class method definitions〉912 ≡1 ostream& ExampleModule::put(ostream& s) const

2 {

3 s << "\\begin{lomaexample}\n";

4 Module::put(s);

5 return s << "\\end{lomaexample}\n";

6 }

This code is used in ¶914.

¶912

Page 307: Book

35.6. Files 293

�35.6 Files

〈File: Loma/Module.h〉913 ≡1 #if !defined(Loma_Module_h)

2 #define Loma_Module_h

3

4 #include <string>

5 #include <list>

6 #include "Loma/Fragment.h"

7

8 namespace Loma

9 {

10 class Module;

11 class File;

12

13 〈ModuleList type declaration〉86514

15 〈Module class declaration〉86416 〈Module class inline method definitions〉870–87217

18 〈TextModule class declaration〉88219 〈TextModule class inline method definitions〉88420

21 〈FileModule class declaration〉88822 〈FileModule class inline method definitions〉891,89223

24 〈CodeModule class declaration〉89725 〈CodeModule class inline method definitions〉900–90226

27 〈ExampleModule class declaration〉90828 〈ExampleModule class inline method definitions〉91029 }

30

31 #endif

¶913

Page 308: Book

294 Chapter 35. Module Class and its Descendants

〈File: Loma/Module.cc〉914 ≡1 #include <string>

2 #include <list>

3 #include "Loma/Module.h"

4 #include "Loma/File.h"

5 #include "Loma/Output.h"

6

7 namespace Loma

8 {

9 〈Module class static field definitions〉86910 〈Module class method definitions〉873–88111 〈TextModule class method definitions〉885–88712 〈FileModule class method definitions〉893–89613 〈CodeModule class method definitions〉903–90714 〈ExampleModule class method definitions〉911,91215 }

¶914

Page 309: Book

Chapter 36

Group Class

�36.1 Declarations

〈Group class declaration〉915 ≡1 class Group

2 {

3 〈Group class field declarations〉9164 〈Group class method declarations〉917,9185 };

This code is used in ¶928.

¶915

〈Group class field declarations〉916 ≡1 private: string name;

2 private: ModuleList moduleList;

This code is used in ¶915.

¶916

〈Group class method declarations〉917 ≡1 private: ostream& putNumbers(ostream&) const;

2 private: uint16_t getNumber() const;

This code is used in ¶915.

¶917

〈Group class method declarations〉918 ≡1 public: Group(string const&);

2 public: string const& getName() const;

3 public: void append(Module*);

4 public: void setUsed(Module*);

5 public: ostream& put(ostream&, uint16_t = 0) const;

6 public: ostream& putCode(

7 ostream&, string const& srcFile, uint16_t = 0) const;

This code is used in ¶915.

¶918

Page 310: Book

296 Chapter 36. Group Class

�36.2 Definitions

〈Group class inline method definitions〉919 ≡1 inline Group::Group(string const& _name) :

2 name(_name),

3 moduleList()

4 {}

This code is used in ¶928.

¶919

〈Group class inline method definitions〉920 ≡1 inline string const& Group::getName() const

2 { return name; }

This code is used in ¶928.

¶920

〈Group class inline method definitions〉921 ≡1 inline uint16_t Group::getNumber() const

2 {

3 list<Module*>::const_iterator p(moduleList.begin());

4 list<Module*>::const_iterator null(moduleList.end());

5 assert(p != null);

6 return (*p)->getNumber();

7 }

This code is used in ¶928.

¶921

〈Group class inline method definitions〉922 ≡1 inline void Group::append(Module* _module)

2 { moduleList.push_back(_module); }

This code is used in ¶928.

¶922

〈Group class inline method definitions〉923 ≡1 inline ostream& operator<<(ostream& s, Group const& g)

2 { return g.put(s); }

This code is used in ¶928.

¶923

〈Group class method definitions〉924 ≡1 void Group::setUsed(Module* _module)

2 {

3 for (list<Module*>::const_iterator p(moduleList.begin()),

4 lim(moduleList.end()); p != lim; ++p)

5 {

6 (*p)->setUsed(_module);

7 }

8 }

This code is used in ¶929.

¶924

Page 311: Book

36.2. Definitions 297

〈Group class method definitions〉925 ≡1 ostream& Group::put(ostream& s, uint16_t indent) const

2 {

3 s << texIndent(indent);

4 s << "\\lomainclude{" << clearText(name) << "}";

5 s << "{" << name << "}";

6 s << "{" << getNumber() << "}";

7 s << "{";

8 putNumbers(s);

9 s << "}\\\\\n";

10 return s;

11 }

This code is used in ¶929.

¶925

〈Group class method definitions〉926 ≡1 ostream& Group::putCode(

2 ostream& s, string const& srcFile, uint16_t indent) const

3 {

4 for (list<Module*>::const_iterator p(moduleList.begin()),

5 lim(moduleList.end()); p != lim; ++p)

6 {

7 (*p)->putCode(s, srcFile, indent);

8 }

9 return s;

10 }

This code is used in ¶929.

¶926

Page 312: Book

298 Chapter 36. Group Class

〈Group class method definitions〉927 ≡1 ostream& Group::putNumbers(ostream& s) const

2 {

3 bool comma = false;

4 list<Module*>::const_iterator p(moduleList.begin());

5 list<Module*>::const_iterator lim(moduleList.end());

6 assert(p != lim);

7 uint16_t first = (*p)->getNumber();

8 uint16_t previous = first;

9 for (++p; p != lim; ++p)

10 {

11 uint16_t const current = (*p)->getNumber();

12 if (current != previous + 1)

13 {

14 if (comma)

15 s << ",";

16 comma = true;

17 if (first == previous)

18 s << first;

19 else if (first + 1 == previous)

20 s << first << "," << previous;

21 else

22 s << first << "--" << previous;

23 first = current;

24 }

25 previous = current;

26 }

27 if (comma)

28 s << ",";

29 if (first == previous)

30 s << first;

31 else if (first + 1 == previous)

32 s << first << "," << previous;

33 else

34 s << first << "--" << previous;

35 return s;

36 }

This code is used in ¶929.

¶927

Page 313: Book

36.3. Files 299

�36.3 Files

〈File: Loma/Group.h〉928 ≡1 #if !defined(_Loma_Group_h)

2 #define _Loma_Group_h

3

4 #include <string>

5 #include "Loma/Module.h"

6

7 namespace Loma

8 {

9 〈Group class declaration〉91510 〈Group class inline method definitions〉919–92311 }

12

13 #endif

¶928

〈File: Loma/Group.cc〉929 ≡1 #include <list>

2 #include "Loma/Group.h"

3 #include "Loma/Output.h"

4

5 namespace Loma

6 {

7 〈Group class method definitions〉924–9278 }

¶929

Page 314: Book

300 Chapter 36. Group Class

Page 315: Book

Chapter 37

File Class

�37.1 Declarations

〈File class declaration〉930 ≡1 class File

2 {

3 〈File class field declarations〉9314 public:

5 〈File class method declarations〉9326 };

This code is used in ¶942.

¶930

〈File class field declarations〉931 ≡1 private: ModuleList* const moduleList;

2 private: ModuleList fileList;

3 private: HashTable<string,Group*> groupTable;

This code is used in ¶930.

¶931

〈File class method declarations〉932 ≡1 public: File(ModuleList*);

2 public: ~File();

3 public: Group* find(string const&) const;

4 public: void registerIt(FileModule*);

5 public: void registerIt(CodeModule*);

6 public: void crossReference();

7 public: void putCode(string const&, string const&) const;

8 public: ostream& put(ostream& s) const;

This code is used in ¶930.

¶932

Page 316: Book

302 Chapter 37. File Class

�37.2 Definitions

〈File class inline method definitions〉933 ≡1 inline ostream& operator<<(ostream& s, File const& f)

2 { return f.put(s); }

This code is used in ¶942.

¶933

〈File class method definitions〉934 ≡1 File::File(ModuleList* _moduleList) :

2 moduleList(_moduleList)

3 { assert (moduleList != 0); }

This code is used in ¶943.

¶934

〈File class method definitions〉935 ≡1 File::~File()

2 {

3 for (list<Module*>::const_iterator p(moduleList->begin()),

4 lim(moduleList->end()); p != lim; ++p)

5 delete *p;

6 delete moduleList;

7 }

This code is used in ¶943.

¶935

〈File class method definitions〉936 ≡1 Group* File::find(string const& s) const

2 {

3 HashTable<string,Group*>::const_iterator p(groupTable.find(s)), null;

4 if (p != null)

5 return *p;

6 else

7 return 0;

8 }

This code is used in ¶943.

¶936

Page 317: Book

37.2. Definitions 303

〈File class method definitions〉937 ≡1 void File::crossReference()

2 {

3 for (list<Module*>::const_iterator p(moduleList->begin()),

4 lim(moduleList->end()); p != lim; ++p)

5 {

6 (*p)->registerIt(*this);

7 }

8 for (list<Module*>::const_iterator p(moduleList->begin()),

9 lim(moduleList->end()); p != lim; ++p)

10 {

11 (*p)->crossReference(*this);

12 }

13 for (list<Module*>::const_iterator p(moduleList->begin()),

14 lim(moduleList->end()); p != lim; ++p)

15 {

16 (*p)->checkused();

17 }

18 }

This code is used in ¶943.

¶937

〈File class method definitions〉938 ≡1 void File::registerIt(FileModule* m)

2 { fileList.push_back(m); }

This code is used in ¶943.

¶938

〈File class method definitions〉939 ≡1 void File::registerIt(CodeModule* m)

2 {

3 HashTable<string,Group*>::const_iterator

4 p(groupTable.find(m->getName())), null;

5 if (p != null)

6 (*p)->append(m);

7 else

8 {

9 Group* const group = new Group(m->getName());

10 assert(group != 0);

11 group->append(m);

12 groupTable.add(m->getName(), group);

13 }

14 }

This code is used in ¶943.

¶939

Page 318: Book

304 Chapter 37. File Class

〈File class method definitions〉940 ≡1 void File::putCode(string const& directory, string const& srcFile) const

2 {

3 for (list<Module*>::const_iterator p(fileList.begin()),

4 lim(fileList.end()); p != lim; ++p)

5 {

6 string const fileName =

7 directory + (*p)->getName();

8 cerr << " " << fileName;

9 ofstream file(fileName.c_str());

10 if (!file)

11 cerr << " Can’t open " << fileName << endl;

12 else

13 (*p)->putCode(file, srcFile);

14 }

15 }

This code is used in ¶943.

¶940

〈File class method definitions〉941 ≡1 ostream& File::put(ostream& s) const

2 {

3 for (list<Module*>::const_iterator p(moduleList->begin()),

4 lim(moduleList->end()); p != lim; ++p)

5 s << **p;

6 return s;

7 }

This code is used in ¶943.

¶941

Page 319: Book

37.3. Files 305

�37.3 Files

〈File: Loma/File.h〉942 ≡1 #if !defined(_Loma_File_h)

2 #define _Loma_File_h

3

4 #include <string>

5 #include "Loma/Group.h"

6 #include "Toolbox/HashTable.h"

7 #include "Toolbox/String.h"

8

9 using namespace Toolbox;

10 using namespace Toolbox::String;

11

12 namespace Loma

13 {

14 〈File class declaration〉93015 〈File class inline method definitions〉93316 }

17

18 #endif

¶942

〈File: Loma/File.cc〉943 ≡1 #include <iostream>

2 #include <fstream>

3 #include "Loma/File.h"

4

5 namespace Loma

6 {

7 〈File class method definitions〉934–9418 }

¶943

Page 320: Book

306 Chapter 37. File Class

Page 321: Book

Chapter 38

Loma Output Routines

�38.1 Declarations

〈Loma output method declarations〉944 ≡1 string textIndent(uint16_t);

2 string texIndent(uint16_t);

3 string texCode(string const&);

4 string clearText(string const&);

5 string noBlanks(string const&);

This code is used in ¶950.

¶944

�38.2 Definitions

〈Loma output method definitions〉945 ≡1 string textIndent(uint16_t indent)

2 {

3 string result;

4

5 for (uint16_t i = indent / 8; i > 0; --i)

6 result += "\t";

7 for (uint16_t i = indent % 8; i > 0; --i)

8 result += " ";

9 return result;

10 }

This code is used in ¶951.

¶945

Page 322: Book

308 Chapter 38. Loma Output Routines

〈Loma output method definitions〉946 ≡1 string texIndent(uint16_t indent)

2 {

3 string result;

4

5 for (uint16_t i = 0; i < indent; i += 4)

6 result += "\\> ";

7 return result;

8 }

This code is used in ¶951.

¶946

〈Loma output method definitions〉947 ≡1 string texCode(string const& t)

2 {

3 string result;

4

5 for (string::const_iterator p(t.begin()), lim(t.end()); p != lim; ++p)

6 {

7 char const c = *p;

8 switch (c)

9 {

10 case ’$’: case ’{’: case ’}’: case ’%’:

11 case ’&’: case ’#’: case ’_’: case ’\\’:

12 case ’~’: case ’^’:

13 case ’.’: case ’!’: case ’?’: case ’:’:

14 result += "\\verb’";

15 result += c;

16 result += "’"; break;

17 default:

18 result += c;

19 break;

20 }

21 }

22 return result;

23 }

This code is used in ¶951.

¶947

Page 323: Book

38.2. Definitions 309

〈Loma output method definitions〉948 ≡1 string clearText(string const& t)

2 {

3 string result;

4

5 string::const_iterator p(t.begin()), lim(t.end());

6 while (p != lim)

7 {

8 if (*p == ’\\’)

9 {

10 ++p;

11 while (p != lim && isalpha(*p))

12 ++p;

13 }

14 else if (isspace(*p))

15 {

16 if (result.size() != 0 &&

17 result [result.size() - 1U] != ’ ’)

18 result += ’ ’;

19 ++p;

20 }

21 else if (isalnum(*p) || *p == ’.’)

22 {

23 result += *p;

24 ++p;

25 }

26 else

27 ++p;

28 }

29 return result;

30 }

This code is used in ¶951.

¶948

〈Loma output method definitions〉949 ≡1 string noBlanks(const string& t)

2 {

3 string result;

4

5 for (string::const_iterator p(t.begin()), lim(t.end()); p != lim; ++p)

6 {

7 if (!isspace(*p))

8 result += *p;

9 }

10 return result;

11 }

This code is used in ¶951.

¶949

Page 324: Book

310 Chapter 38. Loma Output Routines

�38.3 Files

〈File: Loma/Output.h〉950 ≡1 #if !defined(_Loma_Output_h)

2 #define _Loma_Output_h

3

4 #include <string>

5 #include <stdint.h>

6

7 namespace Loma

8 {

9 〈Loma output method declarations〉94410 }

11

12 #endif

¶950

〈File: Loma/Output.cc〉951 ≡1 #include <string>

2 #include <ctype.h>

3 #include "Loma/Output.h"

4 #include "Toolbox/fixwarn.h"

5

6 namespace Loma

7 {

8 〈Loma output method definitions〉945–9499 }

¶951

Page 325: Book
Page 326: Book

312 Chapter 39. Loma Main Program

Chapter 39

Loma Main Program

〈Loma main program definition〉952 ≡1 using Lexis::Token;

2 using Lexis::TokenStream;

3 using LomaTokens::LexisTables;

4 using Loma::File;

5 using Loma::ParserData;

6 using Loma::ItemAttributes;

7 using LomaGrammar::GramatikaTables;

8 using LomaGrammar::Parser;

9 using Gramatika::Value;

10

11 string const lomExtension(".lom");

12 string const texExtension(".tex");

13

14 〈ProcessArgument static method definition〉95415

16 int main(int argc, char* argv[])

17 {

18 if (argc < 2)

19 {

20 cerr << "Usage: loma [@]file [[@]file...]" << endl;

21 return 1;

22 }

23 for (int i = 1; i < argc; ++i)

24 {

25 if (argv [i] [0] == ’@’)

26 {

27 〈read the response file〉95328 }

29 else

30 {

31 int errorCode = ProcessArgument(argv [i]);

32 if (errorCode)

33 return errorCode;

34 }

35 }

36 return 0;

37 }

This code is used in ¶958.

¶952

Page 327: Book

313

〈read the response file〉953 ≡1 string responseFileName(argv [i] + 1);

2

3 cerr << "Reading response file:";

4 cerr << " " << responseFileName << endl;

5 ifstream responseFile(responseFileName.c_str());

6 if (!responseFile)

7 {

8 cerr << "Can’t open " << responseFileName << endl;

9 return 2;

10 }

11 for (;;)

12 {

13 string argument;

14 responseFile >> argument;

15 if (!responseFile)

16 break;

17 int errorCode = ProcessArgument(argument);

18 if (errorCode)

19 return errorCode;

20 }

This code is used in ¶952.

¶953

〈ProcessArgument static method definition〉954 ≡1 int ProcessArgument(string argument)

2 {

3 〈determine the Loma input file〉9554 〈read the Loma input file〉9565 〈write the Loma output file〉9576 return 0;

7 }

This code is used in ¶952.

¶954

〈determine the Loma input file〉955 ≡1 PathName pathName(argument);

2 string lomFile;

3

4 if (pathName.getExtension() == lomExtension)

5 lomFile = pathName;

6 else

7 lomFile = pathName + lomExtension;

8 cerr << "Reading input file:";

9 cerr << " " << lomFile;

This code is used in ¶954.

¶955

Page 328: Book

314 Chapter 39. Loma Main Program

〈read the Loma input file〉956 ≡1 ifstream lomStream(lomFile.c_str());

2 if (!lomStream)

3 {

4 cerr << " Can’t open " << lomFile << endl;

5 return 3;

6 }

7 TokenStream<Token<LexisTables> > tin(lomStream);

8 Parser<ParserData,Token<LexisTables>,ItemAttributes,GramatikaTables> parser;

9 Value<Token<LexisTables>,ItemAttributes> const result = parser.parse(tin);

10 File* file = result.file;

11 cerr << "." << endl;

This code is used in ¶954.

¶956

〈write the Loma output file〉957 ≡1 cerr << "Writing output files:";

2 string texFile = pathName.getBase() + texExtension;

3 cerr << " " << texFile;

4 ofstream texStream(texFile.c_str());

5 if (!texStream)

6 {

7 cerr << " Can’t open " << texFile << endl;

8 return 4;

9 }

10 texStream << *file;

11 file->putCode(pathName.getHead(), lomFile);

12 delete file;

13 cerr << "." << endl;

This code is used in ¶954.

¶957

�39.1 File

〈File: Loma/main.cc〉958 ≡1 #include <iostream.h>

2 #include <fstream.h>

3 #include <stdlib.h>

4 #include "Loma/File.h"

5 #include "Lexis/Lexis.h"

6 #include "lexistab.h"

7 #include "Gramatika/Gramatika.h"

8 #include "gramadef.h"

9 #include "Toolbox/PathName.h"

10

11 〈Loma main program definition〉952

¶958

Page 329: Book

List of Modules

〈Action enumeration declaration〉¶181 64; ¶566 183

〈Action enumeration declaration〉¶181 79; ¶566 193

〈Action enumeration inline method definitions〉¶182 64; ¶567 183

〈Action enumeration inline method definitions〉¶182 79; ¶567 193

〈ActionRecord class declaration〉¶459 149

〈ActionRecord class declaration〉¶459 151

〈ActionRecord class inline method definitions〉¶462 150; ¶463 150; ¶464 150; ¶465 150; ¶466 150; ¶467 150

〈ActionRecord class inline method definitions〉¶462 151

〈ActionRecord method declarations〉¶460 149; ¶461 149

〈ActionRecord method declarations〉¶460 149

〈ActionRecord method definitions〉¶468 150; ¶469 151; ¶470 151

〈ActionRecord method definitions〉¶468 152

〈add shift/goto transitions〉¶528 169

〈add shift/goto transitions〉¶528 169

〈alternation case 1 actions〉¶43 12

〈alternation case 1 actions〉¶43 7

〈alternation case 2 actions〉¶44 13

〈alternation case 2 actions〉¶44 7

〈alternative case 1 actions〉¶45 13

Page 330: Book

316 List of Modules

〈alternative case 1 actions〉¶45 7

〈alternative case 2 actions〉¶46 13

〈alternative case 2 actions〉¶46 7

〈Anchor enumeration declaration〉¶179 64

〈Anchor enumeration declaration〉¶179 79

〈Anchor enumeration inline method definitions〉¶180 64

〈Anchor enumeration inline method definitions〉¶180 79

〈Automaton class declaration〉¶512 165

〈Automaton class declaration〉¶512 170

〈Automaton class field declarations〉¶513 165

〈Automaton class field declarations〉¶513 165

〈Automaton class inline method definitions〉¶516 166; ¶517 166; ¶518 166

〈Automaton class inline method definitions〉¶516 170

〈Automaton class method declarations〉¶514 165; ¶515 165

〈Automaton class method declarations〉¶514 165

〈Automaton class method definitions〉¶519 166; ¶520 166; ¶521 166; ¶522 167; ¶523 167; ¶524 167; ¶525 168;

¶526 168; ¶529 169; ¶530 170

〈Automaton class method definitions〉¶519 170

〈auxiliaryDefinition actions〉¶32 11

〈auxiliaryDefinition actions〉¶32 5

〈Character static method declarations〉¶609 201

〈Character static method declarations〉¶609 201

〈Character static method definitions〉¶611 201; ¶612 202; ¶613 202

〈Character static method definitions〉¶611 201

〈Character utilities declaration〉¶608 201

Page 331: Book

List of Modules 317

〈Character utilities declaration〉¶608 202

〈Character utilities definition〉¶610 201

〈Character utilities definition〉¶610 202

〈CharacterSet class declaration〉¶708 237

〈CharacterSet class declaration〉¶708 238

〈CharacterSet inline method definitions〉¶710 237; ¶711 237; ¶712 238; ¶713 238

〈CharacterSet inline method definitions〉¶710 238

〈CharacterSet method declarations〉¶709 237

〈CharacterSet method declarations〉¶709 237

〈CharacterSet method definitions〉¶714 238

〈CharacterSet method definitions〉¶714 239

〈codeFragment case 1 actions〉¶812 269

〈codeFragment case 1 actions〉¶812 261

〈codeFragment case 2 actions〉¶813 269

〈codeFragment case 2 actions〉¶813 261

〈codeFragment case 3 actions〉¶814 269

〈codeFragment case 3 actions〉¶814 261

〈codeFragment case 4 actions〉¶815 270

〈codeFragment case 4 actions〉¶815 261

〈codeFragment case 5 actions〉¶816 270

〈codeFragment case 5 actions〉¶816 261

〈codeFragment case 6 actions〉¶817 270

〈codeFragment case 6 actions〉¶817 261

〈CodeFragment class declaration〉¶847 276

〈CodeFragment class declaration〉

Page 332: Book

318 List of Modules

¶847 280

〈CodeFragment class field declarations〉¶848 276

〈CodeFragment class field declarations〉¶848 276

〈CodeFragment class inline method definitions〉¶850 276

〈CodeFragment class inline method definitions〉¶850 280

〈CodeFragment class method declarations〉¶849 276

〈CodeFragment class method declarations〉¶849 276

〈CodeFragment class method definitions〉¶851 276; ¶852 277; ¶853 277

〈CodeFragment class method definitions〉¶851 281

〈codeModule case 1 actions〉¶822 271

〈codeModule case 1 actions〉¶822 262

〈codeModule case 2 actions〉¶823 271

〈codeModule case 2 actions〉¶823 262

〈CodeModule class declaration〉¶897 289

〈CodeModule class declaration〉¶897 293

〈CodeModule class field declarations〉¶898 289

〈CodeModule class field declarations〉¶898 289

〈CodeModule class inline method definitions〉¶900 290; ¶901 290; ¶902 290

〈CodeModule class inline method definitions〉¶900 293

〈CodeModule class method declarations〉¶899 290

〈CodeModule class method declarations〉¶899 289

〈CodeModule class method definitions〉¶903 290; ¶904 290; ¶905 291; ¶906 291; ¶907 291

〈CodeModule class method definitions〉¶903 294

〈codeSequence case 1 actions〉¶809 269

〈codeSequence case 1 actions〉¶809 261

Page 333: Book

List of Modules 319

〈codeSequence case 2 actions〉¶810 269

〈codeSequence case 2 actions〉¶810 261

〈codeSequence case 3 actions〉¶811 269

〈codeSequence case 3 actions〉¶811 261

〈CompressedDFA class declaration〉¶154 51

〈CompressedDFA class declaration〉¶154 60

〈CompressedDFA class field declarations〉¶155 51

〈CompressedDFA class field declarations〉¶155 51

〈CompressedDFA class method declarations〉¶156 51; ¶157 52

〈CompressedDFA class method declarations〉¶156 51

〈CompressedDFA class method definitions〉¶158 52; ¶164 54; ¶165 55; ¶166 55; ¶167 56; ¶168 56; ¶169 57; ¶170 58;

¶171 59; ¶172 60

〈CompressedDFA class method definitions〉¶158 61

〈compute the Lexis tables〉¶239 83

〈compute the Lexis tables〉¶239 82

〈construct anchorTable, actionTable and tokenTable〉¶161 54

〈construct anchorTable, actionTable and tokenTable〉¶161 52

〈construct columnTable and columnMap〉¶159 53

〈construct columnTable and columnMap〉¶159 52

〈construct rowTable and rowMap〉¶160 53

〈construct rowTable and rowMap〉¶160 52

〈construct the Gramatika tables〉¶602 196

〈construct the Gramatika tables〉¶602 195

〈construct tokenList〉¶162 54

〈construct tokenList〉¶162 52

Page 334: Book

320 List of Modules

〈construct transitionTable〉¶163 54

〈construct transitionTable〉¶163 52

〈decode hex escape sequence〉¶633 213

〈decode hex escape sequence〉¶633 212

〈decode octal escape sequence〉¶632 213

〈decode octal escape sequence〉¶632 212

〈determine the Loma input file〉¶955 313

〈determine the Loma input file〉¶955 313

〈DFA class declaration〉¶134 41

〈DFA class declaration〉¶134 48

〈DFA class field declarations〉¶136 41

〈DFA class field declarations〉¶136 41

〈DFA class inline method definitions〉¶139 42; ¶140 42; ¶141 42

〈DFA class inline method definitions〉¶139 48

〈DFA class method declarations〉¶137 41; ¶138 42

〈DFA class method declarations〉¶137 41

〈DFA class method definitions〉¶142 42; ¶143 43; ¶144 44; ¶145 44; ¶146 44; ¶147 45; ¶148 46; ¶149 47;

¶150 48; ¶151 48

〈DFA class method definitions〉¶142 49

〈DFA constants〉¶135 41

〈DFA constants〉¶135 41

〈DFAState class declaration〉¶122 37

〈DFAState class declaration〉¶122 39

〈DFAState class field declarations〉¶123 37

〈DFAState class field declarations〉¶123 37

Page 335: Book

List of Modules 321

〈DFAState class inline method definitions〉¶125 38; ¶126 38

〈DFAState class inline method definitions〉¶125 39

〈DFAState class method declarations〉¶124 37

〈DFAState class method declarations〉¶124 37

〈DFAState class method definitions〉¶127 38; ¶128 38; ¶129 38; ¶130 38; ¶131 39

〈DFAState class method definitions〉¶127 40

〈encode octal escape sequence〉¶629 210

〈encode octal escape sequence〉¶629 210

〈EscapeSequence static method declarations〉¶626 209

〈EscapeSequence static method declarations〉¶626 209

〈EscapeSequence static method definitions〉¶628 210; ¶630 211; ¶631 212

〈EscapeSequence static method definitions〉¶628 209

〈EscapeSequence utilities declaration〉¶625 209

〈EscapeSequence utilities declaration〉¶625 213

〈EscapeSequence utilities definition〉¶627 209

〈EscapeSequence utilities definition〉¶627 214

〈exampleModule actions〉¶824 271

〈exampleModule actions〉¶824 262

〈ExampleModule class declaration〉¶908 292

〈ExampleModule class declaration〉¶908 293

〈ExampleModule class inline method definitions〉¶910 292

〈ExampleModule class inline method definitions〉¶910 293

〈ExampleModule class method declarations〉¶909 292

〈ExampleModule class method declarations〉¶909 292

〈ExampleModule class method definitions〉

Page 336: Book

322 List of Modules

¶911 292; ¶912 292

〈ExampleModule class method definitions〉¶911 294

〈expression case 1 actions〉¶39 12

〈expression case 1 actions〉¶39 7

〈expression case 2 actions〉¶40 12

〈expression case 2 actions〉¶40 7

〈expression case 3 actions〉¶41 12

〈expression case 3 actions〉¶41 7

〈expression case 4 actions〉¶42 12

〈expression case 4 actions〉¶42 7

〈file actions〉¶29 10; ¶275 94; ¶788 266

〈file actions〉¶275 89; ¶29 5; ¶788 258

〈File class declaration〉¶930 301

〈File class declaration〉¶930 305

〈File class field declarations〉¶931 301

〈File class field declarations〉¶931 301

〈File class inline method definitions〉¶933 302

〈File class inline method definitions〉¶933 305

〈File class method declarations〉¶932 301

〈File class method declarations〉¶932 301

〈File class method definitions〉¶934 302; ¶935 302; ¶936 302; ¶937 303; ¶938 303; ¶939 303; ¶940 304;

¶941 304

〈File class method definitions〉¶934 305

〈fileModule case 1 actions〉¶820 270

〈fileModule case 1 actions〉¶820 262

〈fileModule case 2 actions〉

Page 337: Book

List of Modules 323

¶821 271

〈fileModule case 2 actions〉¶821 262

〈FileModule class declaration〉¶888 288

〈FileModule class declaration〉¶888 293

〈FileModule class field declarations〉¶889 288

〈FileModule class field declarations〉¶889 288

〈FileModule class inline method definitions〉¶891 288; ¶892 288

〈FileModule class inline method definitions〉¶891 293

〈FileModule class method declarations〉¶890 288

〈FileModule class method declarations〉¶890 288

〈FileModule class method definitions〉¶893 289; ¶894 289; ¶895 289; ¶896 289

〈FileModule class method definitions〉¶893 294

〈Fragment class declaration〉¶830 273

〈Fragment class declaration〉¶830 280

〈Fragment class field declarations〉¶832 273

〈Fragment class field declarations〉¶832 273

〈Fragment class inline method definitions〉¶835 274; ¶836 274

〈Fragment class inline method definitions〉¶835 280

〈Fragment class method declarations〉¶833 273; ¶834 273

〈Fragment class method declarations〉¶833 273

〈Fragment class method definitions〉¶837 274; ¶838 274; ¶839 274

〈Fragment class method definitions〉¶837 281

〈get the next character and compute nextState〉¶231 77

〈get the next character and compute nextState〉¶231 77

〈GotoRecord class declaration〉¶473 153

Page 338: Book

324 List of Modules

〈GotoRecord class declaration〉¶473 155

〈GotoRecord inline method definitions〉¶476 154; ¶477 154; ¶478 154; ¶479 154; ¶480 154

〈GotoRecord inline method definitions〉¶476 155

〈GotoRecord method declarations〉¶474 153; ¶475 153

〈GotoRecord method declarations〉¶474 153

〈GotoRecord method definitions〉¶481 154; ¶482 154; ¶483 155

〈GotoRecord method definitions〉¶481 155

〈Gramatika actions preamble〉¶274 94

〈Gramatika actions preamble〉¶274 97

〈Gramatika auxiliary definitions〉¶242 87

〈Gramatika auxiliary definitions〉¶242 88

〈Gramatika constants〉¶557 181

〈Gramatika constants〉¶557 193

〈Gramatika grammar productions〉¶247 89; ¶248 89; ¶249 89; ¶250 89; ¶251 89; ¶252 90; ¶253 90; ¶254 90;

¶255 90; ¶256 90; ¶257 90; ¶258 91; ¶259 91; ¶260 91; ¶261 91; ¶26292; ¶263 92

〈Gramatika grammar productions〉¶247 97

〈Gramatika main method definition〉¶600 195

〈Gramatika main method definition〉¶600 198

〈Gramatika non-terminal definitions〉¶245 88

〈Gramatika non-terminal definitions〉¶245 97

〈Gramatika start symbol definition〉¶246 89

〈Gramatika start symbol definition〉¶246 97

〈Gramatika token definitions〉¶243 88

〈Gramatika token definitions〉¶243 88

〈Gramatika type declarations〉

Page 339: Book

List of Modules 325

¶553 181; ¶554 181; ¶555 181; ¶556 181

〈Gramatika type declarations〉¶553 193

〈Group class declaration〉¶915 295

〈Group class declaration〉¶915 299

〈Group class field declarations〉¶916 295

〈Group class field declarations〉¶916 295

〈Group class inline method definitions〉¶919 296; ¶920 296; ¶921 296; ¶922 296; ¶923 296

〈Group class inline method definitions〉¶919 299

〈Group class method declarations〉¶917 295; ¶918 295

〈Group class method declarations〉¶917 295

〈Group class method definitions〉¶924 296; ¶925 297; ¶926 297; ¶927 298

〈Group class method definitions〉¶924 299

〈HashTable class declaration〉¶729 245

〈HashTable class declaration〉¶729 252

〈HashTable constants〉¶731 245

〈HashTable constants〉¶731 245

〈HashTable field declarations〉¶732 246

〈HashTable field declarations〉¶732 245

〈HashTable inline method definitions〉¶734 246; ¶735 246

〈HashTable inline method definitions〉¶734 252

〈HashTable iterator declaration〉¶744 249

〈HashTable iterator declaration〉¶744 245

〈HashTable iterator field declarations〉¶745 249

〈HashTable iterator field declarations〉¶745 249

〈HashTable iterator inline method definitions〉¶747 250; ¶748 250; ¶749 250; ¶750 250; ¶751 250

Page 340: Book

326 List of Modules

〈HashTable iterator inline method definitions〉¶747 252

〈HashTable iterator method declarations〉¶746 249

〈HashTable iterator method declarations〉¶746 249

〈HashTable iterator method definitions〉¶752 251; ¶753 252

〈HashTable iterator method definitions〉¶752 253

〈HashTable method declarations〉¶733 246

〈HashTable method declarations〉¶733 245

〈HashTable method definitions〉¶736 246; ¶737 246; ¶738 247; ¶739 247; ¶740 247; ¶741 248; ¶742 248;

¶743 249

〈HashTable method definitions〉¶736 253

〈HashTable type definitions〉¶730 245

〈HashTable type definitions〉¶730 245

〈includeDirective actions〉¶819 270

〈includeDirective actions〉¶819 262

〈includeFragment actions〉¶818 270

〈includeFragment actions〉¶818 262

〈IncludeFragment class declaration〉¶854 278

〈IncludeFragment class declaration〉¶854 280

〈IncludeFragment class field declarations〉¶855 278

〈IncludeFragment class field declarations〉¶855 278

〈IncludeFragment class inline method definitions〉¶857 278

〈IncludeFragment class inline method definitions〉¶857 280

〈IncludeFragment class method declarations〉¶856 278

〈IncludeFragment class method declarations〉¶856 278

〈IncludeFragment class method definitions〉¶858 278; ¶859 279; ¶860 279; ¶861 279

Page 341: Book

List of Modules 327

〈IncludeFragment class method definitions〉¶858 281

〈Input class template declaration〉¶183 65

〈Input class template declaration〉¶183 79

〈Input class template field declarations〉¶186 65

〈Input class template field declarations〉¶186 65

〈Input class template inline method definitions〉¶189 66

〈Input class template inline method definitions〉¶189 79

〈Input class template method declarations〉¶187 65; ¶188 66

〈Input class template method declarations〉¶187 65

〈Input class template method definitions〉¶190 66; ¶191 67; ¶192 67; ¶193 68; ¶194 68; ¶195 68; ¶196 69; ¶197 69;

¶198 69; ¶199 70; ¶200 70; ¶201 70; ¶202 70

〈Input class template method definitions〉¶190 80

〈Input class template type declarations〉¶184 65

〈Input class template type declarations〉¶184 65

〈Input constants〉¶185 65

〈Input constants〉¶185 65

〈item case 1 actions〉¶47 13

〈item case 1 actions〉¶47 8

〈item case 2 actions〉¶48 13

〈item case 2 actions〉¶48 8

〈item case 3 actions〉¶49 13

〈item case 3 actions〉¶49 8

〈item case 4 actions〉¶50 14

〈item case 4 actions〉¶50 8

〈item case 5 actions〉¶51 14

Page 342: Book

328 List of Modules

〈item case 5 actions〉¶51 8

〈item case 6 actions〉¶52 15

〈item case 6 actions〉¶52 8

〈item case 7 actions〉¶53 15

〈item case 7 actions〉¶53 8

〈Item class declaration〉¶373 125

〈Item class declaration〉¶373 131

〈Item class field declarations〉¶374 125

〈Item class field declarations〉¶374 125

〈Item class inline method definitions〉¶376 126; ¶377 126; ¶378 126; ¶379 126; ¶380 127; ¶381 127; ¶382 127;

¶383 127; ¶384 127; ¶385 127; ¶386 127

〈Item class inline method definitions〉¶376 131

〈Item class method declarations〉¶375 126

〈Item class method declarations〉¶375 125

〈Item class method definitions〉¶387 127; ¶388 128; ¶389 128; ¶390 128; ¶391 128; ¶392 129; ¶393 129;

¶394 129; ¶395 129; ¶396 130; ¶397 130; ¶398 130; ¶399 131

〈Item class method definitions〉¶387 131

〈ItemAttributes class declaration〉¶23 9; ¶269 93; ¶782 264

〈ItemAttributes class declaration〉¶23 10; ¶269 94; ¶782 265

〈ItemAttributes class inline method definitions〉¶26 10; ¶272 93; ¶785 265

〈ItemAttributes class inline method definitions〉¶26 10; ¶272 94; ¶785 265

〈ItemAttributes field declarations〉¶24 9; ¶270 93; ¶783 265

〈ItemAttributes field declarations〉¶24 9; ¶270 93; ¶783 264

〈ItemAttributes method declarations〉¶25 10; ¶271 93; ¶784 265

〈ItemAttributes method declarations〉¶25 9; ¶271 93; ¶784 264

〈ItemSet class declaration〉

Page 343: Book

List of Modules 329

¶402 133

〈ItemSet class declaration〉¶402 142

〈ItemSet class field declarations〉¶403 133

〈ItemSet class field declarations〉¶403 133

〈ItemSet class friends〉¶406 134

〈ItemSet class friends〉¶406 133

〈ItemSet class inline method definitions〉¶407 134; ¶408 134; ¶409 134

〈ItemSet class inline method definitions〉¶407 142

〈ItemSet class method declarations〉¶404 133; ¶405 134

〈ItemSet class method declarations〉¶404 133

〈ItemSet class method definitions〉¶410 134; ¶411 135; ¶412 135; ¶413 135; ¶414 135; ¶415 136; ¶416 136;

¶417 136; ¶418 137; ¶419 137; ¶420 138; ¶421 139; ¶422 139; ¶423139; ¶424 140; ¶425 140

〈ItemSet class method definitions〉¶410 142

〈ItemSet iterator class declaration〉¶426 140

〈ItemSet iterator class declaration〉¶426 133

〈ItemSet iterator class field declarations〉¶427 140

〈ItemSet iterator class field declarations〉¶427 140

〈ItemSet iterator class method declarations〉¶428 140; ¶429 140

〈ItemSet iterator class method declarations〉¶428 140

〈ItemSet iterator class method definitions〉¶430 141; ¶431 141; ¶432 141; ¶433 141; ¶434 141; ¶435 141

〈ItemSet iterator class method definitions〉¶430 142

〈LabelType declaration〉¶74 21

〈LabelType declaration〉¶74 21

〈languageDefinitionPart case 1 actions〉¶30 11

〈languageDefinitionPart case 1 actions〉¶30 5

Page 344: Book

330 List of Modules

〈languageDefinitionPart case 2 actions〉¶31 11

〈languageDefinitionPart case 2 actions〉¶31 5

〈languagePart case 1 actions〉¶276 94

〈languagePart case 1 actions〉¶276 89

〈languagePart case 2 actions〉¶277 95

〈languagePart case 2 actions〉¶277 89

〈lexical error recovery〉¶234 78

〈lexical error recovery〉¶234 77

〈Lexis actions preamble〉¶28 10

〈Lexis actions preamble〉¶28 15

〈Lexis auxiliary definitions〉¶1 3

〈Lexis auxiliary definitions〉¶1 4

〈Lexis constants〉¶177 63; ¶178 63

〈Lexis constants〉¶177 79

〈Lexis grammar productions〉¶6 5; ¶7 5; ¶8 5; ¶9 5; ¶10 5; ¶11 6; ¶12 6; ¶13 6; ¶14 7; ¶15 7; ¶16 7; ¶17

8

〈Lexis grammar productions〉¶6 15

〈Lexis main method definition〉¶237 82

〈Lexis main method definition〉¶237 84

〈Lexis non-terminal definitions〉¶4 4

〈Lexis non-terminal definitions〉¶4 15

〈Lexis start symbol definition〉¶5 5

〈Lexis start symbol definition〉¶5 15

〈Lexis token definitions〉¶2 4

〈Lexis token definitions〉¶2 4

Page 345: Book

List of Modules 331

〈Lexis type declarations〉¶175 63; ¶176 63

〈Lexis type declarations〉¶175 79

〈List static method declarations〉¶718 241; ¶719 241; ¶720 241; ¶721 241

〈List static method declarations〉¶718 241

〈List static method definitions〉¶723 242; ¶724 242; ¶725 243; ¶726 243

〈List static method definitions〉¶723 242

〈List utilities declaration〉¶717 241

〈List utilities declaration〉¶717 244

〈List utilities definition〉¶722 242

〈List utilities definition〉¶722 244

〈Loma actions preamble〉¶787 266

〈Loma actions preamble〉¶787 272

〈Loma grammar productions〉¶760 258; ¶761 258; ¶762 259; ¶763 259; ¶764 259; ¶765 259; ¶766 260;

¶767 261; ¶768 261; ¶769 262; ¶770 262; ¶771 262; ¶772 262; ¶773262; ¶774 263; ¶775 263; ¶776 263; ¶777 263

〈Loma grammar productions〉¶760 272

〈Loma main program definition〉¶952 312

〈Loma main program definition〉¶952 314

〈Loma non-terminal definitions〉¶758 258

〈Loma non-terminal definitions〉¶758 272

〈Loma output method declarations〉¶944 307

〈Loma output method declarations〉¶944 310

〈Loma output method definitions〉¶945 307; ¶946 308; ¶947 308; ¶948 309; ¶949 309

〈Loma output method definitions〉¶945 310

〈Loma start symbol definition〉¶759 258

〈Loma start symbol definition〉

Page 346: Book

332 List of Modules

¶759 272

〈Loma token definitions〉¶756 257

〈Loma token definitions〉¶756 257

〈MapEntry field declarations〉¶562 182

〈MapEntry field declarations〉¶562 182

〈MapEntry struct declaration〉¶561 182

〈MapEntry struct declaration〉¶561 193

〈MapEntry struct inline method definitions〉¶563 182

〈MapEntry struct inline method definitions〉¶563 193

〈module case 1 actions〉¶791 266

〈module case 1 actions〉¶791 259

〈module case 2 actions〉¶792 266

〈module case 2 actions〉¶792 259

〈module case 3 actions〉¶793 266

〈module case 3 actions〉¶793 259

〈module case 4 actions〉¶794 266

〈module case 4 actions〉¶794 259

〈Module class declaration〉¶864 283

〈Module class declaration〉¶864 293

〈Module class field declarations〉¶866 283

〈Module class field declarations〉¶866 283

〈Module class inline method definitions〉¶870 284; ¶871 284; ¶872 284

〈Module class inline method definitions〉¶870 293

〈Module class method declarations〉¶867 283; ¶868 284

〈Module class method declarations〉¶867 283

Page 347: Book

List of Modules 333

〈Module class method definitions〉¶873 285; ¶874 285; ¶875 285; ¶876 285; ¶877 285; ¶878 286; ¶879 286;

¶880 286; ¶881 286

〈Module class method definitions〉¶873 294

〈Module class static field definitions〉¶869 284

〈Module class static field definitions〉¶869 294

〈moduleList case 1 actions〉¶789 266

〈moduleList case 1 actions〉¶789 258

〈moduleList case 2 actions〉¶790 266

〈moduleList case 2 actions〉¶790 258

〈ModuleList type declaration〉¶865 283

〈ModuleList type declaration〉¶865 293

〈NFA class declaration〉¶92 27

〈NFA class declaration〉¶92 35

〈NFA class field declarations〉¶93 27

〈NFA class field declarations〉¶93 27

〈NFA class friends〉¶95 28

〈NFA class friends〉¶95 27

〈NFA class inline method definitions〉¶96 28; ¶97 28; ¶98 28; ¶99 29

〈NFA class inline method definitions〉¶96 35

〈NFA class method declarations〉¶94 28

〈NFA class method declarations〉¶94 27

〈NFA class method definitions〉¶100 29; ¶101 29; ¶102 29; ¶103 30; ¶104 30; ¶105 30; ¶106 30; ¶107 30;

¶108 31; ¶109 31; ¶110 31; ¶111 32; ¶112 33; ¶113 33; ¶114 33; ¶11534; ¶116 34; ¶117 34; ¶118 35; ¶119 35

〈NFA class method definitions〉¶100 36

〈NFAState class declaration〉¶73 21

Page 348: Book

334 List of Modules

〈NFAState class declaration〉¶73 26

〈NFAState class inline method definitions〉¶78 22; ¶79 22; ¶80 22; ¶81 23

〈NFAState class inline method definitions〉¶78 26

〈NFAState constants〉¶75 21

〈NFAState constants〉¶75 21

〈NFAState field declarations〉¶76 22

〈NFAState field declarations〉¶76 21

〈NFAState method declarations〉¶77 22

〈NFAState method declarations〉¶77 21

〈NFAState method definitions〉¶82 23; ¶83 23; ¶84 23; ¶85 24; ¶86 24; ¶87 24; ¶88 24; ¶89 25

〈NFAState method definitions〉¶82 26

〈nonterminalDeclaration case 1 actions〉¶281 95

〈nonterminalDeclaration case 1 actions〉¶281 90

〈nonterminalDeclaration case 2 actions〉¶282 95

〈nonterminalDeclaration case 2 actions〉¶282 90

〈normal transition processing〉¶232 78

〈normal transition processing〉¶232 77

〈Parser class template declaration〉¶584 188

〈Parser class template declaration〉¶584 193

〈Parser class template field declarations〉¶586 188

〈Parser class template field declarations〉¶586 188

〈Parser class template method declarations〉¶587 188; ¶588 188

〈Parser class template method declarations〉¶587 188

〈Parser class template method definitions〉¶589 189; ¶590 189; ¶591 189; ¶592 189; ¶593 190; ¶594 190; ¶595 190;

¶596 191; ¶597 192

Page 349: Book

List of Modules 335

〈Parser class template method definitions〉¶589 194

〈Parser class template type declarations〉¶585 188

〈Parser class template type declarations〉¶585 188

〈ParserData class declaration〉¶18 8; ¶264 92; ¶778 264

〈ParserData class declaration〉¶18 9; ¶264 93; ¶778 264

〈ParserData class field declarations〉¶265 92

〈ParserData class field declarations〉¶265 92

〈ParserData class inline method definitions〉¶21 9; ¶267 92

〈ParserData class inline method definitions〉¶21 9; ¶267 93

〈ParserData class method declarations〉¶266 92

〈ParserData class method declarations〉¶266 92

〈ParserData field declarations〉¶19 8

〈ParserData field declarations〉¶19 8

〈ParserData method declarations〉¶20 9

〈ParserData method declarations〉¶20 8

〈ParseTable class declaration〉¶533 171

〈ParseTable class declaration〉¶533 179

〈ParseTable class inline method definitions〉¶538 172

〈ParseTable class inline method definitions〉¶538 179

〈ParseTable class method definitions〉¶539 173; ¶540 173; ¶541 174; ¶542 174; ¶543 175; ¶544 175; ¶545 176;

¶546 176; ¶547 177; ¶548 177; ¶549 178; ¶550 178

〈ParseTable class method definitions〉¶539 179

〈ParseTable constants〉¶534 171

〈ParseTable constants〉¶534 171

〈ParseTable field declarations〉¶535 171

Page 350: Book

336 List of Modules

〈ParseTable field declarations〉¶535 171

〈ParseTable method declarations〉¶536 172; ¶537 172

〈ParseTable method declarations〉¶536 171

〈Partition class declaration〉¶438 143

〈Partition class declaration〉¶438 148

〈Partition class field declarations〉¶439 143

〈Partition class field declarations〉¶439 143

〈Partition class inline method definitions〉¶441 144

〈Partition class inline method definitions〉¶441 148

〈Partition class iterator declaration〉¶446 145

〈Partition class iterator declaration〉¶446 143

〈Partition class method declarations〉¶440 143

〈Partition class method declarations〉¶440 143

〈Partition class method definitions〉¶442 144; ¶443 144; ¶444 145; ¶445 145

〈Partition class method definitions〉¶442 148

〈Partition iterator field declarations〉¶447 145

〈Partition iterator field declarations〉¶447 145

〈Partition iterator method declarations〉¶448 146; ¶449 146

〈Partition iterator method declarations〉¶448 145

〈Partition iterator method definitions〉¶450 146; ¶451 146; ¶452 146; ¶453 147; ¶454 147; ¶455 147; ¶456 147

〈Partition iterator method definitions〉¶450 148

〈PathName class declaration〉¶636 215

〈PathName class declaration〉¶636 218

〈PathName constants〉¶637 215

〈PathName constants〉

Page 351: Book

List of Modules 337

¶637 215

〈PathName inline method definitions〉¶639 216; ¶640 216; ¶641 216; ¶642 216

〈PathName inline method definitions〉¶639 218

〈PathName method declarations〉¶638 216

〈PathName method declarations〉¶638 215

〈PathName method definitions〉¶643 217; ¶644 217; ¶645 217; ¶646 217; ¶647 218

〈PathName method definitions〉¶643 218

〈preamblePart actions〉¶278 95

〈preamblePart actions〉¶278 89

〈process $ $ 〉¶338 109

〈process $ $ 〉¶338 109

〈process $ identifier〉¶339 110

〈process $ identifier〉¶339 109

〈process the partition element *part〉¶527 169

〈process the partition element *part〉¶527 168

〈ProcessArgument static method definition〉¶954 313

〈ProcessArgument static method definition〉¶954 312

〈production case 1 actions〉¶287 96

〈production case 1 actions〉¶287 91

〈production case 2 actions〉¶288 96

〈production case 2 actions〉¶288 91

〈Production class declaration〉¶319 105

〈Production class declaration〉¶319 111

〈Production class field declarations〉¶320 105

〈Production class field declarations〉¶320 105

Page 352: Book

338 List of Modules

〈Production class inline method definitions〉¶323 106; ¶324 106; ¶325 106; ¶326 106; ¶327 106; ¶328 107; ¶329 107

〈Production class inline method definitions〉¶323 111

〈Production class method declarations〉¶321 105; ¶322 106

〈Production class method declarations〉¶321 105

〈Production class method definitions〉¶330 107; ¶331 107; ¶332 107; ¶333 108; ¶334 108; ¶335 108; ¶336 108;

¶337 109

〈Production class method definitions〉¶330 111

〈quotedString actions〉¶825 271

〈quotedString actions〉¶825 263

〈read the Gramatika input file〉¶601 196

〈read the Gramatika input file〉¶601 195

〈read the Lexis input file〉¶238 83

〈read the Lexis input file〉¶238 82

〈read the Loma input file〉¶956 314

〈read the Loma input file〉¶956 313

〈read the response file〉¶953 313

〈read the response file〉¶953 312

〈return the last acceptable token〉¶233 78

〈return the last acceptable token〉¶233 77

〈rightHandSide case 1 actions〉¶285 96

〈rightHandSide case 1 actions〉¶285 91

〈rightHandSide case 2 actions〉¶286 96

〈rightHandSide case 2 actions〉¶286 91

〈rule actions〉¶284 95

〈rule actions〉¶284 91

Page 353: Book

List of Modules 339

〈Set class declaration〉¶666 223

〈Set class declaration〉¶666 235

〈Set constants〉¶667 223

〈Set constants〉¶667 223

〈Set field declarations〉¶668 223

〈Set field declarations〉¶668 223

〈Set inline method definitions〉¶671 224

〈Set inline method definitions〉¶671 235

〈Set iterator declaration〉¶696 232

〈Set iterator declaration〉¶696 223

〈Set iterator field declarations〉¶697 232

〈Set iterator field declarations〉¶697 232

〈Set iterator inline method definitions〉¶699 233; ¶700 233; ¶701 233; ¶702 233; ¶703 233; ¶704 234

〈Set iterator inline method definitions〉¶699 235

〈Set iterator method declarations〉¶698 233

〈Set iterator method declarations〉¶698 232

〈Set iterator method definitions〉¶705 234

〈Set iterator method definitions〉¶705 235

〈Set method declarations〉¶669 223; ¶670 224

〈Set method declarations〉¶669 223

〈Set method definitions〉¶672 224; ¶673 224; ¶674 225; ¶675 225; ¶676 225; ¶677 225; ¶678 226;

¶679 226; ¶680 226; ¶681 226; ¶682 227; ¶683 227; ¶684 227; ¶685227; ¶686 228; ¶687 228; ¶688 228; ¶689 229; ¶690 229; ¶691 229;

¶692 230; ¶693 230; ¶694 231; ¶695 232

〈Set method definitions〉¶672 235

〈Stack class declaration〉¶650 219

Page 354: Book

340 List of Modules

〈Stack class declaration〉¶650 222

〈Stack field declarations〉¶652 219

〈Stack field declarations〉¶652 219

〈Stack inline method definitions〉¶663 221

〈Stack inline method definitions〉¶663 222

〈Stack method declarations〉¶653 219

〈Stack method declarations〉¶653 219

〈Stack method definitions〉¶654 220; ¶655 220; ¶656 220; ¶657 220; ¶658 220; ¶659 221; ¶660 221;

¶661 221; ¶662 221

〈Stack method definitions〉¶654 222

〈Stack type definitions〉¶651 219

〈Stack type definitions〉¶651 219

〈StackItem class template declaration〉¶576 186

〈StackItem class template declaration〉¶576 193

〈StackItem class template field declarations〉¶578 186

〈StackItem class template field declarations〉¶578 186

〈StackItem class template inline method definitions〉¶580 187; ¶581 187; ¶582 187

〈StackItem class template inline method definitions〉¶580 193

〈StackItem class template method declarations〉¶579 186

〈StackItem class template method declarations〉¶579 186

〈StackItem class template method definitions〉¶583 187

〈StackItem class template method definitions〉¶583 194

〈StackItem class template type declarations〉¶577 186

〈StackItem class template type declarations〉¶577 186

〈startPart actions〉¶283 95

Page 355: Book

List of Modules 341

〈startPart actions〉¶283 90

〈State class declaration〉¶55 17; ¶486 157

〈State class declaration〉¶486 162; ¶55 20

〈State class field declarations〉¶56 17; ¶487 157

〈State class field declarations〉¶487 157; ¶56 17

〈State class inline method definitions〉¶58 18; ¶59 18; ¶60 18; ¶61 18; ¶62 18; ¶63 18; ¶64 18; ¶490 158; ¶491

158; ¶492 158; ¶493 158; ¶494 158; ¶495 159; ¶496 159; ¶497 159;

¶498 159

〈State class inline method definitions〉¶490 162; ¶58 20

〈State class method declarations〉¶57 17; ¶488 157; ¶489 158

〈State class method declarations〉¶488 157; ¶57 17

〈State class method definitions〉¶499 159; ¶500 159; ¶501 159; ¶502 160; ¶503 160; ¶504 160; ¶505 160;

¶506 160; ¶507 161; ¶508 161; ¶509 162

〈State class method definitions〉¶499 163

〈State method definitions〉¶65 19; ¶66 19; ¶67 19; ¶68 19; ¶69 19; ¶70 19

〈State method definitions〉¶65 20

〈String constants〉¶620 205

〈String constants〉¶620 205

〈String static method declarations〉¶618 205

〈String static method declarations〉¶618 205

〈String static method definitions〉¶621 206; ¶622 206

〈String static method definitions〉¶621 205

〈String utilities declaration〉¶617 205

〈String utilities declaration〉¶617 206

〈String utilities definition〉¶619 205

〈String utilities definition〉¶619 207

Page 356: Book

342 List of Modules

〈symbol case 1 actions〉¶291 96

〈symbol case 1 actions〉¶291 92

〈symbol case 2 actions〉¶292 96

〈symbol case 2 actions〉¶292 92

〈Symbol class declaration〉¶294 99

〈Symbol class declaration〉¶294 104

〈Symbol class field declarations〉¶295 99

〈Symbol class field declarations〉¶295 99

〈Symbol class inline method definitions〉¶297 100; ¶298 100; ¶299 100; ¶300 100; ¶301 101; ¶302 101; ¶303 101;

¶304 101; ¶305 101; ¶306 101; ¶307 101; ¶308 101; ¶309 101

〈Symbol class inline method definitions〉¶297 104

〈Symbol class method declarations〉¶296 100

〈Symbol class method declarations〉¶296 99

〈Symbol class method definitions〉¶310 102; ¶311 102; ¶312 102; ¶313 102; ¶314 103; ¶315 103; ¶316 103

〈Symbol class method definitions〉¶310 104

〈symbolList case 1 actions〉¶289 96

〈symbolList case 1 actions〉¶289 91

〈symbolList case 2 actions〉¶290 96

〈symbolList case 2 actions〉¶290 91

〈SymbolTable class declaration〉¶342 113

〈SymbolTable class declaration〉¶342 123

〈SymbolTable class field declarations〉¶343 113

〈SymbolTable class field declarations〉¶343 113

〈SymbolTable class inline method definitions〉¶346 114; ¶347 114; ¶348 114; ¶349 114; ¶350 115; ¶351 115; ¶352 115;

¶353 115; ¶354 115; ¶355 115

〈SymbolTable class inline method definitions〉

Page 357: Book

List of Modules 343

¶346 123

〈SymbolTable class method declarations〉¶344 113; ¶345 114

〈SymbolTable class method declarations〉¶344 113

〈SymbolTable class method definitions〉¶356 115; ¶357 116; ¶358 116; ¶359 116; ¶360 117; ¶361 117; ¶362 117;

¶363 117; ¶364 118; ¶365 119; ¶366 119; ¶367 120; ¶368 120; ¶369121; ¶370 122

〈SymbolTable class method definitions〉¶356 123

〈SymbolType enumeration declaration〉¶564 183

〈SymbolType enumeration declaration〉¶564 193

〈SymbolType enumeration inline method definitions〉¶565 183

〈SymbolType enumeration inline method definitions〉¶565 193

〈TableEntry field declarations〉¶559 182

〈TableEntry field declarations〉¶559 182

〈TableEntry struct declaration〉¶558 182

〈TableEntry struct declaration〉¶558 193

〈TableEntry struct inline method definitions〉¶560 182

〈TableEntry struct inline method definitions〉¶560 193

〈terminalDeclaration actions〉¶280 95

〈terminalDeclaration actions〉¶280 90

〈terminalsPart case 2 actions〉¶279 95

〈terminalsPart case 2 actions〉¶279 89

〈textFragment case 1 actions〉¶797 267

〈textFragment case 1 actions〉¶797 259

〈textFragment case 2 actions〉¶798 267

〈textFragment case 2 actions〉¶798 259

〈TextFragment class declaration〉¶840 275

Page 358: Book

344 List of Modules

〈TextFragment class declaration〉¶840 280

〈TextFragment class field declarations〉¶841 275

〈TextFragment class field declarations〉¶841 275

〈TextFragment class inline method definitions〉¶843 275

〈TextFragment class inline method definitions〉¶843 280

〈TextFragment class method declarations〉¶842 275

〈TextFragment class method declarations〉¶842 275

〈TextFragment class method definitions〉¶844 275; ¶845 275; ¶846 275

〈TextFragment class method definitions〉¶844 281

〈textModule actions〉¶795 267

〈textModule actions〉¶795 259

〈TextModule class declaration〉¶882 287

〈TextModule class declaration〉¶882 293

〈TextModule class inline method definitions〉¶884 287

〈TextModule class inline method definitions〉¶884 293

〈TextModule class method definitions〉¶885 287; ¶886 287; ¶887 287

〈TextModule class method definitions〉¶885 294

〈TextModule method declarations〉¶883 287

〈TextModule method declarations〉¶883 287

〈textSequence actions〉¶796 267

〈textSequence actions〉¶796 259

〈textttFragmentList declaration〉¶831 273

〈textttFragmentList declaration〉¶831 280

〈Token class template declaration〉¶203 71

〈Token class template declaration〉

Page 359: Book

List of Modules 345

¶203 79

〈Token class template field declarations〉¶205 71

〈Token class template field declarations〉¶205 71

〈Token class template inline method definitions〉¶207 72; ¶208 72; ¶209 72; ¶210 72

〈Token class template inline method definitions〉¶207 79

〈Token class template method declarations〉¶206 71

〈Token class template method declarations〉¶206 71

〈Token class template method definitions〉¶211 72; ¶212 72; ¶213 73; ¶214 73; ¶215 73; ¶216 73

〈Token class template method definitions〉¶211 80

〈Token class template type declarations〉¶204 71

〈Token class template type declarations〉¶204 71

〈tokenDefinition case 1 actions〉¶37 12

〈tokenDefinition case 1 actions〉¶37 6

〈tokenDefinition case 2 actions〉¶38 12

〈tokenDefinition case 2 actions〉¶38 6

〈tokenDefinitionList case 1 actions〉¶35 11

〈tokenDefinitionList case 1 actions〉¶35 6

〈tokenDefinitionList case 2 actions〉¶36 11

〈tokenDefinitionList case 2 actions〉¶36 6

〈tokenDefinitionsPart case 1 actions〉¶33 11

〈tokenDefinitionsPart case 1 actions〉¶33 6

〈tokenDefinitionsPart case 2 actions〉¶34 11

〈tokenDefinitionsPart case 2 actions〉¶34 6

〈TokenStream class template declaration〉¶218 74

〈TokenStream class template declaration〉¶218 79

Page 360: Book

346 List of Modules

〈TokenStream class template field declarations〉¶220 74

〈TokenStream class template field declarations〉¶220 74

〈TokenStream class template forward declaration〉¶217 74

〈TokenStream class template forward declaration〉¶217 79

〈TokenStream class template inline method definitions〉¶223 75

〈TokenStream class template inline method definitions〉¶223 79

〈TokenStream class template method declarations〉¶221 74; ¶222 74

〈TokenStream class template method declarations〉¶221 74

〈TokenStream class template method definitions〉¶224 75; ¶225 75; ¶226 75; ¶227 75; ¶228 76; ¶229 76; ¶230 77

〈TokenStream class template method definitions〉¶224 80

〈TokenStream class template type declarations〉¶219 74

〈TokenStream class template type declarations〉¶219 74

〈Value class template declaration〉¶568 184

〈Value class template declaration〉¶568 193

〈Value class template field declarations〉¶570 184

〈Value class template field declarations〉¶570 184

〈Value class template inline method definitions〉¶572 185; ¶573 185; ¶574 185

〈Value class template inline method definitions〉¶572 193

〈Value class template method declarations〉¶571 184

〈Value class template method declarations〉¶571 184

〈Value class template method definitions〉¶575 185

〈Value class template method definitions〉¶575 194

〈Value class template type declarations〉¶569 184

〈Value class template type declarations〉¶569 184

〈wordList case 1 actions〉

Page 361: Book

List of Modules 347

¶799 267

〈wordList case 1 actions〉¶799 260

〈wordList case 10 actions〉¶808 269

〈wordList case 10 actions〉¶808 260

〈wordList case 2 actions〉¶800 267

〈wordList case 2 actions〉¶800 260

〈wordList case 3 actions〉¶801 267

〈wordList case 3 actions〉¶801 260

〈wordList case 4 actions〉¶802 268

〈wordList case 4 actions〉¶802 260

〈wordList case 5 actions〉¶803 268

〈wordList case 5 actions〉¶803 260

〈wordList case 6 actions〉¶804 268

〈wordList case 6 actions〉¶804 260

〈wordList case 7 actions〉¶805 268

〈wordList case 7 actions〉¶805 260

〈wordList case 8 actions〉¶806 268

〈wordList case 8 actions〉¶806 260

〈wordList case 9 actions〉¶807 268

〈wordList case 9 actions〉¶807 260

〈WordList struct declaration〉¶780 264

〈WordList struct declaration〉¶780 265

〈WordList struct field declarations〉¶781 264

〈WordList struct field declarations〉¶781 264

〈write the Gramatika action file〉¶604 197

Page 362: Book

348 List of Modules

〈write the Gramatika action file〉¶604 196

〈write the Gramatika definitions file〉¶605 197

〈write the Gramatika definitions file〉¶605 196

〈write the Gramatika output files〉¶603 196

〈write the Gramatika output files〉¶603 195

〈write the Gramatika table file〉¶606 197

〈write the Gramatika table file〉¶606 196

〈write the Lexis output files〉¶240 84

〈write the Lexis output files〉¶240 82

〈write the Loma output file〉¶957 314

〈write the Loma output file〉¶957 313

〈ws case 1 actions〉¶826 271

〈ws case 1 actions〉¶826 263

〈ws case 2 actions〉¶827 271

〈ws case 2 actions〉¶827 263

〈ws case 3 actions〉¶828 271

〈ws case 3 actions〉¶828 263

Page 363: Book

List of Symbols

accept

Lexis:: ¶181(4) 64

Action

Gramatika:: ¶566(1) 183

Lexis:: ¶181(1) 64

action

Gramatika::Production:: ¶320(4) 105

Gramatika::TableEntry:: ¶559(2) 182

Lexis::State:: ¶56(3) 17

actionMap

Gramatika::ParseTable:: ¶535(5) 171

ActionRecord

Gramatika::ActionRecord:: ¶459(1) 149; ¶461(1) 149; ¶468(1) 150

actions

Gramatika::State:: ¶487(4) 157

actionStates

Gramatika::ParseTable:: ¶535(8) 171

actionTable

Gramatika::ParseTable:: ¶535(6) 171

Lexis::CompressedDFA:: ¶155(8) 51

actionTableSize

Gramatika::ParseTable:: ¶535(7) 171

add

Gramatika::ItemSet:: ¶405(7) 134; ¶405(8) 134; ¶415(1) 136; ¶416(1) 136

Gramatika::Production:: ¶322(6) 106; ¶334(1) 108

Toolbox::HashTable<K,V>:: ¶733(3) 246; ¶738(2) 247

addEpsilonItems

Gramatika::State:: ¶489(9) 158; ¶501(1) 159

addGoto

Gramatika::State:: ¶489(11) 158; ¶503(1) 160

addKernelItems

Gramatika::ItemSet:: ¶405(9) 134; ¶417(1) 136

addLookAhead

Gramatika::Item:: ¶375(17) 126; ¶393(1) 129

addReduction

Gramatika::State:: ¶489(12) 158; ¶504(1) 160

addReductions

Gramatika::Automaton:: ¶514(5) 165; ¶529(1) 169

Page 364: Book

350 List of Symbols

Gramatika::State:: ¶489(13) 158; ¶508(1) 161

addShift

Gramatika::State:: ¶489(10) 158; ¶502(1) 160

addTransition

Lexis::DFAState:: ¶124(3) 37; ¶129(1) 38

addUnfinishedState

Gramatika::Automaton:: ¶514(6) 165; ¶524(1) 167

advance

Lexis:: ¶181(3) 64

Lexis::Input<T>:: ¶187(2) 65

advanceDot

Gramatika::Item:: ¶375(16) 126; ¶392(1) 129

advanceDots

Gramatika::ItemSet:: ¶405(10) 134; ¶418(1) 137

advanceStartMark

Lexis::Input<T>:: ¶188(10) 66

Anchor

Lexis:: ¶179(1) 64

anchor

Lexis::State:: ¶56(2) 17

anchorTable

Lexis::CompressedDFA:: ¶155(7) 51

append

Gramatika::Symbol:: ¶296(6) 100; ¶312(1) 102

Loma::Group:: ¶918(3) 295; ¶922(1) 296

appendTo

Toolbox::List<T>:: ¶719(2) 241; ¶724(2) 242

Attributes

Gramatika::Parser<T,A,GT>:: ¶585(2) 188

Gramatika::StackItem<T,A>:: ¶577(2) 186

Gramatika::Value<T,A>:: ¶569(2) 184

Automaton

Gramatika::Automaton:: ¶512(1) 165; ¶515(1) 165; ¶519(1) 166

~Automaton

Gramatika::Automaton:: ¶515(2) 165; ¶520(1) 166

automaton

Gramatika::ParseTable:: ¶535(1) 171

begin

Gramatika::ItemSet:: ¶405(16) 134; ¶425(1) 140

Gramatika::Partition:: ¶440(4) 143; ¶445(1) 145

Toolbox::HashTable<K,V>:: ¶733(8) 246; ¶742(2) 248

Toolbox::Set:: ¶670(23) 224; ¶695(1) 232

both

Lexis:: ¶179(6) 64

buffer

Lexis::Input<T>:: ¶186(2) 65

bufferSize

Lexis::Input<T>:: ¶185(4) 65

Character

Page 365: Book

List of Symbols 351

Toolbox::Character:: ¶608(1) 201; ¶610(1) 201

character

Lexis::NFAState::LabelType:: ¶74(4) 21

CharacterSet

Toolbox::CharacterSet:: ¶708(1) 237; ¶709(1) 237; ¶709(2) 237; ¶709(3)237; ¶710(1) 237; ¶711(1) 237; ¶712(1) 238

characterSet

Lexis::NFAState::LabelType:: ¶74(5) 21

checkAssertions

Lexis::Input<T>:: ¶187(1) 65

checkused

Loma::CodeModule:: ¶899(5) 290; ¶905(1) 291

Loma::Module:: ¶868(6) 284; ¶876(1) 285

clearText

Loma:: ¶944(4) 307; ¶948(1) 309

close

Gramatika::ItemSet:: ¶404(1) 133; ¶420(1) 138

closed

Gramatika::State:: ¶487(6) 157

code

Loma::CodeFragment:: ¶848(3) 276

CodeFragment

Loma::CodeFragment:: ¶847(1) 276; ¶849(1) 276; ¶850(1) 276

~CodeFragment

Loma::CodeFragment:: ¶849(3) 276; ¶851(1) 276

CodeModule

Loma::CodeModule:: ¶897(1) 289; ¶899(1) 290; ¶899(2) 290; ¶900(1) 290;

¶901(1) 290

~CodeModule

Loma::CodeModule:: ¶899(3) 290; ¶903(1) 290

columnMap

Lexis::CompressedDFA:: ¶155(6) 51

compare

Gramatika::ActionRecord:: ¶460(1) 149; ¶469(1) 151

Gramatika::GotoRecord:: ¶474(1) 153; ¶482(1) 154

Gramatika::Item:: ¶375(12) 126; ¶391(1) 128

Gramatika::ItemSet:: ¶405(5) 134; ¶414(1) 135

Gramatika::State:: ¶489(6) 158; ¶500(1) 159

CompressedDFA

Lexis::CompressedDFA:: ¶154(1) 51; ¶157(1) 52; ¶158(1) 52

~CompressedDFA

Lexis::CompressedDFA:: ¶157(2) 52; ¶164(1) 54

computeFirstSets

Gramatika::SymbolTable:: ¶345(12) 114; ¶367(1) 120

connect

Lexis::NFAState:: ¶77(3) 22; ¶77(4) 22; ¶77(5) 22; ¶77(6) 22

Constants

Gramatika:: ¶557(1) 181

Gramatika::ParseTable:: ¶534(1) 171

Page 366: Book

352 List of Symbols

Lexis::DFA:: ¶135(1) 41

Lexis::Input<T>:: ¶185(1) 65

Lexis::NFAState:: ¶75(1) 21

Toolbox::HashTable<K,V>:: ¶731(1) 245

Toolbox::PathName:: ¶637(1) 215

Toolbox::Set:: ¶667(1) 223

const& getLanguage

Lexis::NFA:: ¶94(19) 28

const_iterator

Gramatika::ItemSet::const_iterator:: ¶428(1) 140; ¶429(1) 140; ¶429(2)140; ¶430(1) 141; ¶431(1) 141; ¶432(1) 141

Gramatika::Partition::const_iterator:: ¶448(1) 146; ¶449(1) 146; ¶449(2)146; ¶450(1) 146; ¶451(1) 146; ¶452(1) 146

Toolbox::HashTable<K,V>::const_iterator:: ¶744(1) 249; ¶746(1) 249;

¶746(2) 249; ¶746(5) 249; ¶747(2) 250; ¶748(2) 250; ¶749(2) 250

Toolbox::Set::const_iterator:: ¶696(1) 232; ¶698(1) 233; ¶698(2) 233;

¶698(3) 233; ¶699(1) 233; ¶700(1) 233; ¶701(1) 233

const& operator[]

Gramatika::SymbolTable:: ¶345(15) 114

contains

Toolbox::Set:: ¶670(20) 224; ¶692(1) 230

count

Gramatika::MapEntry:: ¶562(2) 182

createStartState

Gramatika::Automaton:: ¶514(3) 165; ¶525(1) 168

createStates

Gramatika::Automaton:: ¶514(4) 165; ¶526(1) 168

crossReference

Loma::File:: ¶932(6) 301; ¶937(1) 303

Loma::Fragment:: ¶834(3) 273; ¶839(1) 274

Loma::IncludeFragment:: ¶856(3) 278; ¶859(1) 279

Loma::Module:: ¶868(5) 284; ¶878(1) 286

currentLineNumber

Lexis::Input<T>:: ¶186(9) 65

data

Toolbox::Set:: ¶668(1) 223

Toolbox::Stack<T>:: ¶652(1) 219

declareNonterminal

Gramatika::SymbolTable:: ¶345(8) 114; ¶345(9) 114; ¶362(1) 117; ¶363(1)117

declareProductions

Gramatika::SymbolTable:: ¶345(10) 114; ¶364(1) 118

declareStartSymbol

Gramatika::SymbolTable:: ¶345(6) 114; ¶359(1) 116

declareSymbol

Gramatika::SymbolTable:: ¶344(1) 113; ¶360(1) 117

declareTerminal

Gramatika::SymbolTable:: ¶345(7) 114; ¶361(1) 117

decode

Page 367: Book

List of Symbols 353

Toolbox::EscapeSequence:: ¶626(3) 209; ¶631(1) 212

defaultSize

Toolbox::HashTable<K,V>:: ¶731(3) 245

defined

Gramatika::Symbol:: ¶295(6) 99

definitionsTable

Lexis::ParserData:: ¶19(1) 8

DFA

Lexis::DFA:: ¶134(1) 41; ¶138(1) 42

~DFA

Lexis::DFA:: ¶138(2) 42

DFAState

Lexis::DFAState:: ¶122(1) 37; ¶124(1) 37; ¶127(1) 38

~DFAState

Lexis::DFAState:: ¶124(2) 37; ¶128(1) 38

discard

Lexis:: ¶181(5) 64

divShift

Toolbox::Set:: ¶667(3) 223

dot

Toolbox::PathName:: ¶637(3) 215

dotPosition

Gramatika::Item:: ¶374(2) 125

elementSymbol

Gramatika::Partition:: ¶439(4) 143

encode

Toolbox::EscapeSequence:: ¶626(1) 209; ¶626(2) 209; ¶628(1) 210; ¶630(1)211

endMarkLineNumber

Lexis::Input<T>:: ¶186(11) 65

endOfBuffer

Lexis::Input<T>:: ¶186(5) 65

end_of_file

Gramatika:: ¶557(4) 181

endOfFileRead

Lexis::Input<T>:: ¶186(8) 65

endOfInput

Lexis::Input<T>:: ¶186(4) 65

endOfLexeme

Lexis::Input<T>:: ¶186(7) 65

enlarge

Toolbox::Set:: ¶669(1) 223; ¶675(1) 225

epsilon

Gramatika:: ¶557(3) 181

Lexis::NFAState::LabelType:: ¶74(3) 21

epsilonItems

Gramatika::State:: ¶487(3) 157

equals

Toolbox::List<T>:: ¶718(2) 241; ¶723(2) 242

Page 368: Book

354 List of Symbols

error

Gramatika:: ¶566(5) 183

errorState

Lexis:: ¶177(1) 63

EscapeSequence

Toolbox::EscapeSequence:: ¶625(1) 209; ¶627(1) 209

ExampleModule

Loma::ExampleModule:: ¶908(1) 292; ¶909(1) 292; ¶910(1) 292

~ExampleModule

Loma::ExampleModule:: ¶909(2) 292; ¶911(1) 292

fanout

Lexis::NFAState:: ¶76(1) 22

field

Gramatika::Symbol:: ¶295(4) 99

File

Loma::File:: ¶930(1) 301; ¶932(1) 301; ¶934(1) 302

~File

Loma::File:: ¶932(2) 301; ¶935(1) 302

file

Loma::ItemAttributes:: ¶783(9) 265

fileList

Loma::File:: ¶931(2) 301

FileModule

Loma::FileModule:: ¶888(1) 288; ¶890(1) 288; ¶890(2) 288; ¶891(1) 288;

¶892(1) 288

~FileModule

Loma::FileModule:: ¶890(3) 288; ¶893(1) 289

fill

Toolbox::Set:: ¶668(2) 223

find

Loma::File:: ¶932(3) 301; ¶936(1) 302

Toolbox::HashTable<K,V>:: ¶733(7) 246; ¶743(2) 249

firstSet

Gramatika::Symbol:: ¶295(8) 99

Fragment

Loma::Fragment:: ¶830(1) 273; ¶833(1) 273; ¶835(1) 274

~Fragment

Loma::Fragment:: ¶834(1) 273; ¶837(1) 274

fragment

Loma::ItemAttributes:: ¶783(5) 265

fragmentList

Loma::ItemAttributes:: ¶783(6) 265

Loma::Module:: ¶866(3) 283

getAction

Gramatika::ActionRecord:: ¶461(3) 149; ¶463(1) 150

Gramatika::Parser<T,A,GT>:: ¶587(3) 188; ¶590(2) 189

Gramatika::State:: ¶488(1) 157; ¶506(1) 160

Lexis::State:: ¶57(10) 17; ¶62(1) 18

getActionList

Page 369: Book

List of Symbols 355

Gramatika::State:: ¶489(15) 158; ¶496(1) 159

getAnchor

Lexis::State:: ¶57(9) 17; ¶61(1) 18

getBase

Toolbox::PathName:: ¶638(7) 216; ¶645(1) 217

getClosure

Gramatika::ItemSet:: ¶405(13) 134; ¶421(1) 139

getDominantAction

Lexis::NFA:: ¶94(16) 28; ¶113(1) 33

getDominantAnchor

Lexis::NFA:: ¶94(17) 28; ¶114(1) 33

getDominantToken

Lexis::NFA:: ¶94(18) 28; ¶115(1) 34

getDotPosition

Gramatika::Item:: ¶375(9) 126; ¶380(1) 127

getDotSymbol

Gramatika::Item:: ¶375(19) 126; ¶395(1) 129

getDotSymbolNumber

Gramatika::Item:: ¶375(20) 126; ¶396(1) 130

getEntry

Gramatika::Parser<T,A,GT>:: ¶587(1) 188; ¶589(2) 189

getEpsilonClosure

Lexis::NFA:: ¶94(14) 28; ¶111(1) 32

getEpsilonItems

Gramatika::State:: ¶489(4) 158; ¶492(1) 158

getExtension

Toolbox::PathName:: ¶638(8) 216; ¶646(1) 217

getFanout

Lexis::NFAState:: ¶77(7) 22

getField

Gramatika::Symbol:: ¶296(10) 100; ¶302(1) 101

getFirstClosure

Gramatika::Production:: ¶322(11) 106; ¶335(1) 108

Gramatika::Symbol:: ¶296(18) 100; ¶314(1) 103

getFirstSet

Gramatika::Item:: ¶375(21) 126; ¶397(1) 130

Gramatika::Symbol:: ¶296(12) 100; ¶304(1) 101

getGoto

Gramatika::Parser<T,A,GT>:: ¶587(4) 188; ¶591(2) 189

getGotoList

Gramatika::State:: ¶489(16) 158; ¶497(1) 159

getHashValue

Gramatika::ItemSet:: ¶405(14) 134; ¶423(1) 139

getHead

Toolbox::PathName:: ¶638(5) 216; ¶643(1) 217

getItem

Gramatika::ItemSet:: ¶405(12) 134; ¶422(1) 139

getKernelItems

Gramatika::State:: ¶489(3) 158; ¶491(1) 158

Page 370: Book

356 List of Symbols

getLabelType

Lexis::NFAState:: ¶77(8) 22

getLanguage

Gramatika::SymbolTable:: ¶345(20) 114; ¶354(1) 115

Lexis::DFA:: ¶138(8) 42; ¶140(1) 42

Lexis::NFA:: ¶97(1) 28

getLeftHandSide

Gramatika::Item:: ¶375(7) 126; ¶378(1) 126

Gramatika::Production:: ¶322(8) 106; ¶327(1) 106

getLength

Gramatika::Item:: ¶375(10) 126; ¶381(1) 127

Gramatika::Production:: ¶322(10) 106; ¶328(1) 107

getLexeme

Lexis::Input<T>:: ¶188(12) 66

Lexis::Token<LT>:: ¶206(6) 71; ¶208(2) 72

getLineNumber

Lexis::Input<T>:: ¶188(11) 66

Lexis::Token<LT>:: ¶206(7) 71; ¶209(2) 72

getLookAheadSet

Gramatika::Item:: ¶375(11) 126; ¶382(1) 127

getMoveSet

Lexis::NFA:: ¶94(15) 28; ¶112(1) 33

getName

Gramatika::Symbol:: ¶296(9) 100; ¶301(1) 101

Lexis::Token<LT>:: ¶206(8) 71

Loma::Group:: ¶918(2) 295; ¶920(1) 296

Loma::Module:: ¶868(3) 284; ¶871(1) 284

getNextState

Lexis::DFAState:: ¶124(4) 37; ¶130(1) 38

Lexis::NFAState:: ¶77(10) 22

getNFAStateSet

Lexis::DFAState:: ¶124(5) 37; ¶125(1) 38

getNumber

Gramatika::ActionRecord:: ¶461(4) 149; ¶464(1) 150

Gramatika::Production:: ¶322(7) 106; ¶326(1) 106

Gramatika::State:: ¶489(2) 158

Gramatika::StateNumber State:: ¶490(1) 158

Gramatika::Symbol:: ¶296(7) 100; ¶299(1) 100

Lexis::State:: ¶57(8) 17; ¶60(1) 18

Loma::Group:: ¶917(2) 295; ¶921(1) 296

Loma::Module:: ¶868(2) 284; ¶870(1) 284

getNumberOfProductions

Gramatika::SymbolTable:: ¶345(17) 114; ¶351(1) 115

getNumberOfStates

Gramatika::Automaton:: ¶515(5) 165; ¶517(1) 166

Lexis::DFA:: ¶138(4) 42; ¶139(1) 42

Lexis::NFA:: ¶94(8) 28; ¶98(1) 28

getNumberOfSymbols

Gramatika::SymbolTable:: ¶345(16) 114; ¶350(1) 115

Page 371: Book

List of Symbols 357

getPosition

Gramatika::Production:: ¶321(1) 105; ¶332(1) 107

getPostamble

Gramatika::SymbolTable:: ¶345(19) 114; ¶353(1) 115

getPreamble

Gramatika::SymbolTable:: ¶345(18) 114; ¶352(1) 115

getProduction

Gramatika::Item:: ¶375(5) 126; ¶376(1) 126

getProductionList

Gramatika::Symbol:: ¶296(11) 100; ¶303(1) 101

getProductionNumber

Gramatika::Item:: ¶375(6) 126; ¶377(1) 126

getRightHandSide

Gramatika::Item:: ¶375(8) 126; ¶379(1) 126

Gramatika::Production:: ¶322(9) 106; ¶333(1) 108

getRoot

Toolbox::PathName:: ¶638(9) 216; ¶647(1) 218

getScope

Loma::CodeModule:: ¶899(7) 290; ¶906(1) 291

Loma::FileModule:: ¶890(5) 288; ¶895(1) 289

Loma::Module:: ¶868(8) 284; ¶881(1) 286

getSize

Toolbox::Set:: ¶670(21) 224; ¶693(1) 230

Toolbox::Stack<T>:: ¶653(8) 219; ¶662(2) 221

getStartSymbol

Gramatika::SymbolTable:: ¶345(14) 114; ¶349(1) 114

getStateNumber

Gramatika::GotoRecord:: ¶475(3) 153; ¶477(1) 154

Lexis::DFA:: ¶137(1) 41

getSymbol

Gramatika::Partition::const_iterator:: ¶449(4) 146; ¶454(1) 147

Gramatika::SymbolTable:: ¶345(13) 114; ¶366(1) 119

getSymbolNumber

Gramatika::ActionRecord:: ¶461(2) 149; ¶462(1) 150

Gramatika::GotoRecord:: ¶475(2) 153; ¶476(1) 154

getSymbolTable

Gramatika::Automaton:: ¶515(4) 165; ¶516(1) 166

getTail

Toolbox::PathName:: ¶638(6) 216; ¶644(1) 217

getTerminalSymbols

Gramatika::SymbolTable:: ¶345(11) 114; ¶365(1) 119

getToken

Lexis::State:: ¶57(11) 17; ¶63(1) 18

getTop

Toolbox::Stack<T>:: ¶653(7) 219; ¶661(2) 221

getTransition

Lexis::TokenStream<T>:: ¶221(1) 74

getType

Gramatika::Symbol:: ¶296(8) 100; ¶300(1) 100

Page 372: Book

358 List of Symbols

Lexis::Token<LT>:: ¶206(5) 71; ¶207(2) 72

Lexis::TokenStream<T>:: ¶221(6) 74

getUnfinishedState

Gramatika::Automaton:: ¶514(1) 165; ¶523(1) 167

getWidth

Toolbox::String:: ¶618(2) 205; ¶622(1) 206

gotoMap

Gramatika::ParseTable:: ¶535(9) 171

GotoRecord

Gramatika::GotoRecord:: ¶473(1) 153; ¶475(1) 153; ¶481(1) 154

gotos

Gramatika::State:: ¶487(5) 157

gotoStates

Gramatika::ParseTable:: ¶535(12) 171

gotoTable

Gramatika::ParseTable:: ¶535(10) 171

gotoTableSize

Gramatika::ParseTable:: ¶535(11) 171

Group

Loma::Group:: ¶915(1) 295; ¶918(1) 295; ¶919(1) 296

groupTable

Loma::File:: ¶931(3) 301

hash

Gramatika:: ¶409(1) 134

Toolbox::String:: ¶618(1) 205; ¶621(1) 206

HashTable

Toolbox::HashTable<K,V>:: ¶733(1) 246; ¶736(2) 246

~HashTable

Toolbox::HashTable<K,V>:: ¶733(2) 246; ¶737(2) 246

hashTable

Gramatika::Automaton:: ¶513(3) 165

Gramatika::SymbolTable:: ¶343(3) 113

Toolbox::HashTable<K,V>::const_iterator:: ¶745(1) 249

head

Lexis:: ¶179(4) 64

hexToBinary

Toolbox::Character:: ¶609(3) 201; ¶612(1) 202

highBits

Toolbox::String:: ¶620(4) 205

IncludeFragment

Loma::IncludeFragment:: ¶854(1) 278; ¶856(1) 278; ¶857(1) 278

~IncludeFragment

Loma::IncludeFragment:: ¶856(2) 278; ¶858(1) 278

includeGroup

Loma::IncludeFragment:: ¶855(3) 278

indent

Loma::CodeFragment:: ¶848(2) 276

Loma::IncludeFragment:: ¶855(1) 278

initializeGroups

Page 373: Book

List of Symbols 359

Lexis::DFA:: ¶137(2) 41

Input

Lexis::Input<T>:: ¶183(2) 65; ¶188(1) 66

inputStream

Lexis::Input<T>:: ¶186(1) 65

Lexis::TokenStream<T>:: ¶220(1) 74

invalid

Gramatika::ParseTable:: ¶534(3) 171

isAcceptOrDiscardState

Lexis::TokenStream<T>:: ¶221(3) 74

isAcceptState

Lexis::TokenStream<T>:: ¶221(2) 74

isClosed

Gramatika::State:: ¶489(5) 158; ¶493(1) 158

isColumnEquivalent

Lexis::DFA:: ¶138(6) 42

isDefined

Gramatika::Symbol:: ¶296(14) 100; ¶306(1) 101

isEmpty

Toolbox::Stack<T>:: ¶653(5) 219; ¶658(2) 220

isHeadAnchored

Lexis::TokenStream<T>:: ¶221(5) 74

isMoreToRightOfDot

Gramatika::Item:: ¶375(15) 126; ¶385(1) 127

isNonterminal

Gramatika::Symbol:: ¶296(16) 100; ¶308(1) 101

isNullable

Gramatika::Item:: ¶375(22) 126; ¶398(1) 130

Gramatika::Symbol:: ¶296(17) 100; ¶313(1) 102

isOctalDigit

Toolbox::Character:: ¶609(1) 201; ¶613(1) 202

isRowEquivalent

Lexis::DFA:: ¶138(7) 42

isTailAnchored

Lexis::TokenStream<T>:: ¶221(4) 74

isTerminal

Gramatika::Symbol:: ¶296(15) 100; ¶307(1) 101

isTransitionLabelled

Lexis::NFAState:: ¶77(9) 22

isUsed

Gramatika::Symbol:: ¶296(13) 100; ¶305(1) 101

Item

Gramatika::Item:: ¶373(1) 125; ¶375(1) 126; ¶375(2) 126; ¶375(3) 126;

¶387(1) 127; ¶388(1) 128; ¶389(1) 128

item

Gramatika::ItemSet:: ¶403(1) 133

ItemAttributes

Gramatika::ItemAttributes:: ¶271(1) 93; ¶272(1) 93

Lexis::ItemAttributes:: ¶23(1) 9; ¶25(1) 10; ¶26(1) 10

Page 374: Book

360 List of Symbols

Loma::ItemAttributes:: ¶782(1) 264; ¶784(1) 265; ¶785(1) 265

ItemSet

Gramatika::ItemSet:: ¶402(1) 133; ¶405(1) 134; ¶405(3) 134; ¶410(1) 134;

¶412(1) 135

~ItemSet

Gramatika::ItemSet:: ¶405(2) 134; ¶411(1) 135

itemSet

Gramatika::ItemSet::const_iterator:: ¶427(1) 140

ItemSet& operator=

Gramatika::ItemSet:: ¶405(4) 134

kernelItems

Gramatika::State:: ¶487(2) 157

Key

Toolbox::HashTable<K,V>:: ¶730(1) 245

label

Loma::CodeFragment:: ¶848(4) 276

labelChar

Lexis::NFAState:: ¶76(4) 22

labelSet

Lexis::NFAState:: ¶76(5) 22

LabelType

Lexis::NFAState::LabelType:: ¶74(1) 21

labelType

Lexis::NFAState:: ¶76(3) 22

language

Gramatika::ItemAttributes:: ¶270(7) 93

Gramatika::SymbolTable:: ¶343(9) 113

Lexis::CompressedDFA:: ¶155(12) 51

Lexis::DFA:: ¶136(3) 41

Lexis::ItemAttributes:: ¶24(2) 9

Lexis::NFA:: ¶93(1) 27

leftHandSide

Gramatika::Production:: ¶320(2) 105

leftHandSideTable

Gramatika::ParseTable:: ¶535(13) 171

lexeme

Lexis::Token<LT>:: ¶205(2) 71

line

Loma::CodeFragment:: ¶848(1) 276

lineNumber

Lexis::Token<LT>:: ¶205(3) 71

List

Toolbox::HashTable<K,V>:: ¶730(4) 245

Toolbox::List<T>:: ¶717(1) 241

lookAheadSet

Gramatika::Item:: ¶374(3) 125

makeActionMap

Gramatika::ParseTable:: ¶536(2) 172; ¶541(1) 174

makeActionTable

Page 375: Book

List of Symbols 361

Gramatika::ParseTable:: ¶536(3) 172; ¶542(1) 174

makeGotoMap

Gramatika::ParseTable:: ¶536(4) 172; ¶543(1) 175

makeGotoTable

Gramatika::ParseTable:: ¶536(5) 172; ¶544(1) 175

makeProductionTables

Gramatika::ParseTable:: ¶536(1) 172; ¶545(1) 176

MapEntry

Gramatika::MapEntry:: ¶561(1) 182

markEnd

Lexis::Input<T>:: ¶188(7) 66

markStart

Lexis::Input<T>:: ¶188(6) 66

maximumCharacter

Lexis:: ¶178(1) 63

maximumFanout

Lexis::NFAState:: ¶75(3) 21

maximumLexemeLength

Lexis::Input<T>:: ¶185(3) 65

maximumNumberOfStates

Lexis::DFA:: ¶135(3) 41

mergeLookAhead

Gramatika::Item:: ¶375(18) 126; ¶394(1) 129

Gramatika::ItemSet:: ¶405(11) 134; ¶419(1) 137

Gramatika::State:: ¶489(14) 158; ¶505(1) 160

minimize

Lexis::DFA:: ¶138(3) 42

modMask

Toolbox::Set:: ¶667(4) 223

Module

Loma::Module:: ¶864(1) 283; ¶867(1) 283; ¶873(1) 285

~Module

Loma::Module:: ¶868(1) 284; ¶874(1) 285

module

Loma::Fragment:: ¶832(1) 273

Loma::ItemAttributes:: ¶783(7) 265

ModuleList

Loma:: ¶865(1) 283

moduleList

Loma::File:: ¶931(1) 301

Loma::Group:: ¶916(2) 295

Loma::ItemAttributes:: ¶783(8) 265

name

Gramatika::Symbol:: ¶295(1) 99

Loma::Group:: ¶916(1) 295

Loma::IncludeFragment:: ¶855(2) 278

Loma::Module:: ¶866(2) 283

newState

Gramatika::Automaton:: ¶514(2) 165; ¶522(1) 167

Page 376: Book

362 List of Symbols

next

Lexis::NFAState:: ¶76(2) 22

nextInput

Lexis::Input<T>:: ¶186(3) 65

NFA

Lexis::NFA:: ¶92(1) 27; ¶94(1) 28; ¶94(2) 28; ¶94(3) 28; ¶94(4) 28; ¶100(1)29; ¶101(1) 29; ¶102(1) 29; ¶103(1) 30

~NFA

Lexis::NFA:: ¶94(5) 28; ¶104(1) 30

nfa

Lexis::ItemAttributes:: ¶24(1) 9

NFAState

Lexis::NFAState:: ¶73(1) 21; ¶77(1) 22

nfaStateSet

Lexis::DFAState:: ¶123(1) 37

noBlanks

Loma:: ¶944(5) 307; ¶949(1) 309

none

Lexis:: ¶179(3) 64

nonterminal

Gramatika::SymbolType:: ¶564(4) 183

number

Gramatika::Production:: ¶320(1) 105

Gramatika::State:: ¶487(1) 157

Gramatika::Symbol:: ¶295(2) 99

Gramatika::TableEntry:: ¶559(3) 182

Lexis::State:: ¶56(1) 17

Loma::Module:: ¶866(1) 283

numberOfColumns

Lexis::CompressedDFA:: ¶155(3) 51

numberOfElements

Gramatika::Partition:: ¶439(1) 143

numberOfModules

Loma::Module:: ¶866(4) 283; ¶869(1) 284

numberOfNonterminals

Gramatika::SymbolTable:: ¶343(2) 113

numberOfProductions

Gramatika::ParseTable:: ¶535(4) 171

numberOfRows

Lexis::CompressedDFA:: ¶155(2) 51

numberOfStates

Gramatika::ParseTable:: ¶535(3) 171

Lexis::CompressedDFA:: ¶155(1) 51

Lexis::DFA:: ¶136(1) 41

Lexis::NFA:: ¶93(2) 27

numberOfTerminals

Gramatika::SymbolTable:: ¶343(1) 113

numberOfTokens

Lexis::CompressedDFA:: ¶155(4) 51

Page 377: Book

List of Symbols 363

octalToBinary

Toolbox::Character:: ¶609(2) 201; ¶611(1) 201

oneEighth

Toolbox::String:: ¶620(2) 205

operator*

Gramatika::ItemSet::const_iterator:: ¶429(3) 140; ¶433(1) 141

Gramatika::Partition::const_iterator:: ¶449(3) 146; ¶453(1) 147

Lexis:: ¶116(1) 34

Lexis::Input<T>:: ¶188(3) 66

Toolbox::HashTable<K,V>::const_iterator:: ¶746(6) 249; ¶750(2) 250

Toolbox::Set::const_iterator:: ¶698(4) 233; ¶702(1) 233

operator++

Gramatika::ItemSet::const_iterator:: ¶429(4) 140; ¶434(1) 141

Gramatika::Partition::const_iterator:: ¶449(5) 146; ¶455(1) 147

Lexis::Input<T>:: ¶188(4) 66

Toolbox::HashTable<K,V>::const_iterator:: ¶746(7) 249; ¶753(3) 252

Toolbox::Set::const_iterator:: ¶698(5) 233; ¶705(1) 234

operator+=

Toolbox::Set:: ¶670(12) 224; ¶684(1) 227

operator--

Lexis::Input<T>:: ¶188(5) 66

operator-=

Toolbox::Set:: ¶670(13) 224; ¶685(1) 227

operator<<

Gramatika:: ¶309(1) 101; ¶329(1) 107; ¶355(1) 115; ¶386(1) 127; ¶408(1)134; ¶441(1) 144; ¶467(1) 150; ¶480(1) 154; ¶498(1) 159; ¶518(1) 166;

¶538(1) 172; ¶563(1) 182; ¶574(2) 185; ¶582(2) 187

Lexis:: ¶64(1) 18; ¶99(1) 29; ¶126(1) 38; ¶141(1) 42; ¶180(1) 64; ¶210(2) 72

Loma:: ¶836(1) 274; ¶872(1) 284; ¶923(1) 296; ¶933(1) 302

Toolbox:: ¶663(2) 221; ¶671(1) 224; ¶713(1) 238; ¶734(2) 246; ¶735(2) 246

operator<=

Gramatika::ActionRecord:: ¶461(6) 149; ¶466(1) 150

Gramatika::GotoRecord:: ¶475(5) 153; ¶479(1) 154

Gramatika::Item:: ¶375(14) 126; ¶384(1) 127

Toolbox::Set:: ¶670(17) 224; ¶689(1) 229

operator<

Toolbox::Set::const_iterator:: ¶698(7) 233; ¶704(1) 234

operator==

Gramatika::ActionRecord:: ¶461(5) 149; ¶465(1) 150

Gramatika::GotoRecord:: ¶475(4) 153; ¶478(1) 154

Gramatika::Item:: ¶375(13) 126; ¶383(1) 127

Gramatika::ItemSet:: ¶405(6) 134; ¶407(1) 134

Gramatika::State:: ¶489(7) 158; ¶494(1) 158

Toolbox::HashTable<K,V>::const_iterator:: ¶746(8) 249; ¶752(2) 251

Toolbox::Set:: ¶670(15) 224; ¶670(18) 224; ¶687(1) 228; ¶690(1) 229

operator=

Gramatika::Item:: ¶375(4) 126; ¶390(1) 128

Gramatika::ItemSet:: ¶413(1) 135

Lexis::NFAState:: ¶77(2) 22

Page 378: Book

364 List of Symbols

Lexis::State:: ¶57(3) 17; ¶65(1) 19

Toolbox::Set:: ¶670(4) 224; ¶670(5) 224; ¶676(1) 225; ¶677(1) 225

operator>>

Lexis::TokenStream<T>:: ¶222(2) 74

operator[]

Gramatika::Automaton:: ¶515(3) 165; ¶521(1) 166

Gramatika::SymbolTable:: ¶358(1) 116

Lexis::DFA:: ¶138(5) 42

Lexis::NFA:: ¶94(6) 28; ¶94(7) 28; ¶105(1) 30; ¶106(1) 30

Toolbox::Stack<T>:: ¶653(6) 219; ¶659(2) 221

operator!=

Gramatika::ItemSet::const_iterator:: ¶429(5) 140; ¶435(1) 141

Gramatika::Partition::const_iterator:: ¶449(6) 146; ¶456(1) 147

Toolbox::HashTable<K,V>::const_iterator:: ¶746(9) 249; ¶751(2) 250

Toolbox::Set:: ¶670(16) 224; ¶670(19) 224; ¶688(1) 228; ¶691(1) 229

Toolbox::Set::const_iterator:: ¶698(6) 233; ¶703(1) 233

operator!

Lexis:: ¶118(1) 35

operator&=

Toolbox::Set:: ¶670(9) 224; ¶681(1) 226

operator&

Toolbox::Set:: ¶670(8) 224; ¶680(1) 226

operator^=

Toolbox::Set:: ¶670(11) 224; ¶683(1) 227

operator^

Toolbox::Set:: ¶670(10) 224; ¶682(1) 227

operator~

Lexis:: ¶119(1) 35

Toolbox::Set:: ¶670(14) 224; ¶686(1) 228

operator|=

Toolbox::Set:: ¶670(7) 224; ¶679(1) 226

operator|

Lexis:: ¶117(1) 34

Toolbox::Set:: ¶670(6) 224; ¶678(1) 226

operator bool

Lexis::Input<T>:: ¶188(2) 66

Pair

Toolbox::HashTable<K,V>:: ¶730(3) 245

parse

Gramatika::Parser<T,A,GT>:: ¶588(3) 188; ¶597(2) 192

Parser

Gramatika::Parser<T,A,GT>:: ¶584(2) 188; ¶588(1) 188; ¶592(2) 189

~Parser

Gramatika::Parser<T,A,GT>:: ¶588(2) 188; ¶593(2) 190

ParserData

Gramatika::ParserData:: ¶266(1) 92; ¶267(1) 92

Lexis::ParserData:: ¶18(1) 8; ¶20(1) 9; ¶21(1) 9

Loma::ParserData:: ¶778(1) 264

ParseTable

Page 379: Book

List of Symbols 365

Gramatika::ParseTable:: ¶533(1) 171; ¶537(1) 172; ¶539(1) 173

~ParseTable

Gramatika::ParseTable:: ¶537(2) 172; ¶540(1) 173

Partition

Gramatika::Partition:: ¶438(1) 143; ¶440(1) 143; ¶442(1) 144

~Partition

Gramatika::Partition:: ¶440(2) 143; ¶443(1) 144

partition

Gramatika::Partition::const_iterator:: ¶447(1) 145

PathName

Toolbox::PathName:: ¶636(1) 215; ¶638(1) 216; ¶638(2) 216; ¶638(3) 216;

¶638(4) 216; ¶639(1) 216; ¶640(1) 216; ¶641(1) 216; ¶642(1) 216

performAction

Gramatika::Parser<T,A,GT>:: ¶587(8) 188

pop

Toolbox::Stack<T>:: ¶653(2) 219; ¶656(2) 220

position

Toolbox::HashTable<K,V>::const_iterator:: ¶745(2) 249

postamble

Gramatika::SymbolTable:: ¶343(7) 113

preamble

Gramatika::SymbolTable:: ¶343(6) 113

Production

Gramatika::Production:: ¶319(1) 105; ¶322(1) 106; ¶330(1) 107

~Production

Gramatika::Production:: ¶322(2) 106; ¶331(1) 107

production

Gramatika::Item:: ¶374(1) 125

Gramatika::ItemAttributes:: ¶270(3) 93

ProductionIndex

Gramatika:: ¶556(1) 181

productionList

Gramatika::ItemAttributes:: ¶270(4) 93

Gramatika::Symbol:: ¶295(7) 99

ProductionNumber

Gramatika:: ¶555(1) 181

productionTable

Gramatika::SymbolTable:: ¶343(5) 113

ptr

Toolbox::HashTable<K,V>::const_iterator:: ¶745(3) 249

push

Toolbox::Stack<T>:: ¶653(3) 219; ¶655(2) 220

put

Gramatika::ActionRecord:: ¶461(7) 149; ¶470(1) 151

Gramatika::Automaton:: ¶515(6) 165; ¶530(1) 170

Gramatika::GotoRecord:: ¶475(6) 153; ¶483(1) 155

Gramatika::Item:: ¶375(23) 126; ¶399(1) 131

Gramatika::ItemSet:: ¶405(15) 134; ¶424(1) 140

Gramatika::ParseTable:: ¶537(3) 172; ¶550(1) 178

Page 380: Book

366 List of Symbols

Gramatika::Partition:: ¶440(3) 143; ¶444(1) 145

Gramatika::Production:: ¶322(12) 106; ¶336(1) 108

Gramatika::StackItem<T,A>:: ¶579(3) 186; ¶583(2) 187

Gramatika::State:: ¶489(17) 158; ¶509(1) 162

Gramatika::Symbol:: ¶296(19) 100; ¶315(1) 103

Gramatika::SymbolTable:: ¶345(21) 114; ¶368(1) 120

Gramatika::Value<T,A>:: ¶571(3) 184; ¶575(2) 185

Lexis::DFA:: ¶138(9) 42

Lexis::DFAState:: ¶124(6) 37; ¶131(1) 39

Lexis::NFA:: ¶94(13) 28; ¶110(1) 31

Lexis::NFAState:: ¶77(11) 22

Lexis::State:: ¶57(12) 17; ¶70(1) 19

Lexis::Token<LT>:: ¶206(9) 71

Loma::CodeFragment:: ¶849(5) 276; ¶853(1) 277

Loma::CodeModule:: ¶899(8) 290; ¶907(1) 291

Loma::ExampleModule:: ¶909(3) 292; ¶912(1) 292

Loma::File:: ¶932(8) 301; ¶941(1) 304

Loma::FileModule:: ¶890(6) 288; ¶896(1) 289

Loma::Fragment:: ¶834(6) 273

Loma::Group:: ¶918(5) 295; ¶925(1) 297

Loma::IncludeFragment:: ¶856(5) 278; ¶861(1) 279

Loma::Module:: ¶868(9) 284; ¶879(1) 286

Loma::TextFragment:: ¶842(4) 275; ¶846(1) 275

Loma::TextModule:: ¶883(3) 287; ¶886(1) 287

Toolbox::CharacterSet:: ¶709(4) 237; ¶714(1) 238

Toolbox::HashTable<K,V>:: ¶733(6) 246; ¶741(2) 248

Toolbox::List<T>:: ¶721(2) 241; ¶726(2) 243

Toolbox::Set:: ¶670(22) 224; ¶694(1) 231

Toolbox::Stack<T>:: ¶653(9) 219; ¶660(2) 221

putAction

Gramatika::Production:: ¶322(13) 106

putActions

Gramatika::Symbol:: ¶296(20) 100; ¶316(1) 103

Gramatika::SymbolTable:: ¶345(22) 114; ¶369(1) 121

putActionTable

Lexis::CompressedDFA:: ¶156(5) 51; ¶166(1) 55

putAnchorTable

Lexis::CompressedDFA:: ¶156(4) 51; ¶167(1) 56

putCode

Loma::CodeFragment:: ¶849(4) 276; ¶852(1) 277

Loma::File:: ¶932(7) 301; ¶940(1) 304

Loma::Fragment:: ¶834(4) 273

Loma::Group:: ¶918(6) 295; ¶926(1) 297

Loma::IncludeFragment:: ¶856(4) 278; ¶860(1) 279

Loma::Module:: ¶868(10) 284; ¶880(1) 286

Loma::TextFragment:: ¶842(3) 275; ¶845(1) 275

Loma::TextModule:: ¶883(4) 287; ¶887(1) 287

putLeftHandSideTable

Gramatika::ParseTable:: ¶536(10) 172; ¶548(1) 177

Page 381: Book

List of Symbols 367

putMap

Gramatika::ParseTable:: ¶536(6) 172; ¶546(1) 176

Lexis::CompressedDFA:: ¶156(1) 51; ¶165(1) 55

putNumbers

Loma::Group:: ¶917(1) 295; ¶927(1) 298

putPreamble

Gramatika::SymbolTable:: ¶345(23) 114; ¶370(1) 122

putRightHandSideTable

Gramatika::ParseTable:: ¶536(11) 172; ¶549(1) 178

putTable

Gramatika::ParseTable:: ¶536(8) 172; ¶547(1) 177

putTables

Lexis::CompressedDFA:: ¶157(4) 52; ¶172(1) 60

putTokenList

Lexis::CompressedDFA:: ¶156(6) 51; ¶168(1) 56

putTokens

Lexis::CompressedDFA:: ¶157(3) 52; ¶171(1) 59

putTokenTable

Lexis::CompressedDFA:: ¶156(7) 51; ¶169(1) 57

putTransitionTable

Lexis::CompressedDFA:: ¶156(3) 51; ¶170(1) 58

rebuild

Lexis::DFA:: ¶137(4) 41

recover

Gramatika::Parser<T,A,GT>:: ¶587(7) 188; ¶596(2) 191

reduce

Gramatika::Parser<T,A,GT>:: ¶587(6) 188; ¶595(2) 190

Gramatika::State:: ¶488(2) 157; ¶507(1) 161

reduceAction

Gramatika:: ¶566(4) 183

reduceReduceConflicts

Gramatika::State:: ¶487(7) 157

referent

Gramatika::ItemSet::const_iterator:: ¶427(2) 140

Gramatika::Partition::const_iterator:: ¶447(2) 145

Toolbox::Set::const_iterator:: ¶697(2) 232

registerIt

Loma::CodeModule:: ¶899(4) 290; ¶904(1) 290

Loma::File:: ¶932(4) 301; ¶932(5) 301; ¶938(1) 303; ¶939(1) 303

Loma::FileModule:: ¶890(4) 288; ¶894(1) 289

Loma::Module:: ¶868(4) 284; ¶875(1) 285

remove

Toolbox::HashTable<K,V>:: ¶733(4) 246; ¶733(5) 246; ¶739(2) 247; ¶740(2)247

reset

Toolbox::Stack<T>:: ¶653(4) 219; ¶657(2) 220

resultValue

Gramatika::Parser<T,A,GT>:: ¶586(2) 188

returnToEndMark

Page 382: Book

368 List of Symbols

Lexis::Input<T>:: ¶188(9) 66

returnToStartMark

Lexis::Input<T>:: ¶188(8) 66

rightHandSide

Gramatika::Production:: ¶320(3) 105

rightHandSideTable

Gramatika::ParseTable:: ¶535(14) 171

rowMap

Lexis::CompressedDFA:: ¶155(5) 51

scope

Loma::CodeModule:: ¶898(2) 289

separator

Toolbox::PathName:: ¶637(10) 215; ¶637(6) 215

Set

Toolbox::Set:: ¶666(1) 223; ¶670(1) 224; ¶670(3) 224; ¶672(1) 224; ¶674(1)225

~Set

Toolbox::Set:: ¶670(2) 224; ¶673(1) 224

set

Toolbox::Set::const_iterator:: ¶697(1) 232

setAction

Gramatika::Production:: ¶322(5) 106; ¶325(1) 106

Lexis::NFA:: ¶94(10) 28; ¶108(1) 31

Lexis::State:: ¶57(6) 17; ¶68(1) 19

setAnchor

Lexis::NFA:: ¶94(9) 28; ¶107(1) 30

Lexis::State:: ¶57(5) 17; ¶67(1) 19

setClosed

Gramatika::State:: ¶489(8) 158; ¶495(1) 159

setDefined

Gramatika::Symbol:: ¶296(3) 100; ¶297(1) 100

setFirstSet

Gramatika::Symbol:: ¶296(5) 100; ¶311(1) 102

setLanguage

Gramatika::SymbolTable:: ¶345(5) 114; ¶348(1) 114

Lexis::NFA:: ¶94(12) 28; ¶96(1) 28

setLeftHandSide

Gramatika::Production:: ¶322(4) 106; ¶324(1) 106

setModule

Loma::Fragment:: ¶834(2) 273; ¶838(1) 274

setNumber

Gramatika::Production:: ¶322(3) 106; ¶323(1) 106

Lexis::State:: ¶57(4) 17; ¶66(1) 19

setPostamble

Gramatika::SymbolTable:: ¶345(4) 114; ¶347(1) 114

setPreamble

Gramatika::SymbolTable:: ¶345(3) 114; ¶346(1) 114

setToken

Lexis::NFA:: ¶94(11) 28; ¶109(1) 31

Page 383: Book

List of Symbols 369

Lexis::State:: ¶57(7) 17; ¶69(1) 19

setUsed

Gramatika::Symbol:: ¶296(4) 100; ¶298(1) 100

Loma::CodeModule:: ¶899(6) 290; ¶902(1) 290

Loma::Group:: ¶918(4) 295; ¶924(1) 296

Loma::Module:: ¶868(7) 284; ¶877(1) 285

shift

Gramatika::Parser<T,A,GT>:: ¶587(5) 188; ¶594(2) 190

shiftAction

Gramatika:: ¶566(3) 183

shiftReduceConflicts

Gramatika::State:: ¶487(8) 157

sizeOfElement

Gramatika::Partition:: ¶439(3) 143

sortedInsert

Toolbox::List<T>:: ¶720(2) 241; ¶725(2) 243

Stack

Toolbox::Stack<T>:: ¶650(2) 219; ¶653(1) 219; ¶654(2) 220

stack

Gramatika::Parser<T,A,GT>:: ¶586(1) 188

StackItem

Gramatika::StackItem<T,A>:: ¶576(2) 186; ¶579(1) 186; ¶579(2) 186; ¶580(2)187; ¶581(2) 187

start

Gramatika::MapEntry:: ¶562(1) 182

startMarkLineNumber

Lexis::Input<T>:: ¶186(10) 65

startOfElement

Gramatika::Partition:: ¶439(2) 143

startOfLexeme

Lexis::Input<T>:: ¶186(6) 65

startSymbol

Gramatika::SymbolTable:: ¶343(8) 113

State

Gramatika::State:: ¶486(1) 157; ¶489(1) 158; ¶499(1) 159

Lexis::State:: ¶55(1) 17; ¶57(1) 17; ¶57(2) 17; ¶58(1) 18; ¶59(1) 18

state

Gramatika::Automaton:: ¶513(2) 165

Gramatika::StackItem<T,A>:: ¶578(1) 186

Lexis::DFA:: ¶136(2) 41

Lexis::NFA:: ¶93(3) 27

StateNumber

Gramatika:: ¶554(1) 181

Lexis:: ¶175(1) 63

String

Toolbox::String:: ¶617(1) 205; ¶619(1) 205

Symbol

Gramatika::Symbol:: ¶294(1) 99; ¶296(1) 100; ¶310(1) 102

symbol

Page 384: Book

370 List of Symbols

Gramatika::ItemAttributes:: ¶270(5) 93

Gramatika::TableEntry:: ¶559(1) 182

Loma::CodeFragment:: ¶848(5) 276

SymbolNumber

Gramatika:: ¶553(1) 181

SymbolTable

Gramatika::SymbolTable:: ¶342(1) 113; ¶345(1) 114; ¶356(1) 115

~SymbolTable

Gramatika::SymbolTable:: ¶345(2) 114; ¶357(1) 116

symbolTable

Gramatika::Automaton:: ¶513(1) 165

Gramatika::ItemAttributes:: ¶270(6) 93

Gramatika::ParserData:: ¶265(1) 92

Gramatika::ParseTable:: ¶535(2) 171

Gramatika::SymbolTable:: ¶343(4) 113

SymbolType

Gramatika::SymbolType:: ¶564(1) 183

Table

Toolbox::HashTable<K,V>:: ¶730(5) 245

table

Toolbox::HashTable<K,V>:: ¶732(1) 246

TableEntry

Gramatika::TableEntry:: ¶558(1) 182

Lexis:: ¶176(1) 63

Tables

Gramatika::Parser<T,A,GT>:: ¶585(3) 188

Lexis::TokenStream<T>:: ¶219(2) 74

tag

Loma::WordList:: ¶781(2) 264

tail

Lexis:: ¶179(5) 64

terminal

Gramatika::SymbolType:: ¶564(3) 183

texCode

Loma:: ¶944(3) 307; ¶947(1) 308

texIndent

Loma:: ¶944(2) 307; ¶946(1) 308

text

Loma::ItemAttributes:: ¶783(1) 265

Loma::TextFragment:: ¶841(1) 275

Loma::WordList:: ¶781(1) 264

TextFragment

Loma::TextFragment:: ¶840(1) 275; ¶842(1) 275

~TextFragment

Loma::TextFragment:: ¶842(2) 275; ¶844(1) 275

textIndent

Loma:: ¶944(1) 307; ¶945(1) 307

TextModule

Loma::TextModule:: ¶882(1) 287; ¶883(1) 287; ¶884(1) 287

Page 385: Book

List of Symbols 371

~TextModule

Loma::TextModule:: ¶883(2) 287; ¶885(1) 287

threeQuarters

Toolbox::String:: ¶620(3) 205

Token

Gramatika::Parser<T,A,GT>:: ¶585(1) 188

Gramatika::StackItem<T,A>:: ¶577(1) 186

Gramatika::Value<T,A>:: ¶569(1) 184

Lexis::Token<LT>:: ¶206(1) 71; ¶206(2) 71; ¶206(3) 71

Lexis::TokenStream<T>:: ¶219(1) 74

token

Lexis::State:: ¶56(4) 17

tokenList

Lexis::CompressedDFA:: ¶155(11) 51

Token& operator=

Lexis::Token<LT>:: ¶206(4) 71

TokenStream

Gramatika::Parser<T,A,GT>:: ¶585(4) 188

Lexis::TokenStream<T>:: ¶218(2) 74; ¶222(1) 74

tokenTable

Lexis::CompressedDFA:: ¶155(10) 51

TokenTables

Gramatika::Parser<T,A,GT>:: ¶585(5) 188

transition

Lexis::DFAState:: ¶123(2) 37

transitionTable

Lexis::CompressedDFA:: ¶155(9) 51

type

Gramatika::Symbol:: ¶295(3) 99

Lexis::Token<LT>:: ¶205(1) 71

UINT16_BITS

Toolbox::String:: ¶620(1) 205

unfinishedStates

Gramatika::Automaton:: ¶513(4) 165

used

Gramatika::Symbol:: ¶295(5) 99

Loma::CodeModule:: ¶898(1) 289

Value

Gramatika::Value<T,A>:: ¶568(2) 184; ¶571(1) 184; ¶571(2) 184; ¶572(2)185; ¶573(2) 185

Toolbox::HashTable<K,V>:: ¶730(2) 245

WordList

Loma::WordList:: ¶780(1) 264

wordList

Loma::ItemAttributes:: ¶783(2) 265

Page 386: Book

372 List of Symbols

Page 387: Book

List of Files

Gramatika/ActionRecord.cc

¶472 152

Gramatika/ActionRecord.h

¶471 151

Gramatika/Automaton.cc

¶532 170

Gramatika/Automaton.h

¶531 170

Gramatika/GotoRecord.cc

¶485 155

Gramatika/GotoRecord.h

¶484 155

Gramatika/Gramatika.cc

¶599 194

Gramatika/Gramatika.gtk

¶293 97

Gramatika/Gramatika.h

¶598 193

Gramatika/Gramatika.lex

¶244 88

Gramatika/Item.cc

¶401 131

Gramatika/Item.h

¶400 131

Gramatika/ItemAttributes.h

¶273 94

Gramatika/ItemSet.cc

¶437 142

Gramatika/ItemSet.h

¶436 142

Gramatika/main.cc

¶607 198

Gramatika/ParserData.h

¶268 93

Gramatika/ParseTable.cc

¶552 179

Gramatika/ParseTable.h

¶551 179

Page 388: Book

374 List of Files

Gramatika/Partition.cc

¶458 148

Gramatika/Partition.h

¶457 148

Gramatika/Production.cc

¶341 111

Gramatika/Production.h

¶340 111

Gramatika/State.cc

¶511 163

Gramatika/State.h

¶510 162

Gramatika/Symbol.cc

¶318 104

Gramatika/Symbol.h

¶317 104

Gramatika/SymbolTable.cc

¶372 123

Gramatika/SymbolTable.h

¶371 123

Lexis/CompressedDFA.cc

¶174 61

Lexis/CompressedDFA.h

¶173 60

Lexis/DFA.cc

¶153 49

Lexis/DFA.h

¶152 48

Lexis/DFAState.cc

¶133 40

Lexis/DFAState.h

¶132 39

Lexis/ItemAttributes.h

¶27 10

Lexis/Lexis.cc

¶236 80

Lexis/Lexis.gtk

¶54 15

Lexis/Lexis.h

¶235 79

Lexis/Lexis.lex

¶3 4

Lexis/main.cc

¶241 84

Lexis/NFA.cc

¶121 36

Lexis/NFA.h

¶120 35

Lexis/NFAState.cc

Page 389: Book

List of Files 375

¶91 26

Lexis/NFAState.h

¶90 26

Lexis/ParserData.h

¶22 9

Lexis/State.cc

¶72 20

Lexis/State.h

¶71 20

Loma/File.cc

¶943 305

Loma/File.h

¶942 305

Loma/Fragment.cc

¶863 281

Loma/Fragment.h

¶862 280

Loma/Group.cc

¶929 299

Loma/Group.h

¶928 299

Loma/ItemAttributes.h

¶786 265

Loma/Loma.gtk

¶829 272

Loma/Loma.lex

¶757 257

Loma/main.cc

¶958 314

Loma/Module.cc

¶914 294

Loma/Module.h

¶913 293

Loma/Output.cc

¶951 310

Loma/Output.h

¶950 310

Loma/ParserData.h

¶779 264

Toolbox/Character.cc

¶615 202

Toolbox/Character.h

¶614 202

Toolbox/CharacterSet.cc

¶716 239

Toolbox/CharacterSet.h

¶715 238

Toolbox/EscapeSequence.cc

¶635 214

Page 390: Book

376 List of Files

Toolbox/EscapeSequence.h

¶634 213

Toolbox/fixwarn.h

¶616 203

Toolbox/HashTable.cc

¶755 253

Toolbox/HashTable.h

¶754 252

Toolbox/List.cc

¶728 244

Toolbox/List.h

¶727 244

Toolbox/PathName.cc

¶649 218

Toolbox/PathName.h

¶648 218

Toolbox/Set.cc

¶707 235

Toolbox/Set.h

¶706 235

Toolbox/Stack.cc

¶665 222

Toolbox/Stack.h

¶664 222

Toolbox/String.cc

¶624 207

Toolbox/String.h

¶623 206