Source

semantic / doc / grammar-fw.texi

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
\input texinfo  @c -*-texinfo-*-
@c %**start of header
@setfilename grammar-fw.info
@set TITLE  Semantic Grammar Framework Manual
@set AUTHOR Eric M. Ludlam, David Ponce, and Richard Y. Kim
@settitle @value{TITLE}

@c *************************************************************************
@c @ Header
@c *************************************************************************

@c Merge all indexes into a single index for now.
@c We can always separate them later into two or more as needed.
@syncodeindex vr cp
@syncodeindex fn cp
@syncodeindex ky cp
@syncodeindex pg cp
@syncodeindex tp cp

@c @footnotestyle separate
@c @paragraphindent 2
@c @@smallbook
@c %**end of header

@copying
This manual documents Grammar Development with Semantic.

Copyright @copyright{} 2004 Eric M. Ludlam
Copyright @copyright{} 2004 David Ponce
Copyright @copyright{} 2004 Richard Y. Kim

@c If this document includes aspects of previous documents, we need
@c the dates to go back in time to include when those were written.

@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation; with the
Invariant Sections being list their titles, with the Front-Cover Texts
being list, and with the Back-Cover Texts being list.  A copy of the
license is included in the section entitled ``GNU Free Documentation
License''.
@end quotation
@end copying

@ifinfo
@dircategory Emacs
@direntry
* Semantic Grammar Framework: (grammar-fw).
@end direntry
@end ifinfo

@iftex
@finalout
@end iftex

@c @setchapternewpage odd
@c @setchapternewpage off

@ifinfo
This file documents Grammar Development with Semantic.

Copyright @copyright{} 2004 @value{AUTHOR}
@end ifinfo

@titlepage
@sp 10
@title @value{TITLE}
@author by @value{AUTHOR}
@vskip 0pt plus 1 fill
Copyright @copyright{} 2004 @value{AUTHOR}
@page
@vskip 0pt plus 1 fill
@insertcopying
@end titlepage
@page

@c MACRO inclusion
@include semanticheader.texi


@c *************************************************************************
@c @ Document
@c *************************************************************************
@contents

@node top
@top @value{TITLE}

The Semantic Grammar Framework provides a consistent way to write
rule based grammars for use in Emacs.  This document describes how to
use the grammar writing environment, and how to write in the rule
based language.

@menu
* Overview::                    
* Grammar File::                
* Working with grammars::       
* Adding a new grammar mode::   
* GNU Free Documentation License::  
* Index::                       
@end menu

@node Overview
@chapter Overview

@semantic{} version 2 introduced a new grammar framework to provide a
clean and consistent view into parser writing.

All grammars are specified in a common rule based language derived
from the input grammar language used by the GNU parser generator
Bison.  The main differences are that:

@itemize @minus
@item
The C-like syntax is replaced by Emacs Lisp syntax.
@item
Percent declarations are specific to @semantic{}.
@end itemize

Nevertheless, for those who are a familiar with Bison grammar
syntax, and have some knowledge of Emacs Lisp, writing @semantic{}
grammars won't hard.  Moreover, the grammar framework
provides Emacs goodies (indentation, syntax coloring, etc.) to help
edit grammars.

Grammars written in the common rule based language
must be translated into Emacs Lisp code so a @semantic{} parser can
use it.  The framework defines a reusable and flexible
@acronym{API} that simplifies the implementation of grammar-to-lisp
translators.

The @emph{abstract} major mode @code{semantic-grammar-mode} provides
the core functionalities to edit and translate input grammars.

@emph{Concrete} grammar modes, derived from
@code{semantic-grammar-mode}, implement the common @acronym{API} that
translates the input grammar format into Emacs Lisp code
understandable by a particular parser.  An unique file extension
associates input grammars to a @emph{concrete} grammar mode, that is
to a particular translator and parser.

@semantic{} provides these @emph{concrete} grammar modes:

@itemize @minus
@item @code{bovine-grammar-mode}, @xref{BY grammars}.
@item @code{wisent-grammar-mode}, @xref{WY grammars}.
@end itemize

The following diagram presents a global view of the framework:

@flushleft
@image{grammar-fw-ov}
@end flushleft

@node Grammar File
@chapter Grammar File

A @semantic{} grammar file has four main sections, and looks like this:

@example
  %@{
    PROLOGUE
  %@}

  DECLARATIONS

  %%
  GRAMMAR RULES
  %%

  EPILOGUE
@end example

Comments are like Emacs Lisp ones but start with two consecutive
semicolons (@code{;;}) instead of a single one. The single semicolon
(@code{;}) is the end of rule delimiter.

@menu
* Terminal and Nonterminal Symbols::  
* Prologue::                    
* Declarations::                
* Grammar Rules::               
* Epilogue::                    
@end menu

@node Terminal and Nonterminal Symbols
@section Terminal and Nonterminal Symbols

@dfn{Symbols} in @semantic{} grammars represent the grammatical
classifications of the language.  They are valid Emacs Lisp symbols
without special punctuations that require escaping.

A @dfn{terminal symbol} (also known as a @dfn{lexical token class})
represents a class of syntactically equivalent tokens.  You use the
symbol in grammar rules to mean that a token in that class is allowed.
Bison's convention recommends it should be all upper case.

Terminal symbols are part of the lexical tokens returned by the
function @code{semantic-lex-analyzer}.  They indicate what kind of
token has been read.  For more on lexical analysis, @inforef{Writing
Lexers, ,semantic-langdev}.

Terminal symbols are declared using @code{%keyword} (@pxref{keyword
Decl}) or @code{%token} (@pxref{token Decl}) statements.

@table @strong
@item Please note:
Terminal symbols can be @dfn{literal characters} which have the same
syntax used in C for character constants; for example, @code{'+'} is a
@dfn{literal character token class}.  A character token class doesn't
need to be declared, and can be used in precedence declarations like
other terminal symbols (@pxref{precedence Decl}).@*
For now @semantic{} lexers don't handle literal characters terminals.
@end table

A @dfn{nonterminal symbol} stands for a class of syntactically
equivalent groupings.  The symbol name is used in writing grammar
rules.  Bison's convention recommends it should be all lower case.

The symbol @code{error} is a terminal symbol reserved for error
recovery; you shouldn't use it for any other purpose.

@node Prologue
@section Prologue

The @var{prologue} section contains definitions of Emacs Lisp
variables, function and macros that are used in the actions in the
grammar rules.

These are copied to the beginning of the generated parser file so that
they precede the definition of the grammar rules actions.  You can use
@code{require} to get the declarations from other libraries.  If you
don't need any Emacs Lisp declarations, you may omit this section.

You may have more than one @var{prologue} section, intermixed with
other @var{declarations}.  All @var{prologue} sections are copied to
the generated parser file in the order they appear in the grammar.

@node Declarations
@section Declarations

The @var{declarations} section contains declarations that define
terminal symbols, specify precedence, and so on.  In some simple
grammars you may not need any declarations.

Declarations of terminal symbols defines the symbols used in
formulating the grammar and the type associated to categories of
tokens (@pxref{Terminal and Nonterminal Symbols}).

@menu
* package Decl::                
* languagemode Decl::           
* keyword Decl::                
* put Decl::                    
* token Decl::                  
* type Decl::                   
* precedence Decl::             
* default-prec Decl::           
* quotemode Decl::              
* scopestart Decl::             
* start Decl::                  
* use-macros Decl::             
@end menu

@node package Decl
@subsection package Decl

@c Perhaps the declaration below could be
@c %package @var{package-name}
@c or
@c %library @var{library-name}
@c ??

@deffn %-Decl %package library-name
Declare the Emacs Lisp library created from the grammar.

This will generate an Emacs Lisp file named
@file{@var{library-name}.el}, and provide the @var{library-name}
feature at the end of the generated file with:

@lisp
(provide '@var{library-name})
@end lisp

All variable and function names generated from the @var{declarations}
section will be prefixed by @code{@var{library-name}-}, following
Emacs standard coding conventions.

If there is no @code{%package} statement, a default @var{library-name}
is used, of the form:

@lisp
@var{grammar_filename_sans_extention}-@var{grammar_filename_extention}
@end lisp

For instance, the default library name for the grammar in the
@file{foo.wy} file is @code{foo-wy}.
@end deffn

@node languagemode Decl
@subsection languagemode Decl

@deffn %-Decl %languagemode @var{mode1}...
Declare in which major modes Emacs edits the sources that @semantic{}
parses using this grammar.

For instance, the following declares that the grammar will be used to
parse files edited in @code{c-mode} or @code{c++-mode} by Emacs.

@example
%languagemode c-mode c++-mode
@end example

Typically, when a parser file is re-generated from a modified grammar,
language modes are used to refresh local parser settings in buffers
currently edited in those major modes.
@end deffn

@node keyword Decl
@subsection keyword Decl

@deffn %-Decl %keyword @var{keyword-name} @var{keyword-value}
Declare a language keyword (a reserved word).

@table @var
@item keyword-name
Is the terminal symbol used in grammar rules to represent this
reserved word.
@item keyword-value
Is the actual value of the keyword as a string.
@end table

Here is how the @code{if}, @code{else}, @code{endif} keywords might be
declared:

@example
%keyword IF    "if"
%keyword ELSE  "else"
%keyword ENDIF "endif"
@end example

Keywords have the implicit reserved type @code{keyword}
(@pxref{type Decl}).
@end deffn

In the generated library, keyword declarations are defined in the
constant @code{@var{library-name}--keyword-table}.  The keyword table
value is an Emacs Lisp obarray, available at run time in the parsed
buffer, in the buffer local variable
@code{semantic-flex-keywords-obarray}.

@c Oh dear.  Perhaps it's time to obsolete and rename that variable. 

However you shouldn't use that variable directly.  @semantic{} provides
the following @acronym{API} to use with language keywords at run
time.

@defun semantic-lex-keyword-symbol name
@anchor{semantic-lex-keyword-symbol}
Return keyword symbol with @var{NAME} or @code{nil} if not found.
Return @code{nil} otherwise.
@end defun

@defun semantic-lex-keyword-p name
@anchor{semantic-lex-keyword-p}
Return non-@code{nil} if a keyword with @var{NAME} exists in the keyword table.
Return @code{nil} otherwise.
@obsolete{semantic-flex-keyword-p,semantic-lex-keyword-p}
@end defun

@defun semantic-lex-keyword-set name value
@anchor{semantic-lex-keyword-set}
Set value of keyword with @var{NAME} to @var{VALUE} and return @var{VALUE}.
@end defun

@defun semantic-lex-keyword-value name
@anchor{semantic-lex-keyword-value}
Return value of keyword with @var{NAME}.
Signal an error if a keyword with @var{NAME} does not exist.
@end defun

@defun semantic-lex-map-keywords fun &optional property
@anchor{semantic-lex-map-keywords}
Call function @var{FUN} on every semantic keyword.
If optional @var{PROPERTY} is non-@code{nil}, call @var{FUN} only on every keyword which
as a @var{PROPERTY} value.  @var{FUN} receives a semantic keyword as argument.
@obsolete{semantic-flex-map-keywords,semantic-lex-map-keywords}
@end defun

@defun semantic-lex-keywords &optional property
@anchor{semantic-lex-keywords}
Return a list of semantic keywords.
If optional @var{PROPERTY} is non-@code{nil}, return only keywords which have a
@var{PROPERTY} set (@pxref{put Decl}).
@obsolete{semantic-flex-keywords,semantic-lex-keywords}
@end defun

@node put Decl
@subsection put Decl

The @code{%put} statement assigns properties to keywords
(@pxref{keyword Decl}).  For instance, the predefined @code{summary}
property assigns a help string to a keyword.  The help string is
used by @code{semantic-idle-summary-mode} for on-the-fly help.

@deffn  %-Decl %put @var{keyword-name} @var{property value}
@deffnx %-Decl %put @var{keyword-name} @{@var{property1} @var{value1} @dots{}@}
@deffnx %-Decl %put @{@var{keyword-name1} @dots{}@} @var{property} @var{value}
@deffnx %-Decl %put @{@var{keyword-name1} @dots{}@} @{@var{property1} @var{value1} @dots{}@}
Give to @var{keyword-name}, or to several @var{keyword-name}s, a
single @var{property} with @var{value}, or a set of @var{property}s
with respective @var{value}s.

@table @var
@item keyword-name
Is a terminal symbol defined as a keyword.
@item property
Is a property name, which is a valid Emacs Lisp symbol.
@item value
Is a property value, a valid Emacs Lisp constant expression.
@end table
@end deffn

Keyword properties are stored in the keyword table (@pxref{keyword
Decl}).  The following @acronym{API} can be used to handle properties
at run time.

@defun semantic-lex-keyword-put name property value
@anchor{semantic-lex-keyword-put}
For keyword with @var{NAME}, set its @var{PROPERTY} to @var{VALUE}.
@obsolete{semantic-flex-keyword-put,semantic-lex-keyword-put}
@end defun

@defun semantic-lex-keyword-get name property
@anchor{semantic-lex-keyword-get}
For keyword with @var{NAME}, return its @var{PROPERTY} value.
@obsolete{semantic-flex-keyword-get,semantic-lex-keyword-get}
@end defun

@node token Decl
@subsection token Decl

The @code{%token} statement declares a terminal symbol (a @dfn{token})
which is not a keyword.

@deffn  %-Decl %token [<@var{type-name}>] @var{token-name} @var{match-value}
@deffnx %-Decl %token [<@var{type-name}>] @var{token-name1} @dots{}
Respectively declare one token with an optional type, and a match
value, or several tokens with the optional same type, and no match
value.

@table @var
@item type-name
Is an optional symbol, enclosed between @code{<} and @code{>}, that
specifies (and implicitly declares) a @dfn{type} for this token
(@pxref{type Decl}).  If omitted the token has no type.
@item token-name
Is the terminal symbol used in grammar rules to represent this token.
@item match-value
Is an optional string.  Depending on @var{type-name} properties, it
will be interpreted as an ordinary string, a regular expression, or
have a more elaborate meaning.  If omitted the match value will be
@code{nil}, which means that this token will be considered as the
default token of its type (@pxref{type Decl} for more information).
@end table
@end deffn

@table @strong
@item Please note:
For historical compatibility, the form @w{@samp{%token @var{name}
@var{value}}} actually declares a keyword, and is strictly equivalent
to @w{@samp{%keyword @var{name} @var{value}}}.  Because the former is
ambiguous and could be abandoned in future releases of @semantic{}, we
highly recommend to use the latter to declare keywords!
@end table

In the generated library, token definitions are stored in the table of
declared types (@pxref{type Decl}).

@node type Decl
@subsection type Decl

@deffn %-Decl %type <@var{type-name}> [@var{property1} @var{value1} @dots{}]
@c ``type'' has a lot of different meanings in semantic, so you may
@c want to define ``token type'' here.  In tags, we had changed the
@c name to ``class'' for a similar feature.

Explicitly declare a lexical type, and optionally give it properties.

@table @var
@item type-name
Is a symbol that identifies the type.
@item property
Is a property name, a valid Emacs Lisp symbol.
@item value
Is a property value, a valid Emacs Lisp constant expression.
@end table

Even if @code{%token}, @code{%keyword}, and precedence declarations can
implicitly declare types, an explicit declaration is required for
every type:

@itemize @minus
@item To assign it properties.
@item To auto-generate a lexical rule that detects tokens of this type.
For more information, @ref{Auto-generation of lexical rules}.
@end itemize
@end deffn

@table @strong
@item Please note:
Because the grammar framework is implemented in Emacs Lisp, which is a
dynamically typed language, the meaning of @dfn{type} is notably
different between @semantic{} and Bison.  In Bison grammars,
@dfn{type} means @dfn{data type}, and associates an internal
representation to lexical tokens.  In @semantic{} grammars, @dfn{type}
specifies how lexical analysis will scan tokens of this type.
@end table

In the generated library, lexical type declarations are defined in the
constant @code{@var{library-name}--token-table}.  The table
value is an Emacs Lisp obarray, available at run time in the parsed
buffer, in the buffer local variable
@code{semantic-lex-types-obarray}.

However you shouldn't use that variable directly.  @semantic{} provides
the following @acronym{API} to use with lexical types at run
time.

@defun semantic-lex-type-symbol type
@anchor{semantic-lex-type-symbol}
Return symbol with @var{TYPE} or @code{nil} if not found.
@end defun

@defun semantic-lex-type-p type
@anchor{semantic-lex-type-p}
Return non-@code{nil} if a symbol with @var{TYPE} name exists.
@end defun

@defun semantic-lex-type-set type value
@anchor{semantic-lex-type-set}
Set value of symbol with @var{TYPE} name to @var{VALUE} and return @var{VALUE}.
@end defun

@defun semantic-lex-type-value type &optional noerror
@anchor{semantic-lex-type-value}
Return value of symbol with @var{TYPE} name.
If optional argument @var{NOERROR} is non-@code{nil} return @code{nil} if a symbol with
@var{TYPE} name does not exist.  Otherwise signal an error.
@end defun

@defun semantic-lex-type-put type property value &optional add
@anchor{semantic-lex-type-put}
For symbol with @var{TYPE} name, set its @var{PROPERTY} to @var{VALUE}.
If optional argument @var{ADD} is non-@code{nil}, create a new symbol with @var{TYPE}
name if it does not already exist.  Otherwise signal an error.
@end defun

@defun semantic-lex-type-get type property &optional noerror
@anchor{semantic-lex-type-get}
For symbol with @var{TYPE} name, return its @var{PROPERTY} value.
If optional argument @var{NOERROR} is non-@code{nil} return @code{nil} if a symbol with
@var{TYPE} name does not exist.  Otherwise signal an error.
@end defun

@defun semantic-lex-map-types fun &optional property
@anchor{semantic-lex-map-types}
Call function @var{FUN} on every lexical type.
If optional @var{PROPERTY} is non-@code{nil}, call @var{FUN} only on every type symbol
which as a @var{PROPERTY} value.  @var{FUN} receives a type symbol as argument.
@end defun

@defun semantic-lex-types &optional property
@anchor{semantic-lex-types}
Return a list of lexical type symbols.
If optional @var{PROPERTY} is non-@code{nil}, return only type symbols which have
@var{PROPERTY} set.
@end defun

@node precedence Decl
@subsection precedence Decl

@deffn  %-Decl %left [<@var{type-name}>] @var{token1} @dots{}
@deffnx %-Decl %right [<@var{type-name}>] @var{token1} @dots{}
@deffnx %-Decl %nonassoc [<@var{type-name}>] @var{token1} @dots{}
See also @code{%prec} in @ref{Grammar Rules}.

Specify the precedence and associativity of grammar terminals.

For more details, @inforef{Grammar format, , wisent}, @var{PRECEDENCE}.
@end deffn

@c is this true?
@table @strong
@item Please note:
This information has meaning only in @acronym{LALR} grammars.
Defining precedence in a grammar that provides tagging information
for @semantic{} isn't usually necessary.
@end table

@node default-prec Decl
@subsection default-prec Decl

@deffn  %-Decl %default-prec
@deffnx %-Decl %no-default-prec
See also @code{%prec} in @ref{Grammar Rules}.

@code{%no-default-prec} declares that you must specify precedence for
all rules that participate in precedence conflict resolution.  The
default @code{%default-prec} indicates to use the precedence of the
last terminal symbol mentioned in the rule.

For more details, @inforef{Grammar format, , wisent}, @var{PRECEDENCE}.
@end deffn

@table @strong
@item Please note:
This information has meaning only in @acronym{LALR} grammars.
Defining precedence in a grammar that provides tagging information
for @semantic{} isn't usually necessary.
@end table

@node quotemode Decl
@subsection quotemode Decl

@deffn %-Decl %quotemode @var{symbol}
This is a mechanism used to specify how quoting worked in optional
lambda expressions.
@inforef{Optional Lambda Expression, , bovine}.
@end deffn

@table @strong
@item Please note:
This information has meaning only in @dfn{bovine} grammars.
@end table

@node scopestart Decl
@subsection scopestart Decl

@deffn %-Decl %scopestart @var{nonterminal}
The @code{scopestart} declaration specifies the name of a nonterminal
that is used for parsing the body of functional code.  This is used
in the local context parser to find locally defined variables.
@end deffn

@table @strong
@item Please note:
This information has meaning only in @dfn{bovine} grammars.
@end table

@node start Decl
@subsection start Decl

@deffn %-Decl %start @var{nonterminal1} @dots{}
Declare the nonterminal symbols that the parser can use as its goal.
@end deffn

@table @strong
@item Please note:
The exact meaning of start symbols depends on the parser.  For more
information, @inforef{Start nonterminals, , wisent}, and
@inforef{Starting Rules , , bovine}.
@end table

@node use-macros Decl
@subsection use-macros Decl

@deffn %-Decl %use-macros @var{library} @{@var{macro-name1} @dots{}@}
Declare grammar macros local in this grammar.

@table @var
@item library
Is a symbol identifying the Elisp library where to find the macro
expanders.
@item macro-name
Is a symbol identifying a macro.  By convention it should be all
upper case.
@end table

For more information on macros, @ref{Grammar Macros}.
@end deffn

When the Lisp code generator encounters a @code{%use-macros}
declaration, it automatically loads (@code{require}) the specified
@var{library} and associates to each @var{macro-name} an expander
function named @var{library}-@var{macro-name}.
 
For instance:

@example
%use-macros my-grammar-macros @{MY-MACRO@}
@end example
 
Tells the Lisp code generator to first @w{@samp{(require
'my-grammar-macros)}}, then to call the function
@code{my-grammar-macros-MY-MACRO} to expand calls to the
@code{MY-MACRO} macro.
 
Typically, the @file{my-grammar-macros.el} library should look like
this:
 
@lisp
@dots{}

(defun my-grammar-macros-MY-MACRO (&rest args)
  "Grammar macro that passes ARGS to `format' and return a string."
  `(format ,@@args))

@dots{}

;; Don't forget to provide myself!
(provide 'my-grammar-macros)
@end lisp
 
@node Grammar Rules
@section Grammar Rules

A grammar rule has the following general form:

@example
@var{result}: @var{components} @dots{}
      ;
@end example

@var{result} is the nonterminal symbol that this rule describes, and
@var{components} are the various terminal and nonterminal symbols
that are put together by this rule
(@pxref{Terminal and Nonterminal Symbols}).

For example, this rule:

@example
exp: exp PLUS exp
   ;
@end example

Says that two groupings of type @samp{exp}, with a @samp{PLUS} token
in between, can be combined into a larger grouping of type @samp{exp}.

Multiple rules for the same @var{result} are joined with the
vertical-bar character @code{|} as follows:

@example
@var{result}: @var{rule1-components}@dots{}
      | @var{rule2-components}@dots{}
        @dots{}
      ;
@end example

If @var{components} in a rule is empty, it means that @var{result} can
match the empty string.  For example, here is how to define a
comma-separated sequence of zero or more @samp{exp} groupings:

@example
expseq: ;;Empty
      | expseq1
      ;
     
expseq1: exp
       | expseq1 COMMA exp
       ;
@end example

It is customary to write a comment @code{;;Empty} in each rule with no
components.

@table @strong
@item Please note:
In @acronym{LALR} grammars, a @code{%prec} modifier can be written
after the @var{components} of a rule to specify a terminal symbol
whose precedence should be used for that rule.  The syntax is:

@example
%prec @var{token}
@end example

It assigns the rule the precedence of @var{token}, overriding the
precedence that would be deduced for it in the ordinary way.  For
more details, @inforef{Grammar format, , wisent}, @var{PRECEDENCE}.

Here is a typical example:

@example
@dots{}
%left NEG     ;; negation--unary minus
@dots{}

%%
@dots{}

exp: @dots{}
   | '-' exp %prec NEG
     (- $2)
   @dots{}
   ;
@end example
@end table

Scattered among the components can be @var{actions} that determine the
semantics of the rule.  An action is an Emacs Lisp list form, for
example:

@lisp
(cons $1 $2)
@end lisp

To execute a sequence of forms, you can enclose them between braces
like this:

@lisp
@{
  (message "$2=%s" $2)
  $2
 @}
@end lisp

Usually there is only one action and it follows the rule components.

The code in an action can refer to the semantic values of the
components matched by the rule with the construct @samp{$N}, which
stands for the value of the Nth component.

Here is a typical example:

@example
exp: @dots{}
   | exp PLUS exp
     (+ $1 $3)
   @dots{}
   ;
@end example

This rule constructs an @samp{exp} from two smaller @samp{exp}
groupings connected by a plus-sign token.  In the action, @samp{$1}
and @samp{$3} refer to the semantic values of the two component
@samp{exp} groupings, which are the first and third symbols on the
right hand side of the rule.  The sum becomes the semantic value of
the addition-expression just recognized by the rule.  If there were a
useful semantic value associated with the @samp{PLUS} token, it could
be referred to as @samp{$2}.

Note that the vertical-bar character @code{|} is really a rule separator,
and actions are attached to a single rule.

By convention, if you don't specify an action for a rule, the value of
the first symbol in the rule becomes the value of the whole rule:
@code{(progn $1)}.  The default value of an empty rule is @code{nil}.

The exact default behavior depends on the parser.  For more
information, @inforef{Wisent, , wisent}, and @inforef{Bovine, ,
bovine}, manuals.

@table @strong
@item Please note:
In @acronym{LALR} grammars, you can have @dfn{mid-rule actions}, that
is semantic actions put in the middle of a rule.  These actions are
written just like usual end-of-rule actions, but they are executed
before the parser even recognizes the following components.

The mid-rule action itself counts as one of the components of the
rule.  This makes a difference when there is another action later in
the same rule (and usually there is another at the end): you have to
count the actions along with the symbols when working out which number
N to use in @samp{$N}.

The mid-rule action can also have a semantic value, and actions later
in the rule can refer to the value using @samp{$N}.

There is no way to set the value of the entire rule with a mid-rule
action.  The only way to set the value for the entire rule is with an
ordinary action at the end of the rule.

Here is an example taken from the @file{semantic-grammar.wy} grammar
in the distribution:

@example
nonterminal:
    SYMBOL
    ;; Mid-rule action
    (setq semantic-grammar-wy--nterm $1
          semantic-grammar-wy--rindx 0)
    COLON rules SEMI
    ;; End-of-rule action
    (TAG $1 'nonterminal :children $4)
  ;
@end example
@end table

@node Epilogue
@section Epilogue

The @var{epilogue} section contains arbitrary Emacs Lisp code which is
copied verbatim to the end of the parser file, just as the
@var{prologue} is copied to the beginning.  This is the most
convenient place to put anything that you want to have in the parser
file.

For example, it could be convenient to put the definition of the lexer
in the @var{epilogue}, particularly if it includes lexical rules
auto-generated from declarations in the grammar.

If the @var{epilogue} section is empty, you may omit the @code{%%}
that separates it from the grammar rules.

@table @strong
@item Please note:
You can put a @dfn{footer} comment:

@example
;;; my-grammar.by ends here
@end example

At the end of the grammar.  It will not be copied to the parser file
to avoid confusion with the Emacs Lisp library own footer.
@end table

@node Working with grammars
@chapter Working with grammars

@menu
* Editing grammars::            
* Auto-generation of lexical rules::  
* Grammar Macros::              
* BY grammars::                 
* WY grammars::                 
@end menu

@node Editing grammars
@section Editing grammars

@c Document here the various commands and keyboard shortcuts
@c available in semantic-grammar-mode. 

@deffn Command semantic-grammar-create-package &optional force
@anchor{semantic-grammar-create-package}
Create package Lisp code from grammar in current buffer.
Does nothing if the Lisp code seems up to date.
If optional argument @var{FORCE} is non-@code{nil}, unconditionally re-generate the
Lisp code.
@end deffn

You can run the command @command{semantic-grammar-create-package} with
@w{@kbd{C-c C-c}} (or @w{@kbd{C-u C-c C-c}} to unconditionally
re-generate the Lisp code).

Additionally, when this command is run interactively, all open buffers
of that mode have their setup functions re-run.  That way after
compiling your grammar, all relevant buffers will be actively using
that grammar so you can test what you have done.

@deffn Command semantic-grammar-indent
@anchor{semantic-grammar-indent}
Indent the current line.
Use the Lisp or grammar indenter depending on point location.
@end deffn

You can run the command @command{semantic-grammar-indent} with
@kbd{@key{TAB}}.

@deffn Command semantic-grammar-complete
@anchor{semantic-grammar-complete}
Attempt to complete the symbol under point.
Completion is position sensitive.  If the cursor is in a match section of
a rule, then nonterminals symbols are scanned.  If the cursor is in a Lisp
expression then Lisp symbols are completed.
@end deffn

You can run the command @command{semantic-grammar-complete} with
@w{@kbd{@key{META} @key{TAB}}}.

@deffn Command semantic-grammar-find-macro-expander macro-name library
@anchor{semantic-grammar-find-macro-expander}
Visit the Emacs Lisp library where a grammar macro is implemented.
@var{MACRO-NAME} is a symbol that identifies a grammar macro.
@var{LIBRARY} is the name (sans extension) of the Emacs Lisp library where
to start searching the macro implementation.  Lookup in included
libraries, if necessary.
Find a function tag (in current tags table) whose name contains @var{MACRO-NAME}.
Select the buffer containing the tag's definition, and move point there.

@table @strong
@item Please note:
Enabling the @code{global-semanticdb-minor-mode} is highly recommended
because this command uses the @code{semanticdb-find} feature to
automatically search for a macro expander function into included
libraries.  For more details, @inforef{Semantic Database, ,
semantic-appdev}.
@end table
@end deffn

You can run the command @command{semantic-grammar-find-macro-expander}
with @w{@kbd{C-c m}}.

The characters @w{@kbd{| ; % ( ) :}} are @dfn{electric punctuations}.
Each time you type one, the line is re-indented after the character is
inserted.

@deffn Command semantic-grammar-insert-keyword name
@anchor{semantic-grammar-insert-keyword}
Insert a new @code{%keyword} declaration with @var{name}.
Assumes it is typed in with the correct casing.
@end deffn

You can run the command @command{semantic-grammar-insert-keyword}
with @w{@kbd{C-c i k}}.

@node Auto-generation of lexical rules
@section Auto-generation of lexical rules

Using a @code{%type} statement, combined with @code{%keyword} and
@code{%token} ones, permits the declaration of a lexical type and
associates it with patterns that define how to match lexical tokens of
that type.

The grammar construction process exploits that information to
@emph{automagically} generate the definition of a lexical rule (aka a
@dfn{single analyzer}) for each explicitly declared lexical type.

It is then easy to put predefined and auto-generated lexical rules
together to build ad-hoc lexical analyzers.

This chapter details the auto-generation process, and how you can
control it to produce the lexical rules needed to scan your language.

@menu
* The principle::               
* Type properties with a special meaning::  
* Predefined well-known types::  
@end menu

@node The principle
@subsection The principle

@node Type properties with a special meaning
@subsection Type properties with a special meaning

@table @code
@item syntax
The value of the @code{syntax} property should be a @dfn{syntactic
regexp}, that is a regexp that matches buffer data based on the
current Emacs @dfn{syntax table}.

For instance, to grab constituents of @code{symbol} or @code{keyword}
types, the @code{syntax} property value will probably be:

@samp{"\\(\\sw\\|\\s_\\)+"}

For well known types, the @code{syntax} property has a predefined
value that should suit standard needs (@pxref{Predefined well-known
types}).

@item matchdatatype
The value of the @code{matchdatatype} property is a symbol that
specifies which algorithm to use to match the tokens of this type.
These match algorithms are defined:

@table @code
@item regexp
@item string
@item block
@item sexp
@item keyword
@end table

@end table

@node Predefined well-known types
@subsection Predefined well-known types

Default values are provided for well known types like <keyword>,
<symbol>, <string>, <number>, <punctuation>, and <block>.  Those types
assume that the correct patterns are provided by %keyword and %token
statements, a simple "%type <type>" declaration should generally
suffice to auto-generate a suitable lexical rule.

@table @code
@item keyword
@item symbol
@item string
@item number
@item punctuation
@item block
@end table

@node Grammar Macros
@section Grammar Macros

To keep grammars relatively independent of contextual implementations
you can use @dfn{grammar macros} in semantic actions, in place of
direct calls to Emacs Lisp functions.

The predefined @code{TAG} macro is a good example:

@example
(TAG $1 'nonterminal :children $4)
@end example

It offers a common syntax to produce a @semantic{} tag, even if the
underlying implementation is different between @acronym{LALR} and
@acronym{LL} grammars.

A grammar macro is defined by a general @dfn{macro name} (for example,
@code{TAG}) associated to possibly several @dfn{macro expanders}.

A macro expander is an Emacs Lisp function that operates on the
unevaluated expressions for the arguments, and returns a Lisp
expression containing these argument expressions or parts of them.

Macro names are used like Lisp function names in call expressions.  To
help distinguish grammar macro calls from traditional Lisp function
calls, macro names are automatically highlighted in semantic actions.

For more goodies that might help in the development of macros,
@xref{Editing grammars}.

Commonly used macros are provided by the current grammar mode.
@xref{Adding a new grammar mode}.

There are also local macros that only affect the grammar where they
are declared.  @xref{use-macros Decl}.

@node BY grammars
@section BY grammars

@acronym{BY} grammars are in files with the @file{.by} extension
associated to the @code{bovine-grammar-mode} major mode.

In that mode, grammars are converted into an Emacs Lisp form
understandable by the original @semantic{} @acronym{LL} parser.

For more details on how the @acronym{LL} parser works,
@inforef{top, the Bovine Parser Manual, bovine}.
 
@node WY grammars
@section WY grammars

@acronym{WY} grammars are in files with the @file{.wy} extension
associated to the @code{wisent-grammar-mode} major mode.

In that mode, grammars are converted into an Emacs Lisp form
understandable by the @semantic{} Bison-like @acronym{LALR} parser.

For more details on how the @acronym{LALR} parser works,
@inforef{top, the Wisent Parser Manual, wisent}.

@node Adding a new grammar mode
@chapter Adding a new grammar mode

@menu
* Specialized Implementation::  
* Querying grammars::           
@end menu

@node Specialized Implementation
@section Specialized Implementation

Each specialized grammar mode is responsible for implementing the
following @acronym{API}.  Implementations are done with
@emph{overload methods}.
@inforef{Semantic Overload Mechanism, , semantic-langdev}.

@itemize
@item A keyword table builder.
Can use the default implementation provided.

@defun semantic-grammar-keywordtable-builder
@anchor{semantic-grammar-keywordtable-builder}
Return the keyword table table value.
This function can be overriden in semantic using the
symbol @code{grammar-keywordtable-builder}.
@end defun

@item A token table builder.
Can use the default implementation provided.

@defun semantic-grammar-tokentable-builder
@anchor{semantic-grammar-tokentable-builder}
Return the value of the table of lexical tokens.
This function can be overriden in semantic using the
symbol @code{grammar-tokentable-builder}.
@end defun

@item A parser table builder
Must be provided, there is no default implementation.

@defun semantic-grammar-parsetable-builder
@anchor{semantic-grammar-parsetable-builder}
Return the parser table value.
This function can be overriden in semantic using the
symbol @code{grammar-parsetable-builder}.
@end defun

@item A Parser setup code builder.
Must be provided, there is no default implementation.

@defun semantic-grammar-setupcode-builder
@anchor{semantic-grammar-setupcode-builder}
Return the parser setup code form.
This function can be overriden in semantic using the
symbol @code{grammar-setupcode-builder}.
@end defun

@item Common grammar macros.
@c This refers to ASSOC, EXPANDFULL, TAG, etc..
A set of predefined macros is provided.

For more information on macros, @ref{Grammar Macros}.

@defvar semantic-grammar-macros
@anchor{semantic-grammar-macros}
List of associations (@var{MACRO-NAME} . @var{EXPANDER}).
@end defvar

@end itemize

@node Querying grammars
@section Querying grammars

In order to generate the intermediate Emacs Lisp code needed by your
parser, you have to query the grammar for the code, declarations, and
rules it contains.

To obtain those information in a @dfn{context free} manner, grammar
files are parsed by the @semantic{} @acronym{LALR} parser to produce
tags.

To query a grammar you can use the powerful @acronym{API} provided by
@semantic{}.  @inforef{top, Application Development Manual,
semantic-appdev}.

For declarations, the grammar framework provides a specialized high
level @acronym{API}.

@menu
* Grammar Tags::                
* Querying Declarations::       
@end menu

@node Grammar Tags
@subsection Grammar Tags

This section describes the available grammar tags.
@inforef{Semantic Tags, , semantic-appdev}.

Each tag description has the form
@w{@var{class name optional-attributes}}.

@deffn tag code "prologue"
Tag produced from a @var{prologue} section (@pxref{Prologue}).
The prologue code is the text in between tag's bounds.
@end deffn

@deffn tag code "epilogue"
Tag produced from the @var{epilogue} section (@pxref{Epilogue}).
The epilogue code is the text in between tag's bounds.
@end deffn

@deffn tag start @var{nonterminal1} @var{rest-nonterminals}
Tag produced from a @code{%start} declaration (@pxref{start Decl}).

@table @var
@item nonterminal1
First nonterminal symbol name.
@item rest-nonterminals
List of nonterminal symbol names which follow the first one.
Associated to the attribute @code{:rest}.
@end table
@end deffn

@deffn tag languagemode @var{mode1} @var{rest-modes}
Tag produced from a @code{%languagemode} declaration (@pxref{languagemode Decl}).

@table @var
@item mode1
First mode symbol name.
@item rest-modes
List of mode symbol names which follow the first one.  Associated to
the attribute @code{:rest}.
@end table
@end deffn

@deffn tag scopestart @var{nonterminal}
Tag produced from a @code{%scopestart} declaration (@pxref{scopestart Decl}).

@table @var
@item nonterminal
Nonterminal symbol name.
@end table
@end deffn

@deffn tag quotemode @var{symbol}
Tag produced from a @code{%quotemode} declaration (@pxref{quotemode Decl}).

@table @var
@item symbol
Symbol name, value of quote mode.
@end table
@end deffn

@deffn  tag assoc @var{associativity} @var{type-name} @var{tokens}
Tag produced from a @code{%left}, @code{%right}, or @code{%nonassoc}
declaration (@pxref{precedence Decl}).

@table @var
@item associativity
"left", "right", or "nonassoc".
@item type-name
Symbol name of the token type.
Associated to the attribute @code{:type}.
@item tokens
List of terminal symbol names.
Associated to the attribute @code{:value}.
@end table
@end deffn

@deffn tag assoc "default-prec" @var{FLAG}
Tag produced from a @code{%default-prec}, or @code{%no-default-prec}
declaration (@pxref{default-prec Decl}).

@table @var
@item flag
@code{(list "t")} or @code{(list "nil")} for respectively
@code{%default-prec} and @code{%no-default-prec}.
Associated to the attribute @code{:value}.
@end table
@end deffn

@deffn tag keyword @var{keyword-name} @var{keyword-value}
Tag produced from a @code{%keyword} declaration (@pxref{keyword Decl}).

@table @var
@item keyword-value
Name of the keyword symbol.
@item keyword-value
Value of the keyword as a string.
Associated to the attribute @code{:value}.
@end table
@end deffn

@deffn  tag token @var{token-name1} @var{type-name} @var{match-value}
@deffnx tag token @var{token-name1} @var{type-name} @var{rest-tokens}
Tag produced from a @code{%token} declaration (@pxref{token Decl}).

@table @var
@item token-name1
First or unique token symbol name.
@item type-name
Symbol name of the token type.
Associated to the attribute @code{:type}.
@item match-value
Value of the token as a string.
Associated to the attribute @code{:value}.
@item rest-tokens
List of token symbol names which follow the first one in the
declaration.  Associated to the attribute @code{:rest}.
@end table
@end deffn

@deffn tag type @var{type-name} @var{properties}
Tag produced from a @code{%type} declaration (@pxref{type Decl}).

@table @var
@item type-name
Symbol name of the type.
@item properties
List of properties.  Each property is a pair @w{@code{(@var{symbol} . @var{value})}}
where @var{symbol} is the property symbol name, and @var{value} the
property value as string which contains a readable Lisp expression.
Associated to the attribute @code{:value}.
@end table
@end deffn

@deffn tag put @var{keyword-name1} @var{rest-keywords} @var{properties}
Tag produced from a @code{%put} declaration (@pxref{put Decl}).

@table @var
@item keyword-name1
First or unique keyword [or type] symbol name.
@item rest-keywords
List of keyword [or type] symbol names which follow the first one in
the declaration.  Associated to the attribute @code{:rest}.
@item properties
List of properties.  Each property is a pair @w{@code{(@var{symbol} . @var{value})}}
where @var{symbol} is the property symbol name, and @var{value} the
property value as string which contains a readable Lisp expression.
Associated to the attribute @code{:value}.
@end table
@end deffn

@deffn tag macro "macro" @var{library} @var{macro-names}
Tag produced from a @code{%use-macros} declaration (@pxref{use-macros
Decl}).

@table @var
@item library
Library name.  Associated to the attribute @code{:type}.
@item macro-names
List of macro names.  Associated to the attribute @code{:value}.
@end table
@end deffn

@deffn tag nonterminal @var{nonterminal} @var{rule-tags}
Tag produced from a nonterminal description (@pxref{Grammar Rules}).

@table @var
@item nonterminal
Nonterminal symbol name.
@item rule-tags
List rule tags.  Associated to the attribute @code{:children}.
@end table
@end deffn

@deffn tag rule @var{rule-name} :value @var{components} :prec @var{terminal} :expr @var{action}
Tag produced from an individual grammar rule (@pxref{Grammar Rules}).

@table @var
@item rule-name
Generated rule name.
@item components
List of rule's components.  A component is a nonterminal/terminal
symbol name, a string that represent a C-like character, or a list of
one string element which contains the readable Lisp form of a mid-rule
semantic action.  Associated to the attribute @code{:value}.
@item prec
Terminal symbol name or a string that represent a C-like character.
Associated to the attribute @code{:prec}.
@item action
String which contains the readable Lisp form of the main
semantic action.  Associated to the attribute @code{:expr}.
@end table
@end deffn

@node Querying Declarations
@subsection Querying Declarations

This section describes the high level @acronym{API} to query grammar
declarations.

@defun semantic-grammar-prologue
@anchor{semantic-grammar-prologue}
Return grammar prologue code as a string value.
@*@xref{Prologue}.
@end defun

@defun semantic-grammar-epilogue
@anchor{semantic-grammar-epilogue}
Return grammar epilogue code as a string value.
@*@xref{Epilogue}.
@end defun

@defun semantic-grammar-package
@anchor{semantic-grammar-package}
Return the @code{%package} value as a string.
If there is no @code{%package} statement in the grammar, return a default
package name derived from the grammar file name.
@*@xref{package Decl}.
@end defun

@defun semantic-grammar-languagemode
@anchor{semantic-grammar-languagemode}
Return the @code{%languagemode} value as a list of symbols or @code{nil}.
@*@xref{languagemode Decl}.
@end defun

@defun semantic-grammar-start
@anchor{semantic-grammar-start}
Return the @code{%start} value as a list of symbols or @code{nil}.
@*@xref{start Decl}.
@end defun

@defun semantic-grammar-scopestart
@anchor{semantic-grammar-scopestart}
Return the @code{%scopestart} value as a symbol or @code{nil}.
@*@xref{scopestart Decl}.
@end defun

@defun semantic-grammar-quotemode
@anchor{semantic-grammar-quotemode}
Return the @code{%quotemode} value as a symbol or @code{nil}.
@*@xref{quotemode Decl}.
@end defun

@defun semantic-grammar-keywords
@anchor{semantic-grammar-keywords}
Return the language keywords.
That is an alist of (@var{VALUE} . @var{TOKEN}) where @var{VALUE} is
the string value of the keyword and @var{TOKEN} is the terminal symbol
identifying the keyword.
@*@xref{keyword Decl}.
@end defun

@defun semantic-grammar-keyword-properties keywords
@anchor{semantic-grammar-keyword-properties}
Return the list of @var{KEYWORDS} properties.
@*@xref{keyword Decl}, @xref{put Decl}.
@end defun

@defun semantic-grammar-tokens
@anchor{semantic-grammar-tokens}
Return defined lexical tokens.
That is an alist (@var{TYPE} . @var{DEFS}) where type is a
@code{%token <type>} symbol and @var{DEFS} is an alist of (@var{TOKEN}
. @var{VALUE}).  @var{TOKEN} is the terminal symbol identifying the
token and @var{VALUE} is the string value of the token or @code{nil}.
@*@xref{token Decl}.
@end defun

@defun semantic-grammar-token-properties tokens
@anchor{semantic-grammar-token-properties}
Return properties of declared types.
Types are explicitly declared by @code{%type} statements.  Types found
in @var{TOKENS} are those declared implicitly by @code{%token}
statements.  Properties can be set by @code{%put} and @code{%type}
statements.  Properties set by @code{%type} statements take precedence
over those set by @code{%put} statements.
@*@xref{token Decl}, @xref{type Decl}, @xref{put Decl}.
@end defun

@defun semantic-grammar-use-macros
@anchor{semantic-grammar-use-macros}
Return macro definitions from @code{%use-macros} statements.
Also load the specified macro libraries.
@*@xref{use-macros Decl}.
@end defun

@node GNU Free Documentation License
@appendix GNU Free Documentation License

@include fdl.texi

@node Index
@unnumbered Index
@printindex cp

@iftex
@contents
@summarycontents
@end iftex

@bye

@c Following comments are for the benefit of ispell.

@c  LocalWords:  bovinator wisent LALR Elisp automagically Decl API