xref: /minix3/external/bsd/flex/dist/tests/test-bison-yylloc/parser.y (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1357f1050SThomas Veerman /*
2357f1050SThomas Veerman  * This file is part of flex.
3357f1050SThomas Veerman  *
4357f1050SThomas Veerman  * Redistribution and use in source and binary forms, with or without
5357f1050SThomas Veerman  * modification, are permitted provided that the following conditions
6357f1050SThomas Veerman  * are met:
7357f1050SThomas Veerman  *
8357f1050SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
9357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
10357f1050SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
11357f1050SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
12357f1050SThomas Veerman  *    documentation and/or other materials provided with the distribution.
13357f1050SThomas Veerman  *
14357f1050SThomas Veerman  * Neither the name of the University nor the names of its contributors
15357f1050SThomas Veerman  * may be used to endorse or promote products derived from this software
16357f1050SThomas Veerman  * without specific prior written permission.
17357f1050SThomas Veerman  *
18357f1050SThomas Veerman  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19357f1050SThomas Veerman  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20357f1050SThomas Veerman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21357f1050SThomas Veerman  * PURPOSE.
22357f1050SThomas Veerman  */
23357f1050SThomas Veerman 
24*0a6a1f1dSLionel Sambuc %parse-param { void* scanner }
25*0a6a1f1dSLionel Sambuc 
26357f1050SThomas Veerman /*
27357f1050SThomas Veerman    How to compile:
28357f1050SThomas Veerman    bison --defines --output-file="parser.c" --name-prefix="test" parser.y
29357f1050SThomas Veerman  */
30357f1050SThomas Veerman %{
31357f1050SThomas Veerman #include <stdio.h>
32357f1050SThomas Veerman #include <stdlib.h>
33357f1050SThomas Veerman #include <string.h>
34357f1050SThomas Veerman #include "config.h"
35357f1050SThomas Veerman 
36357f1050SThomas Veerman #define YYERROR_VERBOSE 1
37357f1050SThomas Veerman #define YYLEX_PARAM   scanner
38357f1050SThomas Veerman 
39357f1050SThomas Veerman extern int testget_lineno(void*);
40357f1050SThomas Veerman 
41357f1050SThomas Veerman 
42357f1050SThomas Veerman /* A dummy function. A check against seg-faults in yylval->str. */
process_text(char * s)43357f1050SThomas Veerman int process_text(char* s) {
44357f1050SThomas Veerman     int total =0;
45357f1050SThomas Veerman     while(*s) {
46357f1050SThomas Veerman         total += (int) *s;
47357f1050SThomas Veerman         ++s;
48357f1050SThomas Veerman     }
49357f1050SThomas Veerman     return total;
50357f1050SThomas Veerman }
51357f1050SThomas Veerman 
52357f1050SThomas Veerman 
53357f1050SThomas Veerman %}
54357f1050SThomas Veerman 
55357f1050SThomas Veerman %pure_parser
56357f1050SThomas Veerman 
57357f1050SThomas Veerman %union  {
58357f1050SThomas Veerman     int  lineno;
59357f1050SThomas Veerman     char * str;
60357f1050SThomas Veerman }
61357f1050SThomas Veerman %token <str> IDENT
62357f1050SThomas Veerman %token <lineno> LINENO
63357f1050SThomas Veerman %token  EQUAL "="
64357f1050SThomas Veerman %token  COLON ":"
65357f1050SThomas Veerman %token  SPACE " "
66357f1050SThomas Veerman %%
67357f1050SThomas Veerman 
68357f1050SThomas Veerman file:
69357f1050SThomas Veerman      line
70357f1050SThomas Veerman   |  file line
71357f1050SThomas Veerman   ;
72357f1050SThomas Veerman 
73357f1050SThomas Veerman line:
74357f1050SThomas Veerman     LINENO COLON SPACE IDENT EQUAL IDENT
75357f1050SThomas Veerman     {
76357f1050SThomas Veerman         process_text($4);
77357f1050SThomas Veerman         process_text($6);
78357f1050SThomas Veerman         /* Check lineno. */
79357f1050SThomas Veerman         if( $1 != @1.first_line || $1 != testget_lineno(scanner))
80357f1050SThomas Veerman         {
81357f1050SThomas Veerman             yyerror("Parse failed: Line numbers do not match.");
82357f1050SThomas Veerman             YYABORT;
83357f1050SThomas Veerman         }
84357f1050SThomas Veerman 
85357f1050SThomas Veerman         /* Recreate the line to stdout. */
86357f1050SThomas Veerman         printf ( "%04d: %s=%s\n", @1.first_line, $4, $6);
87357f1050SThomas Veerman     }
88357f1050SThomas Veerman     ;
89357f1050SThomas Veerman 
90357f1050SThomas Veerman %%
91357f1050SThomas Veerman 
92*0a6a1f1dSLionel Sambuc int yyerror(void* scanner, char* msg) {
93357f1050SThomas Veerman     fprintf(stderr,"%s\n",msg);
94357f1050SThomas Veerman     return 0;
95357f1050SThomas Veerman }
96357f1050SThomas Veerman 
97