xref: /netbsd-src/external/bsd/flex/dist/tests/bison_yylloc_parser.y (revision 56bd85463476f90bb489799c99292bb30d6771c0)
130da1778Schristos /*
230da1778Schristos  * This file is part of flex.
330da1778Schristos  *
430da1778Schristos  * Redistribution and use in source and binary forms, with or without
530da1778Schristos  * modification, are permitted provided that the following conditions
630da1778Schristos  * are met:
730da1778Schristos  *
830da1778Schristos  * 1. Redistributions of source code must retain the above copyright
930da1778Schristos  *    notice, this list of conditions and the following disclaimer.
1030da1778Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1130da1778Schristos  *    notice, this list of conditions and the following disclaimer in the
1230da1778Schristos  *    documentation and/or other materials provided with the distribution.
1330da1778Schristos  *
1430da1778Schristos  * Neither the name of the University nor the names of its contributors
1530da1778Schristos  * may be used to endorse or promote products derived from this software
1630da1778Schristos  * without specific prior written permission.
1730da1778Schristos  *
1830da1778Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1930da1778Schristos  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2030da1778Schristos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2130da1778Schristos  * PURPOSE.
2230da1778Schristos  */
2330da1778Schristos 
2430da1778Schristos %parse-param { void* scanner }
2530da1778Schristos %lex-param { void* scanner }
2630da1778Schristos 
2730da1778Schristos /*
2830da1778Schristos    How to compile:
2930da1778Schristos    bison --defines --output-file="bison_yylloc_parser.c" --name-prefix="test" parser.y
3030da1778Schristos  */
3130da1778Schristos %{
3230da1778Schristos #include <stdio.h>
3330da1778Schristos #include <stdlib.h>
3430da1778Schristos #include <string.h>
3530da1778Schristos #include "config.h"
3630da1778Schristos #include "bison_yylloc_parser.h"
3730da1778Schristos #include "bison_yylloc_scanner.h"
3830da1778Schristos 
3930da1778Schristos int yyerror(YYLTYPE *location, void* scanner, const char* msg);
4030da1778Schristos 
4130da1778Schristos #define YYERROR_VERBOSE 1
4230da1778Schristos 
4330da1778Schristos 
4430da1778Schristos /* A dummy function. A check against seg-faults in yylval->str. */
process_text(char * s)45*56bd8546Schristos static int process_text(char* s) {
4630da1778Schristos     int total =0;
4730da1778Schristos     while(*s) {
4830da1778Schristos         total += (int) *s;
4930da1778Schristos         ++s;
5030da1778Schristos     }
5130da1778Schristos     return total;
5230da1778Schristos }
5330da1778Schristos 
5430da1778Schristos 
5530da1778Schristos %}
5630da1778Schristos 
5730da1778Schristos %pure-parser
5830da1778Schristos 
5930da1778Schristos %union  {
6030da1778Schristos     int  lineno;
6130da1778Schristos     char * str;
6230da1778Schristos }
6330da1778Schristos %token <str> IDENT
6430da1778Schristos %token <lineno> LINENO
6530da1778Schristos %token  EQUAL "="
6630da1778Schristos %token  COLON ":"
6730da1778Schristos %token  SPACE " "
6830da1778Schristos %%
6930da1778Schristos 
7030da1778Schristos file:
7130da1778Schristos      line
7230da1778Schristos   |  file line
7330da1778Schristos   ;
7430da1778Schristos 
7530da1778Schristos line:
7630da1778Schristos     LINENO COLON SPACE IDENT EQUAL IDENT
7730da1778Schristos     {
7830da1778Schristos         process_text($4);
7930da1778Schristos         process_text($6);
8030da1778Schristos         /* Check lineno. */
8130da1778Schristos         if( $1 != @1.first_line || $1 != testget_lineno(scanner))
8230da1778Schristos         {
8330da1778Schristos             yyerror(0, 0, "Parse failed: Line numbers do not match.");
8430da1778Schristos             YYABORT;
8530da1778Schristos         }
8630da1778Schristos 
8730da1778Schristos         /* Recreate the line to stdout. */
8830da1778Schristos         printf ( "%04d: %s=%s\n", @1.first_line, $4, $6);
8930da1778Schristos     }
9030da1778Schristos     ;
9130da1778Schristos 
9230da1778Schristos %%
9330da1778Schristos 
9430da1778Schristos int yyerror(YYLTYPE *location, void* scanner, const char* msg) {
9530da1778Schristos     (void)location;
9630da1778Schristos     (void)scanner;
9730da1778Schristos     fprintf(stderr,"%s\n",msg);
9830da1778Schristos     return 0;
9930da1778Schristos }
10030da1778Schristos 
101