Commit a84c08795e1090905d8ca349c1c1773db2fde065

Kristian Høgsberg 2010-10-19T21:57:59

Use flex for generating the scanner, add support for parsing from strings Signed-off-by: Kristian Høgsberg <krh@bitplanet.net>

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
diff --git a/configure.ac b/configure.ac
index 5b1efb4..2ab311c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -30,6 +30,7 @@ AC_CONFIG_HEADERS([src/config.h])
 AC_PROG_LIBTOOL
 AC_PROG_CC
 AC_PROG_YACC
+AC_PROG_LEX
 
 m4_ifndef([PKG_PROG_PKG_CONFIG],
     [m4_fatal([Could not locate the pkg-config autoconf macros.
diff --git a/include/X11/extensions/XKBcommon.h b/include/X11/extensions/XKBcommon.h
index 19adc85..6bf7d8a 100644
--- a/include/X11/extensions/XKBcommon.h
+++ b/include/X11/extensions/XKBcommon.h
@@ -808,6 +808,9 @@ xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg);
 _X_EXPORT extern struct xkb_desc *
 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName);
 
+_X_EXPORT extern struct xkb_desc *
+xkb_compile_keymap_from_string(const char *string, const char *mapName);
+
 _X_EXPORT extern struct xkb_component_list *
 xkb_list_components(struct xkb_component_names * ptrns, int *maxMatch);
 
diff --git a/src/xkbcomp/Makefile.am b/src/xkbcomp/Makefile.am
index 2923a6f..375318a 100644
--- a/src/xkbcomp/Makefile.am
+++ b/src/xkbcomp/Makefile.am
@@ -38,4 +38,4 @@ libxkbcomp_la_SOURCES = \
 	xkbparse.y \
 	xkbpath.c \
 	xkbpath.h \
-	xkbscan.c
+	xkbscan.l
diff --git a/src/xkbcomp/parseutils.c b/src/xkbcomp/parseutils.c
index 65b66a9..c8491e7 100644
--- a/src/xkbcomp/parseutils.c
+++ b/src/xkbcomp/parseutils.c
@@ -738,7 +738,7 @@ PrintStmtAddrs(ParseCommon * stmt)
 }
 #endif
 
-static void
+void
 CheckDefaultMap(XkbFile * maps)
 {
     XkbFile *dflt, *tmp;
@@ -768,27 +768,6 @@ CheckDefaultMap(XkbFile * maps)
     return;
 }
 
-int
-XKBParseFile(FILE * file, XkbFile ** pRtrn)
-{
-    if (file)
-    {
-        yyin = file;
-        rtrnValue = NULL;
-        if (yyparse() == 0)
-        {
-            *pRtrn = rtrnValue;
-            CheckDefaultMap(rtrnValue);
-            rtrnValue = NULL;
-            return 1;
-        }
-        *pRtrn = NULL;
-        return 0;
-    }
-    *pRtrn = NULL;
-    return 1;
-}
-
 XkbFile *
 CreateXKBFile(int type, char *name, ParseCommon * defs, unsigned flags)
 {
diff --git a/src/xkbcomp/parseutils.h b/src/xkbcomp/parseutils.h
index 861ec61..6169b87 100644
--- a/src/xkbcomp/parseutils.h
+++ b/src/xkbcomp/parseutils.h
@@ -177,6 +177,8 @@ extern int XKBParseFile(FILE * /* file */ ,
                         XkbFile **      /* pRtrn */
     );
 
+extern int XKBParseString(const char *string, XkbFile ** pRtrn);
+
 extern XkbFile *CreateXKBFile(int /* type */ ,
                               char * /* name */ ,
                               ParseCommon * /* defs */ ,
@@ -186,8 +188,6 @@ extern XkbFile *CreateXKBFile(int /* type */ ,
 extern void yyerror(const char *        /* s */
     );
 
-extern int yywrap(void);
-
 extern int yylex(void);
 extern int yyparse(void);
 
@@ -196,5 +196,6 @@ extern int setScanState(char * /* file */ ,
     );
 
 extern FILE *yyin;
+extern const char *yystring;
 
 #endif /* XKBPARSE_H */
diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c
index 53e6ca5..63858a0 100644
--- a/src/xkbcomp/xkbcomp.c
+++ b/src/xkbcomp/xkbcomp.c
@@ -247,23 +247,12 @@ fail:
     return NULL;
 }
 
-struct xkb_desc *
-xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
+static struct xkb_desc *
+compile_keymap(XkbFile *file, const char *mapName)
 {
-    XkbFile *file, *mapToUse;
+    XkbFile *mapToUse;
     struct xkb_desc * xkb;
 
-    if (!inputFile) {
-        ERROR("no file specified to generate XKB keymap\n");
-        goto fail;
-    }
-
-    setScanState("input", 1);
-    if (!XKBParseFile(inputFile, &file) || !file) {
-        ERROR("failed to parse input xkb file\n");
-        goto fail;
-    }
-
     /* Find map to use */
     if (!(mapToUse = XkbChooseMap(file, mapName)))
         goto unwind_file;
@@ -294,6 +283,44 @@ unwind_xkb:
     XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
 unwind_file:
     /* XXX: here's where we would free the XkbFile */
-fail:
+
     return NULL;
 }
+
+struct xkb_desc *
+xkb_compile_keymap_from_string(const char *string, const char *mapName)
+{
+    XkbFile *file;
+
+    if (!string) {
+        ERROR("no string specified to generate XKB keymap\n");
+        return NULL;
+    }
+
+    setScanState("input", 1);
+    if (!XKBParseString(string, &file) || !file) {
+        ERROR("failed to parse input xkb file\n");
+        return NULL;
+    }
+
+    return compile_keymap(file, mapName);
+}
+
+struct xkb_desc *
+xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
+{
+    XkbFile *file;
+
+    if (!inputFile) {
+        ERROR("no file specified to generate XKB keymap\n");
+	return NULL;
+    }
+
+    setScanState("input", 1);
+    if (!XKBParseFile(inputFile, &file) || !file) {
+        ERROR("failed to parse input xkb file\n");
+	return NULL;
+    }
+
+    return compile_keymap(file, mapName);
+}
diff --git a/src/xkbcomp/xkbparse.y b/src/xkbcomp/xkbparse.y
index 713cc1d..ab21681 100644
--- a/src/xkbcomp/xkbparse.y
+++ b/src/xkbcomp/xkbparse.y
@@ -89,6 +89,7 @@
 	FUNCTION_KEYS		76
 	ALTERNATE_GROUP		77
 %{
+#define DEBUG 1
 #ifdef DEBUG
 #define	YYDEBUG 1
 #endif
@@ -765,23 +766,3 @@ OptMapName	:	MapName	{ $$= $1; }
 
 MapName		:	STRING 	{ $$= strdup(scanBuf); }
 		;
-%%
-void
-yyerror(const char *s)
-{
-    if (warningLevel>0) {
-	(void)fprintf(stderr,"%s: line %d of %s\n",s,lineNum,
-					(scanFile?scanFile:"(unknown)"));
-	if ((warningLevel>3))
-	    (void)fprintf(stderr,"last scanned symbol is: %s\n",scanBuf);
-    }
-    return;
-}
-
-
-int
-yywrap(void)
-{
-   return 1;
-}
-
diff --git a/src/xkbcomp/xkbscan.c b/src/xkbcomp/xkbscan.c
deleted file mode 100644
index 4f8f9e6..0000000
--- a/src/xkbcomp/xkbscan.c
+++ /dev/null
@@ -1,740 +0,0 @@
-/************************************************************
- Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
-
- Permission to use, copy, modify, and distribute this
- software and its documentation for any purpose and without
- fee is hereby granted, provided that the above copyright
- notice appear in all copies and that both that copyright
- notice and this permission notice appear in supporting
- documentation, and that the name of Silicon Graphics not be
- used in advertising or publicity pertaining to distribution
- of the software without specific prior written permission.
- Silicon Graphics makes no representation about the suitability
- of this software for any purpose. It is provided "as is"
- without any express or implied warranty.
-
- SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
- SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
- GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
- THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
- ********************************************************/
-
-#include <stdio.h>
-#include <ctype.h>
-#include <X11/Xos.h>
-
-#include "tokens.h"
-#include "utils.h"
-#include "parseutils.h"
-
-FILE *yyin = NULL;
-
-static char scanFileBuf[1024] = {0};
-char *scanFile = scanFileBuf;
-int lineNum = 0;
-
-int scanInt;
-
-char scanBuf[1024];
-static int scanStrLine = 0;
-
-#define	BUFSIZE	4096
-static char readBuf[BUFSIZE];
-static int readBufPos = 0;
-static int readBufLen = 0;
-
-#ifdef DEBUG
-extern int debugFlags;
-
-static char *
-tokText(int tok)
-{
-    static char buf[32];
-
-    switch (tok)
-    {
-    case END_OF_FILE:
-        snprintf(buf, sizeof(buf), "END_OF_FILE");
-        break;
-    case ERROR_TOK:
-        snprintf(buf, sizeof(buf), "ERROR");
-        break;
-
-    case XKB_KEYMAP:
-        snprintf(buf, sizeof(buf), "XKB_KEYMAP");
-        break;
-    case XKB_KEYCODES:
-        snprintf(buf, sizeof(buf), "XKB_KEYCODES");
-        break;
-    case XKB_TYPES:
-        snprintf(buf, sizeof(buf), "XKB_TYPES");
-        break;
-    case XKB_SYMBOLS:
-        snprintf(buf, sizeof(buf), "XKB_SYMBOLS");
-        break;
-    case XKB_COMPATMAP:
-        snprintf(buf, sizeof(buf), "XKB_COMPATMAP");
-        break;
-    case XKB_GEOMETRY:
-        snprintf(buf, sizeof(buf), "XKB_GEOMETRY");
-        break;
-    case XKB_SEMANTICS:
-        snprintf(buf, sizeof(buf), "XKB_SEMANTICS");
-        break;
-    case XKB_LAYOUT:
-        snprintf(buf, sizeof(buf), "XKB_LAYOUT");
-        break;
-
-    case INCLUDE:
-        snprintf(buf, sizeof(buf), "INCLUDE");
-        break;
-    case OVERRIDE:
-        snprintf(buf, sizeof(buf), "OVERRIDE");
-        break;
-    case AUGMENT:
-        snprintf(buf, sizeof(buf), "AUGMENT");
-        break;
-    case REPLACE:
-        snprintf(buf, sizeof(buf), "REPLACE");
-        break;
-    case ALTERNATE:
-        snprintf(buf, sizeof(buf), "ALTERNATE");
-        break;
-
-    case VIRTUAL_MODS:
-        snprintf(buf, sizeof(buf), "VIRTUAL_MODS");
-        break;
-    case TYPE:
-        snprintf(buf, sizeof(buf), "TYPE");
-        break;
-    case INTERPRET:
-        snprintf(buf, sizeof(buf), "INTERPRET");
-        break;
-    case ACTION_TOK:
-        snprintf(buf, sizeof(buf), "ACTION");
-        break;
-    case KEY:
-        snprintf(buf, sizeof(buf), "KEY");
-        break;
-    case ALIAS:
-        snprintf(buf, sizeof(buf), "ALIAS");
-        break;
-    case GROUP:
-        snprintf(buf, sizeof(buf), "GROUP");
-        break;
-    case MODIFIER_MAP:
-        snprintf(buf, sizeof(buf), "MODIFIER_MAP");
-        break;
-    case INDICATOR:
-        snprintf(buf, sizeof(buf), "INDICATOR");
-        break;
-    case SHAPE:
-        snprintf(buf, sizeof(buf), "SHAPE");
-        break;
-    case KEYS:
-        snprintf(buf, sizeof(buf), "KEYS");
-        break;
-    case ROW:
-        snprintf(buf, sizeof(buf), "ROW");
-        break;
-    case SECTION:
-        snprintf(buf, sizeof(buf), "SECTION");
-        break;
-    case OVERLAY:
-        snprintf(buf, sizeof(buf), "OVERLAY");
-        break;
-    case TEXT:
-        snprintf(buf, sizeof(buf), "TEXT");
-        break;
-    case OUTLINE:
-        snprintf(buf, sizeof(buf), "OUTLINE");
-        break;
-    case SOLID:
-        snprintf(buf, sizeof(buf), "SOLID");
-        break;
-    case LOGO:
-        snprintf(buf, sizeof(buf), "LOGO");
-        break;
-    case VIRTUAL:
-        snprintf(buf, sizeof(buf), "VIRTUAL");
-        break;
-
-    case EQUALS:
-        snprintf(buf, sizeof(buf), "EQUALS");
-        break;
-    case PLUS:
-        snprintf(buf, sizeof(buf), "PLUS");
-        break;
-    case MINUS:
-        snprintf(buf, sizeof(buf), "MINUS");
-        break;
-    case DIVIDE:
-        snprintf(buf, sizeof(buf), "DIVIDE");
-        break;
-    case TIMES:
-        snprintf(buf, sizeof(buf), "TIMES");
-        break;
-    case OBRACE:
-        snprintf(buf, sizeof(buf), "OBRACE");
-        break;
-    case CBRACE:
-        snprintf(buf, sizeof(buf), "CBRACE");
-        break;
-    case OPAREN:
-        snprintf(buf, sizeof(buf), "OPAREN");
-        break;
-    case CPAREN:
-        snprintf(buf, sizeof(buf), "CPAREN");
-        break;
-    case OBRACKET:
-        snprintf(buf, sizeof(buf), "OBRACKET");
-        break;
-    case CBRACKET:
-        snprintf(buf, sizeof(buf), "CBRACKET");
-        break;
-    case DOT:
-        snprintf(buf, sizeof(buf), "DOT");
-        break;
-    case COMMA:
-        snprintf(buf, sizeof(buf), "COMMA");
-        break;
-    case SEMI:
-        snprintf(buf, sizeof(buf), "SEMI");
-        break;
-    case EXCLAM:
-        snprintf(buf, sizeof(buf), "EXCLAM");
-        break;
-    case INVERT:
-        snprintf(buf, sizeof(buf), "INVERT");
-        break;
-
-    case STRING:
-        snprintf(buf, sizeof(buf), "STRING (%s)", scanBuf);
-        break;
-    case INTEGER:
-        snprintf(buf, sizeof(buf), "INTEGER (0x%x)", scanInt);
-        break;
-    case FLOAT:
-        snprintf(buf, sizeof(buf), "FLOAT (%d.%d)",
-                scanInt / XkbGeomPtsPerMM, scanInt % XkbGeomPtsPerMM);
-        break;
-    case IDENT:
-        snprintf(buf, sizeof(buf), "IDENT (%s)", scanBuf);
-        break;
-    case KEYNAME:
-        snprintf(buf, sizeof(buf), "KEYNAME (%s)", scanBuf);
-        break;
-
-    case PARTIAL:
-        snprintf(buf, sizeof(buf), "PARTIAL");
-        break;
-    case DEFAULT:
-        snprintf(buf, sizeof(buf), "DEFAULT");
-        break;
-    case HIDDEN:
-        snprintf(buf, sizeof(buf), "HIDDEN");
-        break;
-
-    case ALPHANUMERIC_KEYS:
-        snprintf(buf, sizeof(buf), "ALPHANUMERIC_KEYS");
-        break;
-    case MODIFIER_KEYS:
-        snprintf(buf, sizeof(buf), "MODIFIER_KEYS");
-        break;
-    case KEYPAD_KEYS:
-        snprintf(buf, sizeof(buf), "KEYPAD_KEYS");
-        break;
-    case FUNCTION_KEYS:
-        snprintf(buf, sizeof(buf), "FUNCTION_KEYS");
-        break;
-    case ALTERNATE_GROUP:
-        snprintf(buf, sizeof(buf), "ALTERNATE_GROUP");
-        break;
-
-    default:
-        snprintf(buf, sizeof(buf), "UNKNOWN");
-        break;
-    }
-    return buf;
-}
-#endif
-
-static char
-scanchar(void)
-{
-    if (readBufPos >= readBufLen) {
-        readBufLen = fread(readBuf, 1, BUFSIZE, yyin);
-        readBufPos = 0;
-        if (!readBufLen)
-            return EOF;
-        if (feof(yyin))
-            readBuf[readBufLen] = EOF;
-    }
-
-    return readBuf[readBufPos++];
-}
-
-static void
-unscanchar(char c)
-{
-    if (readBuf[--readBufPos] != c) {
-        fprintf(stderr, "UNGETCHAR FAILED! Put back %c, was expecting %c at "
-                        "position %d, buf is '%s'\n", c, readBuf[readBufPos],
-                        readBufPos, readBuf);
-        _exit(94);
-    }
-}
-
-int
-setScanState(char *file, int line)
-{
-    if (file != NULL)
-        strncpy(scanFile, file, 1024);
-    if (line >= 0)
-        lineNum = line;
-    return 1;
-}
-
-static int
-yyGetString(void)
-{
-    int ch, i;
-
-    i = 0;
-    while (((ch = scanchar()) != EOF) && (ch != '"'))
-    {
-        if (ch == '\\')
-        {
-            if ((ch = scanchar()) != EOF)
-            {
-                if (ch == 'n')
-                    ch = '\n';
-                else if (ch == 't')
-                    ch = '\t';
-                else if (ch == 'v')
-                    ch = '\v';
-                else if (ch == 'b')
-                    ch = '\b';
-                else if (ch == 'r')
-                    ch = '\r';
-                else if (ch == 'f')
-                    ch = '\f';
-                else if (ch == 'e')
-                    ch = '\033';
-                else if (ch == '0')
-                {
-                    int tmp, stop;
-                    ch = stop = 0;
-                    if (((tmp = scanchar()) != EOF) && (isdigit(tmp))
-                        && (tmp != '8') && (tmp != '9'))
-                    {
-                        ch = (ch * 8) + (tmp - '0');
-                    }
-                    else
-                    {
-                        stop = 1;
-                        unscanchar(tmp);
-                    }
-                    if (!stop)
-                    {
-                        if (((tmp = scanchar()) != EOF)
-                            && (isdigit(tmp)) && (tmp != '8') && (tmp != '9'))
-                        {
-                            ch = (ch * 8) + (tmp - '0');
-                        }
-                        else
-                        {
-                            stop = 1;
-                            unscanchar(tmp);
-                        }
-                    }
-                    if (!stop)
-                    {
-                        if (((tmp = scanchar()) != EOF)
-                            && (isdigit(tmp)) && (tmp != '8') && (tmp != '9'))
-                        {
-                            ch = (ch * 8) + (tmp - '0');
-                        }
-                        else
-                        {
-                            stop = 1;
-                            unscanchar(tmp);
-                        }
-                    }
-                }
-            }
-            else
-                return ERROR_TOK;
-        }
-        if (i < sizeof(scanBuf) - 1)
-            scanBuf[i++] = ch;
-    }
-    if (ch == '"')
-    {
-        scanBuf[i++] = '\0';
-        scanStrLine = lineNum;
-        return STRING;
-    }
-    return ERROR_TOK;
-}
-
-static int
-yyGetKeyName(void)
-{
-    int ch, i;
-
-    i = 0;
-    while (((ch = scanchar()) != EOF) && (ch != '>'))
-    {
-        if (ch == '\\')
-        {
-            if ((ch = scanchar()) != EOF)
-            {
-                if (ch == 'n')
-                    ch = '\n';
-                else if (ch == 't')
-                    ch = '\t';
-                else if (ch == 'v')
-                    ch = '\v';
-                else if (ch == 'b')
-                    ch = '\b';
-                else if (ch == 'r')
-                    ch = '\r';
-                else if (ch == 'f')
-                    ch = '\f';
-                else if (ch == 'e')
-                    ch = '\033';
-                else if (ch == '0')
-                {
-                    int tmp, stop;
-                    ch = stop = 0;
-                    if (((tmp = scanchar()) != EOF) && (isdigit(tmp))
-                        && (tmp != '8') && (tmp != '9'))
-                    {
-                        ch = (ch * 8) + (tmp - '0');
-                    }
-                    else
-                    {
-                        stop = 1;
-                        unscanchar(tmp);
-                    }
-                    if ((!stop) && ((tmp = scanchar()) != EOF)
-                        && (isdigit(tmp)) && (tmp != '8') && (tmp != '9'))
-                    {
-                        ch = (ch * 8) + (tmp - '0');
-                    }
-                    else
-                    {
-                        stop = 1;
-                        unscanchar(tmp);
-                    }
-                    if ((!stop) && ((tmp = scanchar()) != EOF)
-                        && (isdigit(tmp)) && (tmp != '8') && (tmp != '9'))
-                    {
-                        ch = (ch * 8) + (tmp - '0');
-                    }
-                    else
-                    {
-                        stop = 1;
-                        unscanchar(tmp);
-                    }
-                }
-            }
-            else
-                return ERROR_TOK;
-        }
-
-        if (i < sizeof(scanBuf) - 1)
-            scanBuf[i++] = ch;
-    }
-    if ((ch == '>') && (i < 5))
-    {
-        scanBuf[i++] = '\0';
-        scanStrLine = lineNum;
-        return KEYNAME;
-    }
-    return ERROR_TOK;
-}
-
-static struct _Keyword
-{
-    const char *keyword;
-    int token;
-} keywords[] =
-{
-    {
-    "xkb_keymap", XKB_KEYMAP},
-    {
-    "xkb_keycodes", XKB_KEYCODES},
-    {
-    "xkb_types", XKB_TYPES},
-    {
-    "xkb_symbols", XKB_SYMBOLS},
-    {
-    "xkb_compat", XKB_COMPATMAP},
-    {
-    "xkb_compat_map", XKB_COMPATMAP},
-    {
-    "xkb_compatibility", XKB_COMPATMAP},
-    {
-    "xkb_compatibility_map", XKB_COMPATMAP},
-    {
-    "xkb_geometry", XKB_GEOMETRY},
-    {
-    "xkb_semantics", XKB_SEMANTICS},
-    {
-    "xkb_layout", XKB_LAYOUT},
-    {
-    "include", INCLUDE},
-    {
-    "override", OVERRIDE},
-    {
-    "augment", AUGMENT},
-    {
-    "replace", REPLACE},
-    {
-    "alternate", ALTERNATE},
-    {
-    "partial", PARTIAL},
-    {
-    "default", DEFAULT},
-    {
-    "hidden", HIDDEN},
-    {
-    "virtual_modifiers", VIRTUAL_MODS},
-    {
-    "type", TYPE},
-    {
-    "interpret", INTERPRET},
-    {
-    "action", ACTION_TOK},
-    {
-    "key", KEY},
-    {
-    "alias", ALIAS},
-    {
-    "group", GROUP},
-    {
-    "modmap", MODIFIER_MAP},
-    {
-    "mod_map", MODIFIER_MAP},
-    {
-    "modifier_map", MODIFIER_MAP},
-    {
-    "indicator", INDICATOR},
-    {
-    "shape", SHAPE},
-    {
-    "row", ROW},
-    {
-    "keys", KEYS},
-    {
-    "section", SECTION},
-    {
-    "overlay", OVERLAY},
-    {
-    "text", TEXT},
-    {
-    "outline", OUTLINE},
-    {
-    "solid", SOLID},
-    {
-    "logo", LOGO},
-    {
-    "virtual", VIRTUAL},
-    {
-    "alphanumeric_keys", ALPHANUMERIC_KEYS},
-    {
-    "modifier_keys", MODIFIER_KEYS},
-    {
-    "keypad_keys", KEYPAD_KEYS},
-    {
-    "function_keys", FUNCTION_KEYS},
-    {
-    "alternate_group", ALTERNATE_GROUP}
-};
-static int numKeywords = sizeof(keywords) / sizeof(struct _Keyword);
-
-static int
-yyGetIdent(int first)
-{
-    int ch, i, j, found;
-    int rtrn = IDENT;
-
-    scanBuf[0] = first;
-    j = 1;
-    while (((ch = scanchar()) != EOF) && (isalnum(ch) || (ch == '_')))
-    {
-        if (j < sizeof(scanBuf) - 1)
-            scanBuf[j++] = ch;
-    }
-    scanBuf[j++] = '\0';
-    found = 0;
-
-    for (i = 0; (!found) && (i < numKeywords); i++)
-    {
-        if (uStrCaseCmp(scanBuf, keywords[i].keyword) == 0)
-        {
-            rtrn = keywords[i].token;
-            found = 1;
-        }
-    }
-    if (!found)
-    {
-        scanStrLine = lineNum;
-        rtrn = IDENT;
-    }
-
-    if ((ch != EOF) && (!isspace(ch)))
-        unscanchar(ch);
-    else if (ch == '\n')
-        lineNum++;
-
-    return rtrn;
-}
-
-static int
-yyGetNumber(int ch)
-{
-    int isFloat = 0;
-    char buf[1024];
-    int nInBuf = 0;
-
-    buf[0] = ch;
-    nInBuf = 1;
-    while (((ch = scanchar()) != EOF)
-           && (isxdigit(ch) || ((nInBuf == 1) && (ch == 'x'))))
-    {
-        buf[nInBuf++] = ch;
-    }
-    if (ch == '.')
-    {
-        isFloat = 1;
-        buf[nInBuf++] = ch;
-        while (((ch = scanchar()) != EOF) && (isxdigit(ch)))
-        {
-            buf[nInBuf++] = ch;
-        }
-    }
-    buf[nInBuf++] = '\0';
-    if ((ch != EOF) && (!isspace(ch)))
-        unscanchar(ch);
-
-    if (isFloat)
-    {
-        float tmp;
-        if (sscanf(buf, "%g", &tmp) == 1)
-        {
-            scanInt = tmp * XkbGeomPtsPerMM;
-            return FLOAT;
-        }
-    }
-    else if (sscanf(buf, "%i", &scanInt) == 1)
-        return INTEGER;
-    fprintf(stderr, "Malformed number %s\n", buf);
-    return ERROR_TOK;
-}
-
-int
-yylex(void)
-{
-    int ch;
-    int rtrn;
-
-    do
-    {
-        ch = scanchar();
-        if (ch == '\n')
-        {
-            lineNum++;
-        }
-        else if (ch == '#')
-        {                       /* handle shell style '#' comments */
-            do
-            {
-                ch = scanchar();
-            }
-            while ((ch != '\n') && (ch != EOF));
-            lineNum++;
-        }
-        else if (ch == '/')
-        {                       /* handle C++ style double-/ comments */
-            int newch = scanchar();
-            if (newch == '/')
-            {
-                do
-                {
-                    ch = scanchar();
-                }
-                while ((ch != '\n') && (ch != EOF));
-                lineNum++;
-            }
-            else if (newch != EOF)
-            {
-                unscanchar(newch);
-            }
-        }
-    }
-    while ((ch != EOF) && (isspace(ch)));
-    if (ch == '=')
-        rtrn = EQUALS;
-    else if (ch == '+')
-        rtrn = PLUS;
-    else if (ch == '-')
-        rtrn = MINUS;
-    else if (ch == '/')
-        rtrn = DIVIDE;
-    else if (ch == '*')
-        rtrn = TIMES;
-    else if (ch == '{')
-        rtrn = OBRACE;
-    else if (ch == '}')
-        rtrn = CBRACE;
-    else if (ch == '(')
-        rtrn = OPAREN;
-    else if (ch == ')')
-        rtrn = CPAREN;
-    else if (ch == '[')
-        rtrn = OBRACKET;
-    else if (ch == ']')
-        rtrn = CBRACKET;
-    else if (ch == '.')
-        rtrn = DOT;
-    else if (ch == ',')
-        rtrn = COMMA;
-    else if (ch == ';')
-        rtrn = SEMI;
-    else if (ch == '!')
-        rtrn = EXCLAM;
-    else if (ch == '~')
-        rtrn = INVERT;
-    else if (ch == '"')
-        rtrn = yyGetString();
-    else if (ch == '<')
-        rtrn = yyGetKeyName();
-    else if (isalpha(ch) || (ch == '_'))
-        rtrn = yyGetIdent(ch);
-    else if (isdigit(ch))
-        rtrn = yyGetNumber(ch);
-    else if (ch == EOF)
-        rtrn = END_OF_FILE;
-    else
-    {
-#ifdef DEBUG
-        if (debugFlags)
-            fprintf(stderr,
-                    "Unexpected character %c (%d) in input stream\n", ch, ch);
-#endif
-        rtrn = ERROR_TOK;
-    }
-#ifdef DEBUG
-    if (debugFlags & 0x2)
-        fprintf(stderr, "scan: %s\n", tokText(rtrn));
-#endif
-    return rtrn;
-}
diff --git a/src/xkbcomp/xkbscan.l b/src/xkbcomp/xkbscan.l
new file mode 100644
index 0000000..0d3cab6
--- /dev/null
+++ b/src/xkbcomp/xkbscan.l
@@ -0,0 +1,245 @@
+/************************************************************
+ Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
+
+ Permission to use, copy, modify, and distribute this
+ software and its documentation for any purpose and without
+ fee is hereby granted, provided that the above copyright
+ notice appear in all copies and that both that copyright
+ notice and this permission notice appear in supporting
+ documentation, and that the name of Silicon Graphics not be
+ used in advertising or publicity pertaining to distribution
+ of the software without specific prior written permission.
+ Silicon Graphics makes no representation about the suitability
+ of this software for any purpose. It is provided "as is"
+ without any express or implied warranty.
+
+ SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
+ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
+ THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+ ********************************************************/
+
+%{
+
+#include <stdio.h>
+#include <ctype.h>
+#include <X11/Xos.h>
+
+#include "tokens.h"
+#include "utils.h"
+#include "parseutils.h"
+
+const char *yystring;
+static char scanFileBuf[1024] = {0};
+char *scanFile = scanFileBuf;
+int lineNum = 0;
+
+int scanInt;
+
+static char *s;
+char scanBuf[1024];
+
+#define	BUFSIZE	4096
+
+%}
+
+%option case-insensitive
+%option yylineno
+%option noyywrap
+
+%x S_STR S_KEY
+
+%%
+
+"//"[^\n]*
+"#"[^\n]*
+
+\"			s = scanBuf; BEGIN(S_STR);
+\<			s = scanBuf; BEGIN(S_KEY);
+
+<S_STR>\"		BEGIN(INITIAL); *s = '\0'; return STRING;
+<S_KEY>\>		BEGIN(INITIAL); *s = '\0'; return KEYNAME;
+
+<S_STR,S_KEY>\\[0-7]{1,3} {
+			/* octal escape sequence */
+			int result;
+
+			(void) sscanf( yytext + 1, "%o", &result );
+
+			if (result > 0xff) {
+			    fprintf(stderr, "Illegal octal escape %s\n", yytext);
+			    return ERROR_TOK;
+			}
+
+			*s++ = result;
+		    }
+
+<S_STR,S_KEY>\\[0-9]+ {
+			fprintf(stderr, "Illegal octal escape %s\n", yytext);
+			return ERROR_TOK;
+		    }
+
+<S_STR,S_KEY>\\n	*s++ = '\n';
+<S_STR,S_KEY>\\t	*s++ = '\t';
+<S_STR,S_KEY>\\r	*s++ = '\r';
+<S_STR,S_KEY>\\b	*s++ = '\b';
+<S_STR,S_KEY>\\f	*s++ = '\f';
+<S_STR,S_KEY>\\v	*s++ = '\v';
+<S_STR,S_KEY>\\e	*s++ = '\033';
+
+<S_STR,S_KEY>.		*s++ = yytext[0];
+
+xkb_keymap		return XKB_KEYMAP;
+xkb_keycodes		return XKB_KEYCODES;
+xkb_types		return XKB_TYPES;
+xkb_symbols		return XKB_SYMBOLS;
+xkb_compat		return XKB_COMPATMAP;
+xkb_compat_map		return XKB_COMPATMAP;
+xkb_compatibility	return XKB_COMPATMAP;
+xkb_compatibility_map	return XKB_COMPATMAP;
+xkb_geometry		return XKB_GEOMETRY;
+xkb_semantics		return XKB_SEMANTICS;
+xkb_layout		return XKB_LAYOUT;
+include			return INCLUDE;
+override		return OVERRIDE;
+augment			return AUGMENT;
+replace			return REPLACE;
+alternate		return ALTERNATE;
+partial			return PARTIAL;
+default			return DEFAULT;
+hidden			return HIDDEN;
+virtual_modifiers	return VIRTUAL_MODS;
+type			return TYPE;
+interpret		return INTERPRET;
+action			return ACTION_TOK;
+key			return KEY;
+alias			return ALIAS;
+group			return GROUP;
+modmap			return MODIFIER_MAP;
+mod_map			return MODIFIER_MAP;
+modifier_map		return MODIFIER_MAP;
+indicator		return INDICATOR;
+shape			return SHAPE;
+row			return ROW;
+keys			return KEYS;
+section			return SECTION;
+overlay			return OVERLAY;
+text			return TEXT;
+outline			return OUTLINE;
+solid			return SOLID;
+logo			return LOGO;
+virtual			return VIRTUAL;
+alphanumeric_keys	return ALPHANUMERIC_KEYS;
+modifier_keys		return MODIFIER_KEYS;
+keypad_keys		return KEYPAD_KEYS;
+function_keys		return FUNCTION_KEYS;
+alternate_group		return ALTERNATE_GROUP;
+
+[a-zA-Z_][a-zA-Z_0-9]*	memcpy(scanBuf, yytext, yyleng + 1); return IDENT;
+
+0x[a-fA-F0-9]+		|
+[0-9]+			{
+			    char *end;
+			    scanInt = strtol(yytext, &end, 0);
+
+			    return INTEGER;
+			}
+[0-9]+\.[0-9]+ {
+			    char *end;
+			    scanInt = strtod(yytext, &end) * XkbGeomPtsPerMM;
+
+			    return FLOAT;
+			}
+
+"="			return EQUALS;
+"+"			return PLUS;
+"-"			return MINUS;
+"/"			return DIVIDE;
+"*"			return TIMES;
+"{"			return OBRACE;
+"}"			return CBRACE;
+"("			return OPAREN;
+")"			return CPAREN;
+"["			return OBRACKET;
+"]"			return CBRACKET;
+"."			return DOT;
+","			return COMMA;
+";"			return SEMI;
+"!"			return EXCLAM;
+"~"			return INVERT;
+
+[ \t\r\n\v]+		
+
+<<EOF>>			return END_OF_FILE;
+
+.			return ERROR_TOK;
+
+%%
+
+void
+yyerror(const char *s)
+{
+    if (warningLevel>0) {
+	(void)fprintf(stderr,"%s: line %d of %s\n",s,yylineno,
+					(scanFile?scanFile:"(unknown)"));
+	if ((warningLevel>3))
+	    (void)fprintf(stderr,"last scanned symbol is: %s\n",scanBuf);
+    }
+    return;
+}
+
+int setScanState(char *file, int lineno)
+{
+  yylineno = 1;
+  scanFile = file;
+}
+
+int
+XKBParseString(const char *string, XkbFile ** pRtrn)
+{
+    YY_BUFFER_STATE state;
+
+    *pRtrn = NULL;
+    if (string == NULL)
+	return 1;
+
+    state = yy_scan_string(string);
+    rtrnValue = NULL;
+    if (yyparse() != 0)
+	return 0;
+
+    yy_delete_buffer(state);
+    yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
+    *pRtrn = rtrnValue;
+    CheckDefaultMap(rtrnValue);
+    rtrnValue = NULL;
+
+    return 1;
+}
+
+int
+XKBParseFile(FILE * file, XkbFile ** pRtrn)
+{
+    if (file)
+    {
+        yyin = file;
+	yystring = NULL;
+        rtrnValue = NULL;
+        if (yyparse() == 0)
+        {
+            *pRtrn = rtrnValue;
+            CheckDefaultMap(rtrnValue);
+            rtrnValue = NULL;
+            return 1;
+        }
+        *pRtrn = NULL;
+        return 0;
+    }
+    *pRtrn = NULL;
+    return 1;
+}
diff --git a/test/filecomp.c b/test/filecomp.c
index 0ff41f9..d256d72 100644
--- a/test/filecomp.c
+++ b/test/filecomp.c
@@ -33,22 +33,31 @@ authorization from the authors.
 #include "X11/extensions/XKBcommon.h"
 #include "xkbcomp/utils.h"
 
+static char buffer[8192];
+
 int main(int argc, char *argv[])
 {
     char *path, *name;
     FILE *file;
     struct xkb_desc * xkb;
+    int i, len, from_string = 0;
 
     /* Require xkb file */
     if (argc < 2) {
         fprintf(stderr, "Not enough arguments\n");
-        fprintf(stderr, "Usage: %s XKBFILE [NAME]\n",
+        fprintf(stderr, "Usage: %s [-s] XKBFILE [NAME]\n",
                 argv[0]);
         exit(1);
     }
 
-    path = argv[1];
-    name = (argc > 2) ? argv[2] : NULL;
+    i = 1;
+    if (strcmp(argv[1], "-s") == 0) {
+	from_string = 2;
+	i++;
+    }
+
+    path = argv[i];
+    name = (argc > i + 1) ? argv[i + 1] : NULL;
 
     file = fopen(path, "r");
     if (!file) {
@@ -57,7 +66,13 @@ int main(int argc, char *argv[])
         exit(1);
     }
 
-    xkb = xkb_compile_keymap_from_file(file, name);
+    if (from_string) {
+	len = fread(buffer, 1, sizeof buffer, file);
+	buffer[len] = '\0';
+	xkb = xkb_compile_keymap_from_string(buffer, name);
+    } else {
+	xkb = xkb_compile_keymap_from_file(file, name);
+    }
     fclose(file);
 
     if (!xkb) {