]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* read in the grammar specification and record it in the format described in gram.h.
22 All guards are copied into the fguard file and all actions into faction,
23 in each case forming the body of a C function (yyguard or yyaction)
24 which contains a switch statement to decide which guard or action to execute.
26 The entry point is reader(). */
38 #define LTYPESTR "\n#ifndef YYLTYPE\ntypedef\n struct yyltype\n\
39 {\n int timestamp;\n int first_line;\n int first_column;\
40 \n int last_line;\n int last_column;\n char *text;\n }\n\
41 yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
43 /* Number of slots allocated (but not necessarily used yet) in `rline' */
46 extern char *program_name
;
47 extern int definesflag
;
48 extern int nolinesflag
;
49 extern bucket
*symval
;
52 extern int expected_conflicts
;
53 extern char *token_buffer
;
55 extern void init_lex();
56 extern void tabinit();
57 extern void output_headers();
58 extern void output_trailers();
59 extern void free_symtab();
60 extern void open_extra_files();
66 extern int skip_white_space();
67 extern int parse_percent_token();
70 void read_declarations();
71 void copy_definition();
72 void parse_token_decl();
73 void parse_start_decl();
74 void parse_type_decl();
75 void parse_assoc_decl();
76 void parse_union_decl();
77 void parse_expect_decl();
80 void record_rule_line();
82 void output_token_defines();
84 int read_signed_integer();
90 struct symbol_list
*next
;
104 /* Nonzero if components of semantic values are used, implying
105 they must be unions. */
106 static int value_components_used
;
108 static int typed
; /* nonzero if %union has been seen. */
110 static int lastprec
; /* incremented for each %left, %right or %nonassoc seen */
112 static int gensym_count
; /* incremented for each generated symbol */
114 static bucket
*errtoken
;
116 /* Nonzero if any action or guard uses the @n construct. */
117 static int yylsp_needed
;
119 extern char *version_string
;
125 startval
= NULL
; /* start symbol not specified yet. */
128 translations
= 0; /* initially assume token number translation not needed. */
130 /* Nowadays translations is always set to 1,
131 since we give `error' a user-token-number
132 to satisfy the Posix demand for YYERRCODE==256. */
139 rline_allocated
= 10;
140 rline
= NEW2(rline_allocated
, short);
156 /* initialize the symbol table. */
158 /* construct the error token */
159 errtoken
= getsym("error");
160 errtoken
->class = STOKEN
;
161 errtoken
->user_token_number
= 256; /* Value specified by posix. */
162 /* construct a token that represents all undefined literal tokens. */
163 /* it is always token number 2. */
164 getsym("$undefined.")->class = STOKEN
;
165 /* Read the declaration section. Copy %{ ... %} groups to ftable and fdefines file.
166 Also notice any %token, %left, etc. found there. */
167 fprintf(ftable
, "\n/* A Bison parser, made from %s", infile
);
168 fprintf(ftable
, " with Bison version %s */\n\n", version_string
);
169 fprintf(ftable
, "#define YYBISON 1 /* Identify Bison output. */\n\n");
171 /* output the definition of YYLTYPE into the fattrs and fdefines files. */
172 /* fattrs winds up in the .tab.c file, before bison.simple. */
173 fprintf(fattrs
, LTYPESTR
);
174 /* start writing the guard and action files, if they are needed. */
176 /* read in the grammar, build grammar in list form. write out guards and actions. */
178 /* Now we know whether we need the line-number stack.
179 If we do, write its type into the .tab.h file. */
183 fprintf(fdefines
, LTYPESTR
);
185 /* write closing delimiters for actions and guards. */
188 fprintf(ftable
, "#define YYLSP_NEEDED\n\n");
189 /* assign the symbols their symbol numbers.
190 Write #defines for the token symbols into fdefines if requested. */
192 /* convert the grammar into the format described in gram.h. */
194 /* free the symbol table data structure
195 since symbols are now all referred to by symbol number. */
201 /* read from finput until %% is seen. Discard the %%.
202 Handle any % declarations,
203 and copy the contents of any %{ ... %} groups to fattrs. */
213 c
= skip_white_space();
217 tok
= parse_percent_token();
224 case PERCENT_LEFT_CURLY
:
229 parse_token_decl (STOKEN
, SNTERM
);
233 parse_token_decl (SNTERM
, STOKEN
);
253 parse_assoc_decl(LEFT_ASSOC
);
257 parse_assoc_decl(RIGHT_ASSOC
);
261 parse_assoc_decl(NON_ASSOC
);
264 case SEMANTIC_PARSER
:
265 if (semantic_parser
== 0)
277 fatal("junk after `%%' in definition section");
281 fatal("no input grammar");
282 else if (c
>= 040 && c
<= 0177)
283 fatals ("unknown character `%c' in declaration section", c
);
285 fatals ("unknown character with code 0x%x in declaration section", c
);
290 /* copy the contents of a %{ ... %} into the definitions file.
291 The %{ has already been read. Return after reading the %}. */
299 register int after_percent
; /* -1 while reading a character if prev char was % */
303 fprintf(fattrs
, "#line %d \"%s\"\n", lineno
, infile
);
330 if (c
== EOF
|| c
== '\n')
331 fatal("unterminated string");
339 fatal("unterminated string");
354 if (c
!= '*' && c
!= '/')
357 cplus_comment
= (c
== '/');
364 if (!cplus_comment
&& c
== '*')
388 fatal("unterminated comment in `%{' definition");
399 fatal("unterminated `%{' definition");
421 /* parse what comes after %token or %nterm.
422 For %token, what_is is STOKEN and what_is_not is SNTERM.
423 For %nterm, the arguments are reversed. */
426 parse_token_decl (what_is
, what_is_not
)
427 int what_is
, what_is_not
;
429 /* register int start_lineno; JF */
430 register int token
= 0;
432 register char *typename
= 0;
435 /* start_lineno = lineno; JF */
439 if(ungetc(skip_white_space(), finput
) == '%')
442 /* if (lineno != start_lineno)
445 /* we have not passed a newline, so the token now starting is in this declaration */
451 if (token
== TYPENAME
)
453 k
= strlen(token_buffer
);
454 typename
= NEW2(k
+ 1, char);
455 strcpy(typename
, token_buffer
);
456 value_components_used
= 1;
458 else if (token
== IDENTIFIER
)
460 int oldclass
= symval
->class;
462 if (symval
->class == what_is_not
)
463 fatals("symbol %s redefined", symval
->tag
);
464 symval
->class = what_is
;
465 if (what_is
== SNTERM
&& oldclass
!= SNTERM
)
466 symval
->value
= nvars
++;
470 if (symval
->type_name
== NULL
)
471 symval
->type_name
= typename
;
473 fatals("type redeclaration for %s", symval
->tag
);
476 else if (prev
== IDENTIFIER
&& token
== NUMBER
)
478 symval
->user_token_number
= numval
;
482 fatal("invalid text in %token or %nterm declaration");
489 /* parse what comes after %start */
495 fatal("multiple %start declarations");
497 if (lex() != IDENTIFIER
)
498 fatal("invalid %start declaration");
504 /* read in a %type declaration and record its information for get_type_name to access */
511 /* register int start_lineno; JF */
513 if (lex() != TYPENAME
)
514 fatal("ill-formed %type declaration");
516 k
= strlen(token_buffer
);
517 name
= NEW2(k
+ 1, char);
518 strcpy(name
, token_buffer
);
520 /* start_lineno = lineno; */
526 if(ungetc(skip_white_space(), finput
) == '%')
529 /* if (lineno != start_lineno)
532 /* we have not passed a newline, so the token now starting is in this declaration */
544 if (symval
->type_name
== NULL
)
545 symval
->type_name
= name
;
547 fatals("type redeclaration for %s", symval
->tag
);
552 fatal("invalid %type declaration");
559 /* read in a %left, %right or %nonassoc declaration and record its information. */
560 /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC. */
563 parse_assoc_decl (assoc
)
567 register char *name
= NULL
;
568 /* register int start_lineno; JF */
569 register int prev
= 0; /* JF added = 0 to keep lint happy */
571 lastprec
++; /* Assign a new precedence level, never 0. */
573 /* start_lineno = lineno; */
579 if(ungetc(skip_white_space(), finput
) == '%')
582 /* if (lineno != start_lineno)
585 /* we have not passed a newline, so the token now starting is in this declaration */
593 k
= strlen(token_buffer
);
594 name
= NEW2(k
+ 1, char);
595 strcpy(name
, token_buffer
);
602 if (symval
->prec
!= 0)
603 fatals("redefining precedence of %s", symval
->tag
);
604 symval
->prec
= lastprec
;
605 symval
->assoc
= assoc
;
606 if (symval
->class == SNTERM
)
607 fatals("symbol %s redefined", symval
->tag
);
608 symval
->class = STOKEN
;
610 { /* record the type, if one is specified */
611 if (symval
->type_name
== NULL
)
612 symval
->type_name
= name
;
614 fatals("type redeclaration for %s", symval
->tag
);
619 if (prev
== IDENTIFIER
)
621 symval
->user_token_number
= numval
;
625 fatal("invalid text in association declaration");
632 fatal("malformatted association declaration");
642 /* copy the union declaration into fattrs (and fdefines),
643 where it is made into the
644 definition of YYSTYPE, the type of elements of the parser value stack. */
651 register int in_comment
;
655 fatal("multiple %union declarations");
660 fprintf(fattrs
, "\n#line %d \"%s\"\n", lineno
, infile
);
662 fprintf(fattrs
, "\n");
664 fprintf(fattrs
, "typedef union");
666 fprintf(fdefines
, "typedef union");
687 if (c
!= '*' && c
!= '/')
694 cplus_comment
= (c
== '/');
713 fatal("unterminated comment");
715 if (!cplus_comment
&& c
== '*')
739 fatal ("unmatched close-brace (`}')");
743 fprintf(fattrs
, " YYSTYPE;\n");
745 fprintf(fdefines
, " YYSTYPE;\n");
746 /* JF don't choke on trailing semi */
747 c
=skip_white_space();
748 if(c
!=';') ungetc(c
,finput
);
757 /* parse the declaration %expect N which says to expect N
758 shift-reduce conflicts. */
768 while (c
== ' ' || c
== '\t')
772 while (c
>= '0' && c
<= '9')
782 expected_conflicts
= atoi (buffer
);
785 /* that's all of parsing the declaration section */
787 /* Get the data type (alternative in the union) of the value for symbol n in rule rule. */
790 get_type_name(n
, rule
)
794 static char *msg
= "invalid $ value";
797 register symbol_list
*rp
;
808 if (rp
== NULL
|| rp
->sym
== NULL
)
813 return (rp
->sym
->type_name
);
818 /* after %guard is seen in the input file,
819 copy the actual guard into the guards file.
820 If the guard is followed by an action, copy that into the actions file.
821 stack_offset is the number of values in the current rule so far,
822 which says where to find $0 with respect to the top of the stack,
823 for the simple parser in which the stack is not popped until after the guard is run. */
826 copy_guard(rule
, stack_offset
)
835 register char *type_name
;
839 /* offset is always 0 if parser has already popped the stack pointer */
840 if (semantic_parser
) stack_offset
= 0;
842 fprintf(fguard
, "\ncase %d:\n", nrules
);
844 fprintf(fguard
, "#line %d \"%s\"\n", lineno
, infile
);
850 while (brace_flag
? (count
> 0) : (c
!= ';'))
870 fatal("unmatched right brace ('}')");
881 if (c
== EOF
|| c
== '\n')
882 fatal("unterminated string");
890 fatal("unterminated string");
905 if (c
!= '*' && c
!= '/')
908 cplus_comment
= (c
== '/');
915 if (!cplus_comment
&& c
== '*')
939 fatal("unterminated comment");
955 register char *cp
= token_buffer
;
957 while ((c
= getc(finput
)) != '>' && c
> 0)
960 type_name
= token_buffer
;
967 fprintf(fguard
, "yyval");
968 if (!type_name
) type_name
= rule
->sym
->type_name
;
970 fprintf(fguard
, ".%s", type_name
);
971 if(!type_name
&& typed
) /* JF */
972 fprintf(stderr
,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile
,lineno
,rule
->sym
->tag
);
975 else if (isdigit(c
) || c
== '-')
978 n
= read_signed_integer(finput
);
981 if (!type_name
&& n
> 0)
982 type_name
= get_type_name(n
, rule
);
984 fprintf(fguard
, "yyvsp[%d]", n
- stack_offset
);
986 fprintf(fguard
, ".%s", type_name
);
987 if(!type_name
&& typed
) /* JF */
988 fprintf(stderr
,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile
,lineno
,n
,rule
->sym
->tag
);
992 fatals("$%c is invalid",c
); /* JF changed style */
998 if (isdigit(c
) || c
== '-')
1001 n
= read_signed_integer(finput
);
1005 fatals("@%c is invalid",c
); /* JF changed style */
1007 fprintf(fguard
, "yylsp[%d]", n
- stack_offset
);
1013 fatal("unterminated %guard clause");
1019 if (c
!= '}' || count
!= 0)
1023 c
= skip_white_space();
1025 fprintf(fguard
, ";\n break;}");
1027 copy_action(rule
, stack_offset
);
1032 copy_action(rule
, stack_offset
);
1040 /* Assuming that a { has just been seen, copy everything up to the matching }
1041 into the actions file.
1042 stack_offset is the number of values in the current rule so far,
1043 which says where to find $0 with respect to the top of the stack. */
1046 copy_action(rule
, stack_offset
)
1055 register char *type_name
;
1058 /* offset is always 0 if parser has already popped the stack pointer */
1059 if (semantic_parser
) stack_offset
= 0;
1061 fprintf(faction
, "\ncase %d:\n", nrules
);
1063 fprintf(faction
, "#line %d \"%s\"\n", lineno
, infile
);
1093 if (c
== EOF
|| c
== '\n')
1094 fatal("unterminated string");
1102 fatal("unterminated string");
1117 if (c
!= '*' && c
!= '/')
1120 cplus_comment
= (c
== '/');
1127 if (!cplus_comment
&& c
== '*')
1151 fatal("unterminated comment");
1167 register char *cp
= token_buffer
;
1169 while ((c
= getc(finput
)) != '>' && c
> 0)
1172 type_name
= token_buffer
;
1173 value_components_used
= 1;
1179 fprintf(faction
, "yyval");
1180 if (!type_name
) type_name
= get_type_name(0, rule
);
1182 fprintf(faction
, ".%s", type_name
);
1183 if(!type_name
&& typed
) /* JF */
1184 fprintf(stderr
,"%s:%d: warning: $$ of '%s' has no declared type.\n",infile
,lineno
,rule
->sym
->tag
);
1186 else if (isdigit(c
) || c
== '-')
1189 n
= read_signed_integer(finput
);
1192 if (!type_name
&& n
> 0)
1193 type_name
= get_type_name(n
, rule
);
1195 fprintf(faction
, "yyvsp[%d]", n
- stack_offset
);
1197 fprintf(faction
, ".%s", type_name
);
1198 if(!type_name
&& typed
) /* JF */
1199 fprintf(stderr
,"%s:%d: warning: $%d of '%s' has no declared type.\n",infile
,lineno
,n
,rule
->sym
->tag
);
1203 fatals("$%c is invalid",c
); /* JF changed format */
1209 if (isdigit(c
) || c
== '-')
1212 n
= read_signed_integer(finput
);
1216 fatal("invalid @-construct");
1218 fprintf(faction
, "yylsp[%d]", n
- stack_offset
);
1224 fatal("unmatched '{'");
1233 /* above loop exits when c is '}' */
1242 fprintf(faction
, ";\n break;}");
1247 /* generate a dummy symbol, a nonterminal,
1248 whose name cannot conflict with the user's names. */
1253 register bucket
*sym
;
1255 sprintf (token_buffer
, "@%d", ++gensym_count
);
1256 sym
= getsym(token_buffer
);
1257 sym
->class = SNTERM
;
1258 sym
->value
= nvars
++;
1262 /* Parse the input grammar into a one symbol_list structure.
1263 Each rule is represented by a sequence of symbols: the left hand side
1264 followed by the contents of the right hand side, followed by a null pointer
1265 instead of a symbol to terminate the rule.
1266 The next symbol is the lhs of the following rule.
1268 All guards and actions are copied out to the appropriate files,
1269 labelled by the rule number they apply to. */
1275 register bucket
*lhs
;
1276 register symbol_list
*p
;
1277 register symbol_list
*p1
;
1278 register bucket
*bp
;
1280 symbol_list
*crule
; /* points to first symbol_list of current rule. */
1281 /* its symbol is the lhs of the rule. */
1282 symbol_list
*crule1
; /* points to the symbol_list preceding crule. */
1288 while (t
!= TWO_PERCENTS
&& t
!= ENDFILE
)
1290 if (t
== IDENTIFIER
|| t
== BAR
)
1292 register int actionflag
= 0;
1293 int rulelength
= 0; /* number of symbols in rhs of this rule so far */
1294 int xactions
= 0; /* JF for error checking */
1295 bucket
*first_rhs
= 0;
1297 if (t
== IDENTIFIER
)
1303 fatal("ill-formed rule");
1309 fatal("grammar starts with vertical bar");
1315 /* start a new rule and record its lhs. */
1320 record_rule_line ();
1322 p
= NEW(symbol_list
);
1334 /* mark the rule's lhs as a nonterminal if not already so. */
1336 if (lhs
->class == SUNKNOWN
)
1338 lhs
->class = SNTERM
;
1342 else if (lhs
->class == STOKEN
)
1343 fatals("rule given for %s, which is a token", lhs
->tag
);
1345 /* read the rhs of the rule. */
1351 if (! (t
== IDENTIFIER
|| t
== LEFT_CURLY
)) break;
1353 /* If next token is an identifier, see if a colon follows it.
1354 If one does, exit this rule now. */
1355 if (t
== IDENTIFIER
)
1357 register bucket
*ssave
;
1364 if (t1
== COLON
) break;
1366 if(!first_rhs
) /* JF */
1368 /* Not followed by colon =>
1369 process as part of this rule's rhs. */
1372 /* If we just passed an action, that action was in the middle
1373 of a rule, so make a dummy rule to reduce it to a
1377 register bucket
*sdummy
;
1379 /* Since the action was written out with this rule's */
1380 /* number, we must write give the new rule this number */
1381 /* by inserting the new rule before it. */
1383 /* Make a dummy nonterminal, a gensym. */
1386 /* Make a new rule, whose body is empty,
1387 before the current one, so that the action
1388 just read can belong to it. */
1391 record_rule_line ();
1392 p
= NEW(symbol_list
);
1397 crule1
= NEW(symbol_list
);
1399 crule1
->next
= crule
;
1401 /* insert the dummy generated by that rule into this rule. */
1403 p
= NEW(symbol_list
);
1411 if (t
== IDENTIFIER
)
1414 p
= NEW(symbol_list
);
1419 else /* handle an action. */
1421 copy_action(crule
, rulelength
);
1423 xactions
++; /* JF */
1428 /* Put an empty link in the list to mark the end of this rule */
1429 p
= NEW(symbol_list
);
1436 crule
->ruleprec
= symval
;
1441 if (! semantic_parser
)
1442 fatal("%guard present but %semantic_parser not specified");
1444 copy_guard(crule
, rulelength
);
1447 else if (t
== LEFT_CURLY
)
1449 if (actionflag
) fatal("two actions at end of one rule");
1450 copy_action(crule
, rulelength
);
1453 /* If $$ is being set in default way,
1454 warn if any type mismatch. */
1455 else if (!xactions
&& first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1457 if (lhs
->type_name
== 0 || first_rhs
->type_name
== 0
1458 || strcmp(lhs
->type_name
,first_rhs
->type_name
))
1459 fprintf(stderr
, "%s:%d: warning: type clash ('%s' '%s') on default action\n",
1462 lhs
->type_name
? lhs
->type_name
: "",
1463 first_rhs
->type_name
? first_rhs
->type_name
: "");
1465 /* Warn if there is no default for $$ but we need one. */
1466 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1468 "%s:%d: warning: empty rule for typed nonterminal, and no action\n",
1474 /* these things can appear as alternatives to rules. */
1475 else if (t
== TOKEN
)
1477 parse_token_decl(STOKEN
, SNTERM
);
1480 else if (t
== NTERM
)
1482 parse_token_decl(SNTERM
, STOKEN
);
1489 else if (t
== UNION
)
1494 else if (t
== EXPECT
)
1496 parse_expect_decl();
1499 else if (t
== START
)
1505 fatal("invalid input");
1508 if (nsyms
> MAXSHORT
)
1509 fatals("too many symbols (tokens plus nonterminals); maximum %d",
1512 fatal("no input grammar");
1514 if (typed
== 0 /* JF put out same default YYSTYPE as YACC does */
1515 && !value_components_used
)
1517 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1518 but it seems better to be consistent.
1519 Most programs should declare their own type anyway. */
1520 fprintf(fattrs
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1522 fprintf(fdefines
, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1525 /* Report any undefined symbols and consider them nonterminals. */
1527 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1528 if (bp
->class == SUNKNOWN
)
1530 fprintf(stderr
, "symbol %s used, not defined as token, and no rules for it\n",
1534 bp
->value
= nvars
++;
1537 ntokens
= nsyms
- nvars
;
1544 /* Record each rule's source line number in rline table. */
1546 if (nrules
>= rline_allocated
)
1548 rline_allocated
= nrules
* 2;
1549 rline
= (short *) xrealloc (rline
,
1550 rline_allocated
* sizeof (short));
1552 rline
[nrules
] = lineno
;
1556 /* read in a %type declaration and record its information for get_type_name to access */
1563 register char *name
;
1568 fatal("ill-formed %type declaration");
1570 k
= strlen(token_buffer
);
1571 name
= NEW2(k
+ 1, char);
1572 strcpy(name
, token_buffer
);
1587 if (symval
->type_name
== NULL
)
1588 symval
->type_name
= name
;
1590 fatals("type redeclaration for %s", symval
->tag
);
1602 /* assign symbol numbers, and write definition of token names into fdefines.
1603 Set up vectors tags and sprec of names and precedences of symbols. */
1608 register bucket
*bp
;
1609 register int tokno
= 1;
1611 register int last_user_token_number
;
1613 /* int lossage = 0; JF set but not used */
1615 tags
= NEW2(nsyms
+ 1, char *);
1618 sprec
= NEW2(nsyms
, short);
1619 sassoc
= NEW2(nsyms
, short);
1621 max_user_token_number
= 256;
1622 last_user_token_number
= 256;
1624 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1626 if (bp
->class == SNTERM
)
1628 bp
->value
+= ntokens
;
1632 if (translations
&& !(bp
->user_token_number
))
1633 bp
->user_token_number
= ++last_user_token_number
;
1634 if (bp
->user_token_number
> max_user_token_number
)
1635 max_user_token_number
= bp
->user_token_number
;
1636 bp
->value
= tokno
++;
1639 tags
[bp
->value
] = bp
->tag
;
1640 sprec
[bp
->value
] = bp
->prec
;
1641 sassoc
[bp
->value
] = bp
->assoc
;
1649 token_translations
= NEW2(max_user_token_number
+1, short);
1651 /* initialize all entries for literal tokens to 2,
1652 the internal token number for $undefined.,
1653 which represents all invalid inputs. */
1654 for (i
= 0; i
<= max_user_token_number
; i
++)
1655 token_translations
[i
] = 2;
1658 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1660 if (bp
->value
>= ntokens
) continue;
1663 if (token_translations
[bp
->user_token_number
] != 2)
1665 /* JF made this a call to fatals() */
1666 fatals( "tokens %s and %s both assigned number %d",
1667 tags
[token_translations
[bp
->user_token_number
]],
1669 bp
->user_token_number
);
1671 token_translations
[bp
->user_token_number
] = bp
->value
;
1675 error_token_number
= errtoken
->value
;
1677 output_token_defines(ftable
);
1679 if (startval
->class == SUNKNOWN
)
1680 fatals("the start symbol %s is undefined", startval
->tag
);
1681 else if (startval
->class == STOKEN
)
1682 fatals("the start symbol %s is a token", startval
->tag
);
1684 start_symbol
= startval
->value
;
1688 output_token_defines(fdefines
);
1692 if (spec_name_prefix
)
1693 fprintf(fdefines
, "\nextern YYSTYPE %slval;\n", spec_name_prefix
);
1695 fprintf(fdefines
, "\nextern YYSTYPE yylval;\n");
1698 if (semantic_parser
)
1699 for (i
= ntokens
; i
< nsyms
; i
++)
1701 /* don't make these for dummy nonterminals made by gensym. */
1702 if (*tags
[i
] != '@')
1703 fprintf(fdefines
, "#define\tNT%s\t%d\n", tags
[i
], i
);
1706 /* `fdefines' is now a temporary file, so we need to copy its
1707 contents in `done', so we can't close it here. */
1716 output_token_defines(file
)
1721 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1723 if (bp
->value
>= ntokens
) continue;
1725 /* For named tokens, but not literal ones, define the name. */
1726 /* The value is the user token number. */
1728 if ('\'' != *tags
[bp
->value
] && bp
!= errtoken
)
1730 register char *cp
= tags
[bp
->value
];
1733 /* Don't #define nonliteral tokens whose names contain periods. */
1735 while ((c
= *cp
++) && c
!= '.');
1738 fprintf(file
, "#define\t%s\t%d\n", tags
[bp
->value
],
1739 (translations
? bp
->user_token_number
: bp
->value
));
1740 if (semantic_parser
)
1741 fprintf(file
, "#define\tT%s\t%d\n", tags
[bp
->value
],
1752 /* convert the rules into the representation using rrhs, rlhs and ritems. */
1757 register int itemno
;
1758 register int ruleno
;
1759 register symbol_list
*p
;
1760 /* register bucket *bp; JF unused */
1764 ritem
= NEW2(nitems
+ 1, short);
1765 rlhs
= NEW2(nrules
, short) - 1;
1766 rrhs
= NEW2(nrules
, short) - 1;
1767 rprec
= NEW2(nrules
, short) - 1;
1768 rprecsym
= NEW2(nrules
, short) - 1;
1769 rassoc
= NEW2(nrules
, short) - 1;
1777 rlhs
[ruleno
] = p
->sym
->value
;
1778 rrhs
[ruleno
] = itemno
;
1779 ruleprec
= p
->ruleprec
;
1784 ritem
[itemno
++] = p
->sym
->value
;
1785 /* A rule gets by default the precedence and associativity
1786 of the last token in it. */
1787 if (p
->sym
->class == STOKEN
)
1789 rprec
[ruleno
] = p
->sym
->prec
;
1790 rassoc
[ruleno
] = p
->sym
->assoc
;
1795 /* If this rule has a %prec,
1796 the specified symbol's precedence replaces the default. */
1799 rprec
[ruleno
] = ruleprec
->prec
;
1800 rassoc
[ruleno
] = ruleprec
->assoc
;
1801 rprecsym
[ruleno
] = ruleprec
->value
;
1804 ritem
[itemno
++] = -ruleno
;
1813 /* Read a signed integer from STREAM and return its value. */
1816 read_signed_integer (stream
)
1819 register int c
= getc(stream
);
1820 register int sign
= 1;
1831 n
= 10*n
+ (c
- '0');